rev |
line source |
alpar@9
|
1 /* MAXFLOW, Maximum Flow Problem */
|
alpar@9
|
2
|
alpar@9
|
3 /* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
|
alpar@9
|
4
|
alpar@9
|
5 /* The Maximum Flow Problem in a network G = (V, E), where V is a set
|
alpar@9
|
6 of nodes, E within V x V is a set of arcs, is to maximize the flow
|
alpar@9
|
7 from one given node s (source) to another given node t (sink) subject
|
alpar@9
|
8 to conservation of flow constraints at each node and flow capacities
|
alpar@9
|
9 on each arc. */
|
alpar@9
|
10
|
alpar@9
|
11 param n, integer, >= 2;
|
alpar@9
|
12 /* number of nodes */
|
alpar@9
|
13
|
alpar@9
|
14 set V, default {1..n};
|
alpar@9
|
15 /* set of nodes */
|
alpar@9
|
16
|
alpar@9
|
17 set E, within V cross V;
|
alpar@9
|
18 /* set of arcs */
|
alpar@9
|
19
|
alpar@9
|
20 param a{(i,j) in E}, > 0;
|
alpar@9
|
21 /* a[i,j] is capacity of arc (i,j) */
|
alpar@9
|
22
|
alpar@9
|
23 param s, symbolic, in V, default 1;
|
alpar@9
|
24 /* source node */
|
alpar@9
|
25
|
alpar@9
|
26 param t, symbolic, in V, != s, default n;
|
alpar@9
|
27 /* sink node */
|
alpar@9
|
28
|
alpar@9
|
29 var x{(i,j) in E}, >= 0, <= a[i,j];
|
alpar@9
|
30 /* x[i,j] is elementary flow through arc (i,j) to be found */
|
alpar@9
|
31
|
alpar@9
|
32 var flow, >= 0;
|
alpar@9
|
33 /* total flow from s to t */
|
alpar@9
|
34
|
alpar@9
|
35 s.t. node{i in V}:
|
alpar@9
|
36 /* node[i] is conservation constraint for node i */
|
alpar@9
|
37
|
alpar@9
|
38 sum{(j,i) in E} x[j,i] + (if i = s then flow)
|
alpar@9
|
39 /* summary flow into node i through all ingoing arcs */
|
alpar@9
|
40
|
alpar@9
|
41 = /* must be equal to */
|
alpar@9
|
42
|
alpar@9
|
43 sum{(i,j) in E} x[i,j] + (if i = t then flow);
|
alpar@9
|
44 /* summary flow from node i through all outgoing arcs */
|
alpar@9
|
45
|
alpar@9
|
46 maximize obj: flow;
|
alpar@9
|
47 /* objective is to maximize the total flow through the network */
|
alpar@9
|
48
|
alpar@9
|
49 solve;
|
alpar@9
|
50
|
alpar@9
|
51 printf{1..56} "="; printf "\n";
|
alpar@9
|
52 printf "Maximum flow from node %s to node %s is %g\n\n", s, t, flow;
|
alpar@9
|
53 printf "Starting node Ending node Arc capacity Flow in arc\n";
|
alpar@9
|
54 printf "------------- ----------- ------------ -----------\n";
|
alpar@9
|
55 printf{(i,j) in E: x[i,j] != 0}: "%13s %11s %12g %11g\n", i, j,
|
alpar@9
|
56 a[i,j], x[i,j];
|
alpar@9
|
57 printf{1..56} "="; printf "\n";
|
alpar@9
|
58
|
alpar@9
|
59 data;
|
alpar@9
|
60
|
alpar@9
|
61 /* These data correspond to an example from [Christofides]. */
|
alpar@9
|
62
|
alpar@9
|
63 /* Optimal solution is 29 */
|
alpar@9
|
64
|
alpar@9
|
65 param n := 9;
|
alpar@9
|
66
|
alpar@9
|
67 param : E : a :=
|
alpar@9
|
68 1 2 14
|
alpar@9
|
69 1 4 23
|
alpar@9
|
70 2 3 10
|
alpar@9
|
71 2 4 9
|
alpar@9
|
72 3 5 12
|
alpar@9
|
73 3 8 18
|
alpar@9
|
74 4 5 26
|
alpar@9
|
75 5 2 11
|
alpar@9
|
76 5 6 25
|
alpar@9
|
77 5 7 4
|
alpar@9
|
78 6 7 7
|
alpar@9
|
79 6 8 8
|
alpar@9
|
80 7 9 15
|
alpar@9
|
81 8 9 20;
|
alpar@9
|
82
|
alpar@9
|
83 end;
|