Conditional logic in linear expression
AnsweredHi,
I have an linear expression with conditional logic. Gurobi doesn't complain but is giving wrong results. The linear expression essentially converts into a constant value.
Cost = quicksum(data[t].cost for t in range(T) if quicksum(X[q] for q in data[t]) >= 1)
where X is a binary decision variable.
Is there any other way to express this.
-
Official comment
Hi Ritesh,
you cannot directly model constraints conditionally on the result of another constraint.
Lets assume that you want to minimize your Cost. Then what you could do is add constraints for each t in T quicksum(X[q] for q in data[t]) - |data[t]|*y[t] <= 0 (|data[t]| being the cardinality of elements in data[t], y[t] an additional binary).
Then you objective function would be to minimize quicksum(data[t].cost * y[t] for t in range(T)). (Then the objective would always push y[t] down to zero if non of the X[q] with q in data[t] are one in a solution.)
Best,
Michael
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Hi Michael,
Thanks for the idea. That worked nicely.
Ritesh
0
Post is closed for comments.
Comments
3 comments