sos.py#
#!/usr/bin/env python3.7
# Copyright 2024, Gurobi Optimization, LLC
# This example creates a very simple Special Ordered Set (SOS) model.
# The model consists of 3 continuous variables, no linear constraints,
# and a pair of SOS constraints of type 1.
import gurobipy as gp
from gurobipy import GRB
try:
# Create a new model
model = gp.Model("sos")
# Create variables
x0 = model.addVar(ub=1.0, name="x0")
x1 = model.addVar(ub=1.0, name="x1")
x2 = model.addVar(ub=2.0, name="x2")
# Set objective
model.setObjective(2 * x0 + x1 + x2, GRB.MAXIMIZE)
# Add first SOS: x0 = 0 or x1 = 0
model.addSOS(GRB.SOS_TYPE1, [x0, x1], [1, 2])
# Add second SOS: x0 = 0 or x2 = 0
model.addSOS(GRB.SOS_TYPE1, [x0, x2], [1, 2])
model.optimize()
for v in model.getVars():
print('%s %g' % (v.VarName, v.X))
print('Obj: %g' % model.ObjVal)
except gp.GurobiError as e:
print('Error code ' + str(e.errno) + ": " + str(e))
except AttributeError:
print('Encountered an attribute error')