Trouble using gp.abs_()
AnsweredHi, I'm trying to add a constraint using the abs_() function. I'm just doing a very simple example
a = model.addVar(name="a")
b = model.addVar(lb=-1, name="b")
model.addConstr(a == gp.abs_(b), name="abs_constr")
model.update()
This code does not give any error message. But after the addConstr operation, I try model.getConstrByName("abs_constr") and I get None. Based on the optimization result, I can see the constraint is taking effect. But I do not see it anywhere in the model.
Thanks!
-
Hi,
The Model.getConstrByName method is designed to retrieve a linear constraint by its name. However, you can define your absolute constraint using the special method Model.addGenConstrAbs, and then you can use Model.getGenConstrAbs method to retrieve the data associated with a general constraint of type ABS.
import gurobipy as gp
from gurobipy import*
model=gp.Model()
a = model.addVar(name="a")
b = model.addVar(lb=-1, name="b")
absconstr = model.addGenConstrAbs(a, b, "absconstr")
model.update()
(resvar, argvar) = model.getGenConstrAbs(absconstr)where
-
resvar – (Var) The variable whose value will be to equal the absolute value of the argument variable. ( in your case: <gurobi.Var a> )
-
argvar – (Var) The variable for which the absolute value will be taken ( in your case: <gurobi.Var b> ).
I hope this helps.
-Ahmed
0 -
-
Thanks! This is really helpful!
Is there a way I can retrieve the constr obj (ie absconstr)? Cuz I'm trying to update the abs constraint on the fly, it would be nice if I can retrieve the abs constraint by its name so I can remove and add an updated abs constraint.
0 -
Hi Ruixuan,
You could accomplish this with a dictionary and getGenConstrs(), i.e.
my_gen_cons = {gc.GenConstrName:gc for gc in m.getGenConstrs()}
- Riley
0
Please sign in to leave a comment.
Comments
3 comments