How to use addGenConstrMax function?
AnsweredHi there,
when I use this sentence to add the infinity norm constraint:
model.addGenConstrMax(b, [a_pos[i] + a_neg[i] for i in I])
there is an error: 'GurobiError: Invalid data in vars array'. Could you please tell me how to correct this?
I also try to use model.addGenConstrNorm, but it also shows the same error.
Thanks,
Menglei
-
Official comment
This post is more than two 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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Model.addGenConstrMax() is used to set one variable equal to the maximum of a set of variables, and possibly a constant. The \(\texttt{a_pos[i] + a_neg[i]}\) terms are (likely linear) expressions, not variables.
To resolve the issue, introduce auxiliary variables for each of the \(\texttt{a_pos[i] + a_neg[i]}\) terms, then use those auxiliary variables in your call to Model.addGenConstrMax(). For example:
# Add lower bound of -GRB.INFINITY if a_pos[i] + a_neg[i] can be negative
u = model.addVars(I, name="u")
model.addConstrs((a_pos[i] + a_neg[i] == u[i] for i in I), name="define_u")
model.addGenConstrMax(b, u, name="define_b")0 -
Hi Eli,
Thanks for your response. I still have a question about addGenConstrNorm.
alpha = model.addVars (I, S,lb=-GRB.INFINITY, vtype=GRB.CONTINUOUS, name= 'alpha')
beta = model.addVars (S, lb=0,vtype=GRB.CONTINUOUS, name= 'beta')self.model_dro.addGenConstrNorm(beta, alpha, Q, 'normconstr') # Q can be 1,2, GRB.INFINITY.
The error is:
TypeError: {1: <gurobi.Var *Awaiting Model Update*>, 2: <gurobi.Var *Awaiting Model Update*>, 3: <gurobi.Var *Awaiting Model Update*>, 4: <gurobi.Var *Awaiting Model Update*>} is not a variable.
I am so confused about the command. Thanks for your help!
Best,
Menglei
0 -
The first argument to Model.addGenConstrNorm() should be a single Var object, not a tupledict. If you are trying to add a norm constraint for every \( s \in S \), you will have to define the norm constraint for each individual Var object in \(\texttt{beta}\). For example, using the norm() general constraint helper function:
import gurobipy as gp
self.model_dro.addConstrs(
(beta[s] == gp.norm(alpha.select("*", s), Q) for s in S), "normconstr"
)Also, your code adds the variables to Model object \(\texttt{model}\), then adds constraints to Model object \(\texttt{self.model_dro}\). Maybe they are the same model, but I will mention it anyways in case you run into a "Variable not in model" error.
0
Post is closed for comments.
Comments
4 comments