src/work/athos/mincostflows.h
changeset 608 84b04b70ad89
parent 607 327f7cf13843
child 609 0566ac97809b
     1.1 --- a/src/work/athos/mincostflows.h	Tue May 11 15:42:11 2004 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,254 +0,0 @@
     1.4 -// -*- c++ -*-
     1.5 -#ifndef HUGO_MINCOSTFLOWS_H
     1.6 -#define HUGO_MINCOSTFLOWS_H
     1.7 -
     1.8 -///\ingroup galgs
     1.9 -///\file
    1.10 -///\brief An algorithm for finding a flow of value \c k (for small values of \c k) having minimal total cost 
    1.11 -
    1.12 -#include <iostream>
    1.13 -#include <hugo/dijkstra.h>
    1.14 -#include <hugo/graph_wrapper.h>
    1.15 -#include <hugo/maps.h>
    1.16 -#include <vector>
    1.17 -#include <for_each_macros.h>
    1.18 -
    1.19 -namespace hugo {
    1.20 -
    1.21 -/// \addtogroup galgs
    1.22 -/// @{
    1.23 -
    1.24 -  ///\brief Implementation of an algorithm for finding a flow of value \c k 
    1.25 -  ///(for small values of \c k) having minimal total cost between 2 nodes 
    1.26 -  /// 
    1.27 -  ///
    1.28 -  /// The class \ref hugo::MinCostFlows "MinCostFlows" implements
    1.29 -  /// an algorithm for finding a flow of value \c k 
    1.30 -  ///(for small values of \c k) having minimal total cost  
    1.31 -  /// from a given source node to a given target node in an
    1.32 -  /// edge-weighted directed graph having nonnegative integer capacities.
    1.33 -  /// The range of the length (weight) function is nonnegative reals but 
    1.34 -  /// the range of capacity function is the set of nonnegative integers. 
    1.35 -  /// It is not a polinomial time algorithm for counting the minimum cost
    1.36 -  /// maximal flow, since it counts the minimum cost flow for every value 0..M
    1.37 -  /// where \c M is the value of the maximal flow.
    1.38 -  ///
    1.39 -  ///\author Attila Bernath
    1.40 -  template <typename Graph, typename LengthMap, typename CapacityMap>
    1.41 -  class MinCostFlows {
    1.42 -
    1.43 -    typedef typename LengthMap::ValueType Length;
    1.44 -
    1.45 -    //Warning: this should be integer type
    1.46 -    typedef typename CapacityMap::ValueType Capacity;
    1.47 -    
    1.48 -    typedef typename Graph::Node Node;
    1.49 -    typedef typename Graph::NodeIt NodeIt;
    1.50 -    typedef typename Graph::Edge Edge;
    1.51 -    typedef typename Graph::OutEdgeIt OutEdgeIt;
    1.52 -    typedef typename Graph::template EdgeMap<int> EdgeIntMap;
    1.53 -
    1.54 -    //    typedef ConstMap<Edge,int> ConstMap;
    1.55 -
    1.56 -    typedef ResGraphWrapper<const Graph,int,CapacityMap,EdgeIntMap> ResGraphType;
    1.57 -    typedef typename ResGraphType::Edge ResGraphEdge;
    1.58 -
    1.59 -    class ModLengthMap {   
    1.60 -      //typedef typename ResGraphType::template NodeMap<Length> NodeMap;
    1.61 -      typedef typename Graph::template NodeMap<Length> NodeMap;
    1.62 -      const ResGraphType& G;
    1.63 -      //      const EdgeIntMap& rev;
    1.64 -      const LengthMap &ol;
    1.65 -      const NodeMap &pot;
    1.66 -    public :
    1.67 -      typedef typename LengthMap::KeyType KeyType;
    1.68 -      typedef typename LengthMap::ValueType ValueType;
    1.69 -	
    1.70 -      ValueType operator[](typename ResGraphType::Edge e) const {     
    1.71 -	if (G.forward(e))
    1.72 -	  return  ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
    1.73 -	else
    1.74 -	  return -ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
    1.75 -      }     
    1.76 -	
    1.77 -      ModLengthMap(const ResGraphType& _G,
    1.78 -		   const LengthMap &o,  const NodeMap &p) : 
    1.79 -	G(_G), /*rev(_rev),*/ ol(o), pot(p){}; 
    1.80 -    };//ModLengthMap
    1.81 -
    1.82 -
    1.83 -  protected:
    1.84 -    
    1.85 -    //Input
    1.86 -    const Graph& G;
    1.87 -    const LengthMap& length;
    1.88 -    const CapacityMap& capacity;
    1.89 -
    1.90 -
    1.91 -    //auxiliary variables
    1.92 -
    1.93 -    //To store the flow
    1.94 -    EdgeIntMap flow; 
    1.95 -    //To store the potentila (dual variables)
    1.96 -    typename Graph::template NodeMap<Length> potential;
    1.97 -    
    1.98 -    //Container to store found paths
    1.99 -    //std::vector< std::vector<Edge> > paths;
   1.100 -    //typedef DirPath<Graph> DPath;
   1.101 -    //DPath paths;
   1.102 -
   1.103 -
   1.104 -    Length total_length;
   1.105 -
   1.106 -
   1.107 -  public :
   1.108 -
   1.109 -
   1.110 -    MinCostFlows(Graph& _G, LengthMap& _length, CapacityMap& _cap) : G(_G), 
   1.111 -      length(_length), capacity(_cap), flow(_G), potential(_G){ }
   1.112 -
   1.113 -    
   1.114 -    ///Runs the algorithm.
   1.115 -
   1.116 -    ///Runs the algorithm.
   1.117 -    ///Returns k if there are at least k edge-disjoint paths from s to t.
   1.118 -    ///Otherwise it returns the number of found edge-disjoint paths from s to t.
   1.119 -    ///\todo May be it does make sense to be able to start with a nonzero 
   1.120 -    /// feasible primal-dual solution pair as well.
   1.121 -    int run(Node s, Node t, int k) {
   1.122 -
   1.123 -      //Resetting variables from previous runs
   1.124 -      total_length = 0;
   1.125 -      
   1.126 -      FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
   1.127 -	flow.set(e,0);
   1.128 -      }
   1.129 -      
   1.130 -      FOR_EACH_LOC(typename Graph::NodeIt, n, G){
   1.131 -	//cout << potential[n]<<endl;
   1.132 -	potential.set(n,0);
   1.133 -      }
   1.134 -      
   1.135 -
   1.136 -      
   1.137 -      //We need a residual graph
   1.138 -      ResGraphType res_graph(G, capacity, flow);
   1.139 -
   1.140 -      //Initialize the copy of the Dijkstra potential to zero
   1.141 -      
   1.142 -      //typename ResGraphType::template NodeMap<Length> potential(res_graph);
   1.143 -
   1.144 -
   1.145 -      ModLengthMap mod_length(res_graph, length, potential);
   1.146 -
   1.147 -      Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
   1.148 -
   1.149 -      int i;
   1.150 -      for (i=0; i<k; ++i){
   1.151 -	dijkstra.run(s);
   1.152 -	if (!dijkstra.reached(t)){
   1.153 -	  //There are no k paths from s to t
   1.154 -	  break;
   1.155 -	};
   1.156 -	
   1.157 -	{
   1.158 -	  //We have to copy the potential
   1.159 -	  typename ResGraphType::NodeIt n;
   1.160 -	  for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
   1.161 -	      potential[n] += dijkstra.distMap()[n];
   1.162 -	  }
   1.163 -	}
   1.164 -
   1.165 -
   1.166 -	//Augmenting on the sortest path
   1.167 -	Node n=t;
   1.168 -	ResGraphEdge e;
   1.169 -	while (n!=s){
   1.170 -	  e = dijkstra.pred(n);
   1.171 -	  n = dijkstra.predNode(n);
   1.172 -	  res_graph.augment(e,1);
   1.173 -	  //Let's update the total length
   1.174 -	  if (res_graph.forward(e))
   1.175 -	    total_length += length[e];
   1.176 -	  else 
   1.177 -	    total_length -= length[e];	    
   1.178 -	}
   1.179 -
   1.180 -	  
   1.181 -      }
   1.182 -      
   1.183 -
   1.184 -      return i;
   1.185 -    }
   1.186 -
   1.187 -
   1.188 -
   1.189 -
   1.190 -    ///This function gives back the total length of the found paths.
   1.191 -    ///Assumes that \c run() has been run and nothing changed since then.
   1.192 -    Length totalLength(){
   1.193 -      return total_length;
   1.194 -    }
   1.195 -
   1.196 -    ///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must
   1.197 -    ///be called before using this function.
   1.198 -    const EdgeIntMap &getFlow() const { return flow;}
   1.199 -
   1.200 -  ///Returns a const reference to the NodeMap \c potential (the dual solution).
   1.201 -    /// \pre \ref run() must be called before using this function.
   1.202 -    const EdgeIntMap &getPotential() const { return potential;}
   1.203 -
   1.204 -    ///This function checks, whether the given solution is optimal
   1.205 -    ///Running after a \c run() should return with true
   1.206 -    ///In this "state of the art" this only check optimality, doesn't bother with feasibility
   1.207 -    ///
   1.208 -    ///\todo Is this OK here?
   1.209 -    bool checkComplementarySlackness(){
   1.210 -      Length mod_pot;
   1.211 -      Length fl_e;
   1.212 -      FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
   1.213 -	//C^{\Pi}_{i,j}
   1.214 -	mod_pot = length[e]-potential[G.head(e)]+potential[G.tail(e)];
   1.215 -	fl_e = flow[e];
   1.216 -	//	std::cout << fl_e << std::endl;
   1.217 -	if (0<fl_e && fl_e<capacity[e]){
   1.218 -	  if (mod_pot != 0)
   1.219 -	    return false;
   1.220 -	}
   1.221 -	else{
   1.222 -	  if (mod_pot > 0 && fl_e != 0)
   1.223 -	    return false;
   1.224 -	  if (mod_pot < 0 && fl_e != capacity[e])
   1.225 -	    return false;
   1.226 -	}
   1.227 -      }
   1.228 -      return true;
   1.229 -    }
   1.230 -    
   1.231 -    /*
   1.232 -      ///\todo To be implemented later
   1.233 -
   1.234 -    ///This function gives back the \c j-th path in argument p.
   1.235 -    ///Assumes that \c run() has been run and nothing changed since then.
   1.236 -    /// \warning It is assumed that \c p is constructed to be a path of graph \c G. If \c j is greater than the result of previous \c run, then the result here will be an empty path.
   1.237 -    template<typename DirPath>
   1.238 -    void getPath(DirPath& p, int j){
   1.239 -      p.clear();
   1.240 -      typename DirPath::Builder B(p);
   1.241 -      for(typename std::vector<Edge>::iterator i=paths[j].begin(); 
   1.242 -	  i!=paths[j].end(); ++i ){
   1.243 -	B.pushBack(*i);
   1.244 -      }
   1.245 -
   1.246 -      B.commit();
   1.247 -    }
   1.248 -
   1.249 -    */
   1.250 -
   1.251 -  }; //class MinCostFlows
   1.252 -
   1.253 -  ///@}
   1.254 -
   1.255 -} //namespace hugo
   1.256 -
   1.257 -#endif //HUGO_MINCOSTFLOW_H