src/work/athos/min_cost_flow.cc
author alpar
Mon, 13 Sep 2004 18:00:26 +0000
changeset 843 d56fad02dc55
parent 662 0155001b6f65
child 921 818510fa3d99
permissions -rw-r--r--
Minor corrections. "make distclean" still doesn't work.
athos@659
     1
#include <iostream>
athos@661
     2
//#include "test_tools.h"
athos@659
     3
#include <hugo/list_graph.h>
athos@659
     4
#include <mincostflow.h>
athos@659
     5
//#include <path.h>
athos@659
     6
//#include <maps.h>
athos@659
     7
athos@659
     8
using namespace std;
athos@659
     9
using namespace hugo;
athos@659
    10
athos@659
    11
athos@659
    12
athos@659
    13
bool passed = true;
athos@672
    14
athos@659
    15
void check(bool rc, char *msg="") {
athos@659
    16
  passed = passed && rc;
athos@659
    17
  if(!rc) {
athos@659
    18
    std::cerr << "Test failed! ("<< msg << ")" << std::endl; \
athos@659
    19
 
athos@659
    20
athos@659
    21
  }
athos@659
    22
}
athos@672
    23
athos@659
    24
athos@659
    25
athos@659
    26
int main()
athos@659
    27
{
athos@659
    28
athos@659
    29
  typedef ListGraph::Node Node;
athos@659
    30
  typedef ListGraph::Edge Edge;
athos@659
    31
athos@659
    32
  ListGraph graph;
athos@659
    33
athos@659
    34
  //Ahuja könyv példája
athos@659
    35
athos@659
    36
  Node s=graph.addNode();
athos@659
    37
  Node v1=graph.addNode();  
athos@659
    38
  Node v2=graph.addNode();
athos@659
    39
  Node v3=graph.addNode();
athos@659
    40
  Node v4=graph.addNode();
athos@659
    41
  Node v5=graph.addNode();
athos@659
    42
  Node t=graph.addNode();
athos@659
    43
athos@662
    44
  ListGraph::NodeMap<int> supply_demand(graph);
athos@662
    45
athos@662
    46
  supply_demand.set(s, 2);
athos@662
    47
  supply_demand.set(v1, 3);
athos@662
    48
  supply_demand.set(v3, -1);
athos@662
    49
  supply_demand.set(t, -4);
athos@662
    50
athos@659
    51
  Edge s_v1=graph.addEdge(s, v1);
athos@659
    52
  Edge v1_v2=graph.addEdge(v1, v2);
athos@659
    53
  Edge s_v3=graph.addEdge(s, v3);
athos@659
    54
  Edge v2_v4=graph.addEdge(v2, v4);
athos@659
    55
  Edge v2_v5=graph.addEdge(v2, v5);
athos@659
    56
  Edge v3_v5=graph.addEdge(v3, v5);
athos@659
    57
  Edge v4_t=graph.addEdge(v4, t);
athos@659
    58
  Edge v5_t=graph.addEdge(v5, t);
athos@659
    59
  
athos@659
    60
athos@661
    61
  ListGraph::EdgeMap<int> cost(graph);
athos@659
    62
athos@661
    63
  cost.set(s_v1, 6);
athos@661
    64
  cost.set(v1_v2, 4);
athos@661
    65
  cost.set(s_v3, 10);
athos@661
    66
  cost.set(v2_v4, 5);
athos@661
    67
  cost.set(v2_v5, 1);
athos@661
    68
  cost.set(v3_v5, 4);
athos@661
    69
  cost.set(v4_t, 8);
athos@661
    70
  cost.set(v5_t, 8);
athos@659
    71
athos@659
    72
  /*
athos@659
    73
  ListGraph::EdgeMap<int> capacity(graph);
athos@659
    74
athos@659
    75
  capacity.set(s_v1, 2);
athos@659
    76
  capacity.set(v1_v2, 2);
athos@659
    77
  capacity.set(s_v3, 1);
athos@659
    78
  capacity.set(v2_v4, 1);
athos@659
    79
  capacity.set(v2_v5, 1);
athos@659
    80
  capacity.set(v3_v5, 1);
athos@659
    81
  capacity.set(v4_t, 1);
athos@659
    82
  capacity.set(v5_t, 2);
athos@659
    83
  */
athos@659
    84
athos@659
    85
  //  ConstMap<Edge, int> const1map(1);
athos@659
    86
  std::cout << "Enhanced capacity scaling algorithm test (for the mincostflow problem)..." << std::endl;
athos@659
    87
athos@659
    88
  MinCostFlow< ListGraph, ListGraph::EdgeMap<int>, ListGraph::NodeMap<int> >
athos@661
    89
    min_cost_flow_test(graph, cost, supply_demand);
athos@659
    90
athos@662
    91
  min_cost_flow_test.run();
athos@662
    92
  //int k=1;
athos@672
    93
  check(min_cost_flow_test.checkOptimality(), "Is the primal-dual solution pair really optimal?");
athos@659
    94
athos@659
    95
  /*
athos@661
    96
  check(  min_cost_flow_test.run(s,t,k) == 1 && min_cost_flow_test.totalLength() == 19,"One path, total cost should be 19");
athos@659
    97
athos@659
    98
  check(min_cost_flow_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?");
athos@659
    99
  
athos@659
   100
  k=2;
athos@659
   101
  
athos@661
   102
  check(  min_cost_flow_test.run(s,t,k) == 2 && min_cost_flow_test.totalLength() == 41,"Two paths, total cost should be 41");
athos@659
   103
athos@659
   104
  check(min_cost_flow_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?");
athos@659
   105
  
athos@659
   106
  
athos@659
   107
  k=4;
athos@659
   108
athos@661
   109
  check(  min_cost_flow_test.run(s,t,k) == 3 && min_cost_flow_test.totalLength() == 64,"Three paths, total cost should be 64");
athos@659
   110
athos@659
   111
  check(min_cost_flow_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?");
athos@659
   112
athos@672
   113
  */
athos@659
   114
  cout << (passed ? "All tests passed." : "Some of the tests failed!!!")
athos@659
   115
       << endl;
athos@659
   116
athos@659
   117
  return passed ? 0 : 1;
athos@672
   118
  
athos@659
   119
}