model.addConstrs: Can't set variables equal to changing parameter?
AnsweredHi,
I have a VERY simple constraint I want to implement. Here's the general form:
x(t) + y(t) = A(t) for t = {1,4}
where x(t) and y(t) are variables. A(t) is a given vector/array. Can someone please show me how to implement this in Python (I'm using Jupyter Notebook)? I can't imagine it's difficult, but doesn't seem to work for me.
0
-
Official comment
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?. -
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 -
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.
Comments
3 comments