src/work/marci/lp/max_flow_by_lp.cc
author alpar
Mon, 28 Feb 2005 13:03:36 +0000
changeset 1180 f772c360b466
parent 1025 3b1ad8bc21da
permissions -rw-r--r--
Better color to bw conversion
marci@764
     1
// -*- c++ -*-
marci@764
     2
#include <iostream>
marci@764
     3
#include <fstream>
marci@764
     4
alpar@921
     5
#include <lemon/smart_graph.h>
marci@1014
     6
#include <lemon/list_graph.h>
alpar@921
     7
#include <lemon/dimacs.h>
alpar@921
     8
#include <lemon/time_measure.h>
marci@764
     9
//#include <graph_wrapper.h>
marci@1014
    10
#include <lemon/preflow.h>
marci@764
    11
#include <augmenting_flow.h>
marci@764
    12
//#include <preflow_res.h>
marci@1031
    13
//#include <lp_solver_wrapper_2.h>
marci@1017
    14
#include <min_cost_gen_flow.h>
marci@764
    15
marci@764
    16
// Use a DIMACS max flow file as stdin.
marci@764
    17
// max_flow_demo < dimacs_max_flow_file
marci@764
    18
marci@1015
    19
using namespace lemon;
marci@1015
    20
marci@764
    21
int main(int, char **) {
marci@764
    22
marci@1014
    23
  typedef ListGraph MutableGraph;
marci@1025
    24
  typedef ListGraph Graph;
marci@764
    25
  typedef Graph::Node Node;
marci@1015
    26
  typedef Graph::Edge Edge;
marci@764
    27
  typedef Graph::EdgeIt EdgeIt;
marci@764
    28
marci@764
    29
  Graph g;
marci@764
    30
marci@764
    31
  Node s, t;
marci@764
    32
  Graph::EdgeMap<int> cap(g);
marci@764
    33
  //readDimacsMaxFlow(std::cin, g, s, t, cap);
marci@764
    34
  readDimacs(std::cin, g, cap, s, t);
marci@764
    35
  Timer ts;
marci@764
    36
  Graph::EdgeMap<int> flow(g); //0 flow
marci@1014
    37
  Preflow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > 
marci@764
    38
    max_flow_test(g, s, t, cap, flow);
marci@764
    39
  AugmentingFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > 
marci@764
    40
    augmenting_flow_test(g, s, t, cap, flow);
marci@764
    41
  
marci@764
    42
  Graph::NodeMap<bool> cut(g);
marci@764
    43
marci@764
    44
  {
marci@764
    45
    std::cout << "preflow ..." << std::endl;
marci@764
    46
    ts.reset();
marci@764
    47
    max_flow_test.run();
marci@764
    48
    std::cout << "elapsed time: " << ts << std::endl;
marci@764
    49
    std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@1014
    50
    max_flow_test.minCut(cut);
marci@764
    51
marci@1015
    52
    for (EdgeIt e(g); e!=INVALID; ++e) {
alpar@986
    53
      if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@764
    54
	std::cout << "Slackness does not hold!" << std::endl;
alpar@986
    55
      if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@764
    56
	std::cout << "Slackness does not hold!" << std::endl;
marci@764
    57
    }
marci@764
    58
  }
marci@764
    59
marci@764
    60
//   {
marci@764
    61
//     std::cout << "preflow ..." << std::endl;
marci@764
    62
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@764
    63
//     ts.reset();
marci@1014
    64
//     max_flow_test.preflow(Preflow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> >::GEN_FLOW);
marci@764
    65
//     std::cout << "elapsed time: " << ts << std::endl;
marci@764
    66
//     std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@764
    67
marci@764
    68
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) {
alpar@986
    69
//       if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@764
    70
// 	std::cout << "Slackness does not hold!" << std::endl;
alpar@986
    71
//       if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@764
    72
// 	std::cout << "Slackness does not hold!" << std::endl;
marci@764
    73
//     }
marci@764
    74
//   }
marci@764
    75
marci@764
    76
//   {
marci@764
    77
//     std::cout << "wrapped preflow ..." << std::endl;
marci@764
    78
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@764
    79
//     ts.reset();
marci@764
    80
//     pre_flow_res.run();
marci@764
    81
//     std::cout << "elapsed time: " << ts << std::endl;
marci@764
    82
//     std::cout << "flow value: "<< pre_flow_test.flowValue() << std::endl;
marci@764
    83
//   }
marci@764
    84
marci@764
    85
  {
marci@764
    86
    std::cout << "physical blocking flow augmentation ..." << std::endl;
marci@1015
    87
    for (EdgeIt e(g); e!=INVALID; ++e) flow.set(e, 0);
marci@764
    88
    ts.reset();
marci@764
    89
    int i=0;
marci@764
    90
    while (augmenting_flow_test.augmentOnBlockingFlow<MutableGraph>()) { ++i; }
marci@764
    91
    std::cout << "elapsed time: " << ts << std::endl;
marci@764
    92
    std::cout << "number of augmentation phases: " << i << std::endl; 
marci@764
    93
    std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@764
    94
marci@1015
    95
    for (EdgeIt e(g); e!=INVALID; ++e) {
alpar@986
    96
      if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@764
    97
	std::cout << "Slackness does not hold!" << std::endl;
alpar@986
    98
      if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@764
    99
	std::cout << "Slackness does not hold!" << std::endl;
marci@764
   100
    }
marci@764
   101
  }
marci@764
   102
marci@764
   103
//   {
marci@764
   104
//     std::cout << "faster physical blocking flow augmentation ..." << std::endl;
marci@764
   105
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@764
   106
//     ts.reset();
marci@764
   107
//     int i=0;
marci@764
   108
//     while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) { ++i; }
marci@764
   109
//     std::cout << "elapsed time: " << ts << std::endl;
marci@764
   110
//     std::cout << "number of augmentation phases: " << i << std::endl; 
marci@764
   111
//     std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@764
   112
//   }
marci@764
   113
marci@764
   114
  {
marci@764
   115
    std::cout << "on-the-fly blocking flow augmentation ..." << std::endl;
marci@1015
   116
    for (EdgeIt e(g); e!=INVALID; ++e) flow.set(e, 0);
marci@764
   117
    ts.reset();
marci@764
   118
    int i=0;
marci@764
   119
    while (augmenting_flow_test.augmentOnBlockingFlow2()) { ++i; }
marci@764
   120
    std::cout << "elapsed time: " << ts << std::endl;
marci@764
   121
    std::cout << "number of augmentation phases: " << i << std::endl; 
marci@764
   122
    std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@764
   123
marci@1015
   124
    for (EdgeIt e(g); e!=INVALID; ++e) {
alpar@986
   125
      if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@764
   126
	std::cout << "Slackness does not hold!" << std::endl;
alpar@986
   127
      if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@764
   128
	std::cout << "Slackness does not hold!" << std::endl;
marci@764
   129
    }
marci@764
   130
  }
marci@764
   131
marci@764
   132
//   {
marci@764
   133
//     std::cout << "on-the-fly shortest path augmentation ..." << std::endl;
marci@764
   134
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@764
   135
//     ts.reset();
marci@764
   136
//     int i=0;
marci@764
   137
//     while (augmenting_flow_test.augmentOnShortestPath()) { ++i; }
marci@764
   138
//     std::cout << "elapsed time: " << ts << std::endl;
marci@764
   139
//     std::cout << "number of augmentation phases: " << i << std::endl; 
marci@764
   140
//     std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@764
   141
marci@764
   142
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) {
alpar@986
   143
//       if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@764
   144
// 	std::cout << "Slackness does not hold!" << std::endl;
alpar@986
   145
//       if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@764
   146
// 	std::cout << "Slackness does not hold!" << std::endl;
marci@764
   147
//     }
marci@764
   148
//   }
marci@764
   149
marci@764
   150
//   {
marci@764
   151
//     std::cout << "on-the-fly shortest path augmentation ..." << std::endl;
marci@764
   152
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@764
   153
//     ts.reset();
marci@764
   154
//     int i=0;
marci@764
   155
//     while (augmenting_flow_test.augmentOnShortestPath2()) { ++i; }
marci@764
   156
//     std::cout << "elapsed time: " << ts << std::endl;
marci@764
   157
//     std::cout << "number of augmentation phases: " << i << std::endl; 
marci@764
   158
//     std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@764
   159
marci@764
   160
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) {
alpar@986
   161
//       if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@764
   162
// 	std::cout << "Slackness does not hold!" << std::endl;
alpar@986
   163
//       if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@764
   164
// 	std::cout << "Slackness does not hold!" << std::endl;
marci@764
   165
//     }
marci@764
   166
//   }
marci@764
   167
marci@764
   168
  ts.reset();
marci@1015
   169
marci@1015
   170
  Edge e=g.addEdge(t, s);
marci@1015
   171
  Graph::EdgeMap<int> cost(g, 0);
marci@1015
   172
  cost.set(e, -1);
marci@1015
   173
  cap.set(e, 10000);
marci@1015
   174
  typedef ConstMap<Node, int> Excess;
marci@1015
   175
  Excess excess(0);
marci@1015
   176
  typedef ConstMap<Edge, int> LCap;
marci@1015
   177
  LCap lcap(0);
marci@1015
   178
marci@1015
   179
  MinCostGenFlow<Graph, int, Excess, LCap> 
marci@1015
   180
    min_cost(g, excess, lcap, cap, flow, cost); 
marci@1025
   181
  min_cost.feasible();
marci@1031
   182
  min_cost.runByLP();
marci@1015
   183
marci@764
   184
  std::cout << "elapsed time: " << ts << std::endl;
marci@1015
   185
  std::cout << "flow value: "<< flow[e] << std::endl;
marci@764
   186
}