[Lemon-commits] [lemon_svn] athos: r864 - in hugo/trunk/src: hugo work/athos

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:41:55 CET 2006


Author: athos
Date: Tue May 25 17:11:11 2004
New Revision: 864

Modified:
   hugo/trunk/src/hugo/mincostflows.h
   hugo/trunk/src/work/athos/makefile
   hugo/trunk/src/work/athos/min_cost_flow.cc
   hugo/trunk/src/work/athos/mincostflow.h

Log:
Corrected some obvious errors.

Modified: hugo/trunk/src/hugo/mincostflows.h
==============================================================================
--- hugo/trunk/src/hugo/mincostflows.h	(original)
+++ hugo/trunk/src/hugo/mincostflows.h	Tue May 25 17:11:11 2004
@@ -11,7 +11,7 @@
 #include <hugo/graph_wrapper.h>
 #include <hugo/maps.h>
 #include <vector>
-#include <for_each_macros.h>
+#include <hugo/for_each_macros.h>
 
 namespace hugo {
 
@@ -90,7 +90,8 @@
     //To store the flow
     EdgeIntMap flow; 
     //To store the potentila (dual variables)
-    typename Graph::template NodeMap<Length> potential;
+    typedef typename Graph::template NodeMap<Length> PotentialMap;
+    PotentialMap potential;
     
 
     Length total_length;
@@ -184,7 +185,7 @@
 
   ///Returns a const reference to the NodeMap \c potential (the dual solution).
     /// \pre \ref run() must be called before using this function.
-    const EdgeIntMap &getPotential() const { return potential;}
+    const PotentialMap &getPotential() const { return potential;}
 
     ///This function checks, whether the given solution is optimal
     ///Running after a \c run() should return with true

Modified: hugo/trunk/src/work/athos/makefile
==============================================================================
--- hugo/trunk/src/work/athos/makefile	(original)
+++ hugo/trunk/src/work/athos/makefile	Tue May 25 17:11:11 2004
@@ -1,4 +1,4 @@
-BINARIES = minlengthpaths_test minlength_demo
+BINARIES = min_cost_flow
 INCLUDEDIRS= -I../.. -I.. -I../{athos,klao,marci,jacint,alpar,johanna,akos} 
 include ../makefile
 

Modified: hugo/trunk/src/work/athos/min_cost_flow.cc
==============================================================================
--- hugo/trunk/src/work/athos/min_cost_flow.cc	(original)
+++ hugo/trunk/src/work/athos/min_cost_flow.cc	Tue May 25 17:11:11 2004
@@ -1,5 +1,5 @@
 #include <iostream>
-#include "test_tools.h"
+//#include "test_tools.h"
 #include <hugo/list_graph.h>
 #include <mincostflow.h>
 //#include <path.h>
@@ -51,16 +51,16 @@
   Edge v5_t=graph.addEdge(v5, t);
   
 
-  ListGraph::EdgeMap<int> length(graph);
+  ListGraph::EdgeMap<int> cost(graph);
 
-  length.set(s_v1, 6);
-  length.set(v1_v2, 4);
-  length.set(s_v3, 10);
-  length.set(v2_v4, 5);
-  length.set(v2_v5, 1);
-  length.set(v3_v5, 4);
-  length.set(v4_t, 8);
-  length.set(v5_t, 8);
+  cost.set(s_v1, 6);
+  cost.set(v1_v2, 4);
+  cost.set(s_v3, 10);
+  cost.set(v2_v4, 5);
+  cost.set(v2_v5, 1);
+  cost.set(v3_v5, 4);
+  cost.set(v4_t, 8);
+  cost.set(v5_t, 8);
 
   /*
   ListGraph::EdgeMap<int> capacity(graph);
@@ -79,25 +79,25 @@
   std::cout << "Enhanced capacity scaling algorithm test (for the mincostflow problem)..." << std::endl;
 
   MinCostFlow< ListGraph, ListGraph::EdgeMap<int>, ListGraph::NodeMap<int> >
-    min_cost_flow_test(graph, length, supply_demand);
+    min_cost_flow_test(graph, cost, supply_demand);
 
   int k=1;
 
   /*
-  check(  min_cost_flow_test.run(s,t,k) == 1 && min_cost_flow_test.totalLength() == 19,"One path, total length should be 19");
+  check(  min_cost_flow_test.run(s,t,k) == 1 && min_cost_flow_test.totalLength() == 19,"One path, total cost should be 19");
 
   check(min_cost_flow_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?");
   
   k=2;
   
-  check(  min_cost_flow_test.run(s,t,k) == 2 && min_cost_flow_test.totalLength() == 41,"Two paths, total length should be 41");
+  check(  min_cost_flow_test.run(s,t,k) == 2 && min_cost_flow_test.totalLength() == 41,"Two paths, total cost should be 41");
 
   check(min_cost_flow_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?");
   
   
   k=4;
 
-  check(  min_cost_flow_test.run(s,t,k) == 3 && min_cost_flow_test.totalLength() == 64,"Three paths, total length should be 64");
+  check(  min_cost_flow_test.run(s,t,k) == 3 && min_cost_flow_test.totalLength() == 64,"Three paths, total cost should be 64");
 
   check(min_cost_flow_test.checkComplementarySlackness(), "Is the primal-dual solution pair really optimal?");
 

Modified: hugo/trunk/src/work/athos/mincostflow.h
==============================================================================
--- hugo/trunk/src/work/athos/mincostflow.h	(original)
+++ hugo/trunk/src/work/athos/mincostflow.h	Tue May 25 17:11:11 2004
@@ -11,16 +11,16 @@
 #include <hugo/maps.h>
 #include <vector>
 #include <list>
-#include <for_each_macros.h>
-#include <hugo/union_find.h>
+#include <hugo/for_each_macros.h>
+#include <hugo/unionfind.h>
 
 namespace hugo {
 
 /// \addtogroup galgs
 /// @{
 
-  ///\brief Implementation of an algorithm for finding the minimum cost flow 
-  /// of given value in an uncapacitated network
+  ///\brief Implementation of an algorithm for solving the minimum cost general
+  /// flow problem in an uncapacitated network
   /// 
   ///
   /// The class \ref hugo::MinCostFlow "MinCostFlow" implements
@@ -30,17 +30,17 @@
   ///
   /// \warning It is assumed here that the problem has a feasible solution
   ///
-  /// The range of the length (weight) function is nonnegative reals but 
+  /// The range of the cost (weight) function is nonnegative reals but 
   /// the range of capacity function is the set of nonnegative integers. 
   /// It is not a polinomial time algorithm for counting the minimum cost
   /// maximal flow, since it counts the minimum cost flow for every value 0..M
   /// where \c M is the value of the maximal flow.
   ///
   ///\author Attila Bernath
-  template <typename Graph, typename LengthMap, typename SupplyDemandMap>
+  template <typename Graph, typename CostMap, typename SupplyDemandMap>
   class MinCostFlow {
 
-    typedef typename LengthMap::ValueType Length;
+    typedef typename CostMap::ValueType Cost;
 
 
     typedef typename SupplyDemandMap::ValueType SupplyDemand;
@@ -49,63 +49,64 @@
     typedef typename Graph::NodeIt NodeIt;
     typedef typename Graph::Edge Edge;
     typedef typename Graph::OutEdgeIt OutEdgeIt;
-    typedef typename Graph::template EdgeMap<int> EdgeIntMap;
+    typedef typename Graph::template EdgeMap<SupplyDemand> FlowMap;
+    typedef ConstMap<Edge,SupplyDemand> ConstEdgeMap;
 
     //    typedef ConstMap<Edge,int> ConstMap;
 
-    typedef ResGraphWrapper<const Graph,int,CapacityMap,EdgeIntMap> ResGraphType;
-    typedef typename ResGraphType::Edge ResGraphEdge;
+    typedef ResGraphWrapper<const Graph,int,ConstEdgeMap,FlowMap> ResGraph;
+    typedef typename ResGraph::Edge ResGraphEdge;
 
-    class ModLengthMap {   
-      //typedef typename ResGraphType::template NodeMap<Length> NodeMap;
-      typedef typename Graph::template NodeMap<Length> NodeMap;
-      const ResGraphType& res_graph;
+    class ModCostMap {   
+      //typedef typename ResGraph::template NodeMap<Cost> NodeMap;
+      typedef typename Graph::template NodeMap<Cost> NodeMap;
+      const ResGraph& res_graph;
       //      const EdgeIntMap& rev;
-      const LengthMap &ol;
+      const CostMap &ol;
       const NodeMap &pot;
     public :
-      typedef typename LengthMap::KeyType KeyType;
-      typedef typename LengthMap::ValueType ValueType;
+      typedef typename CostMap::KeyType KeyType;
+      typedef typename CostMap::ValueType ValueType;
 	
-      ValueType operator[](typename ResGraphType::Edge e) const {     
+      ValueType operator[](typename ResGraph::Edge e) const {     
 	if (res_graph.forward(e))
 	  return  ol[e]-(pot[res_graph.head(e)]-pot[res_graph.tail(e)]);   
 	else
 	  return -ol[e]-(pot[res_graph.head(e)]-pot[res_graph.tail(e)]);   
       }     
 	
-      ModLengthMap(const ResGraphType& _res_graph,
-		   const LengthMap &o,  const NodeMap &p) : 
+      ModCostMap(const ResGraph& _res_graph,
+		   const CostMap &o,  const NodeMap &p) : 
 	res_graph(_res_graph), /*rev(_rev),*/ ol(o), pot(p){}; 
-    };//ModLengthMap
+    };//ModCostMap
 
 
   protected:
     
     //Input
     const Graph& graph;
-    const LengthMap& length;
+    const CostMap& cost;
     const SupplyDemandMap& supply_demand;//supply or demand of nodes
 
 
     //auxiliary variables
 
     //To store the flow
-    EdgeIntMap flow; 
+    FlowMap flow; 
     //To store the potentila (dual variables)
-    typename Graph::template NodeMap<Length> potential;
+    typename Graph::template NodeMap<Cost> potential;
     //To store excess-deficit values
     SupplyDemandMap excess_deficit;
     
 
-    Length total_length;
+    Cost total_cost;
 
 
   public :
 
 
-    MinCostFlow(Graph& _graph, LengthMap& _length, SupplyDemandMap& _supply_demand) : graph(_graph), 
-      length(_length), supply_demand(_supply_demand), flow(_graph), potential(_graph){ }
+    MinCostFlow(Graph& _graph, CostMap& _cost, SupplyDemandMap& _supply_demand) : graph(_graph), 
+      cost(_cost), supply_demand(_supply_demand), flow(_graph), potential(_graph){ }
 
     
     ///Runs the algorithm.
@@ -117,7 +118,7 @@
     void run() {
 
       //Resetting variables from previous runs
-      //total_length = 0;
+      //total_cost = 0;
 
       typedef typename Graph::template NodeMap<int> HeapMap;
       typedef Heap< Node, SupplyDemand, typename Graph::template NodeMap<int>,
@@ -175,10 +176,11 @@
       //can be the maximum deficit here
       SupplyDemand max_excess = delta;
       
-      //ConstMap<Edge,SupplyDemand> ConstEdgeMap;
-
+      ///\bug This is a serious cheat here, before we have an uncapacitated ResGraph
+      ConstEdgeMap const_inf_map(MAX_INT);
+      
       //We need a residual graph which is uncapacitated
-      ResGraphType res_graph(graph, flow);
+      ResGraph res_graph(graph, const_inf_map, flow);
       
       //An EdgeMap to tell which arcs are abundant
       template typename Graph::EdgeMap<bool> abundant_arcs(graph);
@@ -195,7 +197,7 @@
       typedef ResGraphWrapper<const AbundantGraph,int,CapacityMap,EdgeIntMap> 
 	ResAbGraph;
       //Again uncapacitated
-      ResAbGraph res_ab_graph(abundant_graph, flow);
+      ResAbGraph res_ab_graph(abundant_graph, const_inf_map, flow);
       
       //We need things for the bfs
       typename ResAbGraph::NodeMap<bool> bfs_reached(res_ab_graph);
@@ -209,9 +211,9 @@
 	NullMap<typename ResAbGraph::Node, int> > 
 	bfs(res_ab_graph, bfs_reached, bfs_pred, bfs_dist_dummy);
       
-      ModLengthMap mod_length(res_graph, length, potential);
+      ModCostMap mod_cost(res_graph, cost, potential);
 
-      Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
+      Dijkstra<ResGraph, ModCostMap> dijkstra(res_graph, mod_cost);
 
 
       while (max_excess > 0){
@@ -315,7 +317,7 @@
 	  */
 	  
 	  //We have to change the potential
-	  FOR_EACH_LOC(typename ResGraphType::NodeIt, n, res_graph){
+	  FOR_EACH_LOC(typename ResGraph::NodeIt, n, res_graph){
 	    potential[n] += dijkstra.distMap()[n];
 	  }
 
@@ -328,11 +330,11 @@
 	    n = dijkstra.predNode(n);
 	    res_graph.augment(e,delta);
 	    /*
-	    //Let's update the total length
+	    //Let's update the total cost
 	    if (res_graph.forward(e))
-	      total_length += length[e];
+	      total_cost += cost[e];
 	    else 
-	      total_length -= length[e];	    
+	      total_cost -= cost[e];	    
 	    */
 	  }
 	  
@@ -400,10 +402,10 @@
 
 
 
-    ///This function gives back the total length of the found paths.
+    ///This function gives back the total cost of the found paths.
     ///Assumes that \c run() has been run and nothing changed since then.
-    Length totalLength(){
-      return total_length;
+    Cost totalCost(){
+      return total_cost;
     }
 
     ///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must
@@ -420,11 +422,11 @@
     ///
     ///\todo Is this OK here?
     bool checkComplementarySlackness(){
-      Length mod_pot;
-      Length fl_e;
+      Cost mod_pot;
+      Cost fl_e;
       FOR_EACH_LOC(typename Graph::EdgeIt, e, graph){
 	//C^{\Pi}_{i,j}
-	mod_pot = length[e]-potential[graph.head(e)]+potential[graph.tail(e)];
+	mod_pot = cost[e]-potential[graph.head(e)]+potential[graph.tail(e)];
 	fl_e = flow[e];
 	//	std::cout << fl_e << std::endl;
 	if (0<fl_e && fl_e<capacity[e]){



More information about the Lemon-commits mailing list