Skip to main content

How to use addGenConstrMax function?

Answered

Comments

3 comments

  • Eli Towle
    • Gurobi Staff

    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
  • Menglei Ji
    • Gurobi-versary
    • First Comment
    • First Question

    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
  • Eli Towle
    • Gurobi Staff

    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

Please sign in to leave a comment.