Hi everyone,
I hope you are good. I need help about modeling.
Lets say we have a graph as you can see in Attachments.
I want to see minimum path length and i want to show minimum path such as 1 > 2 > 3 > 6 > 5 > 4.
But i also want to pass from every road (edge). In the example which is shown in Attachments we have 9 road. We can pass from same node twice or more. How can we generate this kind of route?
Thank you very much.
Minimum Route
Minimum Route
 Attachments

 a.png (6.24 KiB) Viewed 144 times
Last edited by Merve 3 weeks ago, edited 1 time in total.
Re: Minimum Route
Hi,
Not sure if I fully understand what you are asking for.
Sounds as if you want to compute a Hamiltonian path (https://en.wikipedia.org/wiki/Hamiltonian_path) and/or a Eulerian Path (https://en.wikipedia.org/wiki/Eulerian_path)?
It will increase your chances to get help if you elaborate your problem and show that you already made some effort to implement a solution yourself (e.g. by sharing some code, even if it is not yet working).
Best,
Fred
Not sure if I fully understand what you are asking for.
Sounds as if you want to compute a Hamiltonian path (https://en.wikipedia.org/wiki/Hamiltonian_path) and/or a Eulerian Path (https://en.wikipedia.org/wiki/Eulerian_path)?
It will increase your chances to get help if you elaborate your problem and show that you already made some effort to implement a solution yourself (e.g. by sharing some code, even if it is not yet working).
Best,
Fred
Re: Minimum Route
Hi,
Thanks for answering me. I am trying to generate a route for sidewalk washing Car. Car should wash every sidewalk. So the car should pass every sidewalk. It is like chinese postman problem. But it is not necessary to turn starting point. It should return or not, doesnt matter. And sometimes Car can pass from same sidewalk twice or more because there couldn't be another option when we extend the graph. And Car should pass minimum time from sidewalk. If the car needs to pass same sidewalk, Number of passing must be fewer as soon as possible.
I tried with a directed graph shown in attachments. Models code is below. It gives me optimum lenght but doesnt give a route. I hope i explained myself more clear This time.
Variable x18, x81, x87, x78, x12, x21, x89, x98, x67, x76, x29, x92, x96, x69, x32, x23, x94, x49, x56, x65, x34, x43, x45, x54;
Variables z;
Equation amac, eq1, eq2, eq3, eq4, eq5, eq6, eq7, eq8, eq9, eq10, eq11, eq12, eq13, eq14, eq15, eq16, eq17, eq18, eq19, eq20, eq21;
amac.. z=e= 2*(x18+x81)+4*(x87+x78)+5*(x12+x21)+3*(x89+x98)+3*(x67+x76)+6*(x29+x92)+4*(x96+x69)+5*(x32+x23)+4*(x94+x49)+4*(x56+x65)+9*(x34+x43)+4*(x45+x54);
eq1.. x21+x81x12x18 =e= 0;
eq2.. x12+x92+x32x21x29x23 =e= 0;
eq3.. x23+x43x32x34 =e= 0;
eq4.. x34+x94+x54x43x49x45 =e= 0;
eq5.. x65+x45x56x54 =e= 0;
eq6.. x76+x96+x56x67x69x65 =e= 0;
eq7.. x67+x87x76x78 =e= 0;
eq8.. x18+x78+x98x81x87x89 =e= 0;
eq9.. x29+x49+x69+x89x92x94x96x98 =e=0;
eq10.. x18+x81 =g= 2;
eq11.. x87+x78 =g= 2;
eq12.. x12+x21 =g= 2;
eq13.. x89+x98 =g= 2;
eq14.. x67+x76 =g= 2;
eq15.. x29+x92 =g= 2;
eq16.. x96+x69 =g= 2;
eq17.. x23+x32 =g= 2;
eq18.. x94+x49 =g= 2;
eq19.. x56+x65 =g= 2;
eq20.. x34+x43 =g= 2;
eq21.. x45+x54 =g= 2;
Model MIP1 / all /;
solve MIP1 using mip minimizing z;
Thanks for answering me. I am trying to generate a route for sidewalk washing Car. Car should wash every sidewalk. So the car should pass every sidewalk. It is like chinese postman problem. But it is not necessary to turn starting point. It should return or not, doesnt matter. And sometimes Car can pass from same sidewalk twice or more because there couldn't be another option when we extend the graph. And Car should pass minimum time from sidewalk. If the car needs to pass same sidewalk, Number of passing must be fewer as soon as possible.
I tried with a directed graph shown in attachments. Models code is below. It gives me optimum lenght but doesnt give a route. I hope i explained myself more clear This time.
Variable x18, x81, x87, x78, x12, x21, x89, x98, x67, x76, x29, x92, x96, x69, x32, x23, x94, x49, x56, x65, x34, x43, x45, x54;
Variables z;
Equation amac, eq1, eq2, eq3, eq4, eq5, eq6, eq7, eq8, eq9, eq10, eq11, eq12, eq13, eq14, eq15, eq16, eq17, eq18, eq19, eq20, eq21;
amac.. z=e= 2*(x18+x81)+4*(x87+x78)+5*(x12+x21)+3*(x89+x98)+3*(x67+x76)+6*(x29+x92)+4*(x96+x69)+5*(x32+x23)+4*(x94+x49)+4*(x56+x65)+9*(x34+x43)+4*(x45+x54);
eq1.. x21+x81x12x18 =e= 0;
eq2.. x12+x92+x32x21x29x23 =e= 0;
eq3.. x23+x43x32x34 =e= 0;
eq4.. x34+x94+x54x43x49x45 =e= 0;
eq5.. x65+x45x56x54 =e= 0;
eq6.. x76+x96+x56x67x69x65 =e= 0;
eq7.. x67+x87x76x78 =e= 0;
eq8.. x18+x78+x98x81x87x89 =e= 0;
eq9.. x29+x49+x69+x89x92x94x96x98 =e=0;
eq10.. x18+x81 =g= 2;
eq11.. x87+x78 =g= 2;
eq12.. x12+x21 =g= 2;
eq13.. x89+x98 =g= 2;
eq14.. x67+x76 =g= 2;
eq15.. x29+x92 =g= 2;
eq16.. x96+x69 =g= 2;
eq17.. x23+x32 =g= 2;
eq18.. x94+x49 =g= 2;
eq19.. x56+x65 =g= 2;
eq20.. x34+x43 =g= 2;
eq21.. x45+x54 =g= 2;
Model MIP1 / all /;
solve MIP1 using mip minimizing z;
Re: Minimum Route
Hi,
First of all, the beauty of modeling languages like GAMS lies in its concise syntax which allows you to write compact indexed optimization models instead of huge incomprehensible scalar models.
I assume your variables xij should be interpreted as "how often does the arc between nodes i and j need to be traversed". I don't see how the model fits with the picture but interpreted like this, you are working on connected graph which makes sense. Implemented in the aforementioned indexed format, this could be implemented with 2 (indexed) variables and 3 (indexed) equations such that data and model become independent.
Still, that does not give you the actual tour but at least it might give you some idea on how to use GAMS properly.
Regarding the tour: The model seems to make certain assumptions (like working on a connected graph) and if they are fulfilled it tells you how often you would need to traverse which edge in which direction to clean every sidewalk (i.e. every edge must be traversed twice) in an optimal solution. But I don't see how this really defines a unique optimal tour. There are most likely many different optimal tours with equal distance.
If you invest a little more work, you might be able to construct an optimal tour from the MIP solution.
Best,
Fred
First of all, the beauty of modeling languages like GAMS lies in its concise syntax which allows you to write compact indexed optimization models instead of huge incomprehensible scalar models.
I assume your variables xij should be interpreted as "how often does the arc between nodes i and j need to be traversed". I don't see how the model fits with the picture but interpreted like this, you are working on connected graph which makes sense. Implemented in the aforementioned indexed format, this could be implemented with 2 (indexed) variables and 3 (indexed) equations such that data and model become independent.
Code: Select all
set i nodes / 1*9, a,b /
e(i,i) arcs / 1.8, 7.8, 1.2, 8.9, 6.7, 2.9, 6.9, 2.3, 4.9, 5.6, 3.4, 4.5 ,a.b/
;
parameter c(i,i) cost / 1.8 2, 7.8 4, 1.2 5, 8.9 3, 6.7 3, 2.9 6, 6.9 4, 2.3 5, 4.9 4, 5.6 4, 3.4 9, 4.5 4 , a.b 99/
;
alias(i,j);
e(i,j)$[ord(i)>ord(j)] = e(j,i);
c(i,j)$[ord(i)>ord(j)] = c(j,i);
display e,c;
integer variable x;
variable z;
equation defz 'define objective'
flowCon(i) 'flow conservation'
bothSides(i,i) 'clean both sides of sidewalk (i.e. travel edge twice)'
;
defz.. z =e= sum(e(i,j), c(i,j)*x(i,j));
flowCon(i).. sum(e(j,i), x(j,i))  sum(e(i,j), x(i,j)) =e= 0;
bothSides(e(i,j)).. x(i,j) + x(j,i) =g= 2;
model m / all /;
solve m min z use mip;
Regarding the tour: The model seems to make certain assumptions (like working on a connected graph) and if they are fulfilled it tells you how often you would need to traverse which edge in which direction to clean every sidewalk (i.e. every edge must be traversed twice) in an optimal solution. But I don't see how this really defines a unique optimal tour. There are most likely many different optimal tours with equal distance.
If you invest a little more work, you might be able to construct an optimal tour from the MIP solution.
Best,
Fred