model give different results after adding constraint to a non-used variable
AnsweredHello,
I found that the model give different results after adding a constraint to a variable which is not used in any objective functions.
I am trying to find the optimal value for `x` with 2 objective functions. `x` is define as:
x = model.addVars(n,m, vtype=GRB.BINARY)
d = model.addVars(n, vtype=GRB.BINARY)
for i inrange(n):
model.addGenConstrIndicator(d[i], True, x.sum(i, '*'), GRB.GREATER_EQUAL, 1.0)
for i inrange(n):
model.addGenConstrIndicator(d[i], False, x.sum(i, '*'), GRB.EQUAL, 0.0)
-
Hi Danning,
The feasible space for an optimization model is defined by all its constraints, regardless of whether their associated decision variables directly appear in the objective function or not. In the example you posted, the value of the decision variables \(d\) constraint the values that the decision variables \(x\) can take, and consequently can change the objective function defined over decision variables \(x\).
Best regards,
Maliheh
0 -
Hi Maliheh,
Thank you for the explanation.
However, I don't really understand why such a constraint would affect the value space of variable `x`. Since `d` is not associated with any objective functions, its value should not be considered during solving the problem. And I think the value of `d` should be totally dependent on the value of `x`. Could you please give me some intuition or example about why `d` is affecting `x`? That would be helpful.
Best,
Danning
0 -
Hi Danning,
My point was that if a decision variable does not appear in the objective function, it does not mean that the decision variable does not play a role in defining the feasible solution space.
I have not seen your whole model. If your model looks like the example code below, you are right that the variables \(d\) will not change the optimal objective value. However, this leaves the main question of why the variables \(d\) are even defined open.
import gurobipy as gp
from gurobipy import GRB
if __name__ == "__main__":
model = gp.Model()
n, m = 3, 4
x = model.addVars(n, m, vtype=GRB.BINARY)
model.setObjective(x.sum(), GRB.MINIMIZE)
d = model.addVars(n, vtype=GRB.BINARY)
for i in range(n):
model.addGenConstrIndicator(d[i], True, x.sum(i, "*"), GRB.GREATER_EQUAL, 1.0)
for i in range(n):
model.addGenConstrIndicator(d[i], False, x.sum(i, "*"), GRB.EQUAL, 0.0)If you add the constraint below to the example code above, the optimal objective function value changes from 0 to 1. This is an example of the case where the decision variables \(d\) do not appear in the objective function, but impose constraints on variables \(x\) and consequently change the objective function.
model.addConstr(d[0] + d[1] == 1)
Best regards,
Maliheh
0
Please sign in to leave a comment.
Comments
3 comments