1 | // -*- c++ -*- |
---|
2 | #ifndef HUGO_MINCOSTFLOW_H |
---|
3 | #define HUGO_MINCOSTFLOW_H |
---|
4 | |
---|
5 | ///\ingroup galgs |
---|
6 | ///\file |
---|
7 | ///\brief An algorithm for finding a flow of value \c k (for small values of \c k) having minimal total cost |
---|
8 | |
---|
9 | |
---|
10 | #include <hugo/dijkstra.h> |
---|
11 | #include <hugo/graph_wrapper.h> |
---|
12 | #include <hugo/maps.h> |
---|
13 | #include <vector> |
---|
14 | #include <for_each_macros.h> |
---|
15 | |
---|
16 | namespace hugo { |
---|
17 | |
---|
18 | /// \addtogroup galgs |
---|
19 | /// @{ |
---|
20 | |
---|
21 | ///\brief Implementation of an algorithm for finding a flow of value \c k |
---|
22 | ///(for small values of \c k) having minimal total cost between 2 nodes |
---|
23 | /// |
---|
24 | /// |
---|
25 | /// The class \ref hugo::MinCostFlow "MinCostFlow" implements |
---|
26 | /// an algorithm for solving the following general minimum cost flow problem> |
---|
27 | /// |
---|
28 | /// |
---|
29 | /// |
---|
30 | /// \warning It is assumed here that the problem has a feasible solution |
---|
31 | /// |
---|
32 | /// The range of the length (weight) function is nonnegative reals but |
---|
33 | /// the range of capacity function is the set of nonnegative integers. |
---|
34 | /// It is not a polinomial time algorithm for counting the minimum cost |
---|
35 | /// maximal flow, since it counts the minimum cost flow for every value 0..M |
---|
36 | /// where \c M is the value of the maximal flow. |
---|
37 | /// |
---|
38 | ///\author Attila Bernath |
---|
39 | template <typename Graph, typename LengthMap, typename SupplyMap> |
---|
40 | class MinCostFlow { |
---|
41 | |
---|
42 | typedef typename LengthMap::ValueType Length; |
---|
43 | |
---|
44 | |
---|
45 | typedef typename SupplyMap::ValueType Supply; |
---|
46 | |
---|
47 | typedef typename Graph::Node Node; |
---|
48 | typedef typename Graph::NodeIt NodeIt; |
---|
49 | typedef typename Graph::Edge Edge; |
---|
50 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
51 | typedef typename Graph::template EdgeMap<int> EdgeIntMap; |
---|
52 | |
---|
53 | // typedef ConstMap<Edge,int> ConstMap; |
---|
54 | |
---|
55 | typedef ResGraphWrapper<const Graph,int,CapacityMap,EdgeIntMap> ResGraphType; |
---|
56 | typedef typename ResGraphType::Edge ResGraphEdge; |
---|
57 | |
---|
58 | class ModLengthMap { |
---|
59 | //typedef typename ResGraphType::template NodeMap<Length> NodeMap; |
---|
60 | typedef typename Graph::template NodeMap<Length> NodeMap; |
---|
61 | const ResGraphType& G; |
---|
62 | // const EdgeIntMap& rev; |
---|
63 | const LengthMap &ol; |
---|
64 | const NodeMap &pot; |
---|
65 | public : |
---|
66 | typedef typename LengthMap::KeyType KeyType; |
---|
67 | typedef typename LengthMap::ValueType ValueType; |
---|
68 | |
---|
69 | ValueType operator[](typename ResGraphType::Edge e) const { |
---|
70 | if (G.forward(e)) |
---|
71 | return ol[e]-(pot[G.head(e)]-pot[G.tail(e)]); |
---|
72 | else |
---|
73 | return -ol[e]-(pot[G.head(e)]-pot[G.tail(e)]); |
---|
74 | } |
---|
75 | |
---|
76 | ModLengthMap(const ResGraphType& _G, |
---|
77 | const LengthMap &o, const NodeMap &p) : |
---|
78 | G(_G), /*rev(_rev),*/ ol(o), pot(p){}; |
---|
79 | };//ModLengthMap |
---|
80 | |
---|
81 | |
---|
82 | protected: |
---|
83 | |
---|
84 | //Input |
---|
85 | const Graph& G; |
---|
86 | const LengthMap& length; |
---|
87 | const SupplyMap& supply;//supply or demand of nodes |
---|
88 | |
---|
89 | |
---|
90 | //auxiliary variables |
---|
91 | |
---|
92 | //To store the flow |
---|
93 | EdgeIntMap flow; |
---|
94 | //To store the potentila (dual variables) |
---|
95 | typename Graph::template NodeMap<Length> potential; |
---|
96 | //To store excess-deficit values |
---|
97 | SupplyMap excess; |
---|
98 | |
---|
99 | |
---|
100 | Length total_length; |
---|
101 | |
---|
102 | |
---|
103 | public : |
---|
104 | |
---|
105 | |
---|
106 | MinCostFlow(Graph& _G, LengthMap& _length, SupplyMap& _supply) : G(_G), |
---|
107 | length(_length), supply(_supply), flow(_G), potential(_G){ } |
---|
108 | |
---|
109 | |
---|
110 | ///Runs the algorithm. |
---|
111 | |
---|
112 | ///Runs the algorithm. |
---|
113 | ///Returns k if there are at least k edge-disjoint paths from s to t. |
---|
114 | ///Otherwise it returns the number of found edge-disjoint paths from s to t. |
---|
115 | ///\todo May be it does make sense to be able to start with a nonzero |
---|
116 | /// feasible primal-dual solution pair as well. |
---|
117 | int run() { |
---|
118 | |
---|
119 | //Resetting variables from previous runs |
---|
120 | total_length = 0; |
---|
121 | |
---|
122 | FOR_EACH_LOC(typename Graph::EdgeIt, e, G){ |
---|
123 | flow.set(e,0); |
---|
124 | } |
---|
125 | |
---|
126 | //Initial value for delta |
---|
127 | Supply delta = 0; |
---|
128 | |
---|
129 | FOR_EACH_LOC(typename Graph::NodeIt, n, G){ |
---|
130 | if (delta < abs(supply[e])){ |
---|
131 | delta = abs(supply[e]); |
---|
132 | } |
---|
133 | excess.set(n,supply[e]); |
---|
134 | //Initialize the copy of the Dijkstra potential to zero |
---|
135 | potential.set(n,0); |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | |
---|
140 | //We need a residual graph which is uncapacitated |
---|
141 | ResGraphType res_graph(G, flow); |
---|
142 | |
---|
143 | |
---|
144 | |
---|
145 | ModLengthMap mod_length(res_graph, length, potential); |
---|
146 | |
---|
147 | Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length); |
---|
148 | |
---|
149 | |
---|
150 | int i; |
---|
151 | for (i=0; i<k; ++i){ |
---|
152 | dijkstra.run(s); |
---|
153 | if (!dijkstra.reached(t)){ |
---|
154 | //There are no k paths from s to t |
---|
155 | break; |
---|
156 | }; |
---|
157 | |
---|
158 | //We have to copy the potential |
---|
159 | FOR_EACH_LOC(typename ResGraphType::NodeIt, n, res_graph){ |
---|
160 | potential[n] += dijkstra.distMap()[n]; |
---|
161 | } |
---|
162 | |
---|
163 | /* |
---|
164 | { |
---|
165 | |
---|
166 | typename ResGraphType::NodeIt n; |
---|
167 | for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) { |
---|
168 | potential[n] += dijkstra.distMap()[n]; |
---|
169 | } |
---|
170 | } |
---|
171 | */ |
---|
172 | |
---|
173 | //Augmenting on the sortest path |
---|
174 | Node n=t; |
---|
175 | ResGraphEdge e; |
---|
176 | while (n!=s){ |
---|
177 | e = dijkstra.pred(n); |
---|
178 | n = dijkstra.predNode(n); |
---|
179 | res_graph.augment(e,delta); |
---|
180 | //Let's update the total length |
---|
181 | if (res_graph.forward(e)) |
---|
182 | total_length += length[e]; |
---|
183 | else |
---|
184 | total_length -= length[e]; |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | return i; |
---|
192 | } |
---|
193 | |
---|
194 | |
---|
195 | |
---|
196 | |
---|
197 | ///This function gives back the total length of the found paths. |
---|
198 | ///Assumes that \c run() has been run and nothing changed since then. |
---|
199 | Length totalLength(){ |
---|
200 | return total_length; |
---|
201 | } |
---|
202 | |
---|
203 | ///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must |
---|
204 | ///be called before using this function. |
---|
205 | const EdgeIntMap &getFlow() const { return flow;} |
---|
206 | |
---|
207 | ///Returns a const reference to the NodeMap \c potential (the dual solution). |
---|
208 | /// \pre \ref run() must be called before using this function. |
---|
209 | const EdgeIntMap &getPotential() const { return potential;} |
---|
210 | |
---|
211 | ///This function checks, whether the given solution is optimal |
---|
212 | ///Running after a \c run() should return with true |
---|
213 | ///In this "state of the art" this only check optimality, doesn't bother with feasibility |
---|
214 | /// |
---|
215 | ///\todo Is this OK here? |
---|
216 | bool checkComplementarySlackness(){ |
---|
217 | Length mod_pot; |
---|
218 | Length fl_e; |
---|
219 | FOR_EACH_LOC(typename Graph::EdgeIt, e, G){ |
---|
220 | //C^{\Pi}_{i,j} |
---|
221 | mod_pot = length[e]-potential[G.head(e)]+potential[G.tail(e)]; |
---|
222 | fl_e = flow[e]; |
---|
223 | // std::cout << fl_e << std::endl; |
---|
224 | if (0<fl_e && fl_e<capacity[e]){ |
---|
225 | if (mod_pot != 0) |
---|
226 | return false; |
---|
227 | } |
---|
228 | else{ |
---|
229 | if (mod_pot > 0 && fl_e != 0) |
---|
230 | return false; |
---|
231 | if (mod_pot < 0 && fl_e != capacity[e]) |
---|
232 | return false; |
---|
233 | } |
---|
234 | } |
---|
235 | return true; |
---|
236 | } |
---|
237 | |
---|
238 | |
---|
239 | }; //class MinCostFlow |
---|
240 | |
---|
241 | ///@} |
---|
242 | |
---|
243 | } //namespace hugo |
---|
244 | |
---|
245 | #endif //HUGO_MINCOSTFLOW_H |
---|