You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.7 KiB

  1. /* SPP, Shortest Path Problem */
  2. /* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
  3. /* Given a directed graph G = (V,E), its edge lengths c(i,j) for all
  4. (i,j) in E, and two nodes s, t in V, the Shortest Path Problem (SPP)
  5. is to find a directed path from s to t whose length is minimal. */
  6. param n, integer, > 0;
  7. /* number of nodes */
  8. set E, within {i in 1..n, j in 1..n};
  9. /* set of edges */
  10. param c{(i,j) in E};
  11. /* c[i,j] is length of edge (i,j); note that edge lengths are allowed
  12. to be of any sign (positive, negative, or zero) */
  13. param s, in {1..n};
  14. /* source node */
  15. param t, in {1..n};
  16. /* target node */
  17. var x{(i,j) in E}, >= 0;
  18. /* x[i,j] = 1 means that edge (i,j) belong to shortest path;
  19. x[i,j] = 0 means that edge (i,j) does not belong to shortest path;
  20. note that variables x[i,j] are binary, however, there is no need to
  21. declare them so due to the totally unimodular constraint matrix */
  22. s.t. r{i in 1..n}: sum{(j,i) in E} x[j,i] + (if i = s then 1) =
  23. sum{(i,j) in E} x[i,j] + (if i = t then 1);
  24. /* conservation conditions for unity flow from s to t; every feasible
  25. solution is a path from s to t */
  26. minimize Z: sum{(i,j) in E} c[i,j] * x[i,j];
  27. /* objective function is the path length to be minimized */
  28. data;
  29. /* Optimal solution is 20 that corresponds to the following shortest
  30. path: s = 1 -> 2 -> 4 -> 8 -> 6 = t */
  31. param n := 8;
  32. param s := 1;
  33. param t := 6;
  34. param : E : c :=
  35. 1 2 1
  36. 1 4 8
  37. 1 7 6
  38. 2 4 2
  39. 3 2 14
  40. 3 4 10
  41. 3 5 6
  42. 3 6 19
  43. 4 5 8
  44. 4 8 13
  45. 5 8 12
  46. 6 5 7
  47. 7 4 5
  48. 8 6 4
  49. 8 7 10;
  50. end;