Gurobi: Invalid data in vars array
回答済みI have a vector of 100 demand values, for each of them i want to define specific constraints. Here is how I've defined my variables.
b1 = {}
for r in blood_types:
for m in M:
for demand_index in range(100):
b1[r, "1", m, demand_index] = Issuing_model.addVar(vtype=GRB.INTEGER, name=f'b1_{r}_{"1"}_{m}_{demand_index}')
I1 = {}
for r in blood_types:
for m in m1:
I1[r, "1", m] = Issuing_model.addVar(vtype=GRB.INTEGER, name=f'I1_{r}_{"1"}_{m}')
while:
M = list(range(41, 0, -1))
m1 = list(range(42, 0, -1))
But the following constraint, gives me the error of:
GurobiError: Invalid data in vars array
for demand_index, demand in enumerate(next_demands_OPM):
next_demand_OPM = demand
for m in M:
if m == 1:
Issuing_model.addConstr(b1["O+", "1", m, demand_index] == gp.max_(0, I1["O+", "1", m+1] - next_demand_OPM))
else:
Issuing_model.addConstr(b1["O+", "1", m, demand_index] == gp.max_(0, I1["O+", "1", m+1] - (next_demand_OPM - sum(I1["O+", "1", j] for j in range(2, m+1)))))
Does anyone have any feedback?
-
正式なコメント
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. -
It looks like you want to combine a max-constraint with a linear expression. This needs to be done in two steps and you need an additional auxiliary variable, e.g.
Issuing_model.addConstr(aux["O+", "1", m, demand_index] == gp.max_(0, I1["O+", "1", m+1]))
Issuing_model.addConstr(b1["O+", "1", m, demand_index] == aux["O+", "1", m, demand_index] - (11 - sum(I1["O+", "1", j] for j in range(2, m+1))))0 -
Thank you Marika for your time. In fact, I'm trying to write the constraint provided in the inserted image. I tried your suggestion and defined auxiliary variable to the model, but the problem stays the same.
b1_{O+, 1, m} = (I1_{O+, 1, m+1} - (next demand OPM - \sum_{j=2}^{m+1}(I1_{O+, 1, j}))^{+})^{+}
0 -
Please provide a minimal reproducible example, see Tutorial: Preparing a Minimal Reproducible Example
0 -
import sys
import gurobipy as gp
from gurobipy import Model, GRB
from datetime import datetime
from gurobipy import quicksum
from gurobipy import max_
from gurobipy import LinExprnext_demands_OPM is a vector of 100 demand values.
m1 = list(range(42, 0, -1))
M = list(range(41, 0, -1))
z11 = {}
for r in blood_types:
for m in M:
for demand_index in range(100):
z11[r, "1", m, demand_index] = Issuing_model.addVar(lb=-GRB.INFINITY, ub=GRB.INFINITY, name=f'z11_{r}_{"1"}_{m}_{demand_index}')
z12 = {}
for r in blood_types:
for m in M:
for demand_index in range(100):
z12[r, "1", m, demand_index] = Issuing_model.addVar(lb=-GRB.INFINITY, ub=GRB.INFINITY, name=f'z12_{r}_{"1"}_{m}_{demand_index}')
z13 = {}
for r in blood_types:
for m in M:
for demand_index in range(100):
z13[r, "1", m, demand_index] = Issuing_model.addVar(lb=-GRB.INFINITY, ub=GRB.INFINITY, name=f'z13_{r}_{"1"}_{m}_{demand_index}')
z14 = {}
for r in blood_types:
for m in M:
for demand_index in range(100):
z14[r, "1", m, demand_index] = Issuing_model.addVar(lb=-GRB.INFINITY, ub=GRB.INFINITY, name=f'z14_{r}_{"1"}_{m}_{demand_index}')b1 = {}
for r in blood_types:
for m in M:
for demand_index in range(100):
b1[r, "1", m, demand_index] = Issuing_model.addVar(vtype=GRB.INTEGER, name=f'b1_{r}_{"1"}_{m}_{demand_index}')I1 = {}
for r in blood_types:
for m in m1:
I1[r, "1", m] = Issuing_model.addVar(vtype=GRB.INTEGER, name=f'I1_{r}_{"1"}_{m}')for demand_index, demand in enumerate(next_demands_OPM):
next_demand_OPM = demand
for m in M:
if m == 1:
Issuing_model.addConstr(z14["O+", "1", m, demand_index] == gp.max_(0, I1["O+", "1", m+1] - next_demand_OPM))
Issuing_model.addConstr(b1["O+", "1", m, demand_index] == z14["O+", "1", m, demand_index])
else:
Issuing_model.addConstr(z11["O+", "1", m, demand_index] == next_demand_OPM - gp.quicksum(I1["O+", "1", j] for j in range(2, m+1)))
Issuing_model.addConstr(z12["O+", "1", m, demand_index] == gp.max_(0, z11["O+", "1", m, demand_index]))
Issuing_model.addConstr(z13["O+", "1", m, demand_index] == I1["O+", "1", m+1] - z12["O+", "1", m, demand_index])
Issuing_model.addConstr(b1["O+", "1", m, demand_index] == gp.max_(0, z13["O+", "1", m, demand_index]))0 -
Please check how Model.addGenConstrMax() is used.
gp.max_(0, I1["O+", "1", m+1] - next_demand_OPM))
is not possible. The input has to be variables over which the MAX will be taken.
I1["O+", "1", m+1] - next_demand_OPM) is a linear expression and needs to be assigned to an (auxiliary) variable first.
So, you probably want to haveIssuing_model.addConstr(z14["O+", "1", m, demand_index] == I1["O+", "1", m+1] - next_demand_OPM)
Issuing_model.addConstr(b1["O+", "1", m, demand_index] == gp.max_(0, z14["O+", "1", m, demand_index]))0 -
Thank you so much Marika for your time and suggestions. That was so valuable to me.
0
投稿コメントは受け付けていません。
コメント
7件のコメント