91 |
94 |
92 //auxiliary variables |
95 //auxiliary variables |
93 |
96 |
94 //To store the flow |
97 //To store the flow |
95 FlowMap flow; |
98 FlowMap flow; |
96 //To store the potentila (dual variables) |
99 //To store the potential (dual variables) |
97 typename Graph::template NodeMap<Cost> potential; |
100 typedef typename Graph::template NodeMap<Cost> PotentialMap; |
|
101 PotentialMap potential; |
98 //To store excess-deficit values |
102 //To store excess-deficit values |
99 SupplyDemandMap excess_deficit; |
103 SupplyDemandMap excess_deficit; |
100 |
104 |
101 |
105 |
102 Cost total_cost; |
106 Cost total_cost; |
103 |
107 |
104 |
108 |
105 public : |
109 public : |
106 |
110 |
107 |
111 |
108 MinCostFlow(Graph& _graph, CostMap& _cost, SupplyDemandMap& _supply_demand) : graph(_graph), |
112 MinCostFlow(Graph& _graph, CostMap& _cost, SupplyDemandMap& _supply_demand): |
109 cost(_cost), supply_demand(_supply_demand), flow(_graph), potential(_graph){ } |
113 graph(_graph), |
|
114 cost(_cost), |
|
115 supply_demand(_supply_demand), |
|
116 flow(_graph), |
|
117 potential(_graph), |
|
118 excess_deficit(_graph){ } |
110 |
119 |
111 |
120 |
112 ///Runs the algorithm. |
121 ///Runs the algorithm. |
113 |
122 |
114 ///Runs the algorithm. |
123 ///Runs the algorithm. |
175 //It'll be allright as an initial value, though this value |
184 //It'll be allright as an initial value, though this value |
176 //can be the maximum deficit here |
185 //can be the maximum deficit here |
177 SupplyDemand max_excess = delta; |
186 SupplyDemand max_excess = delta; |
178 |
187 |
179 ///\bug This is a serious cheat here, before we have an uncapacitated ResGraph |
188 ///\bug This is a serious cheat here, before we have an uncapacitated ResGraph |
180 ConstEdgeMap const_inf_map(MAX_INT); |
189 ConstEdgeMap const_inf_map(MAXINT); |
181 |
190 |
182 //We need a residual graph which is uncapacitated |
191 //We need a residual graph which is uncapacitated |
183 ResGraph res_graph(graph, const_inf_map, flow); |
192 ResGraph res_graph(graph, const_inf_map, flow); |
184 |
193 |
185 //An EdgeMap to tell which arcs are abundant |
194 //An EdgeMap to tell which arcs are abundant |
186 template typename Graph::EdgeMap<bool> abundant_arcs(graph); |
195 typename Graph::template EdgeMap<bool> abundant_arcs(graph); |
187 |
196 |
188 //Let's construct the sugraph consisting only of the abundant edges |
197 //Let's construct the sugraph consisting only of the abundant edges |
189 typedef ConstMap< typename Graph::Node, bool > ConstNodeMap; |
198 typedef ConstMap< typename Graph::Node, bool > ConstNodeMap; |
190 ConstNodeMap const_true_map(true); |
199 ConstNodeMap const_true_map(true); |
191 typedef SubGraphWrapper< Graph, ConstNodeMap, |
200 typedef SubGraphWrapper< const Graph, ConstNodeMap, |
192 template typename Graph::EdgeMap<bool> > |
201 typename Graph::template EdgeMap<bool> > |
193 AbundantGraph; |
202 AbundantGraph; |
194 AbundantGraph abundant_graph(graph, const_true_map, abundant_arcs ); |
203 AbundantGraph abundant_graph(graph, const_true_map, abundant_arcs ); |
195 |
204 |
196 //Let's construct the residual graph for the abundant graph |
205 //Let's construct the residual graph for the abundant graph |
197 typedef ResGraphWrapper<const AbundantGraph,int,CapacityMap,EdgeIntMap> |
206 typedef ResGraphWrapper<const AbundantGraph,int,ConstEdgeMap,FlowMap> |
198 ResAbGraph; |
207 ResAbGraph; |
199 //Again uncapacitated |
208 //Again uncapacitated |
200 ResAbGraph res_ab_graph(abundant_graph, const_inf_map, flow); |
209 ResAbGraph res_ab_graph(abundant_graph, const_inf_map, flow); |
201 |
210 |
202 //We need things for the bfs |
211 //We need things for the bfs |
203 typename ResAbGraph::NodeMap<bool> bfs_reached(res_ab_graph); |
212 typename ResAbGraph::template NodeMap<bool> bfs_reached(res_ab_graph); |
204 typename ResAbGraph::NodeMap<typename ResAbGraph::Edge> |
213 typename ResAbGraph::template NodeMap<typename ResAbGraph::Edge> |
205 bfs_pred(res_ab_graph); |
214 bfs_pred(res_ab_graph); |
206 NullMap<typename ResAbGraph::Node, int> bfs_dist_dummy(res_ab_graph); |
215 NullMap<typename ResAbGraph::Node, int> bfs_dist_dummy; |
207 //We want to run bfs-es (more) on this graph 'res_ab_graph' |
216 //We want to run bfs-es (more) on this graph 'res_ab_graph' |
208 Bfs < ResAbGraph , |
217 Bfs < ResAbGraph , |
209 typename ResAbGraph::NodeMap<bool>, |
218 typename ResAbGraph::template NodeMap<bool>, |
210 typename ResAbGraph::NodeMap<typename ResAbGraph::Edge>, |
219 typename ResAbGraph::template NodeMap<typename ResAbGraph::Edge>, |
211 NullMap<typename ResAbGraph::Node, int> > |
220 NullMap<typename ResAbGraph::Node, int> > |
212 bfs(res_ab_graph, bfs_reached, bfs_pred, bfs_dist_dummy); |
221 bfs(res_ab_graph, bfs_reached, bfs_pred, bfs_dist_dummy); |
|
222 /*This is what Marci wants for a bfs |
|
223 template <typename Graph, |
|
224 typename ReachedMap=typename Graph::template NodeMap<bool>, |
|
225 typename PredMap |
|
226 =typename Graph::template NodeMap<typename Graph::Edge>, |
|
227 typename DistMap=typename Graph::template NodeMap<int> > |
|
228 class Bfs : public BfsIterator<Graph, ReachedMap> { |
|
229 |
|
230 */ |
213 |
231 |
214 ModCostMap mod_cost(res_graph, cost, potential); |
232 ModCostMap mod_cost(res_graph, cost, potential); |
215 |
233 |
216 Dijkstra<ResGraph, ModCostMap> dijkstra(res_graph, mod_cost); |
234 Dijkstra<ResGraph, ModCostMap> dijkstra(res_graph, mod_cost); |
217 |
235 |
408 return total_cost; |
426 return total_cost; |
409 } |
427 } |
410 |
428 |
411 ///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must |
429 ///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must |
412 ///be called before using this function. |
430 ///be called before using this function. |
413 const EdgeIntMap &getFlow() const { return flow;} |
431 const FlowMap &getFlow() const { return flow;} |
414 |
432 |
415 ///Returns a const reference to the NodeMap \c potential (the dual solution). |
433 ///Returns a const reference to the NodeMap \c potential (the dual solution). |
416 /// \pre \ref run() must be called before using this function. |
434 /// \pre \ref run() must be called before using this function. |
417 const EdgeIntMap &getPotential() const { return potential;} |
435 const PotentialMap &getPotential() const { return potential;} |
418 |
436 |
419 ///This function checks, whether the given solution is optimal |
437 ///This function checks, whether the given solution is optimal |
420 ///Running after a \c run() should return with true |
438 ///Running after a \c run() should return with true |
421 ///In this "state of the art" this only check optimality, doesn't bother with feasibility |
439 ///In this "state of the art" this only check optimality, doesn't bother with feasibility |
422 /// |
440 /// |