COIN-OR::LEMON - Graph Library

Ticket #175: circ-test_2a7704daaf11.patch

File circ-test_2a7704daaf11.patch, 6.0 KB (added by Peter Kovacs, 12 years ago)
  • test/circulation_test.cc

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1228053277 -3600
    # Node ID 2a7704daaf11df636e14b3801d6c634e10103b22
    # Parent  b4fbf36feb573d42ed32a2ef8d24dc99c53c0fdc
    Improve test file for Circulation (#175)
    
     - Bug fix: add a missing #include.
     - Add compile test for various functions and named parameters.
     - Use a smaller digraph with lower bounds.
     - Test eight instances instead of two.
     - Remove the doc that was for the demo file.
    
    diff --git a/test/circulation_test.cc b/test/circulation_test.cc
    a b  
    1616 *
    1717 */
    1818
    19 ///\ingroup demos
    20 ///\file
    21 ///\brief Demonstrating the usage of LEMON's General Flow algorithm
    22 ///
    23 /// This demo program reads a general network circulation problem from the
    24 /// file 'circulation-input.lgf', runs the preflow based algorithm for
    25 /// finding a feasible solution and writes the output
    26 /// to 'circulation-input.lgf'
    27 ///
    28 /// \include circulation_demo.cc
    29 
    3019#include <iostream>
    3120
     21#include "test_tools.h"
    3222#include <lemon/list_graph.h>
    3323#include <lemon/circulation.h>
    3424#include <lemon/lgf_reader.h>
     25#include <lemon/concepts/digraph.h>
     26#include <lemon/concepts/maps.h>
     27#include <lemon/elevator.h>
    3528
    3629using namespace lemon;
    3730
    3831char test_lgf[] =
    3932  "@nodes\n"
    40   "label delta\n"
    41   "0     0\n"
    42   "1     13\n"
    43   "2     0\n"
    44   "3     0\n"
    45   "4     0\n"
    46   "5     0\n"
    47   "6     0\n"
    48   "7     0\n"
    49   "8     -13\n"
    50   "9     0\n"
    51   "@edges\n"
    52   "    label lo_cap up_cap\n"
    53   "0 1 0     0      20\n"
    54   "0 2 1     0      0\n"
    55   "1 1 2     0      3\n"
    56   "1 2 3     0      8\n"
    57   "1 3 4     0      8\n"
    58   "2 5 5     0      5\n"
    59   "3 2 6     0      5\n"
    60   "3 5 7     0      5\n"
    61   "3 6 8     0      5\n"
    62   "4 3 9     0      3\n"
    63   "5 7 10    0      3\n"
    64   "5 6 11    0      10\n"
    65   "5 8 12    0      10\n"
    66   "6 8 13    0      8\n"
    67   "8 9 14    0      20\n"
    68   "8 1 15    0      5\n"
    69   "9 5 16    0      5\n"
     33  "label\n"
     34  "0\n"
     35  "1\n"
     36  "2\n"
     37  "3\n"
     38  "4\n"
     39  "5\n"
     40  "@arcs\n"
     41  "     lcap  ucap\n"
     42  "0 1  2  10\n"
     43  "0 2  2  6\n"
     44  "1 3  4  7\n"
     45  "1 4  0  5\n"
     46  "2 4  1  3\n"
     47  "3 5  3  8\n"
     48  "4 5  3  7\n"
    7049  "@attributes\n"
    71   "source 1\n"
    72   "sink   8\n";
     50  "source 0\n"
     51  "sink   5\n";
     52
     53void checkCirculationCompile()
     54{
     55  typedef int VType;
     56  typedef concepts::Digraph Digraph;
     57
     58  typedef Digraph::Node Node;
     59  typedef Digraph::Arc Arc;
     60  typedef concepts::ReadMap<Arc,VType> CapMap;
     61  typedef concepts::ReadMap<Node,VType> DeltaMap;
     62  typedef concepts::ReadWriteMap<Arc,VType> FlowMap;
     63  typedef concepts::WriteMap<Node,bool> BarrierMap;
     64
     65  typedef Elevator<Digraph, Digraph::Node> Elev;
     66  typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev;
     67
     68  Digraph g;
     69  Node n;
     70  Arc a;
     71  CapMap lcap, ucap;
     72  DeltaMap delta;
     73  FlowMap flow;
     74  BarrierMap bar;
     75
     76  Circulation<Digraph, CapMap, CapMap, DeltaMap>
     77    ::SetFlowMap<FlowMap>
     78    ::SetElevator<Elev>
     79    ::SetStandardElevator<LinkedElev>
     80    ::Create circ_test(g,lcap,ucap,delta);
     81
     82  circ_test.lowerCapMap(lcap);
     83  circ_test.upperCapMap(ucap);
     84  circ_test.deltaMap(delta);
     85  flow = circ_test.flowMap();
     86  circ_test.flowMap(flow);
     87
     88  circ_test.init();
     89  circ_test.greedyInit();
     90  circ_test.start();
     91  circ_test.run();
     92
     93  circ_test.barrier(n);
     94  circ_test.barrierMap(bar);
     95  circ_test.flow(a);
     96}
     97
     98template <class G, class LM, class UM, class DM>
     99void checkCirculation(const G& g, const LM& lm, const UM& um,
     100                      const DM& dm, bool find)
     101{
     102  Circulation<G, LM, UM, DM> circ(g, lm, um, dm);
     103  bool ret = circ.run();
     104  if (find) {
     105    check(ret, "A feasible solution should have been found.");
     106    check(circ.checkFlow(), "The found flow is corrupt.");
     107    check(!circ.checkBarrier(), "A barrier should not have been found.");
     108  } else {
     109    check(!ret, "A feasible solution should not have been found.");
     110    check(circ.checkBarrier(), "The found barrier is corrupt.");
     111  }
     112}
    73113
    74114int main (int, char*[])
    75115{
     116  typedef ListDigraph Digraph;
     117  DIGRAPH_TYPEDEFS(Digraph);
    76118
    77     typedef ListDigraph Digraph;
    78     typedef Digraph::Node Node;
    79     typedef Digraph::NodeIt NodeIt;
    80     typedef Digraph::Arc Arc;
    81     typedef Digraph::ArcIt ArcIt;
    82     typedef Digraph::ArcMap<int> ArcMap;
    83     typedef Digraph::NodeMap<int> NodeMap;
    84     typedef Digraph::NodeMap<double> DNodeMap;
     119  Digraph g;
     120  IntArcMap lo(g), up(g);
     121  IntNodeMap delta(g, 0);
     122  Node s, t;
    85123
    86     Digraph g;
    87     ArcMap lo(g);
    88     ArcMap up(g);
    89     NodeMap delta(g);
    90     NodeMap nid(g);
    91     ArcMap eid(g);
    92     Node source, sink;
    93    
    94     std::istringstream input(test_lgf);
    95     DigraphReader<Digraph>(g,input).
    96       arcMap("lo_cap", lo).
    97       arcMap("up_cap", up).
    98       nodeMap("delta", delta).
    99       arcMap("label", eid).
    100       nodeMap("label", nid).
    101       node("source",source).
    102       node("sink",sink).
    103       run();
     124  std::istringstream input(test_lgf);
     125  DigraphReader<Digraph>(g,input).
     126    arcMap("lcap", lo).
     127    arcMap("ucap", up).
     128    node("source",s).
     129    node("sink",t).
     130    run();
    104131
    105     Circulation<Digraph> gen(g,lo,up,delta);
    106     bool ret=gen.run();
    107     check(ret,"A feasible solution should have been found.");
    108     check(gen.checkFlow(), "The found flow is corrupt.");
    109    
    110     delta[source]=14;
    111     delta[sink]=-14;
    112    
    113     bool ret2=gen.run();
    114     check(!ret2,"A feasible solution should not have been found.");
    115     check(gen.checkBarrier(), "The found barrier is corrupt.");
     132  delta[s] = 7; delta[t] = -7;
     133  checkCirculation(g, lo, up, delta, true);
    116134
     135  delta[s] = 13; delta[t] = -13;
     136  checkCirculation(g, lo, up, delta, true);
     137
     138  delta[s] = 6; delta[t] = -6;
     139  checkCirculation(g, lo, up, delta, false);
     140
     141  delta[s] = 14; delta[t] = -14;
     142  checkCirculation(g, lo, up, delta, false);
     143
     144  delta[s] = 7; delta[t] = -13;
     145  checkCirculation(g, lo, up, delta, true);
     146
     147  delta[s] = 5; delta[t] = -15;
     148  checkCirculation(g, lo, up, delta, true);
     149
     150  delta[s] = 10; delta[t] = -11;
     151  checkCirculation(g, lo, up, delta, true);
     152
     153  delta[s] = 11; delta[t] = -10;
     154  checkCirculation(g, lo, up, delta, false);
     155
     156  return 0;
    117157}