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