メインコンテンツへスキップ

## コメント

6件のコメント

• Gurobi Staff

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))))

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}))^{+})^{+}

• Gurobi Staff

Please provide a minimal reproducible example, see Tutorial: Preparing a Minimal Reproducible Example

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 LinExpr

next_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]))

• Gurobi Staff

gp.max_(0, I1["O+", "1", m+1] - next_demand_OPM))
Issuing_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]))