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