-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsos.py
executable file
·44 lines (28 loc) · 1.03 KB
/
sos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3.9
# Copyright 2022, 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')