Calculation of a variable with another variable (VRP)
AnsweredHello everyone,
I am currently modeling a VRP with soft time windows and have a problem calculating one variable. \(\texttt{t[i]}\) is the arrival time of the vehicle at customer i, \(\texttt{et[i]}\) and \(\texttt{lt[i]}\) are the lower and upper limit of the time windows. The vehicle doesn´t have to arrive at the customers within the time window, so there is no constraint setting \(\texttt{et[i] <= t[i] <= lt[i]}\).
I want to check during the optimization process, if the vehicle arrives within the time window and if so, a variable \(\texttt{u[i]}\) should take the value 1 (0 otherwise).
I tried modeling it with if constraints, but I always get the error:
GurobiError: Constraint has no bool value (are you trying "lb <= expr <= ub"?)
I also tried modeling it with a piecewise linear function, but it get the following error:
\(\texttt{mdl.addGenConstrPWL((t[i], u[i], [et[i], et[i], lt[i], lt[i]], [0, 1, 1, 0]) for i in N)}\)
TypeError: addGenConstrPWL() takes at least 5 positional arguments (2 given)
Is there an easy way to determine the value of \(\texttt{u[i]}\)?
Thank you for your help, it is very much appreciated.
Best regards,
Lars
-
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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Lars,
The KB article How do I model conditional statements in Gurobi? describes one possible solution to your issue.
You would have to define 2 new binary variables \(b_{1,i},b_{2,i}\) and model the relationships \(\texttt{if et[i] <= t[i] then b1[i] = 1 else b1[i] = 0}\) and \(\texttt{if t[i] <= lt[i] then b2[i] = 1 else b2[i] = 0}\).
You can then use Gurobi's addGenConstrAnd method to model \(\texttt{if b1[i]=1 and b2[i]=1 then u[i] = 1 else u[i] = 0}\).Best regards,
Jaromił0 -
Hi Jaromił,
thank you for your answer. I tried implementing your idea based on the article you linked like this:
mdl.addConstrs((t[i] >= et[i] + 0.0001 - M * (1 - ul[i])) for i in N)
mdl.addConstrs((t[i] <= et[i] + M * ul[i]) for i in N)
mdl.addConstrs((lt[i] >= t[i] + 0.0001 - M * (1 - uu[i])) for i in N)
mdl.addConstrs((lt[i] <= t[i] + M * uu[i]) for i in N)
mdl.addGenConstrAnd((u[i], [uu[i], ul[i]], "andconstr") for i in N)When I try to run the model, I get the error:
TypeError: addGenConstrAnd() takes at least 3 positional arguments (2 given)What is the problem with the code?
Best regards,
Lars
0 -
Hi Lars,
You have to respect the syntax of the addGenConstrAnd method. It adds only 1 constraint at a time. Thus, your code should look similar to
for i in N:
mdl.addGenConstrAnd(u[i], [uu[i], ul[i]], "andconstr")Alternatively, you can use the addConstrs overload and write
mdl.addConstrs(u[i] == and_([uu[i], ul[i]]) for i in N, "addconstr")
Best regards,
Jaromił0 -
Hi Jaromił,
thank your for your help! The code is now working the way I want it to work.
Best regards,
Lars0
Post is closed for comments.
Comments
5 comments