lemon-project-template-glpk

view deps/glpk/examples/maxcut.mod @ 11:4fc6ad2fb8a6

Test GLPK in src/main.cc
author Alpar Juttner <alpar@cs.elte.hu>
date Sun, 06 Nov 2011 21:43:29 +0100
parents
children
line source
1 /* MAXCUT, Maximum Cut Problem */
3 /* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
5 /* The Maximum Cut Problem in a network G = (V, E), where V is a set
6 of nodes, E is a set of edges, is to find the partition of V into
7 disjoint sets V1 and V2, which maximizes the sum of edge weights
8 w(e), where edge e has one endpoint in V1 and other endpoint in V2.
10 Reference:
11 Garey, M.R., and Johnson, D.S. (1979), Computers and Intractability:
12 A guide to the theory of NP-completeness [Network design, Cuts and
13 Connectivity, Maximum Cut, ND16]. */
15 set E, dimen 2;
16 /* set of edges */
18 param w{(i,j) in E}, >= 0, default 1;
19 /* w[i,j] is weight of edge (i,j) */
21 set V := (setof{(i,j) in E} i) union (setof{(i,j) in E} j);
22 /* set of nodes */
24 var x{i in V}, binary;
25 /* x[i] = 0 means that node i is in set V1
26 x[i] = 1 means that node i is in set V2 */
28 /* We need to include in the objective function only that edges (i,j)
29 from E, for which x[i] != x[j]. This can be modeled through binary
30 variables s[i,j] as follows:
32 s[i,j] = x[i] xor x[j] = (x[i] + x[j]) mod 2, (1)
34 where s[i,j] = 1 iff x[i] != x[j], that leads to the following
35 objective function:
37 z = sum{(i,j) in E} w[i,j] * s[i,j]. (2)
39 To describe "exclusive or" (1) we could think that s[i,j] is a minor
40 bit of the sum x[i] + x[j]. Then introducing binary variables t[i,j],
41 which represent a major bit of the sum x[i] + x[j], we can write:
43 x[i] + x[j] = s[i,j] + 2 * t[i,j]. (3)
45 An easy check shows that conditions (1) and (3) are equivalent.
47 Note that condition (3) can be simplified by eliminating variables
48 s[i,j]. Indeed, from (3) it follows that:
50 s[i,j] = x[i] + x[j] - 2 * t[i,j]. (4)
52 Since the expression in the right-hand side of (4) is integral, this
53 condition can be rewritten in the equivalent form:
55 0 <= x[i] + x[j] - 2 * t[i,j] <= 1. (5)
57 (One might note that (5) means t[i,j] = x[i] and x[j].)
59 Substituting s[i,j] from (4) to (2) leads to the following objective
60 function:
62 z = sum{(i,j) in E} w[i,j] * (x[i] + x[j] - 2 * t[i,j]), (6)
64 which does not include variables s[i,j]. */
66 var t{(i,j) in E}, binary;
67 /* t[i,j] = x[i] and x[j] = (x[i] + x[j]) div 2 */
69 s.t. xor{(i,j) in E}: 0 <= x[i] + x[j] - 2 * t[i,j] <= 1;
70 /* see (4) */
72 maximize z: sum{(i,j) in E} w[i,j] * (x[i] + x[j] - 2 * t[i,j]);
73 /* see (6) */
75 data;
77 /* In this example the network has 15 nodes and 22 edges. */
79 /* Optimal solution is 20 */
81 set E :=
82 1 2, 1 5, 2 3, 2 6, 3 4, 3 8, 4 9, 5 6, 5 7, 6 8, 7 8, 7 12, 8 9,
83 8 12, 9 10, 9 14, 10 11, 10 14, 11 15, 12 13, 13 14, 14 15;
85 end;