test/suurballe_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 21 Nov 2008 14:42:47 +0000
changeset 414 5a7dbeaed70e
parent 357 2f64c4a692a8
child 442 ff48c2738fb2
permissions -rw-r--r--
Port Circulation from svn -r3516 (#175)
Namely,
- port the files
- apply the migrate script
- apply the unify script
- fix the compilation
- strip the demo input file
- break long lines
alpar@357
     1
/* -*- C++ -*-
alpar@357
     2
 *
alpar@357
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@357
     4
 *
alpar@357
     5
 * Copyright (C) 2003-2008
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
#include <fstream>
alpar@357
    21
alpar@357
    22
#include <lemon/list_graph.h>
alpar@357
    23
#include <lemon/lgf_reader.h>
alpar@357
    24
#include <lemon/path.h>
alpar@357
    25
#include <lemon/suurballe.h>
alpar@357
    26
alpar@357
    27
#include "test_tools.h"
alpar@357
    28
alpar@357
    29
using namespace lemon;
alpar@357
    30
kpeter@358
    31
// Check the feasibility of the flow
alpar@357
    32
template <typename Digraph, typename FlowMap>
alpar@357
    33
bool checkFlow( const Digraph& gr, const FlowMap& flow, 
alpar@357
    34
                typename Digraph::Node s, typename Digraph::Node t,
alpar@357
    35
                int value )
alpar@357
    36
{
alpar@357
    37
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
alpar@357
    38
  for (ArcIt e(gr); e != INVALID; ++e)
alpar@357
    39
    if (!(flow[e] == 0 || flow[e] == 1)) return false;
alpar@357
    40
alpar@357
    41
  for (NodeIt n(gr); n != INVALID; ++n) {
alpar@357
    42
    int sum = 0;
alpar@357
    43
    for (OutArcIt e(gr, n); e != INVALID; ++e)
alpar@357
    44
      sum += flow[e];
alpar@357
    45
    for (InArcIt e(gr, n); e != INVALID; ++e)
alpar@357
    46
      sum -= flow[e];
alpar@357
    47
    if (n == s && sum != value) return false;
alpar@357
    48
    if (n == t && sum != -value) return false;
alpar@357
    49
    if (n != s && n != t && sum != 0) return false;
alpar@357
    50
  }
alpar@357
    51
alpar@357
    52
  return true;
alpar@357
    53
}
alpar@357
    54
kpeter@358
    55
// Check the optimalitiy of the flow
alpar@357
    56
template < typename Digraph, typename CostMap, 
alpar@357
    57
           typename FlowMap, typename PotentialMap >
alpar@357
    58
bool checkOptimality( const Digraph& gr, const CostMap& cost,
alpar@357
    59
                      const FlowMap& flow, const PotentialMap& pi )
alpar@357
    60
{
kpeter@358
    61
  // Check the "Complementary Slackness" optimality condition
alpar@357
    62
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
alpar@357
    63
  bool opt = true;
alpar@357
    64
  for (ArcIt e(gr); e != INVALID; ++e) {
alpar@357
    65
    typename CostMap::Value red_cost =
alpar@357
    66
      cost[e] + pi[gr.source(e)] - pi[gr.target(e)];
alpar@357
    67
    opt = (flow[e] == 0 && red_cost >= 0) ||
alpar@357
    68
          (flow[e] == 1 && red_cost <= 0);
alpar@357
    69
    if (!opt) break;
alpar@357
    70
  }
alpar@357
    71
  return opt;
alpar@357
    72
}
alpar@357
    73
kpeter@358
    74
// Check a path
kpeter@358
    75
template <typename Digraph, typename Path>
alpar@357
    76
bool checkPath( const Digraph& gr, const Path& path,
alpar@357
    77
                typename Digraph::Node s, typename Digraph::Node t)
alpar@357
    78
{
kpeter@358
    79
  // Check the "Complementary Slackness" optimality condition
alpar@357
    80
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
alpar@357
    81
  Node n = s;
alpar@357
    82
  for (int i = 0; i < path.length(); ++i) {
alpar@357
    83
    if (gr.source(path.nth(i)) != n) return false;
alpar@357
    84
    n = gr.target(path.nth(i));
alpar@357
    85
  }
alpar@357
    86
  return n == t;
alpar@357
    87
}
alpar@357
    88
alpar@357
    89
alpar@357
    90
int main()
alpar@357
    91
{
alpar@357
    92
  DIGRAPH_TYPEDEFS(ListDigraph);
alpar@357
    93
kpeter@358
    94
  // Read the test digraph
alpar@357
    95
  ListDigraph digraph;
alpar@357
    96
  ListDigraph::ArcMap<int> length(digraph);
alpar@357
    97
  Node source, target;
alpar@357
    98
alpar@357
    99
  std::string fname;
alpar@357
   100
  if(getenv("srcdir"))
alpar@357
   101
    fname = std::string(getenv("srcdir"));
alpar@357
   102
  else fname = ".";
alpar@357
   103
  fname += "/test/min_cost_flow_test.lgf";
alpar@357
   104
alpar@357
   105
  std::ifstream input(fname.c_str());
alpar@357
   106
  check(input, "Input file '" << fname << "' not found");
alpar@357
   107
  DigraphReader<ListDigraph>(digraph, input).
alpar@357
   108
    arcMap("cost", length).
alpar@357
   109
    node("source", source).
alpar@357
   110
    node("target", target).
alpar@357
   111
    run();
alpar@357
   112
  input.close();
alpar@357
   113
  
kpeter@358
   114
  // Find 2 paths
alpar@357
   115
  {
alpar@357
   116
    Suurballe<ListDigraph> suurballe(digraph, length, source, target);
alpar@357
   117
    check(suurballe.run(2) == 2, "Wrong number of paths");
alpar@357
   118
    check(checkFlow(digraph, suurballe.flowMap(), source, target, 2),
alpar@357
   119
          "The flow is not feasible");
alpar@357
   120
    check(suurballe.totalLength() == 510, "The flow is not optimal");
alpar@357
   121
    check(checkOptimality(digraph, length, suurballe.flowMap(), 
alpar@357
   122
                          suurballe.potentialMap()),
alpar@357
   123
          "Wrong potentials");
alpar@357
   124
    for (int i = 0; i < suurballe.pathNum(); ++i)
alpar@357
   125
      check(checkPath(digraph, suurballe.path(i), source, target),
alpar@357
   126
            "Wrong path");
alpar@357
   127
  }
alpar@357
   128
kpeter@358
   129
  // Find 3 paths
alpar@357
   130
  {
alpar@357
   131
    Suurballe<ListDigraph> suurballe(digraph, length, source, target);
alpar@357
   132
    check(suurballe.run(3) == 3, "Wrong number of paths");
alpar@357
   133
    check(checkFlow(digraph, suurballe.flowMap(), source, target, 3),
alpar@357
   134
          "The flow is not feasible");
alpar@357
   135
    check(suurballe.totalLength() == 1040, "The flow is not optimal");
alpar@357
   136
    check(checkOptimality(digraph, length, suurballe.flowMap(), 
alpar@357
   137
                          suurballe.potentialMap()),
alpar@357
   138
          "Wrong potentials");
alpar@357
   139
    for (int i = 0; i < suurballe.pathNum(); ++i)
alpar@357
   140
      check(checkPath(digraph, suurballe.path(i), source, target),
alpar@357
   141
            "Wrong path");
alpar@357
   142
  }
alpar@357
   143
kpeter@358
   144
  // Find 5 paths (only 3 can be found)
alpar@357
   145
  {
alpar@357
   146
    Suurballe<ListDigraph> suurballe(digraph, length, source, target);
alpar@357
   147
    check(suurballe.run(5) == 3, "Wrong number of paths");
alpar@357
   148
    check(checkFlow(digraph, suurballe.flowMap(), source, target, 3),
alpar@357
   149
          "The flow is not feasible");
alpar@357
   150
    check(suurballe.totalLength() == 1040, "The flow is not optimal");
alpar@357
   151
    check(checkOptimality(digraph, length, suurballe.flowMap(), 
alpar@357
   152
                          suurballe.potentialMap()),
alpar@357
   153
          "Wrong potentials");
alpar@357
   154
    for (int i = 0; i < suurballe.pathNum(); ++i)
alpar@357
   155
      check(checkPath(digraph, suurballe.path(i), source, target),
alpar@357
   156
            "Wrong path");
alpar@357
   157
  }
alpar@357
   158
alpar@357
   159
  return 0;
alpar@357
   160
}