0

I use Gecode through its C++ API in a kind of learning context with positive and negative examples. In this context I have two BoolVarArray: positive_bags_ and negative_bags_.

And what I want to do seems very simple: I want to constrain these bags with a minimal growth rate constraint based on a user parameter gmin.

Thereby, the constraint should look like: sum(positive_bags_) >= gmin * sum(negative_bags_). It works using the rel function defined like this: rel(*this, sum(positive_bags_) >= gmin * sum(negative_bags_)) but my problem is that in my case gmin is a float but is casted by rel as an integer.

Therefore I can only constrain positive_bags_ to be 2, 3, ... times bigger than negative_bags_ but I need for my experiments to define gmin as 1.5 for example.

I checked the documentation and did not find a definition of linear that use both Boolean/Integer and Float variables.

Is there some way to define this constraint using a float gmin?

Thanks in advance!

Heleo
  • 25
  • 8
  • Please point to the documentation of the 'sum' method. It seems your variables are being implicitly cast which creates a problem. – count0 Mar 14 '19 at 13:32

2 Answers2

2

If your factor gmincan be expressed as a reasonably small rational n/d (3/2 in your example), then you could use

d * sum(positive_bags_) >= n * sum(negative_bags_)

as your constraint. If there is no small rational that is suitable, then you need to channel your variables to FloatVars and use the FloatVar linear constraint.

Zayenz
  • 1,914
  • 12
  • 11
0

If implicit type-casting is an issue you can try:

(float) sum(positive_bags_) >= (gmin * (float) sum(negative_bags_))

Assuming gmin is a float.

Implicit casting will convert your float to an int. If you want to control what type of rounding you want to apply, wrap the result into <math.h>'s roundf or a rounding function of your choice depending on the type.

count0
  • 2,537
  • 2
  • 22
  • 30
  • Sum is a function in Gecode that (in this case) returns an IntExpr, which is not something that can be cast to a float. – Zayenz Mar 14 '19 at 13:41
  • That's what i thought, but i couldn't find the doc on the gecode site easily, hence i asked for a link. – count0 Mar 14 '19 at 13:48