absolute value constraint on vectorised variable setting
AnsweredHi,
I am using AddMVar to define a matrix/vectorised setting for a quadratic optimisation problem (linear objective with quadratic and linear constraint). for one of the constraint I need to access to absolute value of variables. I used "addGenConstrAbs" but it doesn't look that it works on vector level.
I read in other posts that we dont have access to individual variable at vector level (i.e. v[i]) to use addGenConstrAbs for individual variable.
Do you have any solutions\suggestions?
Thank you.
-
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?. -
Unfortunately, MVar objects cannot currently be used with general constraints. The development team is aware of the limitation. For now, you can use MVar.tolist() (new in Gurobi 9.1) to obtain the list of Var objects associated with the matrix variable(s). These Var objects can be used with Model.addGenConstrAbs()/abs_().
0 -
Thank you Eli for your prompt response.
How would you suggest that i implement the abs constraint at matrix setting?
Does it mean that I should use AddVar instead of AddMVar?
Thank you,
0 -
You can use the MVar.tolist() method I mentioned to obtain the list of Var objects associated with the matrix variables. These Var objects can be used in general constraint functions. E.g.:
import gurobipy as gp
m = gp.Model()
x = m.addMVar(3)
y = m.addMVar(3)
# MVar objects are not currently integrated with general constraints
# m.addConstr(y == gp.abs_(x)) does not work
# Use corresponding Var objects instead
for vx, vy in zip(x.tolist(), y.tolist()):
m.addConstr(vy == gp.abs_(vx))Alternatively, you could just use Var objects throughout by defining your variables with Model.addVar() or Model.addVars(). However, this would require you to rewrite any code that uses matrix semantics.
0
Post is closed for comments.
Comments
4 comments