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,533 @@
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_INSERTION_TSP_H
20
+ #define LEMON_INSERTION_TSP_H
21
+
22
+ /// \ingroup tsp
23
+ /// \file
24
+ /// \brief Insertion algorithm for symmetric TSP
25
+
26
+ #include <vector>
27
+ #include <functional>
28
+ #include <lemon/full_graph.h>
29
+ #include <lemon/maps.h>
30
+ #include <lemon/random.h>
31
+
32
+ namespace lemon {
33
+
34
+ /// \ingroup tsp
35
+ ///
36
+ /// \brief Insertion algorithm for symmetric TSP.
37
+ ///
38
+ /// InsertionTsp implements the insertion heuristic for solving
39
+ /// symmetric \ref tsp "TSP".
40
+ ///
41
+ /// This is a fast and effective tour construction method that has
42
+ /// many variants.
43
+ /// It starts with a subtour containing a few nodes of the graph and it
44
+ /// iteratively inserts the other nodes into this subtour according to a
45
+ /// certain node selection rule.
46
+ ///
47
+ /// This method is among the fastest TSP algorithms, and it typically
48
+ /// provides quite good solutions (usually much better than
49
+ /// \ref NearestNeighborTsp and \ref GreedyTsp).
50
+ ///
51
+ /// InsertionTsp implements four different node selection rules,
52
+ /// from which the most effective one (\e farthest \e node \e selection)
53
+ /// is used by default.
54
+ /// With this choice, the algorithm runs in O(n<sup>2</sup>) time.
55
+ /// For more information, see \ref SelectionRule.
56
+ ///
57
+ /// \tparam CM Type of the cost map.
58
+ template <typename CM>
59
+ class InsertionTsp
60
+ {
61
+ public:
62
+
63
+ /// Type of the cost map
64
+ typedef CM CostMap;
65
+ /// Type of the edge costs
66
+ typedef typename CM::Value Cost;
67
+
68
+ private:
69
+
70
+ GRAPH_TYPEDEFS(FullGraph);
71
+
72
+ const FullGraph &_gr;
73
+ const CostMap &_cost;
74
+ std::vector<Node> _notused;
75
+ std::vector<Node> _tour;
76
+ Cost _sum;
77
+
78
+ public:
79
+
80
+ /// \brief Constants for specifying the node selection rule.
81
+ ///
82
+ /// Enum type containing constants for specifying the node selection
83
+ /// rule for the \ref run() function.
84
+ ///
85
+ /// During the algorithm, nodes are selected for addition to the current
86
+ /// subtour according to the applied rule.
87
+ /// The FARTHEST method is one of the fastest selection rules, and
88
+ /// it is typically the most effective, thus it is the default
89
+ /// option. The RANDOM rule usually gives slightly worse results,
90
+ /// but it is more robust.
91
+ ///
92
+ /// The desired selection rule can be specified as a parameter of the
93
+ /// \ref run() function.
94
+ enum SelectionRule {
95
+
96
+ /// An unvisited node having minimum distance from the current
97
+ /// subtour is selected at each step.
98
+ /// The algorithm runs in O(n<sup>2</sup>) time using this
99
+ /// selection rule.
100
+ NEAREST,
101
+
102
+ /// An unvisited node having maximum distance from the current
103
+ /// subtour is selected at each step.
104
+ /// The algorithm runs in O(n<sup>2</sup>) time using this
105
+ /// selection rule.
106
+ FARTHEST,
107
+
108
+ /// An unvisited node whose insertion results in the least
109
+ /// increase of the subtour's total cost is selected at each step.
110
+ /// The algorithm runs in O(n<sup>3</sup>) time using this
111
+ /// selection rule, but in most cases, it is almost as fast as
112
+ /// with other rules.
113
+ CHEAPEST,
114
+
115
+ /// An unvisited node is selected randomly without any evaluation
116
+ /// at each step.
117
+ /// The global \ref rnd "random number generator instance" is used.
118
+ /// You can seed it before executing the algorithm, if you
119
+ /// would like to.
120
+ /// The algorithm runs in O(n<sup>2</sup>) time using this
121
+ /// selection rule.
122
+ RANDOM
123
+ };
124
+
125
+ public:
126
+
127
+ /// \brief Constructor
128
+ ///
129
+ /// Constructor.
130
+ /// \param gr The \ref FullGraph "full graph" the algorithm runs on.
131
+ /// \param cost The cost map.
132
+ InsertionTsp(const FullGraph &gr, const CostMap &cost)
133
+ : _gr(gr), _cost(cost) {}
134
+
135
+ /// \name Execution Control
136
+ /// @{
137
+
138
+ /// \brief Runs the algorithm.
139
+ ///
140
+ /// This function runs the algorithm.
141
+ ///
142
+ /// \param rule The node selection rule. For more information, see
143
+ /// \ref SelectionRule.
144
+ ///
145
+ /// \return The total cost of the found tour.
146
+ Cost run(SelectionRule rule = FARTHEST) {
147
+ _tour.clear();
148
+
149
+ if (_gr.nodeNum() == 0) return _sum = 0;
150
+ else if (_gr.nodeNum() == 1) {
151
+ _tour.push_back(_gr(0));
152
+ return _sum = 0;
153
+ }
154
+
155
+ switch (rule) {
156
+ case NEAREST:
157
+ init(true);
158
+ start<ComparingSelection<std::less<Cost> >,
159
+ DefaultInsertion>();
160
+ break;
161
+ case FARTHEST:
162
+ init(false);
163
+ start<ComparingSelection<std::greater<Cost> >,
164
+ DefaultInsertion>();
165
+ break;
166
+ case CHEAPEST:
167
+ init(true);
168
+ start<CheapestSelection, CheapestInsertion>();
169
+ break;
170
+ case RANDOM:
171
+ init(true);
172
+ start<RandomSelection, DefaultInsertion>();
173
+ break;
174
+ }
175
+ return _sum;
176
+ }
177
+
178
+ /// @}
179
+
180
+ /// \name Query Functions
181
+ /// @{
182
+
183
+ /// \brief The total cost of the found tour.
184
+ ///
185
+ /// This function returns the total cost of the found tour.
186
+ ///
187
+ /// \pre run() must be called before using this function.
188
+ Cost tourCost() const {
189
+ return _sum;
190
+ }
191
+
192
+ /// \brief Returns a const reference to the node sequence of the
193
+ /// found tour.
194
+ ///
195
+ /// This function returns a const reference to a vector
196
+ /// that stores the node sequence of the found tour.
197
+ ///
198
+ /// \pre run() must be called before using this function.
199
+ const std::vector<Node>& tourNodes() const {
200
+ return _tour;
201
+ }
202
+
203
+ /// \brief Gives back the node sequence of the found tour.
204
+ ///
205
+ /// This function copies the node sequence of the found tour into
206
+ /// an STL container through the given output iterator. The
207
+ /// <tt>value_type</tt> of the container must be <tt>FullGraph::Node</tt>.
208
+ /// For example,
209
+ /// \code
210
+ /// std::vector<FullGraph::Node> nodes(countNodes(graph));
211
+ /// tsp.tourNodes(nodes.begin());
212
+ /// \endcode
213
+ /// or
214
+ /// \code
215
+ /// std::list<FullGraph::Node> nodes;
216
+ /// tsp.tourNodes(std::back_inserter(nodes));
217
+ /// \endcode
218
+ ///
219
+ /// \pre run() must be called before using this function.
220
+ template <typename Iterator>
221
+ void tourNodes(Iterator out) const {
222
+ std::copy(_tour.begin(), _tour.end(), out);
223
+ }
224
+
225
+ /// \brief Gives back the found tour as a path.
226
+ ///
227
+ /// This function copies the found tour as a list of arcs/edges into
228
+ /// the given \ref lemon::concepts::Path "path structure".
229
+ ///
230
+ /// \pre run() must be called before using this function.
231
+ template <typename Path>
232
+ void tour(Path &path) const {
233
+ path.clear();
234
+ for (int i = 0; i < int(_tour.size()) - 1; ++i) {
235
+ path.addBack(_gr.arc(_tour[i], _tour[i+1]));
236
+ }
237
+ if (int(_tour.size()) >= 2) {
238
+ path.addBack(_gr.arc(_tour.back(), _tour.front()));
239
+ }
240
+ }
241
+
242
+ /// @}
243
+
244
+ private:
245
+
246
+ // Initializes the algorithm
247
+ void init(bool min) {
248
+ Edge min_edge = min ? mapMin(_gr, _cost) : mapMax(_gr, _cost);
249
+
250
+ _tour.clear();
251
+ _tour.push_back(_gr.u(min_edge));
252
+ _tour.push_back(_gr.v(min_edge));
253
+
254
+ _notused.clear();
255
+ for (NodeIt n(_gr); n!=INVALID; ++n) {
256
+ if (n != _gr.u(min_edge) && n != _gr.v(min_edge)) {
257
+ _notused.push_back(n);
258
+ }
259
+ }
260
+
261
+ _sum = _cost[min_edge] * 2;
262
+ }
263
+
264
+ // Executes the algorithm
265
+ template <class SelectionFunctor, class InsertionFunctor>
266
+ void start() {
267
+ SelectionFunctor selectNode(_gr, _cost, _tour, _notused);
268
+ InsertionFunctor insertNode(_gr, _cost, _tour, _sum);
269
+
270
+ for (int i=0; i<_gr.nodeNum()-2; ++i) {
271
+ insertNode.insert(selectNode.select());
272
+ }
273
+
274
+ _sum = _cost[_gr.edge(_tour.back(), _tour.front())];
275
+ for (int i = 0; i < int(_tour.size())-1; ++i) {
276
+ _sum += _cost[_gr.edge(_tour[i], _tour[i+1])];
277
+ }
278
+ }
279
+
280
+
281
+ // Implementation of the nearest and farthest selection rule
282
+ template <typename Comparator>
283
+ class ComparingSelection {
284
+ public:
285
+ ComparingSelection(const FullGraph &gr, const CostMap &cost,
286
+ std::vector<Node> &tour, std::vector<Node> &notused)
287
+ : _gr(gr), _cost(cost), _tour(tour), _notused(notused),
288
+ _dist(gr, 0), _compare()
289
+ {
290
+ // Compute initial distances for the unused nodes
291
+ for (unsigned int i=0; i<_notused.size(); ++i) {
292
+ Node u = _notused[i];
293
+ Cost min_dist = _cost[_gr.edge(u, _tour[0])];
294
+ for (unsigned int j=1; j<_tour.size(); ++j) {
295
+ Cost curr = _cost[_gr.edge(u, _tour[j])];
296
+ if (curr < min_dist) {
297
+ min_dist = curr;
298
+ }
299
+ }
300
+ _dist[u] = min_dist;
301
+ }
302
+ }
303
+
304
+ Node select() {
305
+
306
+ // Select an used node with minimum distance
307
+ Cost ins_dist = 0;
308
+ int ins_node = -1;
309
+ for (unsigned int i=0; i<_notused.size(); ++i) {
310
+ Cost curr = _dist[_notused[i]];
311
+ if (_compare(curr, ins_dist) || ins_node == -1) {
312
+ ins_dist = curr;
313
+ ins_node = i;
314
+ }
315
+ }
316
+
317
+ // Remove the selected node from the unused vector
318
+ Node sn = _notused[ins_node];
319
+ _notused[ins_node] = _notused.back();
320
+ _notused.pop_back();
321
+
322
+ // Update the distances of the remaining nodes
323
+ for (unsigned int i=0; i<_notused.size(); ++i) {
324
+ Node u = _notused[i];
325
+ Cost nc = _cost[_gr.edge(sn, u)];
326
+ if (nc < _dist[u]) {
327
+ _dist[u] = nc;
328
+ }
329
+ }
330
+
331
+ return sn;
332
+ }
333
+
334
+ private:
335
+ const FullGraph &_gr;
336
+ const CostMap &_cost;
337
+ std::vector<Node> &_tour;
338
+ std::vector<Node> &_notused;
339
+ FullGraph::NodeMap<Cost> _dist;
340
+ Comparator _compare;
341
+ };
342
+
343
+ // Implementation of the cheapest selection rule
344
+ class CheapestSelection {
345
+ private:
346
+ Cost costDiff(Node u, Node v, Node w) const {
347
+ return
348
+ _cost[_gr.edge(u, w)] +
349
+ _cost[_gr.edge(v, w)] -
350
+ _cost[_gr.edge(u, v)];
351
+ }
352
+
353
+ public:
354
+ CheapestSelection(const FullGraph &gr, const CostMap &cost,
355
+ std::vector<Node> &tour, std::vector<Node> &notused)
356
+ : _gr(gr), _cost(cost), _tour(tour), _notused(notused),
357
+ _ins_cost(gr, 0), _ins_pos(gr, -1)
358
+ {
359
+ // Compute insertion cost and position for the unused nodes
360
+ for (unsigned int i=0; i<_notused.size(); ++i) {
361
+ Node u = _notused[i];
362
+ Cost min_cost = costDiff(_tour.back(), _tour.front(), u);
363
+ int min_pos = 0;
364
+ for (unsigned int j=1; j<_tour.size(); ++j) {
365
+ Cost curr_cost = costDiff(_tour[j-1], _tour[j], u);
366
+ if (curr_cost < min_cost) {
367
+ min_cost = curr_cost;
368
+ min_pos = j;
369
+ }
370
+ }
371
+ _ins_cost[u] = min_cost;
372
+ _ins_pos[u] = min_pos;
373
+ }
374
+ }
375
+
376
+ Cost select() {
377
+
378
+ // Select an used node with minimum insertion cost
379
+ Cost min_cost = 0;
380
+ int min_node = -1;
381
+ for (unsigned int i=0; i<_notused.size(); ++i) {
382
+ Cost curr_cost = _ins_cost[_notused[i]];
383
+ if (curr_cost < min_cost || min_node == -1) {
384
+ min_cost = curr_cost;
385
+ min_node = i;
386
+ }
387
+ }
388
+
389
+ // Remove the selected node from the unused vector
390
+ Node sn = _notused[min_node];
391
+ _notused[min_node] = _notused.back();
392
+ _notused.pop_back();
393
+
394
+ // Insert the selected node into the tour
395
+ const int ipos = _ins_pos[sn];
396
+ _tour.insert(_tour.begin() + ipos, sn);
397
+
398
+ // Update the insertion cost and position of the remaining nodes
399
+ for (unsigned int i=0; i<_notused.size(); ++i) {
400
+ Node u = _notused[i];
401
+ Cost curr_cost = _ins_cost[u];
402
+ int curr_pos = _ins_pos[u];
403
+
404
+ int ipos_prev = ipos == 0 ? _tour.size()-1 : ipos-1;
405
+ int ipos_next = ipos == int(_tour.size())-1 ? 0 : ipos+1;
406
+ Cost nc1 = costDiff(_tour[ipos_prev], _tour[ipos], u);
407
+ Cost nc2 = costDiff(_tour[ipos], _tour[ipos_next], u);
408
+
409
+ if (nc1 <= curr_cost || nc2 <= curr_cost) {
410
+ // A new position is better than the old one
411
+ if (nc1 <= nc2) {
412
+ curr_cost = nc1;
413
+ curr_pos = ipos;
414
+ } else {
415
+ curr_cost = nc2;
416
+ curr_pos = ipos_next;
417
+ }
418
+ }
419
+ else {
420
+ if (curr_pos == ipos) {
421
+ // The minimum should be found again
422
+ curr_cost = costDiff(_tour.back(), _tour.front(), u);
423
+ curr_pos = 0;
424
+ for (unsigned int j=1; j<_tour.size(); ++j) {
425
+ Cost tmp_cost = costDiff(_tour[j-1], _tour[j], u);
426
+ if (tmp_cost < curr_cost) {
427
+ curr_cost = tmp_cost;
428
+ curr_pos = j;
429
+ }
430
+ }
431
+ }
432
+ else if (curr_pos > ipos) {
433
+ ++curr_pos;
434
+ }
435
+ }
436
+
437
+ _ins_cost[u] = curr_cost;
438
+ _ins_pos[u] = curr_pos;
439
+ }
440
+
441
+ return min_cost;
442
+ }
443
+
444
+ private:
445
+ const FullGraph &_gr;
446
+ const CostMap &_cost;
447
+ std::vector<Node> &_tour;
448
+ std::vector<Node> &_notused;
449
+ FullGraph::NodeMap<Cost> _ins_cost;
450
+ FullGraph::NodeMap<int> _ins_pos;
451
+ };
452
+
453
+ // Implementation of the random selection rule
454
+ class RandomSelection {
455
+ public:
456
+ RandomSelection(const FullGraph &, const CostMap &,
457
+ std::vector<Node> &, std::vector<Node> &notused)
458
+ : _notused(notused) {}
459
+
460
+ Node select() const {
461
+ const int index = rnd[_notused.size()];
462
+ Node n = _notused[index];
463
+ _notused[index] = _notused.back();
464
+ _notused.pop_back();
465
+ return n;
466
+ }
467
+
468
+ private:
469
+ std::vector<Node> &_notused;
470
+ };
471
+
472
+
473
+ // Implementation of the default insertion method
474
+ class DefaultInsertion {
475
+ private:
476
+ Cost costDiff(Node u, Node v, Node w) const {
477
+ return
478
+ _cost[_gr.edge(u, w)] +
479
+ _cost[_gr.edge(v, w)] -
480
+ _cost[_gr.edge(u, v)];
481
+ }
482
+
483
+ public:
484
+ DefaultInsertion(const FullGraph &gr, const CostMap &cost,
485
+ std::vector<Node> &tour, Cost &total_cost) :
486
+ _gr(gr), _cost(cost), _tour(tour), _total(total_cost) {}
487
+
488
+ void insert(Node n) const {
489
+ int min = 0;
490
+ Cost min_val =
491
+ costDiff(_tour.front(), _tour.back(), n);
492
+
493
+ for (unsigned int i=1; i<_tour.size(); ++i) {
494
+ Cost tmp = costDiff(_tour[i-1], _tour[i], n);
495
+ if (tmp < min_val) {
496
+ min = i;
497
+ min_val = tmp;
498
+ }
499
+ }
500
+
501
+ _tour.insert(_tour.begin()+min, n);
502
+ _total += min_val;
503
+ }
504
+
505
+ private:
506
+ const FullGraph &_gr;
507
+ const CostMap &_cost;
508
+ std::vector<Node> &_tour;
509
+ Cost &_total;
510
+ };
511
+
512
+ // Implementation of a special insertion method for the cheapest
513
+ // selection rule
514
+ class CheapestInsertion {
515
+ TEMPLATE_GRAPH_TYPEDEFS(FullGraph);
516
+ public:
517
+ CheapestInsertion(const FullGraph &, const CostMap &,
518
+ std::vector<Node> &, Cost &total_cost) :
519
+ _total(total_cost) {}
520
+
521
+ void insert(Cost diff) const {
522
+ _total += diff;
523
+ }
524
+
525
+ private:
526
+ Cost &_total;
527
+ };
528
+
529
+ };
530
+
531
+ }; // namespace lemon
532
+
533
+ #endif