src/work/marci/bipartite_matching_try.cc
author marci
Tue, 17 Aug 2004 13:20:46 +0000
changeset 764 615aca7091d2
parent 642 e812963087f0
child 768 a5e9303a5511
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@410
     1
// -*- c++ -*-
marci@410
     2
#include <iostream>
marci@410
     3
#include <fstream>
marci@410
     4
#include <vector>
marci@410
     5
#include <cstdlib>
marci@410
     6
marci@642
     7
#include <sage_graph.h>
marci@410
     8
//#include <smart_graph.h>
marci@410
     9
//#include <dimacs.h>
marci@555
    10
#include <hugo/time_measure.h>
marci@762
    11
#include <for_each_macros.h>
marci@602
    12
#include <bfs_dfs.h>
marci@557
    13
#include <hugo/graph_wrapper.h>
marci@496
    14
#include <bipartite_graph_wrapper.h>
marci@555
    15
#include <hugo/maps.h>
marci@762
    16
#include <hugo/max_flow.h>
marci@762
    17
#include <augmenting_flow.h>
marci@410
    18
marci@410
    19
/**
marci@410
    20
 * Inicializalja a veletlenszamgeneratort.
marci@410
    21
 * Figyelem, ez nem jo igazi random szamokhoz,
marci@410
    22
 * erre ne bizzad a titkaidat!
marci@410
    23
 */
marci@410
    24
void random_init()
marci@410
    25
{
marci@410
    26
	unsigned int seed = getpid();
marci@410
    27
	seed |= seed << 15;
marci@410
    28
	seed ^= time(0);
marci@410
    29
marci@410
    30
	srand(seed);
marci@410
    31
}
marci@410
    32
marci@410
    33
/**
marci@410
    34
 * Egy veletlen int-et ad vissza 0 es m-1 kozott.
marci@410
    35
 */
marci@410
    36
int random(int m)
marci@410
    37
{
marci@410
    38
  return int( double(m) * rand() / (RAND_MAX + 1.0) );
marci@410
    39
}
marci@410
    40
marci@410
    41
using namespace hugo;
marci@410
    42
marci@410
    43
int main() {
marci@642
    44
  typedef UndirSageGraph Graph; 
marci@410
    45
  typedef Graph::Node Node;
marci@410
    46
  typedef Graph::NodeIt NodeIt;
marci@410
    47
  typedef Graph::Edge Edge;
marci@410
    48
  typedef Graph::EdgeIt EdgeIt;
marci@410
    49
  typedef Graph::OutEdgeIt OutEdgeIt;
marci@410
    50
marci@410
    51
  Graph g;
marci@410
    52
marci@410
    53
  std::vector<Graph::Node> s_nodes;
marci@410
    54
  std::vector<Graph::Node> t_nodes;
marci@410
    55
marci@410
    56
  int a;
marci@410
    57
  std::cout << "number of nodes in the first color class=";
marci@410
    58
  std::cin >> a; 
marci@410
    59
  int b;
marci@410
    60
  std::cout << "number of nodes in the second color class=";
marci@410
    61
  std::cin >> b; 
marci@410
    62
  int m;
marci@410
    63
  std::cout << "number of edges=";
marci@410
    64
  std::cin >> m; 
marci@410
    65
  
marci@410
    66
marci@410
    67
  for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode());
marci@410
    68
  for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode());
marci@410
    69
marci@410
    70
  random_init();
marci@410
    71
  for(int i=0; i<m; ++i) {
marci@410
    72
    g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
marci@410
    73
  }
marci@410
    74
marci@410
    75
  Graph::NodeMap<int> ref_map(g, -1);
marci@410
    76
marci@410
    77
  IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map);
marci@410
    78
  for (int i=0; i<a; ++i) bipartite_map.insert(s_nodes[i], false);
marci@410
    79
  for (int i=0; i<b; ++i) bipartite_map.insert(t_nodes[i], true);
marci@410
    80
marci@410
    81
//   Graph::Node u;
marci@410
    82
//   std::cout << "These nodes will be in S:\n";
marci@410
    83
//   //FIXME azert kellene ++, es invalid vizsgalat u-bol, hogy ezt le lehessen 
marci@410
    84
//   //irni 1etlen FOR_EACH-csel.
marci@410
    85
//   for (bipartite_map.first(u, false); g.valid(u); bipartite_map.next(u)) 
marci@410
    86
//     std::cout << u << " ";
marci@410
    87
//   std::cout << "\n";
marci@410
    88
//   std::cout << "These nodes will be in T:\n";
marci@410
    89
//   for (bipartite_map.first(u, true); g.valid(u); bipartite_map.next(u)) 
marci@410
    90
//     std::cout << u << " ";
marci@410
    91
//   std::cout << "\n";
marci@410
    92
marci@410
    93
  typedef BipartiteGraphWrapper<Graph> BGW;
marci@410
    94
  BGW bgw(g, bipartite_map);
marci@410
    95
marci@410
    96
//   std::cout << "Nodes by NodeIt:\n";
marci@410
    97
//   FOR_EACH_LOC(BGW::NodeIt, n, bgw) {
marci@410
    98
//     std::cout << n << " ";
marci@410
    99
//   }
marci@410
   100
//   std::cout << "\n";
marci@410
   101
//   std::cout << "Nodes in S by ClassNodeIt:\n";
marci@410
   102
//   FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.S_CLASS) {
marci@410
   103
//     std::cout << n << " ";
marci@410
   104
//   }
marci@410
   105
//   std::cout << "\n";
marci@410
   106
//   std::cout << "Nodes in T by ClassNodeIt:\n";
marci@410
   107
//   FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.T_CLASS) {
marci@410
   108
//     std::cout << n << " ";
marci@410
   109
//   }
marci@410
   110
//   std::cout << "\n";
marci@410
   111
//   std::cout << "Edges of the bipartite graph:\n";
marci@410
   112
//   FOR_EACH_LOC(BGW::EdgeIt, e, bgw) {
marci@410
   113
//     std::cout << bgw.tail(e) << "->" << bgw.head(e) << std::endl;
marci@410
   114
//   }
marci@410
   115
marci@498
   116
//  BGW::NodeMap<int> dbyj(bgw);
marci@498
   117
//  BGW::EdgeMap<int> dbyxcj(bgw);
marci@410
   118
marci@410
   119
  typedef stGraphWrapper<BGW> stGW;
marci@410
   120
  stGW stgw(bgw);
marci@410
   121
  ConstMap<stGW::Edge, int> const1map(1);
marci@410
   122
//  stGW::NodeMap<int> ize(stgw);
marci@410
   123
marci@410
   124
//   BfsIterator< BGW, BGW::NodeMap<bool> > bfs(bgw);
marci@410
   125
//   Graph::NodeIt si;
marci@410
   126
//   Graph::Node s; 
marci@410
   127
//   s=g.first(si);
marci@410
   128
//   bfs.pushAndSetReached(BGW::Node(s));
marci@410
   129
//   while (!bfs.finished()) { ++bfs; }
marci@410
   130
marci@410
   131
//   FOR_EACH_LOC(stGW::NodeIt, n, stgw) { 
marci@410
   132
//     std::cout << "out-edges of " << n << ":\n"; 
marci@410
   133
//     FOR_EACH_INC_LOC(stGW::OutEdgeIt, e, stgw, n) { 
marci@410
   134
//       std::cout << " " << e << "\n";
marci@410
   135
//       std::cout << " aNode: " << stgw.aNode(e) << "\n";
marci@410
   136
//       std::cout << " bNode: " << stgw.bNode(e) << "\n";      
marci@410
   137
//     }
marci@410
   138
//     std::cout << "in-edges of " << n << ":\n"; 
marci@410
   139
//     FOR_EACH_INC_LOC(stGW::InEdgeIt, e, stgw, n) { 
marci@410
   140
//       std::cout << " " << e << "\n";
marci@410
   141
//       std::cout << " aNode: " << stgw.aNode(e) << "\n";
marci@410
   142
//       std::cout << " bNode: " << stgw.bNode(e) << "\n";     
marci@410
   143
//     }
marci@410
   144
//   }
marci@410
   145
//   std::cout << "Edges of the stGraphWrapper:\n"; 
marci@410
   146
//   FOR_EACH_LOC(stGW::EdgeIt, n, stgw) { 
marci@410
   147
//     std::cout << " " << n << "\n";
marci@410
   148
//   }
marci@410
   149
marci@410
   150
//   stGW::NodeMap<bool> b(stgw);
marci@410
   151
//   FOR_EACH_LOC(stGW::NodeIt, n, stgw) { 
marci@410
   152
//     std::cout << n << ": " << b[n] <<"\n";
marci@410
   153
//   }
marci@410
   154
marci@410
   155
//   std::cout << "Bfs from s: \n";
marci@410
   156
//   BfsIterator< stGW, stGW::NodeMap<bool> > bfs_stgw(stgw);
marci@410
   157
//   bfs_stgw.pushAndSetReached(stgw.S_NODE);
marci@410
   158
//   while (!bfs_stgw.finished()) { 
marci@410
   159
//     std::cout << " " << stGW::OutEdgeIt(bfs_stgw) << "\n";
marci@410
   160
//     ++bfs_stgw; 
marci@410
   161
//   }
marci@410
   162
marci@410
   163
marci@410
   164
  Timer ts;
marci@410
   165
  ts.reset();
marci@410
   166
  stGW::EdgeMap<int> max_flow(stgw);
marci@762
   167
  AugmentingFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@410
   168
    max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, max_flow);
marci@410
   169
//  while (max_flow_test.augmentOnShortestPath()) { }
marci@642
   170
  typedef SageGraph MutableGraph;
marci@410
   171
//  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
marci@410
   172
  while (max_flow_test.augmentOnBlockingFlow2()) {
marci@410
   173
   std::cout << max_flow_test.flowValue() << std::endl;
marci@410
   174
  }
marci@410
   175
  std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl;
marci@410
   176
  std::cout << "elapsed time: " << ts << std::endl;
marci@410
   177
//   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
marci@410
   178
//     std::cout << e << ": " << max_flow[e] << "\n"; 
marci@410
   179
//   }
marci@410
   180
//   std::cout << "\n";
marci@410
   181
marci@410
   182
  ts.reset();
marci@410
   183
  stGW::EdgeMap<int> pre_flow(stgw);
marci@476
   184
  MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@465
   185
    pre_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, pre_flow/*, true*/);
marci@410
   186
  pre_flow_test.run();
marci@410
   187
  std::cout << "pre flow value: " << max_flow_test.flowValue() << std::endl;
marci@410
   188
  std::cout << "elapsed time: " << ts << std::endl;
marci@410
   189
//   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
marci@410
   190
//     std::cout << e << ": " << pre_flow[e] << "\n"; 
marci@410
   191
//   }
marci@410
   192
//   std::cout << "\n";
marci@410
   193
marci@410
   194
  return 0;
marci@410
   195
}