Welcome to mirror list, hosted at ThFree Co, Russian Federation.

opt2_tsp.h « lemon « lemon-1.3.1 « 3rd « quadriflow « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 686cc9cb89d451f28c3896497eb2971283dc5194 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* -*- mode: C++; indent-tabs-mode: nil; -*-
 *
 * This file is a part of LEMON, a generic C++ optimization library.
 *
 * Copyright (C) 2003-2013
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
 *
 * Permission to use, modify and distribute this software is granted
 * provided that this copyright notice appears in all copies. For
 * precise terms see the accompanying LICENSE file.
 *
 * This software is provided "AS IS" with no warranty of any kind,
 * express or implied, and with no claim as to its suitability for any
 * purpose.
 *
 */

#ifndef LEMON_OPT2_TSP_H
#define LEMON_OPT2_TSP_H

/// \ingroup tsp
/// \file
/// \brief 2-opt algorithm for symmetric TSP.

#include <vector>
#include <lemon/full_graph.h>

namespace lemon {

  /// \ingroup tsp
  ///
  /// \brief 2-opt algorithm for symmetric TSP.
  ///
  /// Opt2Tsp implements the 2-opt heuristic for solving
  /// symmetric \ref tsp "TSP".
  ///
  /// This algorithm starts with an initial tour and iteratively improves it.
  /// At each step, it removes two edges and the reconnects the created two
  /// paths in the other way if the resulting tour is shorter.
  /// The algorithm finishes when no such 2-opt move can be applied, and so
  /// the tour is 2-optimal.
  ///
  /// If no starting tour is given to the \ref run() function, then the
  /// algorithm uses the node sequence determined by the node IDs.
  /// Oherwise, it starts with the given tour.
  ///
  /// This is a rather slow but effective method.
  /// Its typical usage is the improvement of the result of a fast tour
  /// construction heuristic (e.g. the InsertionTsp algorithm).
  ///
  /// \tparam CM Type of the cost map.
  template <typename CM>
  class Opt2Tsp
  {
    public:

      /// Type of the cost map
      typedef CM CostMap;
      /// Type of the edge costs
      typedef typename CM::Value Cost;

    private:

      GRAPH_TYPEDEFS(FullGraph);

      const FullGraph &_gr;
      const CostMap &_cost;
      Cost _sum;
      std::vector<int> _plist;
      std::vector<Node> _path;

    public:

      /// \brief Constructor
      ///
      /// Constructor.
      /// \param gr The \ref FullGraph "full graph" the algorithm runs on.
      /// \param cost The cost map.
      Opt2Tsp(const FullGraph &gr, const CostMap &cost)
        : _gr(gr), _cost(cost) {}

      /// \name Execution Control
      /// @{

      /// \brief Runs the algorithm from scratch.
      ///
      /// This function runs the algorithm starting from the tour that is
      /// determined by the node ID sequence.
      ///
      /// \return The total cost of the found tour.
      Cost run() {
        _path.clear();

        if (_gr.nodeNum() == 0) return _sum = 0;
        else if (_gr.nodeNum() == 1) {
          _path.push_back(_gr(0));
          return _sum = 0;
        }
        else if (_gr.nodeNum() == 2) {
          _path.push_back(_gr(0));
          _path.push_back(_gr(1));
          return _sum = 2 * _cost[_gr.edge(_gr(0), _gr(1))];
        }

        _plist.resize(2*_gr.nodeNum());
        for (int i = 1; i < _gr.nodeNum()-1; ++i) {
          _plist[2*i] = i-1;
          _plist[2*i+1] = i+1;
        }
        _plist[0] = _gr.nodeNum()-1;
        _plist[1] = 1;
        _plist[2*_gr.nodeNum()-2] = _gr.nodeNum()-2;
        _plist[2*_gr.nodeNum()-1] = 0;

        return start();
      }

      /// \brief Runs the algorithm starting from the given tour.
      ///
      /// This function runs the algorithm starting from the given tour.
      ///
      /// \param tour The tour as a path structure. It must be a
      /// \ref checkPath() "valid path" containing excactly n arcs.
      ///
      /// \return The total cost of the found tour.
      template <typename Path>
      Cost run(const Path& tour) {
        _path.clear();

        if (_gr.nodeNum() == 0) return _sum = 0;
        else if (_gr.nodeNum() == 1) {
          _path.push_back(_gr(0));
          return _sum = 0;
        }
        else if (_gr.nodeNum() == 2) {
          _path.push_back(_gr(0));
          _path.push_back(_gr(1));
          return _sum = 2 * _cost[_gr.edge(_gr(0), _gr(1))];
        }

        _plist.resize(2*_gr.nodeNum());
        typename Path::ArcIt it(tour);
        int first = _gr.id(_gr.source(it)),
            prev = first,
            curr = _gr.id(_gr.target(it)),
            next = -1;
        _plist[2*first+1] = curr;
        for (++it; it != INVALID; ++it) {
          next = _gr.id(_gr.target(it));
          _plist[2*curr] = prev;
          _plist[2*curr+1] = next;
          prev = curr;
          curr = next;
        }
        _plist[2*first] = prev;

        return start();
      }

      /// \brief Runs the algorithm starting from the given tour.
      ///
      /// This function runs the algorithm starting from the given tour
      /// (node sequence).
      ///
      /// \param tour A vector that stores all <tt>Node</tt>s of the graph
      /// in the desired order.
      ///
      /// \return The total cost of the found tour.
      Cost run(const std::vector<Node>& tour) {
        _path.clear();

        if (_gr.nodeNum() == 0) return _sum = 0;
        else if (_gr.nodeNum() == 1) {
          _path.push_back(_gr(0));
          return _sum = 0;
        }
        else if (_gr.nodeNum() == 2) {
          _path.push_back(_gr(0));
          _path.push_back(_gr(1));
          return _sum = 2 * _cost[_gr.edge(_gr(0), _gr(1))];
        }

        _plist.resize(2*_gr.nodeNum());
        typename std::vector<Node>::const_iterator it = tour.begin();
        int first = _gr.id(*it),
            prev = first,
            curr = _gr.id(*(++it)),
            next = -1;
        _plist[2*first+1] = curr;
        for (++it; it != tour.end(); ++it) {
          next = _gr.id(*it);
          _plist[2*curr] = prev;
          _plist[2*curr+1] = next;
          prev = curr;
          curr = next;
        }
        _plist[2*first] = curr;
        _plist[2*curr] = prev;
        _plist[2*curr+1] = first;

        return start();
      }

      /// @}

      /// \name Query Functions
      /// @{

      /// \brief The total cost of the found tour.
      ///
      /// This function returns the total cost of the found tour.
      ///
      /// \pre run() must be called before using this function.
      Cost tourCost() const {
        return _sum;
      }

      /// \brief Returns a const reference to the node sequence of the
      /// found tour.
      ///
      /// This function returns a const reference to a vector
      /// that stores the node sequence of the found tour.
      ///
      /// \pre run() must be called before using this function.
      const std::vector<Node>& tourNodes() const {
        return _path;
      }

      /// \brief Gives back the node sequence of the found tour.
      ///
      /// This function copies the node sequence of the found tour into
      /// an STL container through the given output iterator. The
      /// <tt>value_type</tt> of the container must be <tt>FullGraph::Node</tt>.
      /// For example,
      /// \code
      /// std::vector<FullGraph::Node> nodes(countNodes(graph));
      /// tsp.tourNodes(nodes.begin());
      /// \endcode
      /// or
      /// \code
      /// std::list<FullGraph::Node> nodes;
      /// tsp.tourNodes(std::back_inserter(nodes));
      /// \endcode
      ///
      /// \pre run() must be called before using this function.
      template <typename Iterator>
      void tourNodes(Iterator out) const {
        std::copy(_path.begin(), _path.end(), out);
      }

      /// \brief Gives back the found tour as a path.
      ///
      /// This function copies the found tour as a list of arcs/edges into
      /// the given \ref lemon::concepts::Path "path structure".
      ///
      /// \pre run() must be called before using this function.
      template <typename Path>
      void tour(Path &path) const {
        path.clear();
        for (int i = 0; i < int(_path.size()) - 1; ++i) {
          path.addBack(_gr.arc(_path[i], _path[i+1]));
        }
        if (int(_path.size()) >= 2) {
          path.addBack(_gr.arc(_path.back(), _path.front()));
        }
      }

      /// @}

    private:

      // Iterator class for the linked list storage of the tour
      class PathListIt {
        public:
          PathListIt(const std::vector<int> &pl, int i=0)
            : plist(&pl), act(i), last(pl[2*act]) {}
          PathListIt(const std::vector<int> &pl, int i, int l)
            : plist(&pl), act(i), last(l) {}

          int nextIndex() const {
            return (*plist)[2*act] == last ? 2*act+1 : 2*act;
          }

          int prevIndex() const {
            return (*plist)[2*act] == last ? 2*act : 2*act+1;
          }

          int next() const {
            int x = (*plist)[2*act];
            return x == last ? (*plist)[2*act+1] : x;
          }

          int prev() const {
            return last;
          }

          PathListIt& operator++() {
            int tmp = act;
            act = next();
            last = tmp;
            return *this;
          }

          operator int() const {
            return act;
          }

        private:
          const std::vector<int> *plist;
          int act;
          int last;
      };

      // Checks and applies 2-opt move (if it improves the tour)
      bool checkOpt2(const PathListIt& i, const PathListIt& j) {
        Node u  = _gr.nodeFromId(i),
             un = _gr.nodeFromId(i.next()),
             v  = _gr.nodeFromId(j),
             vn = _gr.nodeFromId(j.next());

        if (_cost[_gr.edge(u, un)] + _cost[_gr.edge(v, vn)] >
            _cost[_gr.edge(u, v)] + _cost[_gr.edge(un, vn)])
        {
          _plist[PathListIt(_plist, i.next(), i).prevIndex()] = j.next();
          _plist[PathListIt(_plist, j.next(), j).prevIndex()] = i.next();

          _plist[i.nextIndex()] = j;
          _plist[j.nextIndex()] = i;

          return true;
        }

        return false;
     }

      // Executes the algorithm from the initial tour
      Cost start() {

      restart_search:
        for (PathListIt i(_plist); true; ++i) {
          PathListIt j = i;
          if (++j == 0 || ++j == 0) break;
          for (; j != 0 && j != i.prev(); ++j) {
            if (checkOpt2(i, j))
              goto restart_search;
          }
        }

        PathListIt i(_plist);
        _path.push_back(_gr.nodeFromId(i));
        for (++i; i != 0; ++i)
          _path.push_back(_gr.nodeFromId(i));

        _sum = _cost[_gr.edge(_path.back(), _path.front())];
        for (int i = 0; i < int(_path.size())-1; ++i) {
          _sum += _cost[_gr.edge(_path[i], _path[i+1])];
        }

        return _sum;
      }

  };

}; // namespace lemon

#endif