test/suurballe_test.cc
author Peter Kovacs <kpeter@inf.elte.hu>
Thu, 28 Feb 2013 18:13:48 +0100
changeset 1227 08f2dc76e82e
parent 956 141f9c0db4a3
parent 1171 7e368d9b67f7
child 1259 8b2d4e5d96e4
permissions -rw-r--r--
Rename flow init functions according to Preflow (#177)
alpar@463
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@357
     2
 *
alpar@463
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@357
     4
 *
alpar@956
     5
 * Copyright (C) 2003-2010
alpar@357
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@357
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@357
     8
 *
alpar@357
     9
 * Permission to use, modify and distribute this software is granted
alpar@357
    10
 * provided that this copyright notice appears in all copies. For
alpar@357
    11
 * precise terms see the accompanying LICENSE file.
alpar@357
    12
 *
alpar@357
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@357
    14
 * express or implied, and with no claim as to its suitability for any
alpar@357
    15
 * purpose.
alpar@357
    16
 *
alpar@357
    17
 */
alpar@357
    18
alpar@357
    19
#include <iostream>
alpar@357
    20
alpar@357
    21
#include <lemon/list_graph.h>
alpar@357
    22
#include <lemon/lgf_reader.h>
alpar@357
    23
#include <lemon/path.h>
alpar@357
    24
#include <lemon/suurballe.h>
kpeter@670
    25
#include <lemon/concepts/digraph.h>
kpeter@931
    26
#include <lemon/concepts/heap.h>
alpar@357
    27
alpar@357
    28
#include "test_tools.h"
alpar@357
    29
alpar@357
    30
using namespace lemon;
alpar@357
    31
alpar@442
    32
char test_lgf[] =
alpar@442
    33
  "@nodes\n"
kpeter@670
    34
  "label\n"
kpeter@670
    35
  "1\n"
kpeter@670
    36
  "2\n"
kpeter@670
    37
  "3\n"
kpeter@670
    38
  "4\n"
kpeter@670
    39
  "5\n"
kpeter@670
    40
  "6\n"
kpeter@670
    41
  "7\n"
kpeter@670
    42
  "8\n"
kpeter@670
    43
  "9\n"
kpeter@670
    44
  "10\n"
kpeter@670
    45
  "11\n"
kpeter@670
    46
  "12\n"
alpar@442
    47
  "@arcs\n"
kpeter@670
    48
  "      length\n"
kpeter@670
    49
  " 1  2  70\n"
kpeter@670
    50
  " 1  3 150\n"
kpeter@670
    51
  " 1  4  80\n"
kpeter@670
    52
  " 2  8  80\n"
kpeter@670
    53
  " 3  5 140\n"
kpeter@670
    54
  " 4  6  60\n"
kpeter@670
    55
  " 4  7  80\n"
kpeter@670
    56
  " 4  8 110\n"
kpeter@670
    57
  " 5  7  60\n"
kpeter@670
    58
  " 5 11 120\n"
kpeter@670
    59
  " 6  3   0\n"
kpeter@670
    60
  " 6  9 140\n"
kpeter@670
    61
  " 6 10  90\n"
kpeter@670
    62
  " 7  1  30\n"
kpeter@670
    63
  " 8 12  60\n"
kpeter@670
    64
  " 9 12  50\n"
kpeter@670
    65
  "10 12  70\n"
kpeter@670
    66
  "10  2 100\n"
kpeter@670
    67
  "10  7  60\n"
kpeter@670
    68
  "11 10  20\n"
kpeter@670
    69
  "12 11  30\n"
alpar@442
    70
  "@attributes\n"
alpar@442
    71
  "source  1\n"
alpar@442
    72
  "target 12\n"
alpar@442
    73
  "@end\n";
alpar@442
    74
kpeter@670
    75
// Check the interface of Suurballe
kpeter@670
    76
void checkSuurballeCompile()
kpeter@670
    77
{
kpeter@670
    78
  typedef int VType;
kpeter@670
    79
  typedef concepts::Digraph Digraph;
kpeter@670
    80
kpeter@670
    81
  typedef Digraph::Node Node;
kpeter@670
    82
  typedef Digraph::Arc Arc;
kpeter@670
    83
  typedef concepts::ReadMap<Arc, VType> LengthMap;
alpar@956
    84
kpeter@931
    85
  typedef Suurballe<Digraph, LengthMap> ST;
kpeter@931
    86
  typedef Suurballe<Digraph, LengthMap>
kpeter@931
    87
    ::SetFlowMap<ST::FlowMap>
kpeter@931
    88
    ::SetPotentialMap<ST::PotentialMap>
kpeter@931
    89
    ::SetPath<SimplePath<Digraph> >
kpeter@931
    90
    ::SetHeap<concepts::Heap<VType, Digraph::NodeMap<int> > >
kpeter@931
    91
    ::Create SuurballeType;
kpeter@670
    92
kpeter@670
    93
  Digraph g;
kpeter@670
    94
  Node n;
kpeter@670
    95
  Arc e;
kpeter@670
    96
  LengthMap len;
kpeter@670
    97
  SuurballeType::FlowMap flow(g);
kpeter@670
    98
  SuurballeType::PotentialMap pi(g);
kpeter@670
    99
kpeter@670
   100
  SuurballeType suurb_test(g, len);
kpeter@670
   101
  const SuurballeType& const_suurb_test = suurb_test;
kpeter@670
   102
kpeter@670
   103
  suurb_test
kpeter@670
   104
    .flowMap(flow)
kpeter@670
   105
    .potentialMap(pi);
kpeter@670
   106
kpeter@670
   107
  int k;
kpeter@670
   108
  k = suurb_test.run(n, n);
kpeter@670
   109
  k = suurb_test.run(n, n, k);
kpeter@670
   110
  suurb_test.init(n);
kpeter@927
   111
  suurb_test.fullInit(n);
kpeter@927
   112
  suurb_test.start(n);
kpeter@927
   113
  suurb_test.start(n, k);
kpeter@670
   114
  k = suurb_test.findFlow(n);
kpeter@670
   115
  k = suurb_test.findFlow(n, k);
kpeter@670
   116
  suurb_test.findPaths();
alpar@956
   117
kpeter@670
   118
  int f;
kpeter@670
   119
  VType c;
alpar@1171
   120
  ignore_unused_variable_warning(f,c);
alpar@1171
   121
kpeter@670
   122
  c = const_suurb_test.totalLength();
kpeter@670
   123
  f = const_suurb_test.flow(e);
kpeter@670
   124
  const SuurballeType::FlowMap& fm =
kpeter@670
   125
    const_suurb_test.flowMap();
kpeter@670
   126
  c = const_suurb_test.potential(n);
kpeter@670
   127
  const SuurballeType::PotentialMap& pm =
kpeter@670
   128
    const_suurb_test.potentialMap();
kpeter@670
   129
  k = const_suurb_test.pathNum();
kpeter@670
   130
  Path<Digraph> p = const_suurb_test.path(k);
alpar@956
   131
kpeter@670
   132
  ignore_unused_variable_warning(fm);
kpeter@670
   133
  ignore_unused_variable_warning(pm);
kpeter@670
   134
}
kpeter@670
   135
kpeter@358
   136
// Check the feasibility of the flow
alpar@357
   137
template <typename Digraph, typename FlowMap>
alpar@463
   138
bool checkFlow( const Digraph& gr, const FlowMap& flow,
alpar@357
   139
                typename Digraph::Node s, typename Digraph::Node t,
alpar@357
   140
                int value )
alpar@357
   141
{
alpar@357
   142
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
alpar@357
   143
  for (ArcIt e(gr); e != INVALID; ++e)
alpar@357
   144
    if (!(flow[e] == 0 || flow[e] == 1)) return false;
alpar@357
   145
alpar@357
   146
  for (NodeIt n(gr); n != INVALID; ++n) {
alpar@357
   147
    int sum = 0;
alpar@357
   148
    for (OutArcIt e(gr, n); e != INVALID; ++e)
alpar@357
   149
      sum += flow[e];
alpar@357
   150
    for (InArcIt e(gr, n); e != INVALID; ++e)
alpar@357
   151
      sum -= flow[e];
alpar@357
   152
    if (n == s && sum != value) return false;
alpar@357
   153
    if (n == t && sum != -value) return false;
alpar@357
   154
    if (n != s && n != t && sum != 0) return false;
alpar@357
   155
  }
alpar@357
   156
alpar@357
   157
  return true;
alpar@357
   158
}
alpar@357
   159
kpeter@358
   160
// Check the optimalitiy of the flow
alpar@463
   161
template < typename Digraph, typename CostMap,
alpar@357
   162
           typename FlowMap, typename PotentialMap >
alpar@357
   163
bool checkOptimality( const Digraph& gr, const CostMap& cost,
alpar@357
   164
                      const FlowMap& flow, const PotentialMap& pi )
alpar@357
   165
{
kpeter@358
   166
  // Check the "Complementary Slackness" optimality condition
alpar@357
   167
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
alpar@357
   168
  bool opt = true;
alpar@357
   169
  for (ArcIt e(gr); e != INVALID; ++e) {
alpar@357
   170
    typename CostMap::Value red_cost =
alpar@357
   171
      cost[e] + pi[gr.source(e)] - pi[gr.target(e)];
alpar@357
   172
    opt = (flow[e] == 0 && red_cost >= 0) ||
alpar@357
   173
          (flow[e] == 1 && red_cost <= 0);
alpar@357
   174
    if (!opt) break;
alpar@357
   175
  }
alpar@357
   176
  return opt;
alpar@357
   177
}
alpar@357
   178
kpeter@358
   179
// Check a path
kpeter@358
   180
template <typename Digraph, typename Path>
alpar@357
   181
bool checkPath( const Digraph& gr, const Path& path,
alpar@357
   182
                typename Digraph::Node s, typename Digraph::Node t)
alpar@357
   183
{
alpar@357
   184
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
alpar@357
   185
  Node n = s;
alpar@357
   186
  for (int i = 0; i < path.length(); ++i) {
alpar@357
   187
    if (gr.source(path.nth(i)) != n) return false;
alpar@357
   188
    n = gr.target(path.nth(i));
alpar@357
   189
  }
alpar@357
   190
  return n == t;
alpar@357
   191
}
alpar@357
   192
alpar@357
   193
alpar@357
   194
int main()
alpar@357
   195
{
alpar@357
   196
  DIGRAPH_TYPEDEFS(ListDigraph);
alpar@357
   197
kpeter@358
   198
  // Read the test digraph
alpar@357
   199
  ListDigraph digraph;
alpar@357
   200
  ListDigraph::ArcMap<int> length(digraph);
kpeter@670
   201
  Node s, t;
alpar@357
   202
alpar@442
   203
  std::istringstream input(test_lgf);
alpar@357
   204
  DigraphReader<ListDigraph>(digraph, input).
kpeter@670
   205
    arcMap("length", length).
kpeter@670
   206
    node("source", s).
kpeter@670
   207
    node("target", t).
alpar@357
   208
    run();
alpar@463
   209
kpeter@932
   210
  // Check run()
alpar@357
   211
  {
kpeter@670
   212
    Suurballe<ListDigraph> suurballe(digraph, length);
alpar@956
   213
kpeter@932
   214
    // Find 2 paths
kpeter@670
   215
    check(suurballe.run(s, t) == 2, "Wrong number of paths");
kpeter@670
   216
    check(checkFlow(digraph, suurballe.flowMap(), s, t, 2),
alpar@357
   217
          "The flow is not feasible");
alpar@357
   218
    check(suurballe.totalLength() == 510, "The flow is not optimal");
alpar@463
   219
    check(checkOptimality(digraph, length, suurballe.flowMap(),
alpar@357
   220
                          suurballe.potentialMap()),
alpar@357
   221
          "Wrong potentials");
alpar@357
   222
    for (int i = 0; i < suurballe.pathNum(); ++i)
kpeter@670
   223
      check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
alpar@357
   224
kpeter@932
   225
    // Find 3 paths
kpeter@670
   226
    check(suurballe.run(s, t, 3) == 3, "Wrong number of paths");
kpeter@670
   227
    check(checkFlow(digraph, suurballe.flowMap(), s, t, 3),
alpar@357
   228
          "The flow is not feasible");
alpar@357
   229
    check(suurballe.totalLength() == 1040, "The flow is not optimal");
alpar@463
   230
    check(checkOptimality(digraph, length, suurballe.flowMap(),
alpar@357
   231
                          suurballe.potentialMap()),
alpar@357
   232
          "Wrong potentials");
alpar@357
   233
    for (int i = 0; i < suurballe.pathNum(); ++i)
kpeter@670
   234
      check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
alpar@357
   235
kpeter@932
   236
    // Find 5 paths (only 3 can be found)
kpeter@670
   237
    check(suurballe.run(s, t, 5) == 3, "Wrong number of paths");
kpeter@670
   238
    check(checkFlow(digraph, suurballe.flowMap(), s, t, 3),
alpar@357
   239
          "The flow is not feasible");
alpar@357
   240
    check(suurballe.totalLength() == 1040, "The flow is not optimal");
alpar@463
   241
    check(checkOptimality(digraph, length, suurballe.flowMap(),
alpar@357
   242
                          suurballe.potentialMap()),
alpar@357
   243
          "Wrong potentials");
alpar@357
   244
    for (int i = 0; i < suurballe.pathNum(); ++i)
kpeter@670
   245
      check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
alpar@357
   246
  }
alpar@357
   247
kpeter@932
   248
  // Check fullInit() + start()
kpeter@932
   249
  {
kpeter@932
   250
    Suurballe<ListDigraph> suurballe(digraph, length);
kpeter@932
   251
    suurballe.fullInit(s);
alpar@956
   252
kpeter@932
   253
    // Find 2 paths
kpeter@932
   254
    check(suurballe.start(t) == 2, "Wrong number of paths");
kpeter@932
   255
    check(suurballe.totalLength() == 510, "The flow is not optimal");
kpeter@932
   256
kpeter@932
   257
    // Find 3 paths
kpeter@932
   258
    check(suurballe.start(t, 3) == 3, "Wrong number of paths");
kpeter@932
   259
    check(suurballe.totalLength() == 1040, "The flow is not optimal");
kpeter@932
   260
kpeter@932
   261
    // Find 5 paths (only 3 can be found)
kpeter@932
   262
    check(suurballe.start(t, 5) == 3, "Wrong number of paths");
kpeter@932
   263
    check(suurballe.totalLength() == 1040, "The flow is not optimal");
kpeter@932
   264
  }
kpeter@670
   265
alpar@357
   266
  return 0;
alpar@357
   267
}