src/work/marci/max_flow_demo.cc
author marci
Tue, 17 Aug 2004 13:20:46 +0000
changeset 764 615aca7091d2
parent 652 4dfa1f79bf3e
child 775 e46a1f0623a0
permissions -rw-r--r--
An experimental LPSolverWrapper class which uses glpk. For a short
demo, max flow problems are solved with it. This demo does not
demonstrates, but the main aims of this class are row and column
generation capabilities, i.e. to be a core for easily
implementable branch-and-cut a column generetion algorithms.
marci@475
     1
// -*- c++ -*-
marci@475
     2
#include <iostream>
marci@475
     3
#include <fstream>
marci@475
     4
marci@642
     5
#include <sage_graph.h>
marci@555
     6
#include <hugo/smart_graph.h>
marci@555
     7
#include <hugo/dimacs.h>
marci@555
     8
#include <hugo/time_measure.h>
marci@475
     9
//#include <graph_wrapper.h>
marci@762
    10
#include <hugo/max_flow.h>
marci@762
    11
#include <augmenting_flow.h>
marci@475
    12
//#include <preflow_res.h>
marci@762
    13
#include <for_each_macros.h>
marci@651
    14
#include <graph_concept.h>
marci@475
    15
marci@475
    16
using namespace hugo;
marci@475
    17
marci@475
    18
// Use a DIMACS max flow file as stdin.
marci@475
    19
// read_dimacs_demo < dimacs_max_flow_file
marci@475
    20
marci@475
    21
marci@475
    22
//   struct Ize {
marci@475
    23
//   };
marci@475
    24
  
marci@475
    25
//   struct Mize {
marci@475
    26
//     Ize bumm;
marci@475
    27
//   };
marci@475
    28
marci@475
    29
//   template <typename B>
marci@475
    30
//     class Huha {
marci@475
    31
//     public:
marci@475
    32
//       int u;
marci@475
    33
//       B brr;
marci@475
    34
//     };
marci@475
    35
marci@475
    36
marci@475
    37
int main(int, char **) {
marci@475
    38
marci@642
    39
  typedef SageGraph MutableGraph;
marci@475
    40
marci@651
    41
  //typedef FullFeatureGraphConcept Graph;
marci@762
    42
  //typedef SmartGraph Graph;
marci@762
    43
  typedef SageGraph Graph;
marci@475
    44
  typedef Graph::Node Node;
marci@475
    45
  typedef Graph::EdgeIt EdgeIt;
marci@475
    46
marci@475
    47
marci@475
    48
//   Mize mize[10];
marci@475
    49
//   Mize bize[0];
marci@475
    50
//   Mize zize;
marci@475
    51
//   typedef Mize Tize[0];
marci@475
    52
marci@475
    53
//   std::cout << &zize << " " << sizeof(mize) << sizeof(Tize) << std::endl;
marci@475
    54
//   std::cout << sizeof(bize) << std::endl;
marci@475
    55
marci@475
    56
marci@475
    57
//   Huha<Tize> k;
marci@475
    58
//   std::cout << sizeof(k) << std::endl;
marci@475
    59
marci@475
    60
marci@475
    61
//   struct Bumm {
marci@475
    62
//     //int a;
marci@475
    63
//     bool b;
marci@475
    64
//   };
marci@475
    65
marci@475
    66
//   std::cout << sizeof(Bumm) << std::endl;
marci@475
    67
marci@475
    68
marci@577
    69
  Graph g;
marci@652
    70
marci@475
    71
  Node s, t;
marci@577
    72
  Graph::EdgeMap<int> cap(g);
marci@577
    73
  //readDimacsMaxFlow(std::cin, g, s, t, cap);
marci@577
    74
  readDimacs(std::cin, g, cap, s, t);
marci@475
    75
  Timer ts;
marci@577
    76
  Graph::EdgeMap<int> flow(g); //0 flow
marci@476
    77
  MaxFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > 
marci@577
    78
    max_flow_test(g, s, t, cap, flow);
marci@762
    79
  AugmentingFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > 
marci@762
    80
    augmenting_flow_test(g, s, t, cap, flow);
marci@762
    81
  
marci@646
    82
  Graph::NodeMap<bool> cut(g);
marci@475
    83
marci@475
    84
  {
marci@475
    85
    std::cout << "preflow ..." << std::endl;
marci@475
    86
    ts.reset();
marci@476
    87
    max_flow_test.run();
marci@475
    88
    std::cout << "elapsed time: " << ts << std::endl;
marci@476
    89
    std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@646
    90
    max_flow_test.actMinCut(cut);
marci@646
    91
marci@646
    92
    FOR_EACH_LOC(Graph::EdgeIt, e, g) {
marci@646
    93
      if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) 
marci@646
    94
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
    95
      if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) 
marci@646
    96
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
    97
    }
marci@475
    98
  }
marci@475
    99
marci@475
   100
  {
marci@475
   101
    std::cout << "preflow ..." << std::endl;
marci@577
   102
    FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@475
   103
    ts.reset();
marci@476
   104
    max_flow_test.preflow(MaxFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> >::GEN_FLOW);
marci@475
   105
    std::cout << "elapsed time: " << ts << std::endl;
marci@476
   106
    std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@646
   107
marci@646
   108
    FOR_EACH_LOC(Graph::EdgeIt, e, g) {
marci@646
   109
      if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) 
marci@646
   110
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   111
      if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) 
marci@646
   112
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   113
    }
marci@475
   114
  }
marci@475
   115
marci@475
   116
//   {
marci@475
   117
//     std::cout << "wrapped preflow ..." << std::endl;
marci@577
   118
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@475
   119
//     ts.reset();
marci@475
   120
//     pre_flow_res.run();
marci@475
   121
//     std::cout << "elapsed time: " << ts << std::endl;
marci@475
   122
//     std::cout << "flow value: "<< pre_flow_test.flowValue() << std::endl;
marci@475
   123
//   }
marci@475
   124
marci@475
   125
  {
marci@475
   126
    std::cout << "physical blocking flow augmentation ..." << std::endl;
marci@577
   127
    FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@475
   128
    ts.reset();
marci@475
   129
    int i=0;
marci@762
   130
    while (augmenting_flow_test.augmentOnBlockingFlow<MutableGraph>()) { ++i; }
marci@475
   131
    std::cout << "elapsed time: " << ts << std::endl;
marci@475
   132
    std::cout << "number of augmentation phases: " << i << std::endl; 
marci@762
   133
    std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@646
   134
marci@646
   135
    FOR_EACH_LOC(Graph::EdgeIt, e, g) {
marci@646
   136
      if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) 
marci@646
   137
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   138
      if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) 
marci@646
   139
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   140
    }
marci@475
   141
  }
marci@475
   142
marci@475
   143
//   {
marci@475
   144
//     std::cout << "faster physical blocking flow augmentation ..." << std::endl;
marci@577
   145
//     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@475
   146
//     ts.reset();
marci@475
   147
//     int i=0;
marci@475
   148
//     while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) { ++i; }
marci@475
   149
//     std::cout << "elapsed time: " << ts << std::endl;
marci@475
   150
//     std::cout << "number of augmentation phases: " << i << std::endl; 
marci@475
   151
//     std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@475
   152
//   }
marci@475
   153
marci@475
   154
  {
marci@475
   155
    std::cout << "on-the-fly blocking flow augmentation ..." << std::endl;
marci@577
   156
    FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@475
   157
    ts.reset();
marci@475
   158
    int i=0;
marci@762
   159
    while (augmenting_flow_test.augmentOnBlockingFlow2()) { ++i; }
marci@475
   160
    std::cout << "elapsed time: " << ts << std::endl;
marci@475
   161
    std::cout << "number of augmentation phases: " << i << std::endl; 
marci@762
   162
    std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@646
   163
marci@646
   164
    FOR_EACH_LOC(Graph::EdgeIt, e, g) {
marci@646
   165
      if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) 
marci@646
   166
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   167
      if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) 
marci@646
   168
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   169
    }
marci@475
   170
  }
marci@475
   171
marci@475
   172
  {
marci@475
   173
    std::cout << "on-the-fly shortest path augmentation ..." << std::endl;
marci@577
   174
    FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@475
   175
    ts.reset();
marci@475
   176
    int i=0;
marci@762
   177
    while (augmenting_flow_test.augmentOnShortestPath()) { ++i; }
marci@475
   178
    std::cout << "elapsed time: " << ts << std::endl;
marci@475
   179
    std::cout << "number of augmentation phases: " << i << std::endl; 
marci@762
   180
    std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@646
   181
marci@646
   182
    FOR_EACH_LOC(Graph::EdgeIt, e, g) {
marci@646
   183
      if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) 
marci@646
   184
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   185
      if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) 
marci@646
   186
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   187
    }
marci@475
   188
  }
marci@475
   189
marci@646
   190
  {
marci@646
   191
    std::cout << "on-the-fly shortest path augmentation ..." << std::endl;
marci@646
   192
    FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@646
   193
    ts.reset();
marci@646
   194
    int i=0;
marci@762
   195
    while (augmenting_flow_test.augmentOnShortestPath2()) { ++i; }
marci@646
   196
    std::cout << "elapsed time: " << ts << std::endl;
marci@646
   197
    std::cout << "number of augmentation phases: " << i << std::endl; 
marci@762
   198
    std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@646
   199
marci@646
   200
    FOR_EACH_LOC(Graph::EdgeIt, e, g) {
marci@646
   201
      if (cut[g.tail(e)] && !cut[g.head(e)] && !flow[e]==cap[e]) 
marci@646
   202
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   203
      if (!cut[g.tail(e)] && cut[g.head(e)] && flow[e]>0) 
marci@646
   204
	std::cout << "Slackness does not hold!" << std::endl;
marci@646
   205
    }
marci@646
   206
  }
marci@475
   207
marci@475
   208
  return 0;
marci@475
   209
}