Solving for double instead of IntVar in Python library

926 views
Skip to first unread message

Bart Gajderowicz

unread,
Mar 5, 2014, 12:53:56 AM3/5/14
to or-tools...@googlegroups.com
Hi All,

I'm trying to solve a stochastic problem with probabilities but am having a problem.

I can't multiply an IntVar by a double:

x = solver.IntVar(0,999, 'x')
solver.Add(0.6*x >= 500)

I get the following error:
TypeError: in method 'IntExpr___rmul__', argument 2 of type 'int64'

Is there a way to create a variable that can be multiplied by a double?
Is ScaProd() useful here?

An alternate approach would be to multiply all values by 100, so that I have:

x = solver.IntVar(0,99900, 'x')
solver.Add(600*x >= 50000)

But then I would need to increment the search by 100, instead of 1, 
Is this possible?


I could add the following but would prefer to omit additional constraints:
solver.Add((x % 100) == 0)

Thanks in advance.
Bart

Ondrej Sykora

unread,
Mar 5, 2014, 3:54:59 AM3/5/14
to or-tools...@googlegroups.com
Hi Bart,

the CP solver is integer-based and does not support floating point arithmetic. If you depend on it heavily, you should consider using a linear (MIP) solver.

I do not know the problem you are solving, but in your example, it would be enough to multiply just the coefficients and to keep the variables at their original range:

x = solver.IntVar(0, 999, 'x')
solver.Add(60 * x >= 50000)

Note that if you multiply the domains of the variables by 100 too, product of a variable and a multiplied coefficient is multiplied by 100 twice (once for the coefficient and once for the variable), so in that case your constraint should be rather

x = solver.IntVar(0,99900, 'x')
solver.Add(60 * x >= 5000000) 


--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Ondrej

Bart Gajderowicz

unread,
Mar 5, 2014, 1:54:51 PM3/5/14
to or-tools...@googlegroups.com
Thanks Ondrej,

MIP is not an option at the moment, so I will stick to my alternate solution.

Regarding double multiplication, It was a typo on my part. 

Thanks again.
Reply all
Reply to author
Forward
0 new messages