src/work/marci/bipartite_graph_wrapper_test.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@368
     1
// -*- c++ -*-
marci@368
     2
#include <iostream>
marci@368
     3
#include <fstream>
marci@368
     4
#include <vector>
marci@368
     5
marci@642
     6
#include <sage_graph.h>
marci@389
     7
//#include <smart_graph.h>
marci@368
     8
//#include <dimacs.h>
marci@555
     9
#include <hugo/time_measure.h>
marci@762
    10
#include <for_each_macros.h>
marci@602
    11
#include <bfs_dfs.h>
marci@557
    12
#include <hugo/graph_wrapper.h>
marci@496
    13
#include <bipartite_graph_wrapper.h>
marci@555
    14
#include <hugo/maps.h>
marci@762
    15
#include <hugo/max_flow.h>
marci@762
    16
#include <augmenting_flow.h>
marci@368
    17
marci@368
    18
using namespace hugo;
marci@368
    19
marci@368
    20
int main() {
marci@642
    21
  typedef UndirSageGraph Graph; 
marci@368
    22
  typedef Graph::Node Node;
marci@368
    23
  typedef Graph::NodeIt NodeIt;
marci@368
    24
  typedef Graph::Edge Edge;
marci@368
    25
  typedef Graph::EdgeIt EdgeIt;
marci@368
    26
  typedef Graph::OutEdgeIt OutEdgeIt;
marci@368
    27
marci@368
    28
  Graph g;
marci@409
    29
//   std::vector<Graph::Node> s_nodes;
marci@409
    30
//   std::vector<Graph::Node> t_nodes;
marci@409
    31
//   for (int i=0; i<3; ++i) s_nodes.push_back(g.addNode());
marci@409
    32
//   for (int i=0; i<3; ++i) t_nodes.push_back(g.addNode());
marci@409
    33
//   g.addEdge(s_nodes[0], t_nodes[2]);
marci@409
    34
//   g.addEdge(t_nodes[1], s_nodes[2]);
marci@409
    35
//   g.addEdge(s_nodes[0], t_nodes[1]);
marci@409
    36
  
marci@409
    37
//   Graph::NodeMap<int> ref_map(g, -1);
marci@409
    38
//   IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map);
marci@409
    39
//   for (int i=0; i<3; ++i) bipartite_map.insert(s_nodes[i], false);
marci@409
    40
//   for (int i=0; i<3; ++i) bipartite_map.insert(t_nodes[i], true);
marci@409
    41
marci@409
    42
  std::vector<Graph::Node> nodes;
marci@409
    43
  for (int i=0; i<3; ++i) nodes.push_back(g.addNode());
marci@409
    44
  for (int i=3; i<6; ++i) nodes.push_back(g.addNode());
marci@409
    45
  g.addEdge(nodes[0], nodes[3+2]);
marci@409
    46
  g.addEdge(nodes[3+1], nodes[2]);
marci@409
    47
  g.addEdge(nodes[0], nodes[3+1]);
marci@368
    48
  
marci@368
    49
  Graph::NodeMap<int> ref_map(g, -1);
marci@368
    50
  IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map);
marci@409
    51
  for (int i=0; i<3; ++i) bipartite_map.insert(nodes[i], false);
marci@409
    52
  for (int i=3; i<6; ++i) bipartite_map.insert(nodes[i], true);
marci@409
    53
marci@409
    54
  Graph::Node u;
marci@409
    55
  std::cout << "These nodes will be in S:\n";
marci@409
    56
  //FIXME azert kellene ++, es invalid vizsgalat u-bol, hogy ezt le lehessen 
marci@409
    57
  //irni 1etlen FOR_EACH-csel.
marci@409
    58
  for (bipartite_map.first(u, false); g.valid(u); bipartite_map.next(u)) 
marci@409
    59
    std::cout << u << " ";
marci@409
    60
  std::cout << "\n";
marci@409
    61
  std::cout << "These nodes will be in T:\n";
marci@409
    62
  for (bipartite_map.first(u, true); g.valid(u); bipartite_map.next(u)) 
marci@409
    63
    std::cout << u << " ";
marci@409
    64
  std::cout << "\n";
marci@409
    65
marci@368
    66
  typedef BipartiteGraphWrapper<Graph> BGW;
marci@368
    67
  BGW bgw(g, bipartite_map);
marci@409
    68
marci@409
    69
  std::cout << "Nodes by NodeIt:\n";
marci@409
    70
  FOR_EACH_LOC(BGW::NodeIt, n, bgw) {
marci@409
    71
    std::cout << n << " ";
marci@409
    72
  }
marci@409
    73
  std::cout << "\n";
marci@409
    74
  std::cout << "Nodes in S by ClassNodeIt:\n";
marci@409
    75
  FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.S_CLASS) {
marci@409
    76
    std::cout << n << " ";
marci@409
    77
  }
marci@409
    78
  std::cout << "\n";
marci@409
    79
  std::cout << "Nodes in T by ClassNodeIt:\n";
marci@409
    80
  FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.T_CLASS) {
marci@409
    81
    std::cout << n << " ";
marci@409
    82
  }
marci@409
    83
  std::cout << "\n";
marci@409
    84
  std::cout << "Edges of the bipartite graph:\n";
marci@368
    85
  FOR_EACH_LOC(BGW::EdgeIt, e, bgw) {
marci@368
    86
    std::cout << bgw.tail(e) << "->" << bgw.head(e) << std::endl;
marci@368
    87
  }
marci@368
    88
marci@379
    89
  BGW::NodeMap<int> dbyj(bgw);
marci@379
    90
  BGW::EdgeMap<int> dbyxcj(bgw);
marci@368
    91
marci@379
    92
  typedef stGraphWrapper<BGW> stGW;
marci@379
    93
  stGW stgw(bgw);
marci@379
    94
  ConstMap<stGW::Edge, int> const1map(1);
marci@379
    95
  stGW::NodeMap<int> ize(stgw);
marci@379
    96
  stGW::EdgeMap<int> flow(stgw);
marci@379
    97
marci@379
    98
  BfsIterator< BGW, BGW::NodeMap<bool> > bfs(bgw);
marci@379
    99
  Graph::NodeIt si;
marci@379
   100
  Graph::Node s; 
marci@379
   101
  s=g.first(si);
marci@379
   102
  bfs.pushAndSetReached(BGW::Node(s));
marci@409
   103
  while (!bfs.finished()) { ++bfs; }
marci@379
   104
marci@409
   105
  FOR_EACH_LOC(stGW::NodeIt, n, stgw) { 
marci@409
   106
    std::cout << "out-edges of " << n << ":\n"; 
marci@409
   107
    FOR_EACH_INC_LOC(stGW::OutEdgeIt, e, stgw, n) { 
marci@409
   108
      std::cout << " " << e << "\n";
marci@409
   109
      std::cout << " aNode: " << stgw.aNode(e) << "\n";
marci@409
   110
      std::cout << " bNode: " << stgw.bNode(e) << "\n";      
marci@409
   111
    }
marci@409
   112
    std::cout << "in-edges of " << n << ":\n"; 
marci@409
   113
    FOR_EACH_INC_LOC(stGW::InEdgeIt, e, stgw, n) { 
marci@409
   114
      std::cout << " " << e << "\n";
marci@409
   115
      std::cout << " aNode: " << stgw.aNode(e) << "\n";
marci@409
   116
      std::cout << " bNode: " << stgw.bNode(e) << "\n";     
marci@409
   117
    }
marci@409
   118
  }
marci@409
   119
  std::cout << "Edges of the stGraphWrapper:\n"; 
marci@409
   120
  FOR_EACH_LOC(stGW::EdgeIt, n, stgw) { 
marci@409
   121
    std::cout << " " << n << "\n";
marci@409
   122
  }
marci@379
   123
marci@409
   124
  stGW::NodeMap<bool> b(stgw);
marci@409
   125
  FOR_EACH_LOC(stGW::NodeIt, n, stgw) { 
marci@409
   126
    std::cout << n << ": " << b[n] <<"\n";
marci@409
   127
  }
marci@409
   128
marci@409
   129
  std::cout << "Bfs from s: \n";
marci@409
   130
  BfsIterator< stGW, stGW::NodeMap<bool> > bfs_stgw(stgw);
marci@409
   131
  bfs_stgw.pushAndSetReached(stgw.S_NODE);
marci@409
   132
  while (!bfs_stgw.finished()) { 
marci@409
   133
    std::cout << " " << stGW::OutEdgeIt(bfs_stgw) << "\n";
marci@409
   134
    ++bfs_stgw; 
marci@409
   135
  }
marci@379
   136
  
marci@762
   137
  AugmentingFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@379
   138
    max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow);
marci@409
   139
  while (max_flow_test.augmentOnShortestPath()) { }
marci@393
   140
marci@393
   141
  std::cout << max_flow_test.flowValue() << std::endl;
marci@368
   142
marci@368
   143
  return 0;
marci@368
   144
}