Skip to main content

sum of two variables integer

Answered

Comments

4 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than three 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.
  • Jaromił Najman
    • Gurobi Staff

    You can define an additional integer variable \(z \in \mathbb{Z}\) and define an equality constraint  \(x + y = z\).
    You will have to do define one additional integer variable and equality constraint for every sum \(x+y\).

    0
  • Céline Meister
    • Gurobi-versary
    • First Comment
    • First Question

    Hello Jaromil

    Thank you for your answer. I tried this, but it wouldn't work. Do you know, what's incorrect with this?

    x3 = pd.Series(m4.addVars(stocks), index=stocks)
    y3 = pd.Series(m4.addVars(stocks), index=stocks)
    z3 = pd.Series(m4.addVars(stocks, vtype=gb.GRB.INTEGER), index=stocks)

    m4.addConstr(x3 + y3 = z3)

    0
  • Jaromił Najman
    • Gurobi Staff

    You defined your variables x3,y3,z3 as pandas Series objects. In order to construct constraints in Gurobi, you have to use the the underlying variable objects. Something like the following should work

    x = m4.addVars(stocks)
    y = m4.addVars(stocks)
    z = m4.addVars(stocks, vtype=gb.GRB.INTEGER)
    x3 = pd.Series(x, index=stocks)
    y3 = pd.Series(y, index=stocks)
    z3 = pd.Series(z, index=stocks)

    for s in stocks:
    # I assume here that stocks are integer values
    m.addConstr(x[s] + y[s] == z[s], name="aux_eq_%d"%(s))

    Best regards, 
    Jaromił

    0

Post is closed for comments.