src/test/min_cost_flows_test.cc
changeset 896 3a98a1aa5a8f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/test/min_cost_flows_test.cc	Wed Sep 22 07:32:57 2004 +0000
     1.3 @@ -0,0 +1,107 @@
     1.4 +#include <iostream>
     1.5 +#include "test_tools.h"
     1.6 +#include <hugo/list_graph.h>
     1.7 +#include <hugo/min_cost_flows.h>
     1.8 +//#include <path.h>
     1.9 +//#include <maps.h>
    1.10 +
    1.11 +using namespace std;
    1.12 +using namespace hugo;
    1.13 +
    1.14 +
    1.15 +
    1.16 +bool passed = true;
    1.17 +/*
    1.18 +void check(bool rc, char *msg="") {
    1.19 +  passed = passed && rc;
    1.20 +  if(!rc) {
    1.21 +    std::cerr << "Test failed! ("<< msg << ")" << std::endl; \
    1.22 + 
    1.23 +
    1.24 +  }
    1.25 +}
    1.26 +*/
    1.27 +
    1.28 +
    1.29 +int main()
    1.30 +{
    1.31 +
    1.32 +  typedef ListGraph::Node Node;
    1.33 +  typedef ListGraph::Edge Edge;
    1.34 +
    1.35 +  ListGraph graph;
    1.36 +
    1.37 +  //Ahuja könyv példája
    1.38 +
    1.39 +  Node s=graph.addNode();
    1.40 +  Node v1=graph.addNode();  
    1.41 +  Node v2=graph.addNode();
    1.42 +  Node v3=graph.addNode();
    1.43 +  Node v4=graph.addNode();
    1.44 +  Node v5=graph.addNode();
    1.45 +  Node t=graph.addNode();
    1.46 +
    1.47 +  Edge s_v1=graph.addEdge(s, v1);
    1.48 +  Edge v1_v2=graph.addEdge(v1, v2);
    1.49 +  Edge s_v3=graph.addEdge(s, v3);
    1.50 +  Edge v2_v4=graph.addEdge(v2, v4);
    1.51 +  Edge v2_v5=graph.addEdge(v2, v5);
    1.52 +  Edge v3_v5=graph.addEdge(v3, v5);
    1.53 +  Edge v4_t=graph.addEdge(v4, t);
    1.54 +  Edge v5_t=graph.addEdge(v5, t);
    1.55 +  
    1.56 +
    1.57 +  ListGraph::EdgeMap<int> length(graph);
    1.58 +
    1.59 +  length.set(s_v1, 6);
    1.60 +  length.set(v1_v2, 4);
    1.61 +  length.set(s_v3, 10);
    1.62 +  length.set(v2_v4, 5);
    1.63 +  length.set(v2_v5, 1);
    1.64 +  length.set(v3_v5, 4);
    1.65 +  length.set(v4_t, 8);
    1.66 +  length.set(v5_t, 8);
    1.67 +
    1.68 +  ListGraph::EdgeMap<int> capacity(graph);
    1.69 +
    1.70 +  capacity.set(s_v1, 2);
    1.71 +  capacity.set(v1_v2, 2);
    1.72 +  capacity.set(s_v3, 1);
    1.73 +  capacity.set(v2_v4, 1);
    1.74 +  capacity.set(v2_v5, 1);
    1.75 +  capacity.set(v3_v5, 1);
    1.76 +  capacity.set(v4_t, 1);
    1.77 +  capacity.set(v5_t, 2);
    1.78 +
    1.79 +  //  ConstMap<Edge, int> const1map(1);
    1.80 +  std::cout << "Mincostflows algorithm test..." << std::endl;
    1.81 +
    1.82 +  MinCostFlows< ListGraph, ListGraph::EdgeMap<int>, ListGraph::EdgeMap<int> >
    1.83 +    surb_test(graph, length, capacity);
    1.84 +
    1.85 +  int k=1;
    1.86 +
    1.87 +  check(  surb_test.run(s,t,k) == 1 && surb_test.totalLength() == 19,"One path, total length should be 19");
    1.88 +
    1.89 +  check(surb_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?");
    1.90 +  
    1.91 +  k=2;
    1.92 +  
    1.93 +  check(  surb_test.run(s,t,k) == 2 && surb_test.totalLength() == 41,"Two paths, total length should be 41");
    1.94 +
    1.95 +  check(surb_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?");
    1.96 +  
    1.97 +  
    1.98 +  k=4;
    1.99 +
   1.100 +  check(  surb_test.run(s,t,k) == 3 && surb_test.totalLength() == 64,"Three paths, total length should be 64");
   1.101 +
   1.102 +  check(surb_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?");
   1.103 +
   1.104 +
   1.105 +  cout << (passed ? "All tests passed." : "Some of the tests failed!!!")
   1.106 +       << endl;
   1.107 +
   1.108 +  return passed ? 0 : 1;
   1.109 +
   1.110 +}