src/work/athos/mincostflow.h
changeset 662 0155001b6f65
parent 661 d306e777117e
child 671 708df4dc6ab6
equal deleted inserted replaced
5:6f9a2786a55e 6:14537e3d29ba
     9 #include <hugo/dijkstra.h>
     9 #include <hugo/dijkstra.h>
    10 #include <hugo/graph_wrapper.h>
    10 #include <hugo/graph_wrapper.h>
    11 #include <hugo/maps.h>
    11 #include <hugo/maps.h>
    12 #include <vector>
    12 #include <vector>
    13 #include <list>
    13 #include <list>
       
    14 #include <values.h>
    14 #include <hugo/for_each_macros.h>
    15 #include <hugo/for_each_macros.h>
    15 #include <hugo/unionfind.h>
    16 #include <hugo/unionfind.h>
       
    17 #include <hugo/bin_heap.h>
       
    18 #include <bfs_dfs.h>
    16 
    19 
    17 namespace hugo {
    20 namespace hugo {
    18 
    21 
    19 /// \addtogroup galgs
    22 /// \addtogroup galgs
    20 /// @{
    23 /// @{
    91 
    94 
    92     //auxiliary variables
    95     //auxiliary variables
    93 
    96 
    94     //To store the flow
    97     //To store the flow
    95     FlowMap flow; 
    98     FlowMap flow; 
    96     //To store the potentila (dual variables)
    99     //To store the potential (dual variables)
    97     typename Graph::template NodeMap<Cost> potential;
   100     typedef typename Graph::template NodeMap<Cost> PotentialMap;
       
   101     PotentialMap potential;
    98     //To store excess-deficit values
   102     //To store excess-deficit values
    99     SupplyDemandMap excess_deficit;
   103     SupplyDemandMap excess_deficit;
   100     
   104     
   101 
   105 
   102     Cost total_cost;
   106     Cost total_cost;
   103 
   107 
   104 
   108 
   105   public :
   109   public :
   106 
   110 
   107 
   111 
   108     MinCostFlow(Graph& _graph, CostMap& _cost, SupplyDemandMap& _supply_demand) : graph(_graph), 
   112    MinCostFlow(Graph& _graph, CostMap& _cost, SupplyDemandMap& _supply_demand):
   109       cost(_cost), supply_demand(_supply_demand), flow(_graph), potential(_graph){ }
   113      graph(_graph), 
       
   114      cost(_cost), 
       
   115      supply_demand(_supply_demand), 
       
   116      flow(_graph), 
       
   117      potential(_graph),
       
   118      excess_deficit(_graph){ }
   110 
   119 
   111     
   120     
   112     ///Runs the algorithm.
   121     ///Runs the algorithm.
   113 
   122 
   114     ///Runs the algorithm.
   123     ///Runs the algorithm.
   119 
   128 
   120       //Resetting variables from previous runs
   129       //Resetting variables from previous runs
   121       //total_cost = 0;
   130       //total_cost = 0;
   122 
   131 
   123       typedef typename Graph::template NodeMap<int> HeapMap;
   132       typedef typename Graph::template NodeMap<int> HeapMap;
   124       typedef Heap< Node, SupplyDemand, typename Graph::template NodeMap<int>,
   133       typedef BinHeap< Node, SupplyDemand, typename Graph::template NodeMap<int>,
   125 	std::greater<SupplyDemand> > 	HeapType;
   134 	std::greater<SupplyDemand> > 	HeapType;
   126 
   135 
   127       //A heap for the excess nodes
   136       //A heap for the excess nodes
   128       HeapMap excess_nodes_map(graph,-1);
   137       HeapMap excess_nodes_map(graph,-1);
   129       HeapType excess_nodes(excess_nodes_map);
   138       HeapType excess_nodes(excess_nodes_map);
   131       //A heap for the deficit nodes
   140       //A heap for the deficit nodes
   132       HeapMap deficit_nodes_map(graph,-1);
   141       HeapMap deficit_nodes_map(graph,-1);
   133       HeapType deficit_nodes(deficit_nodes_map);
   142       HeapType deficit_nodes(deficit_nodes_map);
   134 
   143 
   135       //A container to store nonabundant arcs
   144       //A container to store nonabundant arcs
   136       list<Edge> nonabundant_arcs;
   145       std::list<Edge> nonabundant_arcs;
   137 
   146 
   138 	
   147 	
   139       FOR_EACH_LOC(typename Graph::EdgeIt, e, graph){
   148       FOR_EACH_LOC(typename Graph::EdgeIt, e, graph){
   140 	flow.set(e,0);
   149 	flow.set(e,0);
   141 	nonabundant_arcs.push_back(e);
   150 	nonabundant_arcs.push_back(e);
   145       SupplyDemand delta = 0;
   154       SupplyDemand delta = 0;
   146 
   155 
   147       typedef UnionFindEnum<Node, Graph::template NodeMap> UFE;
   156       typedef UnionFindEnum<Node, Graph::template NodeMap> UFE;
   148 
   157 
   149       //A union-find structure to store the abundant components
   158       //A union-find structure to store the abundant components
   150       UFE::MapType abund_comp_map(graph);
   159       typename UFE::MapType abund_comp_map(graph);
   151       UFE abundant_components(abund_comp_map);
   160       UFE abundant_components(abund_comp_map);
   152 
   161 
   153 
   162 
   154 
   163 
   155       FOR_EACH_LOC(typename Graph::NodeIt, n, graph){
   164       FOR_EACH_LOC(typename Graph::NodeIt, n, graph){
   175       //It'll be allright as an initial value, though this value 
   184       //It'll be allright as an initial value, though this value 
   176       //can be the maximum deficit here
   185       //can be the maximum deficit here
   177       SupplyDemand max_excess = delta;
   186       SupplyDemand max_excess = delta;
   178       
   187       
   179       ///\bug This is a serious cheat here, before we have an uncapacitated ResGraph
   188       ///\bug This is a serious cheat here, before we have an uncapacitated ResGraph
   180       ConstEdgeMap const_inf_map(MAX_INT);
   189       ConstEdgeMap const_inf_map(MAXINT);
   181       
   190       
   182       //We need a residual graph which is uncapacitated
   191       //We need a residual graph which is uncapacitated
   183       ResGraph res_graph(graph, const_inf_map, flow);
   192       ResGraph res_graph(graph, const_inf_map, flow);
   184       
   193       
   185       //An EdgeMap to tell which arcs are abundant
   194       //An EdgeMap to tell which arcs are abundant
   186       template typename Graph::EdgeMap<bool> abundant_arcs(graph);
   195       typename Graph::template EdgeMap<bool> abundant_arcs(graph);
   187 
   196 
   188       //Let's construct the sugraph consisting only of the abundant edges
   197       //Let's construct the sugraph consisting only of the abundant edges
   189       typedef ConstMap< typename Graph::Node, bool > ConstNodeMap;
   198       typedef ConstMap< typename Graph::Node, bool > ConstNodeMap;
   190       ConstNodeMap const_true_map(true);
   199       ConstNodeMap const_true_map(true);
   191       typedef SubGraphWrapper< Graph, ConstNodeMap, 
   200       typedef SubGraphWrapper< const Graph, ConstNodeMap, 
   192 	 template typename Graph::EdgeMap<bool> > 
   201 	 typename Graph::template EdgeMap<bool> > 
   193 	AbundantGraph;
   202 	AbundantGraph;
   194       AbundantGraph abundant_graph(graph, const_true_map, abundant_arcs );
   203       AbundantGraph abundant_graph(graph, const_true_map, abundant_arcs );
   195       
   204       
   196       //Let's construct the residual graph for the abundant graph
   205       //Let's construct the residual graph for the abundant graph
   197       typedef ResGraphWrapper<const AbundantGraph,int,CapacityMap,EdgeIntMap> 
   206       typedef ResGraphWrapper<const AbundantGraph,int,ConstEdgeMap,FlowMap> 
   198 	ResAbGraph;
   207 	ResAbGraph;
   199       //Again uncapacitated
   208       //Again uncapacitated
   200       ResAbGraph res_ab_graph(abundant_graph, const_inf_map, flow);
   209       ResAbGraph res_ab_graph(abundant_graph, const_inf_map, flow);
   201       
   210       
   202       //We need things for the bfs
   211       //We need things for the bfs
   203       typename ResAbGraph::NodeMap<bool> bfs_reached(res_ab_graph);
   212       typename ResAbGraph::template NodeMap<bool> bfs_reached(res_ab_graph);
   204       typename ResAbGraph::NodeMap<typename ResAbGraph::Edge> 
   213       typename ResAbGraph::template NodeMap<typename ResAbGraph::Edge> 
   205 	bfs_pred(res_ab_graph); 
   214 	bfs_pred(res_ab_graph); 
   206       NullMap<typename ResAbGraph::Node, int> bfs_dist_dummy(res_ab_graph);
   215       NullMap<typename ResAbGraph::Node, int> bfs_dist_dummy;
   207       //We want to run bfs-es (more) on this graph 'res_ab_graph'
   216       //We want to run bfs-es (more) on this graph 'res_ab_graph'
   208       Bfs < ResAbGraph , 
   217       Bfs < ResAbGraph , 
   209 	typename ResAbGraph::NodeMap<bool>, 
   218 	typename ResAbGraph::template NodeMap<bool>, 
   210 	typename ResAbGraph::NodeMap<typename ResAbGraph::Edge>,
   219 	typename ResAbGraph::template NodeMap<typename ResAbGraph::Edge>,
   211 	NullMap<typename ResAbGraph::Node, int> > 
   220 	NullMap<typename ResAbGraph::Node, int> > 
   212 	bfs(res_ab_graph, bfs_reached, bfs_pred, bfs_dist_dummy);
   221 	bfs(res_ab_graph, bfs_reached, bfs_pred, bfs_dist_dummy);
       
   222       /*This is what Marci wants for a bfs
       
   223 	template <typename Graph, 
       
   224 	    typename ReachedMap=typename Graph::template NodeMap<bool>, 
       
   225 	    typename PredMap
       
   226 	    =typename Graph::template NodeMap<typename Graph::Edge>, 
       
   227 	    typename DistMap=typename Graph::template NodeMap<int> > 
       
   228 	    class Bfs : public BfsIterator<Graph, ReachedMap> {
       
   229 
       
   230        */
   213       
   231       
   214       ModCostMap mod_cost(res_graph, cost, potential);
   232       ModCostMap mod_cost(res_graph, cost, potential);
   215 
   233 
   216       Dijkstra<ResGraph, ModCostMap> dijkstra(res_graph, mod_cost);
   234       Dijkstra<ResGraph, ModCostMap> dijkstra(res_graph, mod_cost);
   217 
   235 
   228 	 * Beginning of the delta scaling phase 
   246 	 * Beginning of the delta scaling phase 
   229 	*/
   247 	*/
   230 	//Merge and stuff
   248 	//Merge and stuff
   231 	{
   249 	{
   232 	  SupplyDemand buf=8*number_of_nodes*delta;
   250 	  SupplyDemand buf=8*number_of_nodes*delta;
   233 	  list<Edge>::iterator i = nonabundant_arcs.begin();
   251 	  typename std::list<Edge>::iterator i = nonabundant_arcs.begin();
   234 	  while ( i != nonabundant_arcs.end() ){
   252 	  while ( i != nonabundant_arcs.end() ){
   235 	    if (flow[i]>=buf){
   253 	    if (flow[i]>=buf){
   236 	      Node a = abundant_components.find(res_graph.head(i));
   254 	      Node a = abundant_components.find(res_graph.head(i));
   237 	      Node b = abundant_components.find(res_graph.tail(i));
   255 	      Node b = abundant_components.find(res_graph.tail(i));
   238 	      //Merge
   256 	      //Merge
   299 	if (max_excess < deficit_nodes[t]){
   317 	if (max_excess < deficit_nodes[t]){
   300 	  max_excess = deficit_nodes[t];
   318 	  max_excess = deficit_nodes[t];
   301 	}
   319 	}
   302 
   320 
   303 
   321 
   304 	while(max_excess > (n-1)*delta/n){
   322 	while(max_excess > (number_of_nodes-1)*delta/number_of_nodes){
   305 	  
   323 	  
   306 	  
   324 	  
   307 	  //s es t valasztasa
   325 	  //s es t valasztasa
   308 	  
   326 	  
   309 	  //Dijkstra part	
   327 	  //Dijkstra part	
   408       return total_cost;
   426       return total_cost;
   409     }
   427     }
   410 
   428 
   411     ///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must
   429     ///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must
   412     ///be called before using this function.
   430     ///be called before using this function.
   413     const EdgeIntMap &getFlow() const { return flow;}
   431     const FlowMap &getFlow() const { return flow;}
   414 
   432 
   415   ///Returns a const reference to the NodeMap \c potential (the dual solution).
   433   ///Returns a const reference to the NodeMap \c potential (the dual solution).
   416     /// \pre \ref run() must be called before using this function.
   434     /// \pre \ref run() must be called before using this function.
   417     const EdgeIntMap &getPotential() const { return potential;}
   435     const PotentialMap &getPotential() const { return potential;}
   418 
   436 
   419     ///This function checks, whether the given solution is optimal
   437     ///This function checks, whether the given solution is optimal
   420     ///Running after a \c run() should return with true
   438     ///Running after a \c run() should return with true
   421     ///In this "state of the art" this only check optimality, doesn't bother with feasibility
   439     ///In this "state of the art" this only check optimality, doesn't bother with feasibility
   422     ///
   440     ///