addGenConstrIndicator for Sum and more than one Binary Variable

1,455 views
Skip to first unread message

Patrick

unread,
Sep 28, 2017, 9:33:25 AM9/28/17
to Gurobi Optimization
Hi all!

I have a model where I use a lot of constraints, that only need to be considered if a variable is true. For that I use model.addGenConstrIndicator according to this ressource: http://www.gurobi.com/documentation/7.5/refman/py_model_addgenconstrindic.html

Now I have a constraint, which only needs to be considered if two variables are true.

The Variable is y[k,i,l].

Another constraint is:

for i in range(numCustomer):
    m.addConstr(quicksum(quicksum(y[k,i,l] for l in range(numGridpoints) if l!=0) for k in range(numGridpoints) if k!=0) == 1, name="Eq13," + str(i))

So the Sum of all y over k and l is 1.

The constraint I try to implement is, where M is a big enough number:



So I know that the Sum of y[k,i,l] is 1. Is that possible to use in the addGenConstrIndicator function? Because the two possible outputs are 0 and 1, but I don't know how the function calculates it internally.

Also I have two Indicators, y[k,i,l] over k and over l. Can I evaluate both Binaries in one Constraint?

Michael Winkler

unread,
Sep 28, 2017, 11:58:26 AM9/28/17
to Gurobi Optimization
Hi Patrick,

you could combine two binary variables into one via an so-called And-constraint (x = y1 * y2, all binary), see http://www.gurobi.com/documentation/7.5/refman/py_model_addgenconstrand.html, and then use this resultant variable x as indicator variable. Does this help you?

Best,
Michael

Patrick

unread,
Sep 28, 2017, 4:09:24 PM9/28/17
to Gurobi Optimization
Such a simple, yet effective solution! Thanks :)

Patrick

unread,
Sep 29, 2017, 9:12:49 AM9/29/17
to Gurobi Optimization
Hi Michael,

I'm still stuck. Does the AddGenConstrAnd Function work with sums? I used:

z ={}
for k in range(numGridpoints):
    for i in range(numCustomer):
        for l in range(numGridpoints):
            if k!=0 and l!=0:
                z[k,i,l] = m.addVar(vtype=GRB.BINARY, name= "z%d,%d,%d"%(k,i,l))
                m.addGenConstrAnd(z[k,i,l] , [quicksum(y[k,i,l] for k in range(numGridpoints) if k!=0),quicksum(y[k,i,l] for l in range(numGridpoints) if l!=0)] , "slackconstraintEq17,%d,%d,%d"%(k,i,l))

So first I added a slackvariable z and then tried to add the Constraint. But I get the error:
GurobiError: Invalid data in vars array

Do you know what am I doing wrong and/or how it would work?



Am Donnerstag, 28. September 2017 17:58:26 UTC+2 schrieb Michael Winkler:

Tobias Achterberg

unread,
Oct 1, 2017, 6:02:21 PM10/1/17
to gur...@googlegroups.com
No, the AddGenConstrAnd() method takes the resultant variable "resvar" and a list of
variables "vars" (i.e., a Python list) as input, see
http://www.gurobi.com/documentation/7.5/refman/py_model_addgenconstrand.html

Your quicksum() call results in a sum of variables, which is a linear expression.

For example, if you say

m.addGenConstrAnd(x[0], [x[1],x[2],x[3],x[4]])

this means that x[0] should be 1 if and only if all of x[1],...,x[4] are 1.


Regards,

Tobias
Reply all
Reply to author
Forward
0 new messages