Modifying the capacity constraints in multiple scenarios
AnsweredHi there.
I have a base model and would like to run multiple scenarios on it.
Specifically, I have been trying to change the capacity constraints and variable objective coefficients which varies on the base model to fixed values on a given scenario.
Here is the capacity constraint on the base model:
c1 = model.addConstrs(gp.quicksum(demand[i] * x[i, j] for i in customers) <= capacity[j] * y[j] for j in destinations)
Where x and y are binary variables.
Now, I have tried to alter the RHS of this constraint using the multi-scenarios attributes as the following:
for j in destinations:
c1[j].ScenNRhs = fixed_capacity * y[j]
y[j].ScenNObj = fixed_cost
Where fixed_capacity and fixed_cost are now constant values.
But Gurobi returned:
AttributeError: Attribute 'ScenNRhs' takes double value
I thought that this error was caused by the iteration over c1, so to overcome that I run the following code
for j in destinations:
c1.ScenNRhs = fixed_capacity * y[j]
y[j].ScenNObj = fixed_cost
This last code ran without any error. After running and retrieving the solutions, I figured that not all destinations were captured by the modification on c1. In fact, c1 constraint was violated several times, capturing more demand than the capacity available by a given destination. That may be due to not iterating over the destinations in c1, but when I tried to iterate over c1, the previous error is returned.
I am sure something is missing here.
-
Hi Yuri,
To identify the RHS and the LHS of the constraint, you need to reformulate the constraint such that all variables are on the left-hand side and all constants are on the right-hand side. Reformulating the capacity constraints to
c1 = model.addConstrs(gp.quicksum(demand[i] * x[i, j] for i in customers) - capacity[j] * y[j] <= 0 for j in destinations)
gives a RHS of 0.
Changing the coefficient of a variable in a constraint is not directly possible when defining a scenario. You need to add two constraints and then deactivate one of them in each scenario. Deactivate here means to set the RHS such that the constraints are always satisfied.
Here is a rough idea:# define the demand constraints
c1 = model.addConstrs(gp.quicksum(demand[i] * x[i, j] for i in customers) <= capacity[j] * y[j] for j in destinations)
# define demand constraints with different capacity
c2 = model.addConstrs(gp.quicksum(demand[i] * x[i, j] for i in customers) <= fixed_capacity * y[j] for j in destinations)
# deactivate the c2 constraints (for the base scenario)
for j in destinations:
c2[j].Rhs = GRB.INFINITY# define scenario (deactivate c1 and activate c2)
for j in destinations:
c1[j].ScenNRhs = GRB.INFINITY
c2[j].ScenNRhs = 0I hope this helps,
Marika1
Please sign in to leave a comment.
Comments
1 comment