ladanyi@1919
|
1 |
/* -*- C++ -*-
|
ladanyi@1919
|
2 |
* demo/simann_maxcut_demo.cc - Part of LEMON, a generic C++ optimization
|
ladanyi@1919
|
3 |
* library
|
ladanyi@1919
|
4 |
*
|
ladanyi@1919
|
5 |
* Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
ladanyi@1919
|
6 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
ladanyi@1919
|
7 |
*
|
ladanyi@1919
|
8 |
* Permission to use, modify and distribute this software is granted
|
ladanyi@1919
|
9 |
* provided that this copyright notice appears in all copies. For
|
ladanyi@1919
|
10 |
* precise terms see the accompanying LICENSE file.
|
ladanyi@1919
|
11 |
*
|
ladanyi@1919
|
12 |
* This software is provided "AS IS" with no warranty of any kind,
|
ladanyi@1919
|
13 |
* express or implied, and with no claim as to its suitability for any
|
ladanyi@1919
|
14 |
* purpose.
|
ladanyi@1919
|
15 |
*
|
ladanyi@1919
|
16 |
*/
|
ladanyi@1919
|
17 |
|
ladanyi@1919
|
18 |
/// \ingroup demos
|
ladanyi@1919
|
19 |
/// \file
|
ladanyi@1919
|
20 |
/// \brief A program demonstrating the simulated annealing algorithm class.
|
ladanyi@1919
|
21 |
///
|
ladanyi@1919
|
22 |
/// This program tries to find a maximal cut in a graph using simulated
|
ladanyi@1919
|
23 |
/// annealing. It starts from a random solution and then in each step it
|
ladanyi@1919
|
24 |
/// chooses a node and moves it to the other side of the cut.
|
ladanyi@1919
|
25 |
///
|
ladanyi@1919
|
26 |
/// \include simann_maxcut_demo.cc
|
ladanyi@1919
|
27 |
|
ladanyi@1919
|
28 |
#include <iostream>
|
ladanyi@1919
|
29 |
#include <cstdlib>
|
ladanyi@1919
|
30 |
|
ladanyi@1919
|
31 |
#include <lemon/simann.h>
|
ladanyi@1919
|
32 |
#include <lemon/list_graph.h>
|
ladanyi@1919
|
33 |
#include <lemon/graph_reader.h>
|
ladanyi@1919
|
34 |
|
ladanyi@1919
|
35 |
using namespace lemon;
|
ladanyi@1919
|
36 |
|
ladanyi@1919
|
37 |
typedef ListGraph Graph;
|
ladanyi@1919
|
38 |
typedef Graph::Node Node;
|
ladanyi@1919
|
39 |
typedef Graph::Edge Edge;
|
ladanyi@1919
|
40 |
typedef Graph::NodeIt NodeIt;
|
ladanyi@1919
|
41 |
typedef Graph::EdgeIt EdgeIt;
|
ladanyi@1919
|
42 |
typedef Graph::OutEdgeIt OutEdgeIt;
|
ladanyi@1919
|
43 |
typedef Graph::InEdgeIt InEdgeIt;
|
ladanyi@1919
|
44 |
|
ladanyi@1919
|
45 |
class Entity : public EntityBase
|
ladanyi@1919
|
46 |
{
|
ladanyi@1919
|
47 |
public:
|
ladanyi@1919
|
48 |
Graph& g;
|
ladanyi@1919
|
49 |
Graph::EdgeMap<int>& w;
|
ladanyi@1919
|
50 |
Graph::NodeMap<bool> a;
|
ladanyi@1919
|
51 |
int sum;
|
ladanyi@1919
|
52 |
Node last_moved;
|
ladanyi@1919
|
53 |
Entity(Graph& _g, Graph::EdgeMap<int>& _w) : g(_g), w(_w), a(_g) {}
|
ladanyi@1919
|
54 |
double mutate() {
|
ladanyi@1919
|
55 |
static const int node_num = countNodes(g);
|
ladanyi@1919
|
56 |
int i = 1 + (int) (node_num * (rand() / (RAND_MAX + 1.0)));
|
ladanyi@1919
|
57 |
NodeIt n(g);
|
ladanyi@1919
|
58 |
int j = 1;
|
ladanyi@1919
|
59 |
while (j < i) {
|
ladanyi@1919
|
60 |
++n;
|
ladanyi@1919
|
61 |
++j;
|
ladanyi@1919
|
62 |
}
|
ladanyi@1919
|
63 |
for (OutEdgeIt e(g, n); e != INVALID; ++e) {
|
ladanyi@1919
|
64 |
if (a[n] != a[g.target(e)]) sum -= w[e];
|
ladanyi@1919
|
65 |
if (a[n] == a[g.target(e)]) sum += w[e];
|
ladanyi@1919
|
66 |
}
|
ladanyi@1919
|
67 |
for (InEdgeIt e(g, n); e != INVALID; ++e) {
|
ladanyi@1919
|
68 |
if (a[g.source(e)] != a[n]) sum -= w[e];
|
ladanyi@1919
|
69 |
if (a[g.source(e)] == a[n]) sum += w[e];
|
ladanyi@1919
|
70 |
}
|
ladanyi@1919
|
71 |
bool b = a[n];
|
ladanyi@1919
|
72 |
a[n] = !b;
|
ladanyi@1919
|
73 |
last_moved = n;
|
ladanyi@1919
|
74 |
return -sum;
|
ladanyi@1919
|
75 |
}
|
ladanyi@1919
|
76 |
void revert() {
|
ladanyi@1919
|
77 |
for (OutEdgeIt e(g, last_moved); e != INVALID; ++e) {
|
ladanyi@1919
|
78 |
if (a[last_moved] != a[g.target(e)]) sum -= w[e];
|
ladanyi@1919
|
79 |
if (a[last_moved] == a[g.target(e)]) sum += w[e];
|
ladanyi@1919
|
80 |
}
|
ladanyi@1919
|
81 |
for (InEdgeIt e(g, last_moved); e != INVALID; ++e) {
|
ladanyi@1919
|
82 |
if (a[g.source(e)] != a[last_moved]) sum -= w[e];
|
ladanyi@1919
|
83 |
if (a[g.source(e)] == a[last_moved]) sum += w[e];
|
ladanyi@1919
|
84 |
}
|
ladanyi@1919
|
85 |
bool b = a[last_moved];
|
ladanyi@1919
|
86 |
a[last_moved] = !b;
|
ladanyi@1919
|
87 |
}
|
ladanyi@1919
|
88 |
Entity* clone() { return new Entity(*this); }
|
ladanyi@1919
|
89 |
void randomize() {
|
ladanyi@1919
|
90 |
for (NodeIt n(g); n != INVALID; ++n)
|
ladanyi@1919
|
91 |
a[n] = false;
|
ladanyi@1919
|
92 |
for (NodeIt n(g); n != INVALID; ++n)
|
ladanyi@1919
|
93 |
if (rand() < 0.5) a[n] = true;
|
ladanyi@1919
|
94 |
sum = 0;
|
ladanyi@1919
|
95 |
for (EdgeIt e(g); e != INVALID; ++e)
|
ladanyi@1919
|
96 |
if (a[g.source(e)] != a[g.target(e)])
|
ladanyi@1919
|
97 |
sum += w[e];
|
ladanyi@1919
|
98 |
}
|
ladanyi@1919
|
99 |
};
|
ladanyi@1919
|
100 |
|
ladanyi@1919
|
101 |
int main()
|
ladanyi@1919
|
102 |
{
|
ladanyi@1919
|
103 |
Graph g;
|
ladanyi@1919
|
104 |
Graph::EdgeMap<int> w(g);
|
ladanyi@1919
|
105 |
|
ladanyi@1919
|
106 |
GraphReader<Graph> reader("simann_maxcut_demo.lgf", g);
|
ladanyi@1919
|
107 |
reader.readEdgeMap("weight", w);
|
ladanyi@1919
|
108 |
reader.run();
|
ladanyi@1919
|
109 |
|
ladanyi@1919
|
110 |
Entity e(g, w);
|
ladanyi@1919
|
111 |
|
ladanyi@1919
|
112 |
SimAnn simann;
|
ladanyi@1919
|
113 |
SimpleController ctrl;
|
ladanyi@1919
|
114 |
simann.setController(ctrl);
|
ladanyi@1919
|
115 |
simann.setEntity(e);
|
ladanyi@1919
|
116 |
simann.run();
|
ladanyi@1919
|
117 |
|
ladanyi@1919
|
118 |
Entity* be = (Entity *) simann.getBestEntity();
|
ladanyi@1919
|
119 |
std::cout << be->sum << std::endl;
|
ladanyi@1919
|
120 |
for (NodeIt n(g); n != INVALID; ++n)
|
ladanyi@1919
|
121 |
if (be->a[n]) std::cout << g.id(n) << ": 1" << std::endl;
|
ladanyi@1919
|
122 |
else std::cout << g.id(n) << ": 0" << std::endl;
|
ladanyi@1919
|
123 |
}
|