Skip to main content

How to use addGenConstrMax function?

Answered

Comments

4 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    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.
  • 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

Post is closed for comments.