Skip to main content

model.addConstrs: Can't set variables equal to changing parameter?

Answered

Comments

3 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 why not try our AI Gurobot?.
  • Eli Towle
    • Gurobi Staff

    Below is a simple example that shows how to add two constraints of this form. You can make two calls to Model.addConstr(), or add both constraints at once in a single call to Model.addConstrs().

    import gurobipy as gp
    from gurobipy import GRB
    import random


    n = 5
    A = [random.random() for t in range(n)]

    m = gp.Model()
    x = m.addVars(n, name='x')
    y = m.addVars(n, name='y')

    # Option 1: two calls to Model.addConstr()
    for t in [1, 4]:
        m.addConstr(x[t] + y[t] == A[t], name=f'con[{t}]')

    # Option 2: one call to Model.addConstrs()
    m.addConstrs((x[t] + y[t] == A[t] for t in [1, 4]), name='con')

    I recommend looking at the Python API documentation and the functional code examples.

    1
  • Quentin De Boever
    • Gurobi-versary
    • First Question
    • First Comment

    Thanks Eli! I got it working. I appreciate your help. Maybe you can help me on my other question?

    0

Post is closed for comments.