kpeter@1035
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
kpeter@1035
|
2 |
*
|
kpeter@1035
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
kpeter@1035
|
4 |
*
|
alpar@1092
|
5 |
* Copyright (C) 2003-2013
|
kpeter@1035
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
kpeter@1035
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
kpeter@1035
|
8 |
*
|
kpeter@1035
|
9 |
* Permission to use, modify and distribute this software is granted
|
kpeter@1035
|
10 |
* provided that this copyright notice appears in all copies. For
|
kpeter@1035
|
11 |
* precise terms see the accompanying LICENSE file.
|
kpeter@1035
|
12 |
*
|
kpeter@1035
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
kpeter@1035
|
14 |
* express or implied, and with no claim as to its suitability for any
|
kpeter@1035
|
15 |
* purpose.
|
kpeter@1035
|
16 |
*
|
kpeter@1035
|
17 |
*/
|
kpeter@1035
|
18 |
|
kpeter@1035
|
19 |
#include <iostream>
|
kpeter@1035
|
20 |
|
kpeter@1035
|
21 |
#include <lemon/full_graph.h>
|
kpeter@1035
|
22 |
#include <lemon/math.h>
|
kpeter@1035
|
23 |
#include <lemon/maps.h>
|
kpeter@1035
|
24 |
#include <lemon/random.h>
|
kpeter@1035
|
25 |
#include <lemon/dim2.h>
|
kpeter@1035
|
26 |
|
kpeter@1035
|
27 |
#include <lemon/nearest_neighbor_tsp.h>
|
kpeter@1035
|
28 |
#include <lemon/greedy_tsp.h>
|
kpeter@1035
|
29 |
#include <lemon/insertion_tsp.h>
|
kpeter@1035
|
30 |
#include <lemon/christofides_tsp.h>
|
kpeter@1035
|
31 |
#include <lemon/opt2_tsp.h>
|
kpeter@1035
|
32 |
|
kpeter@1035
|
33 |
#include "test_tools.h"
|
kpeter@1035
|
34 |
|
kpeter@1035
|
35 |
using namespace lemon;
|
kpeter@1035
|
36 |
|
kpeter@1035
|
37 |
// // Tests checkMetricCost() function
|
kpeter@1035
|
38 |
// void metricCostTest() {
|
kpeter@1035
|
39 |
// GRAPH_TYPEDEFS(FullGraph);
|
kpeter@1035
|
40 |
// FullGraph g(10);
|
kpeter@1035
|
41 |
// check(checkMetricCost(g, constMap<Edge>(0)), "Wrong checkMetricCost()");
|
kpeter@1035
|
42 |
// check(checkMetricCost(g, constMap<Edge>(1)), "Wrong checkMetricCost()");
|
kpeter@1035
|
43 |
// check(!checkMetricCost(g, constMap<Edge>(-1)), "Wrong checkMetricCost()");
|
alpar@1092
|
44 |
//
|
kpeter@1035
|
45 |
// FullGraph::EdgeMap<float> cost(g);
|
kpeter@1035
|
46 |
// for (NodeIt u(g); u != INVALID; ++u) {
|
kpeter@1035
|
47 |
// for (NodeIt v(g); v != INVALID; ++v) {
|
kpeter@1035
|
48 |
// if (u == v) continue;
|
kpeter@1035
|
49 |
// float x1 = g.id(u), x2 = g.id(v);
|
kpeter@1035
|
50 |
// float y1 = x1 * x1, y2 = x2 * x2;
|
kpeter@1035
|
51 |
// cost[g.edge(u, v)] = std::sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
|
kpeter@1035
|
52 |
// }
|
kpeter@1035
|
53 |
// }
|
kpeter@1035
|
54 |
// check(checkMetricCost(g, cost), "Wrong checkMetricCost()");
|
kpeter@1035
|
55 |
// float eps = Tolerance<float>::defaultEpsilon();
|
kpeter@1035
|
56 |
// cost[g.edge(g(0), g(9))] =
|
kpeter@1035
|
57 |
// cost[g.edge(g(0), g(8))] + cost[g.edge(g(8), g(9))] + eps * 2;
|
kpeter@1035
|
58 |
// check(!checkMetricCost(g, cost), "Wrong checkMetricCost()");
|
kpeter@1035
|
59 |
// check(checkMetricCost(g, cost, Tolerance<float>(eps * 4)),
|
kpeter@1035
|
60 |
// "Wrong checkMetricCost()");
|
kpeter@1035
|
61 |
// }
|
kpeter@1035
|
62 |
|
kpeter@1035
|
63 |
// Checks tour validity
|
kpeter@1037
|
64 |
template <typename Container>
|
kpeter@1037
|
65 |
bool checkTour(const FullGraph &gr, const Container &p) {
|
kpeter@1035
|
66 |
FullGraph::NodeMap<bool> used(gr, false);
|
alpar@1092
|
67 |
|
kpeter@1037
|
68 |
int node_cnt = 0;
|
alpar@1093
|
69 |
for (typename Container::const_iterator it = p.begin(); it != p.end(); ++it)
|
alpar@1093
|
70 |
{
|
alpar@1093
|
71 |
FullGraph::Node node = *it;
|
alpar@1093
|
72 |
if (used[node]) return false;
|
alpar@1093
|
73 |
used[node] = true;
|
alpar@1093
|
74 |
++node_cnt;
|
alpar@1093
|
75 |
}
|
alpar@1092
|
76 |
|
kpeter@1037
|
77 |
return (node_cnt == gr.nodeNum());
|
kpeter@1035
|
78 |
}
|
kpeter@1035
|
79 |
|
kpeter@1035
|
80 |
// Checks tour validity
|
kpeter@1037
|
81 |
bool checkTourPath(const FullGraph &gr, const Path<FullGraph> &p) {
|
kpeter@1035
|
82 |
FullGraph::NodeMap<bool> used(gr, false);
|
alpar@1092
|
83 |
|
kpeter@1035
|
84 |
if (!checkPath(gr, p)) return false;
|
kpeter@1035
|
85 |
if (gr.nodeNum() <= 1 && p.length() != 0) return false;
|
kpeter@1035
|
86 |
if (gr.nodeNum() > 1 && p.length() != gr.nodeNum()) return false;
|
kpeter@1035
|
87 |
|
kpeter@1035
|
88 |
for (int i = 0; i < p.length(); ++i) {
|
kpeter@1035
|
89 |
if (used[gr.target(p.nth(i))]) return false;
|
kpeter@1035
|
90 |
used[gr.target(p.nth(i))] = true;
|
kpeter@1035
|
91 |
}
|
kpeter@1035
|
92 |
return true;
|
kpeter@1035
|
93 |
}
|
kpeter@1035
|
94 |
|
kpeter@1035
|
95 |
// Checks tour cost
|
kpeter@1035
|
96 |
template <typename CostMap>
|
kpeter@1035
|
97 |
bool checkCost(const FullGraph &gr, const std::vector<FullGraph::Node> &p,
|
kpeter@1035
|
98 |
const CostMap &cost, typename CostMap::Value total)
|
kpeter@1035
|
99 |
{
|
kpeter@1035
|
100 |
typedef typename CostMap::Value Cost;
|
kpeter@1035
|
101 |
|
kpeter@1035
|
102 |
Cost s = 0;
|
kpeter@1035
|
103 |
for (int i = 0; i < int(p.size()) - 1; ++i)
|
kpeter@1035
|
104 |
s += cost[gr.edge(p[i], p[i+1])];
|
kpeter@1035
|
105 |
if (int(p.size()) >= 2)
|
kpeter@1035
|
106 |
s += cost[gr.edge(p.back(), p.front())];
|
kpeter@1035
|
107 |
|
kpeter@1035
|
108 |
return !Tolerance<Cost>().different(s, total);
|
kpeter@1035
|
109 |
}
|
kpeter@1035
|
110 |
|
kpeter@1035
|
111 |
// Checks tour cost
|
kpeter@1035
|
112 |
template <typename CostMap>
|
kpeter@1035
|
113 |
bool checkCost(const FullGraph &, const Path<FullGraph> &p,
|
kpeter@1035
|
114 |
const CostMap &cost, typename CostMap::Value total)
|
kpeter@1035
|
115 |
{
|
kpeter@1035
|
116 |
typedef typename CostMap::Value Cost;
|
kpeter@1035
|
117 |
|
kpeter@1035
|
118 |
Cost s = 0;
|
kpeter@1035
|
119 |
for (int i = 0; i < p.length(); ++i)
|
kpeter@1035
|
120 |
s += cost[p.nth(i)];
|
kpeter@1035
|
121 |
|
kpeter@1035
|
122 |
return !Tolerance<Cost>().different(s, total);
|
kpeter@1035
|
123 |
}
|
kpeter@1035
|
124 |
|
kpeter@1035
|
125 |
// Tests a TSP algorithm on small graphs
|
kpeter@1035
|
126 |
template <typename TSP>
|
kpeter@1035
|
127 |
void tspTestSmall(const std::string &alg_name) {
|
kpeter@1035
|
128 |
GRAPH_TYPEDEFS(FullGraph);
|
kpeter@1035
|
129 |
|
kpeter@1035
|
130 |
for (int n = 0; n <= 5; ++n) {
|
kpeter@1035
|
131 |
FullGraph g(n);
|
kpeter@1035
|
132 |
unsigned nsize = n;
|
kpeter@1035
|
133 |
int esize = n <= 1 ? 0 : n;
|
kpeter@1035
|
134 |
|
kpeter@1035
|
135 |
TSP alg(g, constMap<Edge, int>(1));
|
kpeter@1035
|
136 |
|
kpeter@1035
|
137 |
check(alg.run() == esize, alg_name + ": Wrong total cost");
|
kpeter@1035
|
138 |
check(alg.tourCost() == esize, alg_name + ": Wrong total cost");
|
kpeter@1035
|
139 |
|
kpeter@1037
|
140 |
std::list<Node> list1(nsize), list2;
|
kpeter@1037
|
141 |
std::vector<Node> vec1(nsize), vec2;
|
kpeter@1037
|
142 |
alg.tourNodes(list1.begin());
|
kpeter@1037
|
143 |
alg.tourNodes(vec1.begin());
|
kpeter@1037
|
144 |
alg.tourNodes(std::front_inserter(list2));
|
kpeter@1037
|
145 |
alg.tourNodes(std::back_inserter(vec2));
|
kpeter@1037
|
146 |
check(checkTour(g, alg.tourNodes()), alg_name + ": Wrong node sequence");
|
kpeter@1037
|
147 |
check(checkTour(g, list1), alg_name + ": Wrong node sequence");
|
kpeter@1037
|
148 |
check(checkTour(g, vec1), alg_name + ": Wrong node sequence");
|
kpeter@1037
|
149 |
check(checkTour(g, list2), alg_name + ": Wrong node sequence");
|
kpeter@1037
|
150 |
check(checkTour(g, vec2), alg_name + ": Wrong node sequence");
|
kpeter@1037
|
151 |
check(checkCost(g, vec1, constMap<Edge, int>(1), esize),
|
kpeter@1035
|
152 |
alg_name + ": Wrong tour cost");
|
kpeter@1035
|
153 |
|
kpeter@1035
|
154 |
SimplePath<FullGraph> path;
|
kpeter@1035
|
155 |
alg.tour(path);
|
kpeter@1035
|
156 |
check(path.length() == esize, alg_name + ": Wrong tour");
|
kpeter@1037
|
157 |
check(checkTourPath(g, path), alg_name + ": Wrong tour");
|
kpeter@1035
|
158 |
check(checkCost(g, path, constMap<Edge, int>(1), esize),
|
kpeter@1035
|
159 |
alg_name + ": Wrong tour cost");
|
kpeter@1035
|
160 |
}
|
kpeter@1035
|
161 |
}
|
kpeter@1035
|
162 |
|
kpeter@1035
|
163 |
// Tests a TSP algorithm on random graphs
|
kpeter@1035
|
164 |
template <typename TSP>
|
kpeter@1035
|
165 |
void tspTestRandom(const std::string &alg_name) {
|
kpeter@1035
|
166 |
GRAPH_TYPEDEFS(FullGraph);
|
kpeter@1035
|
167 |
|
kpeter@1035
|
168 |
FullGraph g(20);
|
kpeter@1035
|
169 |
FullGraph::NodeMap<dim2::Point<double> > pos(g);
|
kpeter@1035
|
170 |
DoubleEdgeMap cost(g);
|
kpeter@1035
|
171 |
|
kpeter@1035
|
172 |
TSP alg(g, cost);
|
kpeter@1035
|
173 |
Opt2Tsp<DoubleEdgeMap > opt2(g, cost);
|
kpeter@1035
|
174 |
|
kpeter@1035
|
175 |
for (int i = 1; i <= 3; i++) {
|
kpeter@1035
|
176 |
for (NodeIt u(g); u != INVALID; ++u) {
|
kpeter@1035
|
177 |
pos[u] = dim2::Point<double>(rnd(), rnd());
|
kpeter@1035
|
178 |
}
|
kpeter@1035
|
179 |
for (NodeIt u(g); u != INVALID; ++u) {
|
kpeter@1035
|
180 |
for (NodeIt v(g); v != INVALID; ++v) {
|
kpeter@1035
|
181 |
if (u == v) continue;
|
kpeter@1035
|
182 |
cost[g.edge(u, v)] = (pos[u] - pos[v]).normSquare();
|
kpeter@1035
|
183 |
}
|
kpeter@1035
|
184 |
}
|
alpar@1092
|
185 |
|
kpeter@1035
|
186 |
check(alg.run() > 0, alg_name + ": Wrong total cost");
|
kpeter@1035
|
187 |
|
kpeter@1035
|
188 |
std::vector<Node> vec;
|
kpeter@1037
|
189 |
alg.tourNodes(std::back_inserter(vec));
|
kpeter@1035
|
190 |
check(checkTour(g, vec), alg_name + ": Wrong node sequence");
|
kpeter@1035
|
191 |
check(checkCost(g, vec, cost, alg.tourCost()),
|
kpeter@1035
|
192 |
alg_name + ": Wrong tour cost");
|
kpeter@1035
|
193 |
|
kpeter@1035
|
194 |
SimplePath<FullGraph> path;
|
kpeter@1035
|
195 |
alg.tour(path);
|
kpeter@1037
|
196 |
check(checkTourPath(g, path), alg_name + ": Wrong tour");
|
kpeter@1035
|
197 |
check(checkCost(g, path, cost, alg.tourCost()),
|
kpeter@1035
|
198 |
alg_name + ": Wrong tour cost");
|
alpar@1092
|
199 |
|
kpeter@1035
|
200 |
check(!Tolerance<double>().less(alg.tourCost(), opt2.run(alg.tourNodes())),
|
kpeter@1035
|
201 |
"2-opt improvement: Wrong total cost");
|
kpeter@1035
|
202 |
check(checkTour(g, opt2.tourNodes()),
|
kpeter@1035
|
203 |
"2-opt improvement: Wrong node sequence");
|
kpeter@1035
|
204 |
check(checkCost(g, opt2.tourNodes(), cost, opt2.tourCost()),
|
kpeter@1035
|
205 |
"2-opt improvement: Wrong tour cost");
|
alpar@1092
|
206 |
|
kpeter@1035
|
207 |
check(!Tolerance<double>().less(alg.tourCost(), opt2.run(path)),
|
kpeter@1035
|
208 |
"2-opt improvement: Wrong total cost");
|
kpeter@1035
|
209 |
check(checkTour(g, opt2.tourNodes()),
|
kpeter@1035
|
210 |
"2-opt improvement: Wrong node sequence");
|
kpeter@1035
|
211 |
check(checkCost(g, opt2.tourNodes(), cost, opt2.tourCost()),
|
kpeter@1035
|
212 |
"2-opt improvement: Wrong tour cost");
|
kpeter@1035
|
213 |
}
|
kpeter@1035
|
214 |
}
|
kpeter@1035
|
215 |
|
alpar@1092
|
216 |
// Algorithm class for Nearest Insertion
|
kpeter@1035
|
217 |
template <typename CM>
|
kpeter@1035
|
218 |
class NearestInsertionTsp : public InsertionTsp<CM> {
|
kpeter@1035
|
219 |
public:
|
kpeter@1035
|
220 |
NearestInsertionTsp(const FullGraph &gr, const CM &cost)
|
kpeter@1035
|
221 |
: InsertionTsp<CM>(gr, cost) {}
|
kpeter@1035
|
222 |
typename CM::Value run() {
|
kpeter@1035
|
223 |
return InsertionTsp<CM>::run(InsertionTsp<CM>::NEAREST);
|
kpeter@1035
|
224 |
}
|
kpeter@1035
|
225 |
};
|
kpeter@1035
|
226 |
|
alpar@1092
|
227 |
// Algorithm class for Farthest Insertion
|
kpeter@1035
|
228 |
template <typename CM>
|
kpeter@1035
|
229 |
class FarthestInsertionTsp : public InsertionTsp<CM> {
|
kpeter@1035
|
230 |
public:
|
kpeter@1035
|
231 |
FarthestInsertionTsp(const FullGraph &gr, const CM &cost)
|
kpeter@1035
|
232 |
: InsertionTsp<CM>(gr, cost) {}
|
kpeter@1035
|
233 |
typename CM::Value run() {
|
kpeter@1035
|
234 |
return InsertionTsp<CM>::run(InsertionTsp<CM>::FARTHEST);
|
kpeter@1035
|
235 |
}
|
kpeter@1035
|
236 |
};
|
kpeter@1035
|
237 |
|
alpar@1092
|
238 |
// Algorithm class for Cheapest Insertion
|
kpeter@1035
|
239 |
template <typename CM>
|
kpeter@1035
|
240 |
class CheapestInsertionTsp : public InsertionTsp<CM> {
|
kpeter@1035
|
241 |
public:
|
kpeter@1035
|
242 |
CheapestInsertionTsp(const FullGraph &gr, const CM &cost)
|
kpeter@1035
|
243 |
: InsertionTsp<CM>(gr, cost) {}
|
kpeter@1035
|
244 |
typename CM::Value run() {
|
kpeter@1035
|
245 |
return InsertionTsp<CM>::run(InsertionTsp<CM>::CHEAPEST);
|
kpeter@1035
|
246 |
}
|
kpeter@1035
|
247 |
};
|
kpeter@1035
|
248 |
|
alpar@1092
|
249 |
// Algorithm class for Random Insertion
|
kpeter@1035
|
250 |
template <typename CM>
|
kpeter@1035
|
251 |
class RandomInsertionTsp : public InsertionTsp<CM> {
|
kpeter@1035
|
252 |
public:
|
kpeter@1035
|
253 |
RandomInsertionTsp(const FullGraph &gr, const CM &cost)
|
kpeter@1035
|
254 |
: InsertionTsp<CM>(gr, cost) {}
|
kpeter@1035
|
255 |
typename CM::Value run() {
|
kpeter@1035
|
256 |
return InsertionTsp<CM>::run(InsertionTsp<CM>::RANDOM);
|
kpeter@1035
|
257 |
}
|
kpeter@1035
|
258 |
};
|
kpeter@1035
|
259 |
|
kpeter@1035
|
260 |
int main() {
|
kpeter@1035
|
261 |
GRAPH_TYPEDEFS(FullGraph);
|
kpeter@1035
|
262 |
|
kpeter@1035
|
263 |
// metricCostTest();
|
kpeter@1035
|
264 |
|
kpeter@1035
|
265 |
tspTestSmall<NearestNeighborTsp<ConstMap<Edge, int> > >("Nearest Neighbor");
|
kpeter@1035
|
266 |
tspTestSmall<GreedyTsp<ConstMap<Edge, int> > >("Greedy");
|
kpeter@1035
|
267 |
tspTestSmall<NearestInsertionTsp<ConstMap<Edge, int> > >("Nearest Insertion");
|
alpar@1093
|
268 |
tspTestSmall<FarthestInsertionTsp<ConstMap<Edge, int> > >
|
alpar@1093
|
269 |
("Farthest Insertion");
|
alpar@1093
|
270 |
tspTestSmall<CheapestInsertionTsp<ConstMap<Edge, int> > >
|
alpar@1093
|
271 |
("Cheapest Insertion");
|
kpeter@1035
|
272 |
tspTestSmall<RandomInsertionTsp<ConstMap<Edge, int> > >("Random Insertion");
|
kpeter@1035
|
273 |
tspTestSmall<ChristofidesTsp<ConstMap<Edge, int> > >("Christofides");
|
kpeter@1035
|
274 |
tspTestSmall<Opt2Tsp<ConstMap<Edge, int> > >("2-opt");
|
kpeter@1035
|
275 |
|
kpeter@1035
|
276 |
tspTestRandom<NearestNeighborTsp<DoubleEdgeMap > >("Nearest Neighbor");
|
kpeter@1035
|
277 |
tspTestRandom<GreedyTsp<DoubleEdgeMap > >("Greedy");
|
kpeter@1035
|
278 |
tspTestRandom<NearestInsertionTsp<DoubleEdgeMap > >("Nearest Insertion");
|
kpeter@1035
|
279 |
tspTestRandom<FarthestInsertionTsp<DoubleEdgeMap > >("Farthest Insertion");
|
kpeter@1035
|
280 |
tspTestRandom<CheapestInsertionTsp<DoubleEdgeMap > >("Cheapest Insertion");
|
kpeter@1035
|
281 |
tspTestRandom<RandomInsertionTsp<DoubleEdgeMap > >("Random Insertion");
|
kpeter@1035
|
282 |
tspTestRandom<ChristofidesTsp<DoubleEdgeMap > >("Christofides");
|
kpeter@1035
|
283 |
tspTestRandom<Opt2Tsp<DoubleEdgeMap > >("2-opt");
|
kpeter@1035
|
284 |
|
kpeter@1035
|
285 |
return 0;
|
kpeter@1035
|
286 |
}
|