Gecode: constraining integer variables using a float value

235 Views Asked by At

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!

2

There are 2 best solutions below

0
Zayenz On

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.

2
count0 On

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.