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