Affect binary value only when continuous variable is negative
AnsweredHello,
Is it possible to create a constraint where a continuous variable can only affect a binary variable's value if the continuous variable is negative? I want to replace a hard-coded value and make it into a variable. From hard-coded TW_end:
m.addConstrs(((x[k, l, i] == 0) for i in N for l in L for k in K if TW_end[i][k] <= 0), name="fruitStartPassed")
To calculating tw_e from the variable v_vy (GRB.INTEGER variable between 1 and 5 cm/s) and some parameters :
m.addConstrs(((tw_e[k, i] * (v_vy / 100) == (Y[i] - (self.q_vy + k*self.cell_l))) for i in N for k in K), name="TW_end")
then adding the constraint to cause x[k,l,i] to be 0 if tw_e < 0:
m.addConstrs(((tw_e[k, i] >= -t_move * (1 - x[k, l, i]) + (0.0001 * x[k, l, i])) for i in N for l in L for k in K), name="fruitStartPassed")
where -t_move is the lower bound for tw_e, and t_move is obtained from:
m.addConstr((t_move * (v_vy / 100) == self.travel_l), name="movementTime" )
where v_vy is the same variable described above and travel_l is a parameter. The constraint is based on the answer in https://or.stackexchange.com/questions/5860/link-a-binary-variable-to-continuous-variable-in-java-gurobi by Oguz Toragay. I tried only using the second constraint which I think theoretically forces x[k,l,i] = 0 if tw_e < 0, while avoiding the first constraint because I do not want to force x[k,l,i] = 1 otherwise.
When the constraint with t_move as a variable comes into effect, it results in an infeasible result. If it doesn't have to be used because all tw_e values are positive, the solver runs fine. If I hard-code t_move (to usually be too big), the constraint works fine. Is this because the constraint is the wrong one to use in this setting? If so, is there a constraint I could use?
Thank you,
Natalie
-
Official comment
This post is more than two 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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Natalie,
The constraint looks correct as along as the variable bounds of all constraints are set correctly, i.e., t_move >= 0 and tw_e has a non-default negative lower bound.
When the constraint with t_move as a variable comes into effect, it results in an infeasible result.
Did you try to find the cause of infeasibility as described in How do I determine why my model is infeasible?
Best regards,
Jaromił0 -
Hi Jaromil,
The link you provided helped me determine that the lower bound I was using was wrong. I kept having trouble with that until I found a simpler version of the constraints which work:
m.addConstrs(((x[k, l, i] == 0) for i in N for l in L for k in K if Y[i] - (self.q_vy + (k + 1)*self.cell_l) >= self.travel_l), name="fruitInHorizon")
m.addConstrs(((x[k, l, i] == 0) for i in N for l in L for k in K if Y[i] - (self.q_vy + k*self.cell_l) <= 0 ), name="fruitStartPassed")This removed the need for the problematic lower bounds and the extra auxiliary variable t_move.
Thank you!
Natalie
0
Post is closed for comments.
Comments
3 comments