Skip to main content

Invalid argument to Model.addConstr

Answered

Comments

3 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.
  • Riley Clement
    • Gurobi Staff

    Hi Yutian,

    I'll try and help you debug, rather than give you the answer.

    The full error gives you the exact place where the error occurs:

    GurobiError                               Traceback (most recent call last)
    Cell In[4], line 89
         85 for i in var:
         86     expr = sum(xi[i] * x[i] for i in var) - y_tilde[i]  # This creates a LinExpr using Gurobi's variable x[i]
    ---> 89     m0.addConstr(z[i] >= expr)
         90     m0.addConstr(z[i] >= -expr)
         92 m0.setObjective(objective, sense=GRB.MINIMIZE)

    File src/gurobipy/model.pxi:3675, in gurobipy.Model.addConstr()

    File src/gurobipy/model.pxi:3467, in gurobipy.Model.__addConstr()

    GurobiError: Invalid argument to Model.addConstr

     

    The error is saying the addConstr method does not like something about z[i] >= expr.  If you put the following lines of code just before the line which causes the error, then it will print the value of z[i] and expr to screen.

        print(f"{z[i]=}")
        print(f"{expr=}")

    Does the output look like what you would expect?

    - Riley

     

    0
  • Yutian He
    • Gurobi-versary
    • Curious
    • Conversationalist

    Hi Riley,

    Thank you for the two-line code! You are right, looks like it is not what I want, I will check it later and give you more feedback. Thank you again!

    0

Post is closed for comments.