Skip to content

Instantly share code, notes, and snippets.

@odow
Created September 2, 2018 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odow/8a4b4b529ae47c18b64b57feebd45a47 to your computer and use it in GitHub Desktop.
Save odow/8a4b4b529ae47c18b64b57feebd45a47 to your computer and use it in GitHub Desktop.
Gurobi: delete variable in quadratic expression
using Gurobi
model = Gurobi.Model(Gurobi.Env(), "model")
Gurobi.add_cvar!(model, 0.0, 0.0, 100.0)
Gurobi.add_cvar!(model, 0.0, 0.0, 100.0)
Gurobi.update_model!(model)
Gurobi.add_constr!(model, [1, 2], [1.0, 2.0], '=', 2.0)
Gurobi.update_model!(model)
# (x-y)^2 = x^2 - 2xy + y^2
Gurobi.add_qpterms!(model, [1, 1, 2], [1, 2, 2], [1.0, -2.0, 1.0])
Gurobi.update_model!(model)
Gurobi.optimize(model)
@assert Gurobi.get_dblattrarray(model, "x", 1, 2) ≈ [2/3, 2/3] atol=1e-4
Gurobi.del_vars!(model, 1)
Gurobi.update_model!(model)
Gurobi.optimize(model)
# y = 1
@assert Gurobi.get_dblattrarray(model, "x", 1, 1) ≈ [1.0] atol=1e-4
# quadratic term is y^2
@assert Gurobi.getq(model) == (Int32[0], Int32[0], [1.0])
# objective value is 1, so the cross-term -2xy has been dropped
@assert Gurobi.get_objval(model) == 1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment