Problems about general constrains
AnsweredHi, all
I'm working with a traffic signal optimization model and I faced some problems with the general constrains.
In my model, variable Sending must be the minimum value of three other variables, so at first I define two binary variables zx3 and zx4 that ensure Sending = min{N[i,0,fftt] - N[i,1,t-1], PQ[i], w/v*(Kjam[i] - n[i,1,t])}. A feasible solution can be obtained.
Where PQ, w, v and Kjam are constants, N and n are continuous variables.
Origin code:
if fftt <= 0:
m.addConstr(Sending[i,t-1] <= 0 - N[i,1,t-1])
m.addConstr(Sending[i,t-1] >= 0 - N[i,1,t-1] - M * zx3[i,t])
else:
m.addConstr(Sending[i,t-1] <= N[i,0,fftt] - N[i,1,t-1])
m.addConstr(Sending[i,t-1] >= N[i,0,fftt] - N[i,1,t-1] - M *zx3[i,t])
m.addConstr(Sending[i,t-1] <= PQ[i])
m.addConstr(Sending[i,t-1] >= PQ[i] - M * zx4[i,t])
m.addConstr(Sending[i,t-1] <= w/v * (Kjam[i] - n[i,1,t]))
m.addConstr(Sending[i,t-1] >= w/v * (Kjam[i] - n[i,1,t]) - M * (2 - zx3[i,t] - zx4[i,t]))
Then, I try to use genral constrains to simplify my code, and the code use general constrains is shown below.
Two variables space and freeflow are added to represent w/v * (Kjam[i] - n[i,1,t]) and N[i,0,fftt] - N[i,1,t-1]
m.addConstr(space[i,1,t] == w/v * (Kjam[i] - n[i,1,t]))
if fftt <= 0:
m.addConstr(freeflow[i,t-1] == 0 - N[i,1,t-1])
else:
m.addConstr(freeflow[i,t-1] == N[i,0,fftt] - N[i,1,t-1])
m.addGenConstrMin(Sending[i,t-1], [freeflow[i,t-1], PQ[i], space[i,1,t]])
However, there is no feasible solutions with this code. I wonder if anything gets wrong with my code or the model?
-
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 Liu,
The two formulations both look okay to me. Could you post a small code snippet that has the same result (one model feasible, the other infeasible), but includes the definitions of all variables/constants? Ideally, this snippet would build a smaller version of the model, so the code is easier to look through. Maybe you can narrow down the problem to a single value of \( i \) and/or \( t \).
It would also help if you used the "code" style to format the code so the Python indenting isn't lost.
Thanks,
Eli
0 -
Hi Eli,
Thanks for your response.
Here is the code that includes definitions of these variables.
U = [11, 12, 25, 26, 27, 28, 29, 30]
maxcell = 2
Tc = 100
n = {}
space = {}
for i in U:
for j in range(1,maxcell):
for t in range(1, Tc+2):
n[i,j,t] = m.addVar(lb = 0.0, ub = Kjam[i], vtype=GRB.CONTINUOUS, name='n%s_%s_%s'%(i,j,t))
space[i,j,t] = m.addVar(vtype=GRB.BINARY, name='space%s_%s_%s'%(i,j,t))
N = {}
G = {}
Sending = {}
zx3 = {}
zx4 = {}
freeflow = {}
for i in U:
for t in range(Tc+1):
for j in [0,1]:
N[i,j,t] = m.addVar(lb = 0.0, vtype=GRB.CONTINUOUS, name='N%s_%s_%s'%(i,j,t))
G[i,t] = m.addVar(lb = 0.0, vtype=GRB.CONTINUOUS, name='G%s_%s'%(i,t))
Sending[i,t] = m.addVar(lb = 0.0, vtype=GRB.CONTINUOUS, name='S%s_%s'%(i,t))
zx3[i,t] = m.addVar( vtype=GRB.BINARY, name='z3%s_%s'%(i,t))
zx4[i,t] = m.addVar( vtype=GRB.BINARY, name='z4%s_%s'%(i,t))
freeflow[i,t] = m.addVar(lb = 0.0, vtype=GRB.CONTINUOUS, name='ff%s_%s'%(i,t))And I have try to change Tc from 100 to 40, which is the cycle of traffic signal and it can't be lesser than 40. But there is still no feasible solution. Also, i may can't be a single value because an intersection has 4 branches of roads and 8 different directions so the minimum size of list U is 8.
If you need more information, you may access my github to get a runnable version of the complete code. You can find definition of these variables at line 875 to 918, and general constrains can be found at line 993 to 998. Line 1000 to 1011 is the commented code that not use general constrains, a feasible solution can be obtained with these constrains.
My github: https://github.com/yanyueliu/Lane_based_model
Best regards,
Liu
0 -
Hi Liu,
The definition of the \( \texttt{space} \) variables doesn't look right:
space[i,j,t] = m.addVar(vtype=GRB.BINARY, name='space%s_%s_%s'%(i,j,t))
It looks like these should be continuous (and maybe unbounded with \( \texttt{lb=-GRB.INFINITY} \)).
Thanks,
Eli
0 -
Hi Eli,
Thank you for pointing out my mistake.
I am so careless and when I define space I just copied code above without change and check.
The problem is solved after variable type is changed as contiuous.
Best regards,
Liu
0
Post is closed for comments.
Comments
5 comments