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