With the linear solver, can you create a new variable as the sum and/or product of other variables.

934 views
Skip to first unread message

Rhys Davies

unread,
Jul 21, 2018, 11:16:57 AM7/21/18
to or-tools-discuss
In Python, I have a set of BoolVars and I would like to create a new variable that is either the product or the sum of the boolean variables.

I would then like to use this new var in the objective.


I have attached a small example (that is not running to try and illustrate what I am trying to achieve.

I appreciate that I am probably being somewhat moronic...

Thanks for any help
bool_example.py

Laurent Perron

unread,
Jul 21, 2018, 1:50:49 PM7/21/18
to or-tools-discuss
Product no, sum should be:

solver.Add(new_var == sum([bool var array]))
Laurent Perron | Operations Research | lpe...@google.com | (33) 1 42 68 53 00



--
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/d/optout.

Rhys Davies

unread,
Jul 21, 2018, 2:03:02 PM7/21/18
to or-tools-discuss
Thanks.... :)

That helps greatly

Rhys Davies

unread,
Jul 26, 2018, 5:08:50 AM7/26/18
to or-tools-discuss
Hi Laurent

I am still having an issue with the Boolean sum. The solver is essential converting the BoolVar into an integer.

For example:

    var1 = solver.BoolVar('var1')
    var2 = solver.BoolVar('var2')
    var3 = solver.BoolVar('var3')
    sum1 = solver.BoolVar('sum1')

    solver.Add(sum1 == solver.Sum([var1, var2, var3]))

sum1 has a max value of 1, so only one of the vars can be true at a time...

I can, of course, set sum1 as an IntVar.

    sum1 = solver.IntVar(0, 3, 'sum1')

but then I do not know how to set the coefficient in the objective function

objective = solver.Objective()
objective.SetCoefficient(sum1, 80)

This now sets all the vars to True, sum1 is three and the objective function comes out at 240.

I would like to add 80 if and only if sum1 is not 0.

Is there a way to do this? Or does break the linearity?

I was expecting the solver.Sum to obey Boolean Algebra:
0+0+0 = 0
1+0+0 = 1
1+1+0 = 1
1+1+1 = 0

Regards

Rhys

Rhys Davies

unread,
Jul 26, 2018, 5:11:47 AM7/26/18
to or-tools-discuss
Lets try that algebra again :(

0+0+0 = 0
1+0+0 = 1
1+1+0 = 1
1+1+1 = 1

Laurent Perron

unread,
Jul 26, 2018, 9:40:14 AM7/26/18
to or-tools-discuss
This is a linear solver, not a boolean one :-)

Just to sum1 >= var1; sum1 >= var2; sum3 >= var1; sum1 <= var1 + var2 + var3.

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


Laurent Perron

unread,
Jul 26, 2018, 9:53:28 AM7/26/18
to or-tools...@googlegroups.com
Correct version:

just do: 
  sum1 >= var1; 
  sum1 >= var2;
  sum1 >= var3; 
  sum1 <= var1 + var2 + var3.

Rhys Davies

unread,
Jul 26, 2018, 12:39:26 PM7/26/18
to or-tools-discuss
That works perfectly.

For anyone interested:

def boolean_linear_sum(solver, bool_vars, bool_sum):
for var in bool_vars:
solver.Add(bool_sum >= var)
    solver.Add(bool_sum <= solver.Sum(bool_vars)) 
Reply all
Reply to author
Forward
0 new messages