I need to solve a gurobi model repeatedly (with different variable values each iteration). Rather than rebuild the model each iteration, I have tried setting up the model, then looping through repeated optimizations, but the variable values do not update. Here is a simple example.
n = Model("Test")
a = n.addVar(lb=0,name = "a")
b = n.addVar(lb=0,name = "b")
a=1
b=1
x = n.addVar(lb=0,name = "x")
y = n.addVar(lb=0,name = "y")
n.update()
n.setObjective(a*x + b*y,GRB.MAXIMIZE)
n.addConstr(x + y <= 10)
n.addConstr(2*x + 3*y <= 20)
n.addConstr(y<=5)
n.update
n.optimize()
for v in n.getVars():
print('%s %g' % (v.varName, v.x))
print('Obj: %g' % n.objVal)
for i in (1,10):
n.update()
a=i*2
b=100/i
n.optimize()
for v in n.getVars():
print('%s %g' % (v.varName, v.x))
How do I use the existing model over and over again?