lemongraph 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +8 -0
  3. data/LICENSE +674 -0
  4. data/README.md +6 -0
  5. data/ext/lemon-1.3.1/AUTHORS +26 -0
  6. data/ext/lemon-1.3.1/CMakeLists.txt +371 -0
  7. data/ext/lemon-1.3.1/INSTALL +167 -0
  8. data/ext/lemon-1.3.1/LICENSE +32 -0
  9. data/ext/lemon-1.3.1/NEWS +337 -0
  10. data/ext/lemon-1.3.1/README +50 -0
  11. data/ext/lemon-1.3.1/cmake/FindCOIN.cmake +110 -0
  12. data/ext/lemon-1.3.1/cmake/FindGLPK.cmake +55 -0
  13. data/ext/lemon-1.3.1/cmake/FindGhostscript.cmake +10 -0
  14. data/ext/lemon-1.3.1/cmake/FindILOG.cmake +102 -0
  15. data/ext/lemon-1.3.1/cmake/FindSOPLEX.cmake +23 -0
  16. data/ext/lemon-1.3.1/cmake/LEMONConfig.cmake.in +13 -0
  17. data/ext/lemon-1.3.1/cmake/nsis/lemon.ico +0 -0
  18. data/ext/lemon-1.3.1/cmake/nsis/uninstall.ico +0 -0
  19. data/ext/lemon-1.3.1/cmake/version.cmake +1 -0
  20. data/ext/lemon-1.3.1/cmake/version.cmake.in +1 -0
  21. data/ext/lemon-1.3.1/contrib/CMakeLists.txt +19 -0
  22. data/ext/lemon-1.3.1/lemon/CMakeLists.txt +91 -0
  23. data/ext/lemon-1.3.1/lemon/adaptors.h +3638 -0
  24. data/ext/lemon-1.3.1/lemon/arg_parser.cc +474 -0
  25. data/ext/lemon-1.3.1/lemon/arg_parser.h +440 -0
  26. data/ext/lemon-1.3.1/lemon/assert.h +214 -0
  27. data/ext/lemon-1.3.1/lemon/base.cc +37 -0
  28. data/ext/lemon-1.3.1/lemon/bellman_ford.h +1116 -0
  29. data/ext/lemon-1.3.1/lemon/bfs.h +1754 -0
  30. data/ext/lemon-1.3.1/lemon/bin_heap.h +347 -0
  31. data/ext/lemon-1.3.1/lemon/binomial_heap.h +445 -0
  32. data/ext/lemon-1.3.1/lemon/bits/alteration_notifier.h +472 -0
  33. data/ext/lemon-1.3.1/lemon/bits/array_map.h +351 -0
  34. data/ext/lemon-1.3.1/lemon/bits/bezier.h +174 -0
  35. data/ext/lemon-1.3.1/lemon/bits/default_map.h +182 -0
  36. data/ext/lemon-1.3.1/lemon/bits/edge_set_extender.h +627 -0
  37. data/ext/lemon-1.3.1/lemon/bits/enable_if.h +131 -0
  38. data/ext/lemon-1.3.1/lemon/bits/graph_adaptor_extender.h +401 -0
  39. data/ext/lemon-1.3.1/lemon/bits/graph_extender.h +1332 -0
  40. data/ext/lemon-1.3.1/lemon/bits/lock.h +65 -0
  41. data/ext/lemon-1.3.1/lemon/bits/map_extender.h +332 -0
  42. data/ext/lemon-1.3.1/lemon/bits/path_dump.h +177 -0
  43. data/ext/lemon-1.3.1/lemon/bits/solver_bits.h +194 -0
  44. data/ext/lemon-1.3.1/lemon/bits/traits.h +388 -0
  45. data/ext/lemon-1.3.1/lemon/bits/variant.h +494 -0
  46. data/ext/lemon-1.3.1/lemon/bits/vector_map.h +244 -0
  47. data/ext/lemon-1.3.1/lemon/bits/windows.cc +166 -0
  48. data/ext/lemon-1.3.1/lemon/bits/windows.h +44 -0
  49. data/ext/lemon-1.3.1/lemon/bucket_heap.h +594 -0
  50. data/ext/lemon-1.3.1/lemon/capacity_scaling.h +1014 -0
  51. data/ext/lemon-1.3.1/lemon/cbc.cc +460 -0
  52. data/ext/lemon-1.3.1/lemon/cbc.h +129 -0
  53. data/ext/lemon-1.3.1/lemon/christofides_tsp.h +254 -0
  54. data/ext/lemon-1.3.1/lemon/circulation.h +807 -0
  55. data/ext/lemon-1.3.1/lemon/clp.cc +464 -0
  56. data/ext/lemon-1.3.1/lemon/clp.h +164 -0
  57. data/ext/lemon-1.3.1/lemon/color.cc +44 -0
  58. data/ext/lemon-1.3.1/lemon/color.h +204 -0
  59. data/ext/lemon-1.3.1/lemon/concept_check.h +77 -0
  60. data/ext/lemon-1.3.1/lemon/concepts/bpgraph.h +1029 -0
  61. data/ext/lemon-1.3.1/lemon/concepts/digraph.h +491 -0
  62. data/ext/lemon-1.3.1/lemon/concepts/graph.h +788 -0
  63. data/ext/lemon-1.3.1/lemon/concepts/graph_components.h +2134 -0
  64. data/ext/lemon-1.3.1/lemon/concepts/heap.h +324 -0
  65. data/ext/lemon-1.3.1/lemon/concepts/maps.h +223 -0
  66. data/ext/lemon-1.3.1/lemon/concepts/path.h +312 -0
  67. data/ext/lemon-1.3.1/lemon/config.h.in +22 -0
  68. data/ext/lemon-1.3.1/lemon/connectivity.h +1688 -0
  69. data/ext/lemon-1.3.1/lemon/core.h +2506 -0
  70. data/ext/lemon-1.3.1/lemon/cost_scaling.h +1607 -0
  71. data/ext/lemon-1.3.1/lemon/counter.h +249 -0
  72. data/ext/lemon-1.3.1/lemon/cplex.cc +994 -0
  73. data/ext/lemon-1.3.1/lemon/cplex.h +292 -0
  74. data/ext/lemon-1.3.1/lemon/cycle_canceling.h +1230 -0
  75. data/ext/lemon-1.3.1/lemon/dfs.h +1637 -0
  76. data/ext/lemon-1.3.1/lemon/dheap.h +352 -0
  77. data/ext/lemon-1.3.1/lemon/dijkstra.h +1303 -0
  78. data/ext/lemon-1.3.1/lemon/dim2.h +726 -0
  79. data/ext/lemon-1.3.1/lemon/dimacs.h +448 -0
  80. data/ext/lemon-1.3.1/lemon/edge_set.h +1420 -0
  81. data/ext/lemon-1.3.1/lemon/edmonds_karp.h +556 -0
  82. data/ext/lemon-1.3.1/lemon/elevator.h +982 -0
  83. data/ext/lemon-1.3.1/lemon/error.h +276 -0
  84. data/ext/lemon-1.3.1/lemon/euler.h +287 -0
  85. data/ext/lemon-1.3.1/lemon/fib_heap.h +475 -0
  86. data/ext/lemon-1.3.1/lemon/fractional_matching.h +2139 -0
  87. data/ext/lemon-1.3.1/lemon/full_graph.h +1082 -0
  88. data/ext/lemon-1.3.1/lemon/glpk.cc +1012 -0
  89. data/ext/lemon-1.3.1/lemon/glpk.h +263 -0
  90. data/ext/lemon-1.3.1/lemon/gomory_hu.h +568 -0
  91. data/ext/lemon-1.3.1/lemon/graph_to_eps.h +1186 -0
  92. data/ext/lemon-1.3.1/lemon/greedy_tsp.h +251 -0
  93. data/ext/lemon-1.3.1/lemon/grid_graph.h +699 -0
  94. data/ext/lemon-1.3.1/lemon/grosso_locatelli_pullan_mc.h +840 -0
  95. data/ext/lemon-1.3.1/lemon/hao_orlin.h +1015 -0
  96. data/ext/lemon-1.3.1/lemon/hartmann_orlin_mmc.h +654 -0
  97. data/ext/lemon-1.3.1/lemon/howard_mmc.h +651 -0
  98. data/ext/lemon-1.3.1/lemon/hypercube_graph.h +459 -0
  99. data/ext/lemon-1.3.1/lemon/insertion_tsp.h +533 -0
  100. data/ext/lemon-1.3.1/lemon/karp_mmc.h +590 -0
  101. data/ext/lemon-1.3.1/lemon/kruskal.h +324 -0
  102. data/ext/lemon-1.3.1/lemon/lemon.pc.in +10 -0
  103. data/ext/lemon-1.3.1/lemon/lgf_reader.h +3854 -0
  104. data/ext/lemon-1.3.1/lemon/lgf_writer.h +2687 -0
  105. data/ext/lemon-1.3.1/lemon/list_graph.h +2510 -0
  106. data/ext/lemon-1.3.1/lemon/lp.h +95 -0
  107. data/ext/lemon-1.3.1/lemon/lp_base.cc +30 -0
  108. data/ext/lemon-1.3.1/lemon/lp_base.h +2147 -0
  109. data/ext/lemon-1.3.1/lemon/lp_skeleton.cc +143 -0
  110. data/ext/lemon-1.3.1/lemon/lp_skeleton.h +234 -0
  111. data/ext/lemon-1.3.1/lemon/maps.h +4057 -0
  112. data/ext/lemon-1.3.1/lemon/matching.h +3505 -0
  113. data/ext/lemon-1.3.1/lemon/math.h +77 -0
  114. data/ext/lemon-1.3.1/lemon/max_cardinality_search.h +794 -0
  115. data/ext/lemon-1.3.1/lemon/min_cost_arborescence.h +808 -0
  116. data/ext/lemon-1.3.1/lemon/nagamochi_ibaraki.h +702 -0
  117. data/ext/lemon-1.3.1/lemon/nauty_reader.h +113 -0
  118. data/ext/lemon-1.3.1/lemon/nearest_neighbor_tsp.h +238 -0
  119. data/ext/lemon-1.3.1/lemon/network_simplex.h +1659 -0
  120. data/ext/lemon-1.3.1/lemon/opt2_tsp.h +367 -0
  121. data/ext/lemon-1.3.1/lemon/pairing_heap.h +474 -0
  122. data/ext/lemon-1.3.1/lemon/path.h +1164 -0
  123. data/ext/lemon-1.3.1/lemon/planarity.h +2754 -0
  124. data/ext/lemon-1.3.1/lemon/preflow.h +985 -0
  125. data/ext/lemon-1.3.1/lemon/quad_heap.h +343 -0
  126. data/ext/lemon-1.3.1/lemon/radix_heap.h +438 -0
  127. data/ext/lemon-1.3.1/lemon/radix_sort.h +487 -0
  128. data/ext/lemon-1.3.1/lemon/random.cc +29 -0
  129. data/ext/lemon-1.3.1/lemon/random.h +1005 -0
  130. data/ext/lemon-1.3.1/lemon/smart_graph.h +1344 -0
  131. data/ext/lemon-1.3.1/lemon/soplex.cc +465 -0
  132. data/ext/lemon-1.3.1/lemon/soplex.h +158 -0
  133. data/ext/lemon-1.3.1/lemon/static_graph.h +476 -0
  134. data/ext/lemon-1.3.1/lemon/suurballe.h +776 -0
  135. data/ext/lemon-1.3.1/lemon/time_measure.h +610 -0
  136. data/ext/lemon-1.3.1/lemon/tolerance.h +242 -0
  137. data/ext/lemon-1.3.1/lemon/unionfind.h +1824 -0
  138. data/ext/lemon-1.3.1/scripts/unify-sources.sh +390 -0
  139. data/ext/lemon-1.3.1/scripts/valgrind-wrapper.sh +22 -0
  140. data/ext/lemongraph/arc_map.cc +1007 -0
  141. data/ext/lemongraph/digraph.cc +282 -0
  142. data/ext/lemongraph/digraph_arc.cc +153 -0
  143. data/ext/lemongraph/digraph_node.cc +277 -0
  144. data/ext/lemongraph/edge_map.cc +770 -0
  145. data/ext/lemongraph/extconf.rb +53 -0
  146. data/ext/lemongraph/graph.cc +351 -0
  147. data/ext/lemongraph/graph_arc.cc +95 -0
  148. data/ext/lemongraph/graph_edge.cc +153 -0
  149. data/ext/lemongraph/graph_item.cc +76 -0
  150. data/ext/lemongraph/graph_node.cc +321 -0
  151. data/ext/lemongraph/lemongraph.cc +260 -0
  152. data/ext/lemongraph/lemongraph.hh +295 -0
  153. data/ext/lemongraph/lemongraph.map +6 -0
  154. data/ext/lemongraph/lemongraph_export.hh +31 -0
  155. data/ext/lemongraph/node_map.cc +1011 -0
  156. data/lemongraph.gemspec +176 -0
  157. data/lib/lemongraph/graphviz.rb +240 -0
  158. data/lib/lemongraph/version.rb +4 -0
  159. data/lib/lemongraph.rb +21 -0
  160. data/samples/lemondeps.rb +38 -0
  161. metadata +202 -0
@@ -0,0 +1,367 @@
1
+ /* -*- mode: C++; indent-tabs-mode: nil; -*-
2
+ *
3
+ * This file is a part of LEMON, a generic C++ optimization library.
4
+ *
5
+ * Copyright (C) 2003-2013
6
+ * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
+ * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
+ *
9
+ * Permission to use, modify and distribute this software is granted
10
+ * provided that this copyright notice appears in all copies. For
11
+ * precise terms see the accompanying LICENSE file.
12
+ *
13
+ * This software is provided "AS IS" with no warranty of any kind,
14
+ * express or implied, and with no claim as to its suitability for any
15
+ * purpose.
16
+ *
17
+ */
18
+
19
+ #ifndef LEMON_OPT2_TSP_H
20
+ #define LEMON_OPT2_TSP_H
21
+
22
+ /// \ingroup tsp
23
+ /// \file
24
+ /// \brief 2-opt algorithm for symmetric TSP.
25
+
26
+ #include <vector>
27
+ #include <lemon/full_graph.h>
28
+
29
+ namespace lemon {
30
+
31
+ /// \ingroup tsp
32
+ ///
33
+ /// \brief 2-opt algorithm for symmetric TSP.
34
+ ///
35
+ /// Opt2Tsp implements the 2-opt heuristic for solving
36
+ /// symmetric \ref tsp "TSP".
37
+ ///
38
+ /// This algorithm starts with an initial tour and iteratively improves it.
39
+ /// At each step, it removes two edges and the reconnects the created two
40
+ /// paths in the other way if the resulting tour is shorter.
41
+ /// The algorithm finishes when no such 2-opt move can be applied, and so
42
+ /// the tour is 2-optimal.
43
+ ///
44
+ /// If no starting tour is given to the \ref run() function, then the
45
+ /// algorithm uses the node sequence determined by the node IDs.
46
+ /// Oherwise, it starts with the given tour.
47
+ ///
48
+ /// This is a rather slow but effective method.
49
+ /// Its typical usage is the improvement of the result of a fast tour
50
+ /// construction heuristic (e.g. the InsertionTsp algorithm).
51
+ ///
52
+ /// \tparam CM Type of the cost map.
53
+ template <typename CM>
54
+ class Opt2Tsp
55
+ {
56
+ public:
57
+
58
+ /// Type of the cost map
59
+ typedef CM CostMap;
60
+ /// Type of the edge costs
61
+ typedef typename CM::Value Cost;
62
+
63
+ private:
64
+
65
+ GRAPH_TYPEDEFS(FullGraph);
66
+
67
+ const FullGraph &_gr;
68
+ const CostMap &_cost;
69
+ Cost _sum;
70
+ std::vector<int> _plist;
71
+ std::vector<Node> _path;
72
+
73
+ public:
74
+
75
+ /// \brief Constructor
76
+ ///
77
+ /// Constructor.
78
+ /// \param gr The \ref FullGraph "full graph" the algorithm runs on.
79
+ /// \param cost The cost map.
80
+ Opt2Tsp(const FullGraph &gr, const CostMap &cost)
81
+ : _gr(gr), _cost(cost) {}
82
+
83
+ /// \name Execution Control
84
+ /// @{
85
+
86
+ /// \brief Runs the algorithm from scratch.
87
+ ///
88
+ /// This function runs the algorithm starting from the tour that is
89
+ /// determined by the node ID sequence.
90
+ ///
91
+ /// \return The total cost of the found tour.
92
+ Cost run() {
93
+ _path.clear();
94
+
95
+ if (_gr.nodeNum() == 0) return _sum = 0;
96
+ else if (_gr.nodeNum() == 1) {
97
+ _path.push_back(_gr(0));
98
+ return _sum = 0;
99
+ }
100
+ else if (_gr.nodeNum() == 2) {
101
+ _path.push_back(_gr(0));
102
+ _path.push_back(_gr(1));
103
+ return _sum = 2 * _cost[_gr.edge(_gr(0), _gr(1))];
104
+ }
105
+
106
+ _plist.resize(2*_gr.nodeNum());
107
+ for (int i = 1; i < _gr.nodeNum()-1; ++i) {
108
+ _plist[2*i] = i-1;
109
+ _plist[2*i+1] = i+1;
110
+ }
111
+ _plist[0] = _gr.nodeNum()-1;
112
+ _plist[1] = 1;
113
+ _plist[2*_gr.nodeNum()-2] = _gr.nodeNum()-2;
114
+ _plist[2*_gr.nodeNum()-1] = 0;
115
+
116
+ return start();
117
+ }
118
+
119
+ /// \brief Runs the algorithm starting from the given tour.
120
+ ///
121
+ /// This function runs the algorithm starting from the given tour.
122
+ ///
123
+ /// \param tour The tour as a path structure. It must be a
124
+ /// \ref checkPath() "valid path" containing excactly n arcs.
125
+ ///
126
+ /// \return The total cost of the found tour.
127
+ template <typename Path>
128
+ Cost run(const Path& tour) {
129
+ _path.clear();
130
+
131
+ if (_gr.nodeNum() == 0) return _sum = 0;
132
+ else if (_gr.nodeNum() == 1) {
133
+ _path.push_back(_gr(0));
134
+ return _sum = 0;
135
+ }
136
+ else if (_gr.nodeNum() == 2) {
137
+ _path.push_back(_gr(0));
138
+ _path.push_back(_gr(1));
139
+ return _sum = 2 * _cost[_gr.edge(_gr(0), _gr(1))];
140
+ }
141
+
142
+ _plist.resize(2*_gr.nodeNum());
143
+ typename Path::ArcIt it(tour);
144
+ int first = _gr.id(_gr.source(it)),
145
+ prev = first,
146
+ curr = _gr.id(_gr.target(it)),
147
+ next = -1;
148
+ _plist[2*first+1] = curr;
149
+ for (++it; it != INVALID; ++it) {
150
+ next = _gr.id(_gr.target(it));
151
+ _plist[2*curr] = prev;
152
+ _plist[2*curr+1] = next;
153
+ prev = curr;
154
+ curr = next;
155
+ }
156
+ _plist[2*first] = prev;
157
+
158
+ return start();
159
+ }
160
+
161
+ /// \brief Runs the algorithm starting from the given tour.
162
+ ///
163
+ /// This function runs the algorithm starting from the given tour
164
+ /// (node sequence).
165
+ ///
166
+ /// \param tour A vector that stores all <tt>Node</tt>s of the graph
167
+ /// in the desired order.
168
+ ///
169
+ /// \return The total cost of the found tour.
170
+ Cost run(const std::vector<Node>& tour) {
171
+ _path.clear();
172
+
173
+ if (_gr.nodeNum() == 0) return _sum = 0;
174
+ else if (_gr.nodeNum() == 1) {
175
+ _path.push_back(_gr(0));
176
+ return _sum = 0;
177
+ }
178
+ else if (_gr.nodeNum() == 2) {
179
+ _path.push_back(_gr(0));
180
+ _path.push_back(_gr(1));
181
+ return _sum = 2 * _cost[_gr.edge(_gr(0), _gr(1))];
182
+ }
183
+
184
+ _plist.resize(2*_gr.nodeNum());
185
+ typename std::vector<Node>::const_iterator it = tour.begin();
186
+ int first = _gr.id(*it),
187
+ prev = first,
188
+ curr = _gr.id(*(++it)),
189
+ next = -1;
190
+ _plist[2*first+1] = curr;
191
+ for (++it; it != tour.end(); ++it) {
192
+ next = _gr.id(*it);
193
+ _plist[2*curr] = prev;
194
+ _plist[2*curr+1] = next;
195
+ prev = curr;
196
+ curr = next;
197
+ }
198
+ _plist[2*first] = curr;
199
+ _plist[2*curr] = prev;
200
+ _plist[2*curr+1] = first;
201
+
202
+ return start();
203
+ }
204
+
205
+ /// @}
206
+
207
+ /// \name Query Functions
208
+ /// @{
209
+
210
+ /// \brief The total cost of the found tour.
211
+ ///
212
+ /// This function returns the total cost of the found tour.
213
+ ///
214
+ /// \pre run() must be called before using this function.
215
+ Cost tourCost() const {
216
+ return _sum;
217
+ }
218
+
219
+ /// \brief Returns a const reference to the node sequence of the
220
+ /// found tour.
221
+ ///
222
+ /// This function returns a const reference to a vector
223
+ /// that stores the node sequence of the found tour.
224
+ ///
225
+ /// \pre run() must be called before using this function.
226
+ const std::vector<Node>& tourNodes() const {
227
+ return _path;
228
+ }
229
+
230
+ /// \brief Gives back the node sequence of the found tour.
231
+ ///
232
+ /// This function copies the node sequence of the found tour into
233
+ /// an STL container through the given output iterator. The
234
+ /// <tt>value_type</tt> of the container must be <tt>FullGraph::Node</tt>.
235
+ /// For example,
236
+ /// \code
237
+ /// std::vector<FullGraph::Node> nodes(countNodes(graph));
238
+ /// tsp.tourNodes(nodes.begin());
239
+ /// \endcode
240
+ /// or
241
+ /// \code
242
+ /// std::list<FullGraph::Node> nodes;
243
+ /// tsp.tourNodes(std::back_inserter(nodes));
244
+ /// \endcode
245
+ ///
246
+ /// \pre run() must be called before using this function.
247
+ template <typename Iterator>
248
+ void tourNodes(Iterator out) const {
249
+ std::copy(_path.begin(), _path.end(), out);
250
+ }
251
+
252
+ /// \brief Gives back the found tour as a path.
253
+ ///
254
+ /// This function copies the found tour as a list of arcs/edges into
255
+ /// the given \ref lemon::concepts::Path "path structure".
256
+ ///
257
+ /// \pre run() must be called before using this function.
258
+ template <typename Path>
259
+ void tour(Path &path) const {
260
+ path.clear();
261
+ for (int i = 0; i < int(_path.size()) - 1; ++i) {
262
+ path.addBack(_gr.arc(_path[i], _path[i+1]));
263
+ }
264
+ if (int(_path.size()) >= 2) {
265
+ path.addBack(_gr.arc(_path.back(), _path.front()));
266
+ }
267
+ }
268
+
269
+ /// @}
270
+
271
+ private:
272
+
273
+ // Iterator class for the linked list storage of the tour
274
+ class PathListIt {
275
+ public:
276
+ PathListIt(const std::vector<int> &pl, int i=0)
277
+ : plist(&pl), act(i), last(pl[2*act]) {}
278
+ PathListIt(const std::vector<int> &pl, int i, int l)
279
+ : plist(&pl), act(i), last(l) {}
280
+
281
+ int nextIndex() const {
282
+ return (*plist)[2*act] == last ? 2*act+1 : 2*act;
283
+ }
284
+
285
+ int prevIndex() const {
286
+ return (*plist)[2*act] == last ? 2*act : 2*act+1;
287
+ }
288
+
289
+ int next() const {
290
+ int x = (*plist)[2*act];
291
+ return x == last ? (*plist)[2*act+1] : x;
292
+ }
293
+
294
+ int prev() const {
295
+ return last;
296
+ }
297
+
298
+ PathListIt& operator++() {
299
+ int tmp = act;
300
+ act = next();
301
+ last = tmp;
302
+ return *this;
303
+ }
304
+
305
+ operator int() const {
306
+ return act;
307
+ }
308
+
309
+ private:
310
+ const std::vector<int> *plist;
311
+ int act;
312
+ int last;
313
+ };
314
+
315
+ // Checks and applies 2-opt move (if it improves the tour)
316
+ bool checkOpt2(const PathListIt& i, const PathListIt& j) {
317
+ Node u = _gr.nodeFromId(i),
318
+ un = _gr.nodeFromId(i.next()),
319
+ v = _gr.nodeFromId(j),
320
+ vn = _gr.nodeFromId(j.next());
321
+
322
+ if (_cost[_gr.edge(u, un)] + _cost[_gr.edge(v, vn)] >
323
+ _cost[_gr.edge(u, v)] + _cost[_gr.edge(un, vn)])
324
+ {
325
+ _plist[PathListIt(_plist, i.next(), i).prevIndex()] = j.next();
326
+ _plist[PathListIt(_plist, j.next(), j).prevIndex()] = i.next();
327
+
328
+ _plist[i.nextIndex()] = j;
329
+ _plist[j.nextIndex()] = i;
330
+
331
+ return true;
332
+ }
333
+
334
+ return false;
335
+ }
336
+
337
+ // Executes the algorithm from the initial tour
338
+ Cost start() {
339
+
340
+ restart_search:
341
+ for (PathListIt i(_plist); true; ++i) {
342
+ PathListIt j = i;
343
+ if (++j == 0 || ++j == 0) break;
344
+ for (; j != 0 && j != i.prev(); ++j) {
345
+ if (checkOpt2(i, j))
346
+ goto restart_search;
347
+ }
348
+ }
349
+
350
+ PathListIt i(_plist);
351
+ _path.push_back(_gr.nodeFromId(i));
352
+ for (++i; i != 0; ++i)
353
+ _path.push_back(_gr.nodeFromId(i));
354
+
355
+ _sum = _cost[_gr.edge(_path.back(), _path.front())];
356
+ for (int i = 0; i < int(_path.size())-1; ++i) {
357
+ _sum += _cost[_gr.edge(_path[i], _path[i+1])];
358
+ }
359
+
360
+ return _sum;
361
+ }
362
+
363
+ };
364
+
365
+ }; // namespace lemon
366
+
367
+ #endif