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

## コメント

1件のコメント

• Gurobi Staff

Your approach is generally correct, but there are a few issues with how the constraints are being added and iterated. Here's a revised version of your code to ensure that constraints are applied correctly based on the day of the week:

1. Ensure all blood types and hospitals are included in the loop when adding constraints.
2. Correctly handle the indexing of yy variables within the inner loop.

Note that the default lower bound for variables in Gurobi is zero. Assuming y and yy variables cannot take negative values, there is no need to add constraints for the conditions.

y[r, "2", "42"] >= 0yy[r, "2", "42", demand_index] >= 0

Here is the revised version:

from gurobipy import Model, GRB

# Define sets
days_of_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
constraint_days_Loc = ["Tuesday", "Wednesday", "Friday", "Saturday", "Sunday"]
blood_types = ["O+", "O-"]
num_hospitals = ["1", "2"]

# Initialize model
Issuing_model = Model()

# Define variables
y = {}
yy = {}
for r in blood_types:
for i in num_hospitals:
y[r, i, "42"] = Issuing_model.addVar(vtype=GRB.INTEGER, name=f'y_{r}_{i}_42')
for demand_index in range(100):
yy[r, i, "42", demand_index] = Issuing_model.addVar(vtype=GRB.INTEGER, name=f'yy_{r}_{i}_42_{demand_index}')

for day in days_of_week:
if day in constraint_days_Loc:
for r in blood_types:
Issuing_model.addConstr(y[r, "2", "42"] == 0, name=f"non_order_days_Loc_{day}_{r}")

if day in ["Tuesday", "Friday", "Saturday"]:
for r in blood_types:
for demand_index in range(100):
Issuing_model.addConstr(yy[r, "2", "42", demand_index] == 0, name=f"non_order_days_yy_{day}_{r}_{demand_index}")

# Optimize model
Issuing_model.optimize()


This code:

1. Iterates over days_of_week.
2. Checks if the current day is in constraint_days_Loc to set constraints for y and yy variables accordingly.
3. Adds constraints based on the conditions specified, ensuring all variables are iterated correctly.

Ensure that you replace num_hospitals and blood_types with the correct sets if they are different in your actual implementation. This script ensures the constraints are applied correctly for each combination of blood_types and num_hospitals on specified days.

サインインしてコメントを残してください。