Skip to main content

Constraint in Gurobi

Ongoing

Comments

8 comments

  • Ronald van der Velden
    Gurobi Staff Gurobi Staff

    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,
    Ronald

    0
  • Shahrzad Valizadeh
    Curious
    Conversationalist

    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
  • Ronald van der Velden
    Gurobi Staff Gurobi Staff

    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
  • Shahrzad Valizadeh
    Curious
    Conversationalist

    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
  • Ronald van der Velden
    Gurobi Staff Gurobi Staff

    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
  • Shahrzad Valizadeh
    Curious
    Conversationalist

    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
  • Ronald van der Velden
    Gurobi Staff Gurobi Staff

    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,
    Ronald

    0
  • Shahrzad Valizadeh
    Curious
    Conversationalist

    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.