TypeError when using gurobipy.Model.addGenConstrIndicator
回答済みHi,
I am encountering a problem with a logical constraint. I get the same error when trying ".addGenConstrIndicator". I posted the code below including the error message. I am grateful for any kind of help. Note that this is a test case because I encountered problems in my main model applying a similar logic. The overall goal is to indicate (finish = 1) when beets are finished (beet = 0).
Thank you!
from gurobipy import Model, GRB, quicksum
# Define the sets I and T
I = [1, 2]
T = [0, 1, 2, 3, 4]
# Create a new Gurobi model
model = Model("test_model")
# Define fixed beet_flow values
beet_flow = {
(1, 0, 0, 0): 1500.0,
(1, 0, 0, 1): 5.1,
(1, 0, 0, 2): 5.0,
(1, 0, 0, 3): 0.0,
(1, 0, 0, 4): 0.0,
(2, 0, 0, 0): 100.5,
(2, 0, 0, 1): 10.1,
(2, 0, 0, 2): 10.0,
(2, 0, 0, 3): 10.0,
(2, 0, 0, 4): 0.0
}
finish = model.addVars(I, T, vtype=GRB.BINARY, name="finish")
# Ensure the 'finish' variable is 1 when beets are 0 using indicator constraints
for i in I:
for t in T:
beet = (beet_flow[i, 0, 0, t])
model.addConstr((finish[i, t] == 1) >> (beet <= 1))
# Optimize the model
model.optimize()
# Check and print the results
if model.status == GRB.OPTIMAL:
for i in I:
for t in T:
print(f"finish[{i},{t}] = {finish[i, t].x}")
print(f"Beets [{i}, {t}]: {beet_flow[i,0,0,t]}")
Output:
Traceback (most recent call last):
File "/Users/...", line 36, in <module>
model.addConstr((finish[i, t] == 1) >> (beet <= 1))
File "src/gurobipy/model.pxi", line 3637, in gurobipy.Model.addConstr
File "src/gurobipy/model.pxi", line 4714, in gurobipy.Model.addGenConstrIndicator
TypeError: unsupported operand type(s) for -=: 'NoneType' and 'float'
-
正式なコメント
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. -
Hi Ludwig,
it seems that "beet" is not a variable, so you also cannot use it to construct the constraint "beet <= 1". You may have to add another call to addVariable() to do something with those beet values.
I hope that helps.
Cheers,
Matthias1 -
Thank you that solved the problem
0
投稿コメントは受け付けていません。
コメント
3件のコメント