Constraint in Gurobi
OngoingI 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)
-
Hi,
My first thought is, did you properly link the y and yy variables through any additional constraint? If so, what exactly do you mean by "wrong result"?
Kind regards,
Ronald0 -
Thank you for your quick response. There is no more constraint linking y and yy, and by the wrong result I mean, Tuesday which is in constraint_days_Loc, is a non order day. Definitely, by the following constraint:
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}")the variable y[r, "2", "42"] which is order value, should be equal to zero, but the code gives it a positive value.
0 -
If y and yy are completely unrelated, this would suggest you could solve two independent models - one for y and one for yy. Is that the case? It would make debugging easier, too...
Regarding the issue with Tuesday: I noticed that variable y does not use day as an index. You only use day in the constraint name, but that is not sufficient - now all 7 days share the same variable y[r, "2", "42"]. Could you correct that and see if the problem remains?
0 -
Let's consider the constraint only for the variable "y". As I mentioned before, I have:
days_of_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
and:
constraint_days_Loc = ["Tuesday", "Wednesday", "Friday", "Saturday", "Sunday"]
r = ["O+", "O-"]
then:
for day in days_of_week:
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}")This loop starts from "Monday" to "Sunday", and adding these constraints will force the model to put y[r, "2", "42"] =0 for all "r" for Tuesday. But unfortunately, for "Tuesday" the variable gets a positive value.
0 -
I agree that y[r, "2", "42"] should be 0 and this would require some more debugging to understand. But I'm concerned there are other problems with the model. For example, you don't loop over the days when you create the y variables. So for certain days you want y[r, "2", "42"]=0 and for other days y[r, "2", "42"]>=0 while this is the exact same variable - there is no y variable for each day.
0 -
What if I have defined the variable "y" within the loop over days of the week. I mean:
for day in days_of_week: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"}')and the rest of the code which is adding constraints.
0 -
You would still store the variables for all days, in the same location in your y[] array. I would recommend spending some more time debugging this on your side - this could take a couple of hours. When you get stuck, please post a runnable, fully reproducible piece of code.
Kind regards,
Ronald0 -
In this case, a day_index should be represented for each day of the week and the variable "y", should be defined over all day_indexes, such as:
y = {}
for r in blood_types:
for i in num_hospitals:for day_index in range(6):
y[r, i, "42", day_index] = Issuing_model.addVar(vtype=GRB.INTEGER, name=f'y_{r}_{i}_{"42"}_{day_index}')
0
Please sign in to leave a comment.
Comments
8 comments