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.

77 lines
2.0 KiB

  1. /* ASSIGN, Assignment Problem */
  2. /* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
  3. /* The assignment problem is one of the fundamental combinatorial
  4. optimization problems.
  5. In its most general form, the problem is as follows:
  6. There are a number of agents and a number of tasks. Any agent can be
  7. assigned to perform any task, incurring some cost that may vary
  8. depending on the agent-task assignment. It is required to perform all
  9. tasks by assigning exactly one agent to each task in such a way that
  10. the total cost of the assignment is minimized.
  11. (From Wikipedia, the free encyclopedia.) */
  12. param m, integer, > 0;
  13. /* number of agents */
  14. param n, integer, > 0;
  15. /* number of tasks */
  16. set I := 1..m;
  17. /* set of agents */
  18. set J := 1..n;
  19. /* set of tasks */
  20. param c{i in I, j in J}, >= 0;
  21. /* cost of allocating task j to agent i */
  22. var x{i in I, j in J}, >= 0;
  23. /* x[i,j] = 1 means task j is assigned to agent i
  24. note that variables x[i,j] are binary, however, there is no need to
  25. declare them so due to the totally unimodular constraint matrix */
  26. s.t. phi{i in I}: sum{j in J} x[i,j] <= 1;
  27. /* each agent can perform at most one task */
  28. s.t. psi{j in J}: sum{i in I} x[i,j] = 1;
  29. /* each task must be assigned exactly to one agent */
  30. minimize obj: sum{i in I, j in J} c[i,j] * x[i,j];
  31. /* the objective is to find a cheapest assignment */
  32. solve;
  33. printf "\n";
  34. printf "Agent Task Cost\n";
  35. printf{i in I} "%5d %5d %10g\n", i, sum{j in J} j * x[i,j],
  36. sum{j in J} c[i,j] * x[i,j];
  37. printf "----------------------\n";
  38. printf " Total: %10g\n", sum{i in I, j in J} c[i,j] * x[i,j];
  39. printf "\n";
  40. data;
  41. /* These data correspond to an example from [Christofides]. */
  42. /* Optimal solution is 76 */
  43. param m := 8;
  44. param n := 8;
  45. param c : 1 2 3 4 5 6 7 8 :=
  46. 1 13 21 20 12 8 26 22 11
  47. 2 12 36 25 41 40 11 4 8
  48. 3 35 32 13 36 26 21 13 37
  49. 4 34 54 7 8 12 22 11 40
  50. 5 21 6 45 18 24 34 12 48
  51. 6 42 19 39 15 14 16 28 46
  52. 7 16 34 38 3 34 40 22 24
  53. 8 26 20 5 17 45 31 37 43 ;
  54. end;