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