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