Constraints in Gurobi
回答済みI have defined days of week at the start of my code as:
days_of_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
I also have set of:
constraint_days_Loc = ["Tuesday", "Wednesday", "Friday", "Saturday", "Sunday"]
and variables as:
y = {}
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"}')
yy = {}
for r in blood_types:
for i in num_hospitals:
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}')
in which r = ["O+", "O-"], and num_hospitals = ["1", "2"]
in a loop over days of the week as:
for day in days_of_week:
I want to check if the current day is in set constraint_days_Loc, for all "r":
y[r, "2", "42"] == 0
else:
y[r, "2", "42"] >= 0
if day in ["Tuesday", "Friday", "Saturday"]:
for all demand_index, yy[r, "2", "42", demand_index] == 0
else:
yy[r, "2", "42", demand_index] >= 0
I did it by the following constraints, but unfortunately gives wrong result:
if day.strip() in constraint_days_Loc:
for r in blood_types:
Issuing_model.addConstr(y[r, "2", "42"] == 0, name=f"non_order_days_Loc_{day}")
else:
for r in blood_types:
Issuing_model.addConstr(y[r, "2", "42"] >= 0, name = f"order_days_Loc_{day}")
# Checking whether tomorrow is an orde day or not in Local
if day.strip() in ["Tuesday", "Friday", "Saturday"]:
for demand_index in range(100):
Issuing_model.addConstr(yy[r, "2", "42", demand_index] == 0)
else:
for demand_index in range(100):
Issuing_model.addConstr(yy[r, "2", "42", demand_index] >= 0)
-
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:
- Ensure all blood types and hospitals are included in the loop when adding constraints.
- 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"] >= 0
yy[r, "2", "42", demand_index] >= 0Here 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}') # Add constraints 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:
- Iterates over
days_of_week
. - Checks if the current day is in
constraint_days_Loc
to set constraints fory
andyy
variables accordingly. - Adds constraints based on the conditions specified, ensuring all variables are iterated correctly.
Ensure that you replace
num_hospitals
andblood_types
with the correct sets if they are different in your actual implementation. This script ensures the constraints are applied correctly for each combination ofblood_types
andnum_hospitals
on specified days.0
サインインしてコメントを残してください。
コメント
1件のコメント