src/test/min_cost_flow_test.cc
author alpar
Wed, 22 Sep 2004 12:42:19 +0000
changeset 904 b40afcf42a4d
child 906 17f31d280385
permissions -rw-r--r--
Do not document registry and map defines.
alpar@899
     1
#include <iostream>
alpar@899
     2
#include "test_tools.h"
alpar@899
     3
#include <hugo/list_graph.h>
alpar@899
     4
#include <hugo/min_cost_flow.h>
alpar@899
     5
//#include <path.h>
alpar@899
     6
//#include <maps.h>
alpar@899
     7
alpar@899
     8
using namespace std;
alpar@899
     9
using namespace hugo;
alpar@899
    10
alpar@899
    11
alpar@899
    12
alpar@899
    13
bool passed = true;
alpar@899
    14
/*
alpar@899
    15
void check(bool rc, char *msg="") {
alpar@899
    16
  passed = passed && rc;
alpar@899
    17
  if(!rc) {
alpar@899
    18
    std::cerr << "Test failed! ("<< msg << ")" << std::endl; \
alpar@899
    19
 
alpar@899
    20
alpar@899
    21
  }
alpar@899
    22
}
alpar@899
    23
*/
alpar@899
    24
alpar@899
    25
alpar@899
    26
int main()
alpar@899
    27
{
alpar@899
    28
alpar@899
    29
  typedef ListGraph::Node Node;
alpar@899
    30
  typedef ListGraph::Edge Edge;
alpar@899
    31
alpar@899
    32
  ListGraph graph;
alpar@899
    33
alpar@899
    34
  //Ahuja könyv példája
alpar@899
    35
alpar@899
    36
  Node s=graph.addNode();
alpar@899
    37
  Node v1=graph.addNode();  
alpar@899
    38
  Node v2=graph.addNode();
alpar@899
    39
  Node v3=graph.addNode();
alpar@899
    40
  Node v4=graph.addNode();
alpar@899
    41
  Node v5=graph.addNode();
alpar@899
    42
  Node t=graph.addNode();
alpar@899
    43
alpar@899
    44
  Edge s_v1=graph.addEdge(s, v1);
alpar@899
    45
  Edge v1_v2=graph.addEdge(v1, v2);
alpar@899
    46
  Edge s_v3=graph.addEdge(s, v3);
alpar@899
    47
  Edge v2_v4=graph.addEdge(v2, v4);
alpar@899
    48
  Edge v2_v5=graph.addEdge(v2, v5);
alpar@899
    49
  Edge v3_v5=graph.addEdge(v3, v5);
alpar@899
    50
  Edge v4_t=graph.addEdge(v4, t);
alpar@899
    51
  Edge v5_t=graph.addEdge(v5, t);
alpar@899
    52
  
alpar@899
    53
alpar@899
    54
  ListGraph::EdgeMap<int> length(graph);
alpar@899
    55
alpar@899
    56
  length.set(s_v1, 6);
alpar@899
    57
  length.set(v1_v2, 4);
alpar@899
    58
  length.set(s_v3, 10);
alpar@899
    59
  length.set(v2_v4, 5);
alpar@899
    60
  length.set(v2_v5, 1);
alpar@899
    61
  length.set(v3_v5, 4);
alpar@899
    62
  length.set(v4_t, 8);
alpar@899
    63
  length.set(v5_t, 8);
alpar@899
    64
alpar@899
    65
  ListGraph::EdgeMap<int> capacity(graph);
alpar@899
    66
alpar@899
    67
  capacity.set(s_v1, 2);
alpar@899
    68
  capacity.set(v1_v2, 2);
alpar@899
    69
  capacity.set(s_v3, 1);
alpar@899
    70
  capacity.set(v2_v4, 1);
alpar@899
    71
  capacity.set(v2_v5, 1);
alpar@899
    72
  capacity.set(v3_v5, 1);
alpar@899
    73
  capacity.set(v4_t, 1);
alpar@899
    74
  capacity.set(v5_t, 2);
alpar@899
    75
alpar@899
    76
  //  ConstMap<Edge, int> const1map(1);
alpar@899
    77
  std::cout << "Mincostflows algorithm test..." << std::endl;
alpar@899
    78
alpar@899
    79
  MinCostFlow< ListGraph, ListGraph::EdgeMap<int>, ListGraph::EdgeMap<int> >
alpar@899
    80
    surb_test(graph, length, capacity);
alpar@899
    81
alpar@899
    82
  int k=1;
alpar@899
    83
alpar@899
    84
  check(  surb_test.run(s,t,k) == 1 && surb_test.totalLength() == 19,"One path, total length should be 19");
alpar@899
    85
alpar@899
    86
  check(surb_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?");
alpar@899
    87
  
alpar@899
    88
  k=2;
alpar@899
    89
  
alpar@899
    90
  check(  surb_test.run(s,t,k) == 2 && surb_test.totalLength() == 41,"Two paths, total length should be 41");
alpar@899
    91
alpar@899
    92
  check(surb_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?");
alpar@899
    93
  
alpar@899
    94
  
alpar@899
    95
  k=4;
alpar@899
    96
alpar@899
    97
  check(  surb_test.run(s,t,k) == 3 && surb_test.totalLength() == 64,"Three paths, total length should be 64");
alpar@899
    98
alpar@899
    99
  check(surb_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?");
alpar@899
   100
alpar@899
   101
alpar@899
   102
  cout << (passed ? "All tests passed." : "Some of the tests failed!!!")
alpar@899
   103
       << endl;
alpar@899
   104
alpar@899
   105
  return passed ? 0 : 1;
alpar@899
   106
alpar@899
   107
}