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