Skip to main content

Which is the index of variables added after variable deletion?

Answered

Comments

2 comments

  • Official comment
    Simranjit Kaur
    Gurobi Staff Gurobi Staff
    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?.
  • Juan Orozco
    Gurobi Staff Gurobi Staff

    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 GRB

    m = 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.