import gurobipy as gp
from gurobipy import GRB
with gp.Env(empty=True) as env:
env.start()
with gp.Model(env=env) as m:
# Set parameters
# m.setParam(GRB.Param.TimeLimit, 3600) # time limit
# m.setParam(GRB.Param.DisplayInterval, 60) # have fewer logs (interval in seconds)
# m.setParam(GRB.Param.LogToConsole, 0) # disable logs
# m.setParam(GRB.Param.Threads, 40) # number of threads
# m.setParam(GRB.Param.NonConvex, 2) # Non-convex optimization
# Create variables
var_x = m.addVars(n, vtype=GRB.CONTINUOUS) # real vector
var_y = m.addVars( # 2-d real vector
n,
k,
lb=0,
ub=1,
vtype=GRB.CONTINUOUS
)
# Set objective.
m.setObjective(0, GRB.MINIMIZE)
# Add constraints
for i in range(n):
for j in range(i + 1, n):
constr = gp.QuadExpr(sum(var_x[(i, p)] * var_x[(j, p)] for p in range(k)))
m.addConstr(constr == 1, f'c_edge_{i}_{j}')
# Optimize model.
m.optimize()
# Print solution.
if m.SolCount == 0:
print('Infeasible.')
else:
for i in range(n):
s = ' '.join('%.2f' % var_x[(i, p)].x for p in range(k))
print(s)
Last active
April 26, 2022 06:14
-
-
Save mogproject/2aff575b78214720f2123e1e5b2ab0e5 to your computer and use it in GitHub Desktop.
Gurobi Cheatsheet
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment