Gurobi SOS constraints in Python

483 views
Skip to first unread message

Jaldert Rombouts

unread,
Apr 12, 2015, 9:04:25 PM4/12/15
to gur...@googlegroups.com
I am trying to use type 1 SOS constraints via the Python API, but it seems like Gurobi ignores my constraints.

If I use quicksum constraints, the solver works as expected:

# Constraint: Each customer is served exactly once
# customers is a list of customers
# facilities is a list of facilities (that may or may not be opened)
# y_wc is a list of binary Gurobi variables representing whether facility w serves customer c,
# for example, y_wc[0] represents whether facility 0 serves customer 0, etc.

for c in range(len(customers)):
    m
.addConstr(quicksum([y_wc[w * len(customers) + c] for w in range(len(facilities))]) == 1)

However, if I try to replace the code above by SOS constraints as follows, the solver seems to ignore these - it immediately gives an "optimal" solution where no single facility is opened and no customer is served.

# SOS constraint
for c in range(len(customers)):
    m
.addSOS(GRB.SOS_TYPE1, [y_wc[w * len(customers) + c] for w in range(len(facilities))])

If I write the model to a mps file, the SOS constraints do seem to be in place (3 facilities, 4 customers):

# serves(w,c) is the name of the y_wc variables

SOS
 S1 s0
    serves
(0,0)     1
    serves
(1,0)     2
    serves
(2,0)     3
 S1 s1
    serves
(0,1)     1
    serves
(1,1)     2
    serves
(2,1)     3
 S1 s2
    serves
(0,2)     1
    serves
(1,2)     2
    serves
(2,2)     3
 S1 s3
    serves
(0,3)     1
    serves
(1,3)     2
    serves
(2,3)     3

I'm sure that I'm missing something here - any help appreciated!

I am using:

gurobi6.0.3_mac64 
grurobipy.sys.api_version returns 1013
OS X version 10.10.3 (14D131)

Renan Garcia

unread,
Apr 13, 2015, 8:56:29 AM4/13/15
to gur...@googlegroups.com
The quicksum constraint you've specified is not exactly the same as a type 1 SOS constraint. The former specifies *exactly one* variable must equal one, while the latter specifies *at most one* variable can equal one.

--

---
You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jaldert Rombouts

unread,
Apr 14, 2015, 2:42:43 AM4/14/15
to gur...@googlegroups.com
Ah - clear! 

Thank you!


Op maandag 13 april 2015 05:56:29 UTC-7 schreef Renan Garcia:
Reply all
Reply to author
Forward
0 new messages