src/work/athos/mincostflow.h
author marci
Fri, 14 May 2004 14:41:30 +0000
changeset 636 e59b0c363a9e
parent 633 305bd9c56f10
child 645 d93d8b9906d1
permissions -rw-r--r--
(none)
athos@610
     1
// -*- c++ -*-
athos@633
     2
#ifndef HUGO_MINCOSTFLOW_H
athos@633
     3
#define HUGO_MINCOSTFLOW_H
athos@610
     4
athos@610
     5
///\ingroup galgs
athos@610
     6
///\file
athos@610
     7
///\brief An algorithm for finding a flow of value \c k (for small values of \c k) having minimal total cost 
athos@610
     8
athos@611
     9
athos@610
    10
#include <hugo/dijkstra.h>
athos@610
    11
#include <hugo/graph_wrapper.h>
athos@610
    12
#include <hugo/maps.h>
athos@610
    13
#include <vector>
athos@610
    14
#include <for_each_macros.h>
athos@610
    15
athos@610
    16
namespace hugo {
athos@610
    17
athos@610
    18
/// \addtogroup galgs
athos@610
    19
/// @{
athos@610
    20
athos@610
    21
  ///\brief Implementation of an algorithm for finding a flow of value \c k 
athos@610
    22
  ///(for small values of \c k) having minimal total cost between 2 nodes 
athos@610
    23
  /// 
athos@610
    24
  ///
athos@633
    25
  /// The class \ref hugo::MinCostFlow "MinCostFlow" implements
athos@633
    26
  /// an algorithm for solving the following general minimum cost flow problem>
athos@633
    27
  /// 
athos@633
    28
  ///
athos@633
    29
  ///
athos@633
    30
  /// \warning It is assumed here that the problem has a feasible solution
athos@633
    31
  ///
athos@610
    32
  /// The range of the length (weight) function is nonnegative reals but 
athos@610
    33
  /// the range of capacity function is the set of nonnegative integers. 
athos@610
    34
  /// It is not a polinomial time algorithm for counting the minimum cost
athos@610
    35
  /// maximal flow, since it counts the minimum cost flow for every value 0..M
athos@610
    36
  /// where \c M is the value of the maximal flow.
athos@610
    37
  ///
athos@610
    38
  ///\author Attila Bernath
athos@635
    39
  template <typename Graph, typename LengthMap, typename SupplyDemandMap>
athos@633
    40
  class MinCostFlow {
athos@610
    41
athos@610
    42
    typedef typename LengthMap::ValueType Length;
athos@610
    43
athos@633
    44
athos@635
    45
    typedef typename SupplyDemandMap::ValueType SupplyDemand;
athos@610
    46
    
athos@610
    47
    typedef typename Graph::Node Node;
athos@610
    48
    typedef typename Graph::NodeIt NodeIt;
athos@610
    49
    typedef typename Graph::Edge Edge;
athos@610
    50
    typedef typename Graph::OutEdgeIt OutEdgeIt;
athos@610
    51
    typedef typename Graph::template EdgeMap<int> EdgeIntMap;
athos@610
    52
athos@610
    53
    //    typedef ConstMap<Edge,int> ConstMap;
athos@610
    54
athos@610
    55
    typedef ResGraphWrapper<const Graph,int,CapacityMap,EdgeIntMap> ResGraphType;
athos@610
    56
    typedef typename ResGraphType::Edge ResGraphEdge;
athos@610
    57
athos@610
    58
    class ModLengthMap {   
athos@610
    59
      //typedef typename ResGraphType::template NodeMap<Length> NodeMap;
athos@610
    60
      typedef typename Graph::template NodeMap<Length> NodeMap;
athos@610
    61
      const ResGraphType& G;
athos@610
    62
      //      const EdgeIntMap& rev;
athos@610
    63
      const LengthMap &ol;
athos@610
    64
      const NodeMap &pot;
athos@610
    65
    public :
athos@610
    66
      typedef typename LengthMap::KeyType KeyType;
athos@610
    67
      typedef typename LengthMap::ValueType ValueType;
athos@610
    68
	
athos@610
    69
      ValueType operator[](typename ResGraphType::Edge e) const {     
athos@610
    70
	if (G.forward(e))
athos@610
    71
	  return  ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
athos@610
    72
	else
athos@610
    73
	  return -ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
athos@610
    74
      }     
athos@610
    75
	
athos@610
    76
      ModLengthMap(const ResGraphType& _G,
athos@610
    77
		   const LengthMap &o,  const NodeMap &p) : 
athos@610
    78
	G(_G), /*rev(_rev),*/ ol(o), pot(p){}; 
athos@610
    79
    };//ModLengthMap
athos@610
    80
athos@610
    81
athos@610
    82
  protected:
athos@610
    83
    
athos@610
    84
    //Input
athos@610
    85
    const Graph& G;
athos@610
    86
    const LengthMap& length;
athos@635
    87
    const SupplyDemandMap& supply_demand;//supply or demand of nodes
athos@610
    88
athos@610
    89
athos@610
    90
    //auxiliary variables
athos@610
    91
athos@610
    92
    //To store the flow
athos@610
    93
    EdgeIntMap flow; 
athos@610
    94
    //To store the potentila (dual variables)
athos@610
    95
    typename Graph::template NodeMap<Length> potential;
athos@633
    96
    //To store excess-deficit values
athos@635
    97
    SupplyDemandMap excess_deficit;
athos@610
    98
    
athos@610
    99
athos@610
   100
    Length total_length;
athos@610
   101
athos@610
   102
athos@610
   103
  public :
athos@610
   104
athos@610
   105
athos@635
   106
    MinCostFlow(Graph& _G, LengthMap& _length, SupplyDemandMap& _supply_demand) : G(_G), 
athos@635
   107
      length(_length), supply_demand(_supply_demand), flow(_G), potential(_G){ }
athos@610
   108
athos@610
   109
    
athos@610
   110
    ///Runs the algorithm.
athos@610
   111
athos@610
   112
    ///Runs the algorithm.
athos@635
   113
athos@610
   114
    ///\todo May be it does make sense to be able to start with a nonzero 
athos@610
   115
    /// feasible primal-dual solution pair as well.
athos@633
   116
    int run() {
athos@610
   117
athos@610
   118
      //Resetting variables from previous runs
athos@635
   119
      //total_length = 0;
athos@635
   120
athos@635
   121
      typedef typename Graph::template NodeMap<int> HeapMap;
athos@635
   122
      typedef Heap<Node, SupplyDemand, typename Graph::template NodeMap<int>,
athos@635
   123
	std::greater<SupplyDemand> > 	HeapType;
athos@635
   124
athos@635
   125
      //A heap for the excess nodes
athos@635
   126
      HeapMap excess_nodes_map(G,-1);
athos@635
   127
      HeapType excess_nodes(excess_nodes_map);
athos@635
   128
athos@635
   129
      //A heap for the deficit nodes
athos@635
   130
      HeapMap deficit_nodes_map(G,-1);
athos@635
   131
      HeapType deficit_nodes(deficit_nodes_map);
athos@635
   132
athos@610
   133
      
athos@610
   134
      FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
athos@610
   135
	flow.set(e,0);
athos@610
   136
      }
athos@633
   137
athos@633
   138
      //Initial value for delta
athos@635
   139
      SupplyDemand delta = 0;
athos@635
   140
athos@610
   141
      FOR_EACH_LOC(typename Graph::NodeIt, n, G){
athos@635
   142
       	excess_deficit.set(n,supply_demand[n]);
athos@635
   143
	//A supply node
athos@635
   144
	if (excess_deficit[n] > 0){
athos@635
   145
	  excess_nodes.push(n,excess_deficit[n]);
athos@633
   146
	}
athos@635
   147
	//A demand node
athos@635
   148
	if (excess_deficit[n] < 0){
athos@635
   149
	  deficit_nodes.push(n, - excess_deficit[n]);
athos@635
   150
	}
athos@635
   151
	//Finding out starting value of delta
athos@635
   152
	if (delta < abs(excess_deficit[n])){
athos@635
   153
	  delta = abs(excess_deficit[n]);
athos@635
   154
	}
athos@633
   155
	//Initialize the copy of the Dijkstra potential to zero
athos@610
   156
	potential.set(n,0);
athos@610
   157
      }
athos@610
   158
athos@635
   159
      //It'll be allright as an initial value, though this value 
athos@635
   160
      //can be the maximum deficit here
athos@635
   161
      SupplyDemand max_excess = delta;
athos@610
   162
      
athos@633
   163
      //We need a residual graph which is uncapacitated
athos@633
   164
      ResGraphType res_graph(G, flow);
athos@610
   165
athos@633
   166
athos@610
   167
      
athos@610
   168
      ModLengthMap mod_length(res_graph, length, potential);
athos@610
   169
athos@610
   170
      Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
athos@610
   171
athos@633
   172
athos@635
   173
      while (max_excess > 0){
athos@635
   174
athos@610
   175
	
athos@635
   176
	//Merge and stuff
athos@635
   177
athos@635
   178
	Node s = excess_nodes.top(); 
athos@635
   179
	SupplyDemand max_excess = excess_nodes[s];
athos@635
   180
	Node t = deficit_nodes.top(); 
athos@635
   181
	if (max_excess < dificit_nodes[t]){
athos@635
   182
	  max_excess = dificit_nodes[t];
athos@635
   183
	}
athos@635
   184
athos@635
   185
athos@635
   186
	while(max_excess > ){
athos@635
   187
athos@635
   188
	  
athos@635
   189
	  //s es t valasztasa
athos@635
   190
athos@635
   191
	  //Dijkstra part	
athos@635
   192
	  dijkstra.run(s);
athos@635
   193
athos@635
   194
	  /*We know from theory that t can be reached
athos@635
   195
	  if (!dijkstra.reached(t)){
athos@635
   196
	    //There are no k paths from s to t
athos@635
   197
	    break;
athos@635
   198
	  };
athos@635
   199
	  */
athos@635
   200
	  
athos@635
   201
	  //We have to change the potential
athos@635
   202
	  FOR_EACH_LOC(typename ResGraphType::NodeIt, n, res_graph){
athos@635
   203
	    potential[n] += dijkstra.distMap()[n];
athos@635
   204
	  }
athos@635
   205
athos@635
   206
athos@635
   207
	  //Augmenting on the sortest path
athos@635
   208
	  Node n=t;
athos@635
   209
	  ResGraphEdge e;
athos@635
   210
	  while (n!=s){
athos@635
   211
	    e = dijkstra.pred(n);
athos@635
   212
	    n = dijkstra.predNode(n);
athos@635
   213
	    res_graph.augment(e,delta);
athos@635
   214
	    /*
athos@635
   215
	    //Let's update the total length
athos@635
   216
	    if (res_graph.forward(e))
athos@635
   217
	      total_length += length[e];
athos@635
   218
	    else 
athos@635
   219
	      total_length -= length[e];	    
athos@635
   220
	    */
athos@635
   221
	  }
athos@635
   222
athos@635
   223
	  //Update the excess_nodes heap
athos@635
   224
	  if (delta >= excess_nodes[s]){
athos@635
   225
	    if (delta > excess_nodes[s])
athos@635
   226
	      deficit_nodes.push(s,delta - excess_nodes[s]);
athos@635
   227
	    excess_nodes.pop();
athos@635
   228
	    
athos@635
   229
	  } 
athos@635
   230
	  else{
athos@635
   231
	    excess_nodes[s] -= delta;
athos@635
   232
	  }
athos@635
   233
	  //Update the deficit_nodes heap
athos@635
   234
	  if (delta >= deficit_nodes[t]){
athos@635
   235
	    if (delta > deficit_nodes[t])
athos@635
   236
	      excess_nodes.push(t,delta - deficit_nodes[t]);
athos@635
   237
	    deficit_nodes.pop();
athos@635
   238
	    
athos@635
   239
	  } 
athos@635
   240
	  else{
athos@635
   241
	    deficit_nodes[t] -= delta;
athos@635
   242
	  }
athos@635
   243
	  //Dijkstra part ends here
athos@633
   244
	}
athos@633
   245
athos@633
   246
	/*
athos@635
   247
	 * End of the delta scaling phase 
athos@635
   248
	*/
athos@633
   249
athos@635
   250
	//Whatever this means
athos@635
   251
	delta = delta / 2;
athos@635
   252
athos@635
   253
	/*This is not necessary here
athos@635
   254
	//Update the max_excess
athos@635
   255
	max_excess = 0;
athos@635
   256
	FOR_EACH_LOC(typename Graph::NodeIt, n, G){
athos@635
   257
	  if (max_excess < excess_deficit[n]){
athos@635
   258
	    max_excess = excess_deficit[n];
athos@610
   259
	  }
athos@610
   260
	}
athos@633
   261
	*/
athos@635
   262
	//Reset delta if still too big
athos@635
   263
	if (8*number_of_nodes*max_excess <= delta){
athos@635
   264
	  delta = max_excess;
athos@635
   265
	  
athos@610
   266
	}
athos@610
   267
	  
athos@635
   268
      }//while(max_excess > 0)
athos@610
   269
      
athos@610
   270
athos@610
   271
      return i;
athos@610
   272
    }
athos@610
   273
athos@610
   274
athos@610
   275
athos@610
   276
athos@610
   277
    ///This function gives back the total length of the found paths.
athos@610
   278
    ///Assumes that \c run() has been run and nothing changed since then.
athos@610
   279
    Length totalLength(){
athos@610
   280
      return total_length;
athos@610
   281
    }
athos@610
   282
athos@610
   283
    ///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must
athos@610
   284
    ///be called before using this function.
athos@610
   285
    const EdgeIntMap &getFlow() const { return flow;}
athos@610
   286
athos@610
   287
  ///Returns a const reference to the NodeMap \c potential (the dual solution).
athos@610
   288
    /// \pre \ref run() must be called before using this function.
athos@610
   289
    const EdgeIntMap &getPotential() const { return potential;}
athos@610
   290
athos@610
   291
    ///This function checks, whether the given solution is optimal
athos@610
   292
    ///Running after a \c run() should return with true
athos@610
   293
    ///In this "state of the art" this only check optimality, doesn't bother with feasibility
athos@610
   294
    ///
athos@610
   295
    ///\todo Is this OK here?
athos@610
   296
    bool checkComplementarySlackness(){
athos@610
   297
      Length mod_pot;
athos@610
   298
      Length fl_e;
athos@610
   299
      FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
athos@610
   300
	//C^{\Pi}_{i,j}
athos@610
   301
	mod_pot = length[e]-potential[G.head(e)]+potential[G.tail(e)];
athos@610
   302
	fl_e = flow[e];
athos@610
   303
	//	std::cout << fl_e << std::endl;
athos@610
   304
	if (0<fl_e && fl_e<capacity[e]){
athos@610
   305
	  if (mod_pot != 0)
athos@610
   306
	    return false;
athos@610
   307
	}
athos@610
   308
	else{
athos@610
   309
	  if (mod_pot > 0 && fl_e != 0)
athos@610
   310
	    return false;
athos@610
   311
	  if (mod_pot < 0 && fl_e != capacity[e])
athos@610
   312
	    return false;
athos@610
   313
	}
athos@610
   314
      }
athos@610
   315
      return true;
athos@610
   316
    }
athos@610
   317
    
athos@610
   318
athos@633
   319
  }; //class MinCostFlow
athos@610
   320
athos@610
   321
  ///@}
athos@610
   322
athos@610
   323
} //namespace hugo
athos@610
   324
athos@610
   325
#endif //HUGO_MINCOSTFLOW_H