Which is the index of variables added after variable deletion?
AnsweredConsider I have added three variables and update/solved the model, then I deleted one of them, and added one more variable. Now I want to change an attribute of the last variable added, which is the index of this last variable before I call the model update? Is it three? (Because one previous variable was deleted.) Or is it four (Because the deletion was not yet processed.) Or did I understand it wrong and I should not be modifying attributes of variables that were not "really" in the model (i.e., not called an updated since added them).
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
The safest way to change model object attributes is to reference such objects by name. Here's a Python script that shows how to do this (notice that there is no need to call model.update(), as model.optimize() will update all pending changes to the model):
import gurobipy as gp
from gurobipy import GRBm = gp.Model("bip")
x = m.addVars(3, vtype=GRB.BINARY, name="x")m.setObjective(x[0] + x[1] + 2*x[2], GRB.MAXIMIZE)
m.addConstr(gp.quicksum((i+1)*x[i] for i in range(3)) <= 4, "c0")
m.addConstr(x[0] + x[1] >= 1, "c1")m.optimize()
m.remove(m.getVarByName("x[2]"))
x[3] = m.addVar(vtype=GRB.BINARY, name="x[3]")
x[3].obj = 10
m.chgCoeff(m.getConstrByName("c0"), x[3], 3.0)m.optimize()
0
Post is closed for comments.
Comments
2 comments