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