test/tsp_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Tue, 19 Sep 2017 15:23:43 +0200
changeset 1376 bc571f16e1e9
parent 1271 fb1c7da561ce
permissions -rw-r--r--
Merge bugfix #607
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
 *
alpar@1270
     5
 * Copyright (C) 2003-2013
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()");
alpar@1270
    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@1205
    64
template <typename Container>
kpeter@1205
    65
bool checkTour(const FullGraph &gr, const Container &p) {
kpeter@1203
    66
  FullGraph::NodeMap<bool> used(gr, false);
alpar@1270
    67
kpeter@1205
    68
  int node_cnt = 0;
alpar@1271
    69
  for (typename Container::const_iterator it = p.begin(); it != p.end(); ++it)
alpar@1271
    70
    {
alpar@1271
    71
      FullGraph::Node node = *it;
alpar@1271
    72
      if (used[node]) return false;
alpar@1271
    73
      used[node] = true;
alpar@1271
    74
      ++node_cnt;
alpar@1271
    75
    }
alpar@1270
    76
kpeter@1205
    77
  return (node_cnt == gr.nodeNum());
kpeter@1203
    78
}
kpeter@1203
    79
kpeter@1203
    80
// Checks tour validity
kpeter@1205
    81
bool checkTourPath(const FullGraph &gr, const Path<FullGraph> &p) {
kpeter@1203
    82
  FullGraph::NodeMap<bool> used(gr, false);
alpar@1270
    83
kpeter@1203
    84
  if (!checkPath(gr, p)) return false;
kpeter@1203
    85
  if (gr.nodeNum() <= 1 && p.length() != 0) return false;
kpeter@1203
    86
  if (gr.nodeNum() > 1 && p.length() != gr.nodeNum()) return false;
kpeter@1203
    87
kpeter@1203
    88
  for (int i = 0; i < p.length(); ++i) {
kpeter@1203
    89
    if (used[gr.target(p.nth(i))]) return false;
kpeter@1203
    90
    used[gr.target(p.nth(i))] = true;
kpeter@1203
    91
  }
kpeter@1203
    92
  return true;
kpeter@1203
    93
}
kpeter@1203
    94
kpeter@1203
    95
// Checks tour cost
kpeter@1203
    96
template <typename CostMap>
kpeter@1203
    97
bool checkCost(const FullGraph &gr, const std::vector<FullGraph::Node> &p,
kpeter@1203
    98
               const CostMap &cost, typename CostMap::Value total)
kpeter@1203
    99
{
kpeter@1203
   100
  typedef typename CostMap::Value Cost;
kpeter@1203
   101
kpeter@1203
   102
  Cost s = 0;
kpeter@1203
   103
  for (int i = 0; i < int(p.size()) - 1; ++i)
kpeter@1203
   104
    s += cost[gr.edge(p[i], p[i+1])];
kpeter@1203
   105
  if (int(p.size()) >= 2)
kpeter@1203
   106
    s += cost[gr.edge(p.back(), p.front())];
kpeter@1203
   107
kpeter@1203
   108
  return !Tolerance<Cost>().different(s, total);
kpeter@1203
   109
}
kpeter@1203
   110
kpeter@1203
   111
// Checks tour cost
kpeter@1203
   112
template <typename CostMap>
kpeter@1203
   113
bool checkCost(const FullGraph &, const Path<FullGraph> &p,
kpeter@1203
   114
               const CostMap &cost, typename CostMap::Value total)
kpeter@1203
   115
{
kpeter@1203
   116
  typedef typename CostMap::Value Cost;
kpeter@1203
   117
kpeter@1203
   118
  Cost s = 0;
kpeter@1203
   119
  for (int i = 0; i < p.length(); ++i)
kpeter@1203
   120
    s += cost[p.nth(i)];
kpeter@1203
   121
kpeter@1203
   122
  return !Tolerance<Cost>().different(s, total);
kpeter@1203
   123
}
kpeter@1203
   124
kpeter@1203
   125
// Tests a TSP algorithm on small graphs
kpeter@1203
   126
template <typename TSP>
kpeter@1203
   127
void tspTestSmall(const std::string &alg_name) {
kpeter@1203
   128
  GRAPH_TYPEDEFS(FullGraph);
kpeter@1203
   129
kpeter@1203
   130
  for (int n = 0; n <= 5; ++n) {
kpeter@1203
   131
    FullGraph g(n);
kpeter@1203
   132
    unsigned nsize = n;
kpeter@1203
   133
    int esize = n <= 1 ? 0 : n;
kpeter@1203
   134
alpar@1303
   135
    ConstMap<Edge, int> cost_map(1);
alpar@1303
   136
    TSP alg(g, cost_map);
kpeter@1203
   137
kpeter@1203
   138
    check(alg.run() == esize, alg_name + ": Wrong total cost");
kpeter@1203
   139
    check(alg.tourCost() == esize, alg_name + ": Wrong total cost");
kpeter@1203
   140
kpeter@1205
   141
    std::list<Node> list1(nsize), list2;
kpeter@1205
   142
    std::vector<Node> vec1(nsize), vec2;
kpeter@1205
   143
    alg.tourNodes(list1.begin());
kpeter@1205
   144
    alg.tourNodes(vec1.begin());
kpeter@1205
   145
    alg.tourNodes(std::front_inserter(list2));
kpeter@1205
   146
    alg.tourNodes(std::back_inserter(vec2));
kpeter@1205
   147
    check(checkTour(g, alg.tourNodes()), alg_name + ": Wrong node sequence");
kpeter@1205
   148
    check(checkTour(g, list1), alg_name + ": Wrong node sequence");
kpeter@1205
   149
    check(checkTour(g, vec1), alg_name + ": Wrong node sequence");
kpeter@1205
   150
    check(checkTour(g, list2), alg_name + ": Wrong node sequence");
kpeter@1205
   151
    check(checkTour(g, vec2), alg_name + ": Wrong node sequence");
kpeter@1205
   152
    check(checkCost(g, vec1, constMap<Edge, int>(1), esize),
kpeter@1203
   153
      alg_name + ": Wrong tour cost");
kpeter@1203
   154
kpeter@1203
   155
    SimplePath<FullGraph> path;
kpeter@1203
   156
    alg.tour(path);
kpeter@1203
   157
    check(path.length() == esize, alg_name + ": Wrong tour");
kpeter@1205
   158
    check(checkTourPath(g, path), alg_name + ": Wrong tour");
kpeter@1203
   159
    check(checkCost(g, path, constMap<Edge, int>(1), esize),
kpeter@1203
   160
      alg_name + ": Wrong tour cost");
kpeter@1203
   161
  }
kpeter@1203
   162
}
kpeter@1203
   163
kpeter@1203
   164
// Tests a TSP algorithm on random graphs
kpeter@1203
   165
template <typename TSP>
kpeter@1203
   166
void tspTestRandom(const std::string &alg_name) {
kpeter@1203
   167
  GRAPH_TYPEDEFS(FullGraph);
kpeter@1203
   168
kpeter@1203
   169
  FullGraph g(20);
kpeter@1203
   170
  FullGraph::NodeMap<dim2::Point<double> > pos(g);
kpeter@1203
   171
  DoubleEdgeMap cost(g);
kpeter@1203
   172
kpeter@1203
   173
  TSP alg(g, cost);
kpeter@1203
   174
  Opt2Tsp<DoubleEdgeMap > opt2(g, cost);
kpeter@1203
   175
kpeter@1203
   176
  for (int i = 1; i <= 3; i++) {
kpeter@1203
   177
    for (NodeIt u(g); u != INVALID; ++u) {
kpeter@1203
   178
      pos[u] = dim2::Point<double>(rnd(), rnd());
kpeter@1203
   179
    }
kpeter@1203
   180
    for (NodeIt u(g); u != INVALID; ++u) {
kpeter@1203
   181
      for (NodeIt v(g); v != INVALID; ++v) {
kpeter@1203
   182
        if (u == v) continue;
kpeter@1203
   183
        cost[g.edge(u, v)] = (pos[u] - pos[v]).normSquare();
kpeter@1203
   184
      }
kpeter@1203
   185
    }
alpar@1270
   186
kpeter@1203
   187
    check(alg.run() > 0, alg_name + ": Wrong total cost");
kpeter@1203
   188
kpeter@1203
   189
    std::vector<Node> vec;
kpeter@1205
   190
    alg.tourNodes(std::back_inserter(vec));
kpeter@1203
   191
    check(checkTour(g, vec), alg_name + ": Wrong node sequence");
kpeter@1203
   192
    check(checkCost(g, vec, cost, alg.tourCost()),
kpeter@1203
   193
      alg_name + ": Wrong tour cost");
kpeter@1203
   194
kpeter@1203
   195
    SimplePath<FullGraph> path;
kpeter@1203
   196
    alg.tour(path);
kpeter@1205
   197
    check(checkTourPath(g, path), alg_name + ": Wrong tour");
kpeter@1203
   198
    check(checkCost(g, path, cost, alg.tourCost()),
kpeter@1203
   199
      alg_name + ": Wrong tour cost");
alpar@1270
   200
kpeter@1203
   201
    check(!Tolerance<double>().less(alg.tourCost(), opt2.run(alg.tourNodes())),
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");
alpar@1270
   207
kpeter@1203
   208
    check(!Tolerance<double>().less(alg.tourCost(), opt2.run(path)),
kpeter@1203
   209
      "2-opt improvement: Wrong total cost");
kpeter@1203
   210
    check(checkTour(g, opt2.tourNodes()),
kpeter@1203
   211
      "2-opt improvement: Wrong node sequence");
kpeter@1203
   212
    check(checkCost(g, opt2.tourNodes(), cost, opt2.tourCost()),
kpeter@1203
   213
      "2-opt improvement: Wrong tour cost");
kpeter@1203
   214
  }
kpeter@1203
   215
}
kpeter@1203
   216
alpar@1270
   217
// Algorithm class for Nearest Insertion
kpeter@1203
   218
template <typename CM>
kpeter@1203
   219
class NearestInsertionTsp : public InsertionTsp<CM> {
kpeter@1203
   220
public:
kpeter@1203
   221
  NearestInsertionTsp(const FullGraph &gr, const CM &cost)
kpeter@1203
   222
    : InsertionTsp<CM>(gr, cost) {}
kpeter@1203
   223
  typename CM::Value run() {
kpeter@1203
   224
    return InsertionTsp<CM>::run(InsertionTsp<CM>::NEAREST);
kpeter@1203
   225
  }
kpeter@1203
   226
};
kpeter@1203
   227
alpar@1270
   228
// Algorithm class for Farthest Insertion
kpeter@1203
   229
template <typename CM>
kpeter@1203
   230
class FarthestInsertionTsp : public InsertionTsp<CM> {
kpeter@1203
   231
public:
kpeter@1203
   232
  FarthestInsertionTsp(const FullGraph &gr, const CM &cost)
kpeter@1203
   233
    : InsertionTsp<CM>(gr, cost) {}
kpeter@1203
   234
  typename CM::Value run() {
kpeter@1203
   235
    return InsertionTsp<CM>::run(InsertionTsp<CM>::FARTHEST);
kpeter@1203
   236
  }
kpeter@1203
   237
};
kpeter@1203
   238
alpar@1270
   239
// Algorithm class for Cheapest Insertion
kpeter@1203
   240
template <typename CM>
kpeter@1203
   241
class CheapestInsertionTsp : public InsertionTsp<CM> {
kpeter@1203
   242
public:
kpeter@1203
   243
  CheapestInsertionTsp(const FullGraph &gr, const CM &cost)
kpeter@1203
   244
    : InsertionTsp<CM>(gr, cost) {}
kpeter@1203
   245
  typename CM::Value run() {
kpeter@1203
   246
    return InsertionTsp<CM>::run(InsertionTsp<CM>::CHEAPEST);
kpeter@1203
   247
  }
kpeter@1203
   248
};
kpeter@1203
   249
alpar@1270
   250
// Algorithm class for Random Insertion
kpeter@1203
   251
template <typename CM>
kpeter@1203
   252
class RandomInsertionTsp : public InsertionTsp<CM> {
kpeter@1203
   253
public:
kpeter@1203
   254
  RandomInsertionTsp(const FullGraph &gr, const CM &cost)
kpeter@1203
   255
    : InsertionTsp<CM>(gr, cost) {}
kpeter@1203
   256
  typename CM::Value run() {
kpeter@1203
   257
    return InsertionTsp<CM>::run(InsertionTsp<CM>::RANDOM);
kpeter@1203
   258
  }
kpeter@1203
   259
};
kpeter@1203
   260
kpeter@1203
   261
int main() {
kpeter@1203
   262
  GRAPH_TYPEDEFS(FullGraph);
kpeter@1203
   263
kpeter@1203
   264
  // metricCostTest();
kpeter@1203
   265
kpeter@1203
   266
  tspTestSmall<NearestNeighborTsp<ConstMap<Edge, int> > >("Nearest Neighbor");
kpeter@1203
   267
  tspTestSmall<GreedyTsp<ConstMap<Edge, int> > >("Greedy");
kpeter@1203
   268
  tspTestSmall<NearestInsertionTsp<ConstMap<Edge, int> > >("Nearest Insertion");
alpar@1271
   269
  tspTestSmall<FarthestInsertionTsp<ConstMap<Edge, int> > >
alpar@1271
   270
    ("Farthest Insertion");
alpar@1271
   271
  tspTestSmall<CheapestInsertionTsp<ConstMap<Edge, int> > >
alpar@1271
   272
    ("Cheapest Insertion");
kpeter@1203
   273
  tspTestSmall<RandomInsertionTsp<ConstMap<Edge, int> > >("Random Insertion");
kpeter@1203
   274
  tspTestSmall<ChristofidesTsp<ConstMap<Edge, int> > >("Christofides");
kpeter@1203
   275
  tspTestSmall<Opt2Tsp<ConstMap<Edge, int> > >("2-opt");
kpeter@1203
   276
kpeter@1203
   277
  tspTestRandom<NearestNeighborTsp<DoubleEdgeMap > >("Nearest Neighbor");
kpeter@1203
   278
  tspTestRandom<GreedyTsp<DoubleEdgeMap > >("Greedy");
kpeter@1203
   279
  tspTestRandom<NearestInsertionTsp<DoubleEdgeMap > >("Nearest Insertion");
kpeter@1203
   280
  tspTestRandom<FarthestInsertionTsp<DoubleEdgeMap > >("Farthest Insertion");
kpeter@1203
   281
  tspTestRandom<CheapestInsertionTsp<DoubleEdgeMap > >("Cheapest Insertion");
kpeter@1203
   282
  tspTestRandom<RandomInsertionTsp<DoubleEdgeMap > >("Random Insertion");
kpeter@1203
   283
  tspTestRandom<ChristofidesTsp<DoubleEdgeMap > >("Christofides");
kpeter@1203
   284
  tspTestRandom<Opt2Tsp<DoubleEdgeMap > >("2-opt");
kpeter@1203
   285
kpeter@1203
   286
  return 0;
kpeter@1203
   287
}