test/tsp_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Sun, 09 Jan 2011 15:06:55 +0100
changeset 1036 dff32ce3db71
child 1037 d3dcc49e6403
permissions -rw-r--r--
Make InsertionTsp much faster and improve docs (#386)
kpeter@1035
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@1035
     2
 *
kpeter@1035
     3
 * This file is a part of LEMON, a generic C++ optimization library.
kpeter@1035
     4
 *
kpeter@1035
     5
 * Copyright (C) 2003-2010
kpeter@1035
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@1035
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@1035
     8
 *
kpeter@1035
     9
 * Permission to use, modify and distribute this software is granted
kpeter@1035
    10
 * provided that this copyright notice appears in all copies. For
kpeter@1035
    11
 * precise terms see the accompanying LICENSE file.
kpeter@1035
    12
 *
kpeter@1035
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@1035
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@1035
    15
 * purpose.
kpeter@1035
    16
 *
kpeter@1035
    17
 */
kpeter@1035
    18
kpeter@1035
    19
#include <iostream>
kpeter@1035
    20
kpeter@1035
    21
#include <lemon/full_graph.h>
kpeter@1035
    22
#include <lemon/math.h>
kpeter@1035
    23
#include <lemon/maps.h>
kpeter@1035
    24
#include <lemon/random.h>
kpeter@1035
    25
#include <lemon/dim2.h>
kpeter@1035
    26
kpeter@1035
    27
#include <lemon/nearest_neighbor_tsp.h>
kpeter@1035
    28
#include <lemon/greedy_tsp.h>
kpeter@1035
    29
#include <lemon/insertion_tsp.h>
kpeter@1035
    30
#include <lemon/christofides_tsp.h>
kpeter@1035
    31
#include <lemon/opt2_tsp.h>
kpeter@1035
    32
kpeter@1035
    33
#include "test_tools.h"
kpeter@1035
    34
kpeter@1035
    35
using namespace lemon;
kpeter@1035
    36
kpeter@1035
    37
// // Tests checkMetricCost() function
kpeter@1035
    38
// void metricCostTest() {
kpeter@1035
    39
//   GRAPH_TYPEDEFS(FullGraph);
kpeter@1035
    40
//   FullGraph g(10);
kpeter@1035
    41
//   check(checkMetricCost(g, constMap<Edge>(0)), "Wrong checkMetricCost()");
kpeter@1035
    42
//   check(checkMetricCost(g, constMap<Edge>(1)), "Wrong checkMetricCost()");
kpeter@1035
    43
//   check(!checkMetricCost(g, constMap<Edge>(-1)), "Wrong checkMetricCost()");
kpeter@1035
    44
//   
kpeter@1035
    45
//   FullGraph::EdgeMap<float> cost(g);
kpeter@1035
    46
//   for (NodeIt u(g); u != INVALID; ++u) {
kpeter@1035
    47
//     for (NodeIt v(g); v != INVALID; ++v) {
kpeter@1035
    48
//       if (u == v) continue;
kpeter@1035
    49
//       float x1 = g.id(u), x2 = g.id(v);
kpeter@1035
    50
//       float y1 = x1 * x1, y2 = x2 * x2;
kpeter@1035
    51
//       cost[g.edge(u, v)] = std::sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
kpeter@1035
    52
//     }
kpeter@1035
    53
//   }
kpeter@1035
    54
//   check(checkMetricCost(g, cost), "Wrong checkMetricCost()");
kpeter@1035
    55
//   float eps = Tolerance<float>::defaultEpsilon();
kpeter@1035
    56
//   cost[g.edge(g(0), g(9))] =
kpeter@1035
    57
//     cost[g.edge(g(0), g(8))] + cost[g.edge(g(8), g(9))] + eps * 2;
kpeter@1035
    58
//   check(!checkMetricCost(g, cost), "Wrong checkMetricCost()");
kpeter@1035
    59
//   check(checkMetricCost(g, cost, Tolerance<float>(eps * 4)),
kpeter@1035
    60
//     "Wrong checkMetricCost()");
kpeter@1035
    61
// }
kpeter@1035
    62
kpeter@1035
    63
// Checks tour validity
kpeter@1035
    64
bool checkTour(const FullGraph &gr, const std::vector<FullGraph::Node> &p) {
kpeter@1035
    65
  FullGraph::NodeMap<bool> used(gr, false);
kpeter@1035
    66
  
kpeter@1035
    67
  int nodes = 0;
kpeter@1035
    68
  for (int i = 0; i < int(p.size()); ++i) {
kpeter@1035
    69
    if (used[p[i]]) return false;
kpeter@1035
    70
    used[p[i]] = true;
kpeter@1035
    71
    ++nodes;
kpeter@1035
    72
  }
kpeter@1035
    73
  
kpeter@1035
    74
  return (nodes == gr.nodeNum());
kpeter@1035
    75
}
kpeter@1035
    76
kpeter@1035
    77
// Checks tour validity
kpeter@1035
    78
bool checkTour(const FullGraph &gr, const Path<FullGraph> &p) {
kpeter@1035
    79
  FullGraph::NodeMap<bool> used(gr, false);
kpeter@1035
    80
  
kpeter@1035
    81
  if (!checkPath(gr, p)) return false;
kpeter@1035
    82
  if (gr.nodeNum() <= 1 && p.length() != 0) return false;
kpeter@1035
    83
  if (gr.nodeNum() > 1 && p.length() != gr.nodeNum()) return false;
kpeter@1035
    84
kpeter@1035
    85
  for (int i = 0; i < p.length(); ++i) {
kpeter@1035
    86
    if (used[gr.target(p.nth(i))]) return false;
kpeter@1035
    87
    used[gr.target(p.nth(i))] = true;
kpeter@1035
    88
  }
kpeter@1035
    89
  return true;
kpeter@1035
    90
}
kpeter@1035
    91
kpeter@1035
    92
// Checks tour cost
kpeter@1035
    93
template <typename CostMap>
kpeter@1035
    94
bool checkCost(const FullGraph &gr, const std::vector<FullGraph::Node> &p,
kpeter@1035
    95
               const CostMap &cost, typename CostMap::Value total)
kpeter@1035
    96
{
kpeter@1035
    97
  typedef typename CostMap::Value Cost;
kpeter@1035
    98
kpeter@1035
    99
  Cost s = 0;
kpeter@1035
   100
  for (int i = 0; i < int(p.size()) - 1; ++i)
kpeter@1035
   101
    s += cost[gr.edge(p[i], p[i+1])];
kpeter@1035
   102
  if (int(p.size()) >= 2)
kpeter@1035
   103
    s += cost[gr.edge(p.back(), p.front())];
kpeter@1035
   104
kpeter@1035
   105
  return !Tolerance<Cost>().different(s, total);
kpeter@1035
   106
}
kpeter@1035
   107
kpeter@1035
   108
// Checks tour cost
kpeter@1035
   109
template <typename CostMap>
kpeter@1035
   110
bool checkCost(const FullGraph &, const Path<FullGraph> &p,
kpeter@1035
   111
               const CostMap &cost, typename CostMap::Value total)
kpeter@1035
   112
{
kpeter@1035
   113
  typedef typename CostMap::Value Cost;
kpeter@1035
   114
kpeter@1035
   115
  Cost s = 0;
kpeter@1035
   116
  for (int i = 0; i < p.length(); ++i)
kpeter@1035
   117
    s += cost[p.nth(i)];
kpeter@1035
   118
kpeter@1035
   119
  return !Tolerance<Cost>().different(s, total);
kpeter@1035
   120
}
kpeter@1035
   121
kpeter@1035
   122
// Tests a TSP algorithm on small graphs
kpeter@1035
   123
template <typename TSP>
kpeter@1035
   124
void tspTestSmall(const std::string &alg_name) {
kpeter@1035
   125
  GRAPH_TYPEDEFS(FullGraph);
kpeter@1035
   126
kpeter@1035
   127
  for (int n = 0; n <= 5; ++n) {
kpeter@1035
   128
    FullGraph g(n);
kpeter@1035
   129
    unsigned nsize = n;
kpeter@1035
   130
    int esize = n <= 1 ? 0 : n;
kpeter@1035
   131
kpeter@1035
   132
    TSP alg(g, constMap<Edge, int>(1));
kpeter@1035
   133
kpeter@1035
   134
    check(alg.run() == esize, alg_name + ": Wrong total cost");
kpeter@1035
   135
    check(alg.tourCost() == esize, alg_name + ": Wrong total cost");
kpeter@1035
   136
kpeter@1035
   137
    std::list<Node> list;
kpeter@1035
   138
    std::vector<Node> vec;
kpeter@1035
   139
    alg.tourNodes(list);
kpeter@1035
   140
    alg.tourNodes(vec);
kpeter@1035
   141
    check(list.size() == nsize, alg_name + ": Wrong node sequence");
kpeter@1035
   142
    check(vec.size() == nsize,  alg_name + ": Wrong node sequence");
kpeter@1035
   143
    check(alg.tourNodes().size() == nsize, alg_name + ": Wrong node sequence");
kpeter@1035
   144
    check(checkTour(g, vec), alg_name + ": Wrong node sequence");
kpeter@1035
   145
    check(checkCost(g, vec, constMap<Edge, int>(1), esize),
kpeter@1035
   146
      alg_name + ": Wrong tour cost");
kpeter@1035
   147
kpeter@1035
   148
    SimplePath<FullGraph> path;
kpeter@1035
   149
    alg.tour(path);
kpeter@1035
   150
    check(path.length() == esize, alg_name + ": Wrong tour");
kpeter@1035
   151
    check(checkTour(g, path), alg_name + ": Wrong tour");
kpeter@1035
   152
    check(checkCost(g, path, constMap<Edge, int>(1), esize),
kpeter@1035
   153
      alg_name + ": Wrong tour cost");
kpeter@1035
   154
  }
kpeter@1035
   155
}
kpeter@1035
   156
kpeter@1035
   157
// Tests a TSP algorithm on random graphs
kpeter@1035
   158
template <typename TSP>
kpeter@1035
   159
void tspTestRandom(const std::string &alg_name) {
kpeter@1035
   160
  GRAPH_TYPEDEFS(FullGraph);
kpeter@1035
   161
kpeter@1035
   162
  FullGraph g(20);
kpeter@1035
   163
  FullGraph::NodeMap<dim2::Point<double> > pos(g);
kpeter@1035
   164
  DoubleEdgeMap cost(g);
kpeter@1035
   165
kpeter@1035
   166
  TSP alg(g, cost);
kpeter@1035
   167
  Opt2Tsp<DoubleEdgeMap > opt2(g, cost);
kpeter@1035
   168
kpeter@1035
   169
  for (int i = 1; i <= 3; i++) {
kpeter@1035
   170
    for (NodeIt u(g); u != INVALID; ++u) {
kpeter@1035
   171
      pos[u] = dim2::Point<double>(rnd(), rnd());
kpeter@1035
   172
    }
kpeter@1035
   173
    for (NodeIt u(g); u != INVALID; ++u) {
kpeter@1035
   174
      for (NodeIt v(g); v != INVALID; ++v) {
kpeter@1035
   175
        if (u == v) continue;
kpeter@1035
   176
        cost[g.edge(u, v)] = (pos[u] - pos[v]).normSquare();
kpeter@1035
   177
      }
kpeter@1035
   178
    }
kpeter@1035
   179
    
kpeter@1035
   180
    check(alg.run() > 0, alg_name + ": Wrong total cost");
kpeter@1035
   181
kpeter@1035
   182
    std::vector<Node> vec;
kpeter@1035
   183
    alg.tourNodes(vec);
kpeter@1035
   184
    check(checkTour(g, vec), alg_name + ": Wrong node sequence");
kpeter@1035
   185
    check(checkCost(g, vec, cost, alg.tourCost()),
kpeter@1035
   186
      alg_name + ": Wrong tour cost");
kpeter@1035
   187
kpeter@1035
   188
    SimplePath<FullGraph> path;
kpeter@1035
   189
    alg.tour(path);
kpeter@1035
   190
    check(checkTour(g, path), alg_name + ": Wrong tour");
kpeter@1035
   191
    check(checkCost(g, path, cost, alg.tourCost()),
kpeter@1035
   192
      alg_name + ": Wrong tour cost");
kpeter@1035
   193
    
kpeter@1035
   194
    check(!Tolerance<double>().less(alg.tourCost(), opt2.run(alg.tourNodes())),
kpeter@1035
   195
      "2-opt improvement: Wrong total cost");
kpeter@1035
   196
    check(checkTour(g, opt2.tourNodes()),
kpeter@1035
   197
      "2-opt improvement: Wrong node sequence");
kpeter@1035
   198
    check(checkCost(g, opt2.tourNodes(), cost, opt2.tourCost()),
kpeter@1035
   199
      "2-opt improvement: Wrong tour cost");
kpeter@1035
   200
    
kpeter@1035
   201
    check(!Tolerance<double>().less(alg.tourCost(), opt2.run(path)),
kpeter@1035
   202
      "2-opt improvement: Wrong total cost");
kpeter@1035
   203
    check(checkTour(g, opt2.tourNodes()),
kpeter@1035
   204
      "2-opt improvement: Wrong node sequence");
kpeter@1035
   205
    check(checkCost(g, opt2.tourNodes(), cost, opt2.tourCost()),
kpeter@1035
   206
      "2-opt improvement: Wrong tour cost");
kpeter@1035
   207
  }
kpeter@1035
   208
}
kpeter@1035
   209
kpeter@1035
   210
// Algorithm class for Nearest Insertion 
kpeter@1035
   211
template <typename CM>
kpeter@1035
   212
class NearestInsertionTsp : public InsertionTsp<CM> {
kpeter@1035
   213
public:
kpeter@1035
   214
  NearestInsertionTsp(const FullGraph &gr, const CM &cost)
kpeter@1035
   215
    : InsertionTsp<CM>(gr, cost) {}
kpeter@1035
   216
  typename CM::Value run() {
kpeter@1035
   217
    return InsertionTsp<CM>::run(InsertionTsp<CM>::NEAREST);
kpeter@1035
   218
  }
kpeter@1035
   219
};
kpeter@1035
   220
kpeter@1035
   221
// Algorithm class for Farthest Insertion 
kpeter@1035
   222
template <typename CM>
kpeter@1035
   223
class FarthestInsertionTsp : public InsertionTsp<CM> {
kpeter@1035
   224
public:
kpeter@1035
   225
  FarthestInsertionTsp(const FullGraph &gr, const CM &cost)
kpeter@1035
   226
    : InsertionTsp<CM>(gr, cost) {}
kpeter@1035
   227
  typename CM::Value run() {
kpeter@1035
   228
    return InsertionTsp<CM>::run(InsertionTsp<CM>::FARTHEST);
kpeter@1035
   229
  }
kpeter@1035
   230
};
kpeter@1035
   231
kpeter@1035
   232
// Algorithm class for Cheapest Insertion 
kpeter@1035
   233
template <typename CM>
kpeter@1035
   234
class CheapestInsertionTsp : public InsertionTsp<CM> {
kpeter@1035
   235
public:
kpeter@1035
   236
  CheapestInsertionTsp(const FullGraph &gr, const CM &cost)
kpeter@1035
   237
    : InsertionTsp<CM>(gr, cost) {}
kpeter@1035
   238
  typename CM::Value run() {
kpeter@1035
   239
    return InsertionTsp<CM>::run(InsertionTsp<CM>::CHEAPEST);
kpeter@1035
   240
  }
kpeter@1035
   241
};
kpeter@1035
   242
kpeter@1035
   243
// Algorithm class for Random Insertion 
kpeter@1035
   244
template <typename CM>
kpeter@1035
   245
class RandomInsertionTsp : public InsertionTsp<CM> {
kpeter@1035
   246
public:
kpeter@1035
   247
  RandomInsertionTsp(const FullGraph &gr, const CM &cost)
kpeter@1035
   248
    : InsertionTsp<CM>(gr, cost) {}
kpeter@1035
   249
  typename CM::Value run() {
kpeter@1035
   250
    return InsertionTsp<CM>::run(InsertionTsp<CM>::RANDOM);
kpeter@1035
   251
  }
kpeter@1035
   252
};
kpeter@1035
   253
kpeter@1035
   254
int main() {
kpeter@1035
   255
  GRAPH_TYPEDEFS(FullGraph);
kpeter@1035
   256
kpeter@1035
   257
  // metricCostTest();
kpeter@1035
   258
kpeter@1035
   259
  tspTestSmall<NearestNeighborTsp<ConstMap<Edge, int> > >("Nearest Neighbor");
kpeter@1035
   260
  tspTestSmall<GreedyTsp<ConstMap<Edge, int> > >("Greedy");
kpeter@1035
   261
  tspTestSmall<NearestInsertionTsp<ConstMap<Edge, int> > >("Nearest Insertion");
kpeter@1035
   262
  tspTestSmall<FarthestInsertionTsp<ConstMap<Edge, int> > >("Farthest Insertion");
kpeter@1035
   263
  tspTestSmall<CheapestInsertionTsp<ConstMap<Edge, int> > >("Cheapest Insertion");
kpeter@1035
   264
  tspTestSmall<RandomInsertionTsp<ConstMap<Edge, int> > >("Random Insertion");
kpeter@1035
   265
  tspTestSmall<ChristofidesTsp<ConstMap<Edge, int> > >("Christofides");
kpeter@1035
   266
  tspTestSmall<Opt2Tsp<ConstMap<Edge, int> > >("2-opt");
kpeter@1035
   267
kpeter@1035
   268
  tspTestRandom<NearestNeighborTsp<DoubleEdgeMap > >("Nearest Neighbor");
kpeter@1035
   269
  tspTestRandom<GreedyTsp<DoubleEdgeMap > >("Greedy");
kpeter@1035
   270
  tspTestRandom<NearestInsertionTsp<DoubleEdgeMap > >("Nearest Insertion");
kpeter@1035
   271
  tspTestRandom<FarthestInsertionTsp<DoubleEdgeMap > >("Farthest Insertion");
kpeter@1035
   272
  tspTestRandom<CheapestInsertionTsp<DoubleEdgeMap > >("Cheapest Insertion");
kpeter@1035
   273
  tspTestRandom<RandomInsertionTsp<DoubleEdgeMap > >("Random Insertion");
kpeter@1035
   274
  tspTestRandom<ChristofidesTsp<DoubleEdgeMap > >("Christofides");
kpeter@1035
   275
  tspTestRandom<Opt2Tsp<DoubleEdgeMap > >("2-opt");
kpeter@1035
   276
kpeter@1035
   277
  return 0;
kpeter@1035
   278
}