solver.Prod inside solver.Sum??

1,831 views
Skip to first unread message

OG

unread,
Apr 7, 2014, 11:43:10 AM4/7/14
to or-tools...@googlegroups.com
Hi!

I am starting to use or-tools within the Python environment. I've been able to solve some problems but now I'm stuck trying to express one of the constraints. I want to multiply element by element two 2D-square arrays and then sum up all the items. One of the arrays contains already calculated values and the other one contains decision variables. I was trying to use something like solver.sum(Solver.Prod(...)) but it is not working. This is the expression I've coded with no success:

solver.Add(solver.Sum([solver.Prod(M[i,j],D[i][j]) for i in range(n) for j in range(n)]) == d)

M is the array containing the decision variables (0 or 1 values), D the array with the numers, n is the length of the arrays and d is another decision variable.

Any ideas on how to express this constraint?

Thank you in advance. Regards,

OG

hakank

unread,
Apr 7, 2014, 12:06:04 PM4/7/14
to or-tools...@googlegroups.com
What I understand of your problem, you can use "*" instead of Prod(), i.e.

       solver.Add(solver.Sum([M[(i,j)]*D[i][j] for i in range(n)

                           for j in range(n)]) == d)

/hakank

OG

unread,
Apr 7, 2014, 12:20:42 PM4/7/14
to or-tools...@googlegroups.com
Thanks for your answer Hakank. I've tried your option and I get the following error:

TypeError: unsupported operand type(s) for *: 'IntVar' and 'float'

Is there a way to multiply integers with float variables?

hakank

unread,
Apr 7, 2014, 12:32:48 PM4/7/14
to or-tools...@googlegroups.com

If you are using the CP interface (using "from constraint_solver import pywrapcp" or something like that) there is - what I know of - no support to  mix decision variables defined as IntVar() with float values. Perhaps you are using the MIP interface? Then try to define M[(i,j)] using NumVar() instead.

/hakank

OG

unread,
Apr 7, 2014, 3:18:58 PM4/7/14
to or-tools...@googlegroups.com
Yes, I am using the CP interface, so I guess I'll need to reformulate my program. Thank you for your help Hakank.

Message has been deleted

Laurent Perron

unread,
Mar 22, 2017, 7:54:47 AM3/22/17
to or-tools-discuss
Hi

A lot of the API is moved to the IntVar/IntVar[] classes

    Intvar[] hire = solver.MakeIntVarArray(num_workers, 0, 1, "workers");
    IntVar total_cost = hire.ScalProd(cost).Var();

To see what is available, please have a look at the Helper.cs files in this directory:


I am sorry we do not have a better documentation.

Thanks

Laurent Perron | Operations Research | lpe...@google.com | (33) 1 42 68 53 00


2017-03-15 10:42 GMT-04:00 Darknight85 <pooya....@gmail.com>:
I am trying to do the exact same thing with the C# interface, but I couldn't find a solver.Sum() method. I have a vector of variables and a vector of coefficients that need to be multiplied and then summed. Any thoughts?

--
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-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pooya Rezaei

unread,
Mar 27, 2017, 10:21:24 AM3/27/17
to or-tools-discuss
I was looking at the LinearSolver, but I saw that there are some Helper.cs files for that as well. 

After looking at the examples, I found out how to use LINQ with the or-tools, which is pretty powerful! Just for the record, this can also be written as:

var total_cost = Enmerable.Range(0, num_workers).Select(i => cost[i]*hire[i]).ToArray().Sum();
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages