#!/usr/bin/python # Copyright 2012, Gurobi Optimization, Inc. # 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. from gurobipy import * try: # Create a new model model = 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") # Integrate new variables model.update() # 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 v.varName, v.x print 'Obj:', model.objVal except GurobiError: print 'Encountered an error'