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