kpeter@814: /* -*- C++ -*- kpeter@814: * kpeter@814: * This file is a part of LEMON, a generic C++ optimization library kpeter@814: * kpeter@814: * Copyright (C) 2003-2008 kpeter@814: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport kpeter@814: * (Egervary Research Group on Combinatorial Optimization, EGRES). kpeter@814: * kpeter@814: * Permission to use, modify and distribute this software is granted kpeter@814: * provided that this copyright notice appears in all copies. For kpeter@814: * precise terms see the accompanying LICENSE file. kpeter@814: * kpeter@814: * This software is provided "AS IS" with no warranty of any kind, kpeter@814: * express or implied, and with no claim as to its suitability for any kpeter@814: * purpose. kpeter@814: * kpeter@814: */ kpeter@814: kpeter@814: #ifndef LEMON_CANCEL_AND_TIGHTEN_H kpeter@814: #define LEMON_CANCEL_AND_TIGHTEN_H kpeter@814: kpeter@814: /// \ingroup min_cost_flow kpeter@814: /// kpeter@814: /// \file kpeter@814: /// \brief Cancel and Tighten algorithm for finding a minimum cost flow. kpeter@814: kpeter@814: #include kpeter@814: kpeter@814: #include kpeter@814: #include kpeter@814: #include kpeter@814: #include kpeter@814: #include kpeter@814: #include kpeter@814: kpeter@814: #include kpeter@814: kpeter@814: namespace lemon { kpeter@814: kpeter@814: /// \addtogroup min_cost_flow kpeter@814: /// @{ kpeter@814: kpeter@814: /// \brief Implementation of the Cancel and Tighten algorithm for kpeter@814: /// finding a minimum cost flow. kpeter@814: /// kpeter@814: /// \ref CancelAndTighten implements the Cancel and Tighten algorithm for kpeter@814: /// finding a minimum cost flow. kpeter@814: /// kpeter@814: /// \tparam Digraph The digraph type the algorithm runs on. kpeter@814: /// \tparam LowerMap The type of the lower bound map. kpeter@814: /// \tparam CapacityMap The type of the capacity (upper bound) map. kpeter@814: /// \tparam CostMap The type of the cost (length) map. kpeter@814: /// \tparam SupplyMap The type of the supply map. kpeter@814: /// kpeter@814: /// \warning kpeter@814: /// - Arc capacities and costs should be \e non-negative \e integers. kpeter@814: /// - Supply values should be \e signed \e integers. kpeter@814: /// - The value types of the maps should be convertible to each other. kpeter@814: /// - \c CostMap::Value must be signed type. kpeter@814: /// kpeter@814: /// \author Peter Kovacs kpeter@814: template < typename Digraph, kpeter@814: typename LowerMap = typename Digraph::template ArcMap, kpeter@814: typename CapacityMap = typename Digraph::template ArcMap, kpeter@814: typename CostMap = typename Digraph::template ArcMap, kpeter@814: typename SupplyMap = typename Digraph::template NodeMap > kpeter@814: class CancelAndTighten kpeter@814: { kpeter@814: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); kpeter@814: kpeter@814: typedef typename CapacityMap::Value Capacity; kpeter@814: typedef typename CostMap::Value Cost; kpeter@814: typedef typename SupplyMap::Value Supply; kpeter@814: typedef typename Digraph::template ArcMap CapacityArcMap; kpeter@814: typedef typename Digraph::template NodeMap SupplyNodeMap; kpeter@814: kpeter@814: typedef ResidualDigraph< const Digraph, kpeter@814: CapacityArcMap, CapacityArcMap > ResDigraph; kpeter@814: kpeter@814: public: kpeter@814: kpeter@814: /// The type of the flow map. kpeter@814: typedef typename Digraph::template ArcMap FlowMap; kpeter@814: /// The type of the potential map. kpeter@814: typedef typename Digraph::template NodeMap PotentialMap; kpeter@814: kpeter@814: private: kpeter@814: kpeter@814: /// \brief Map adaptor class for handling residual arc costs. kpeter@814: /// kpeter@814: /// Map adaptor class for handling residual arc costs. kpeter@814: class ResidualCostMap : public MapBase kpeter@814: { kpeter@814: typedef typename ResDigraph::Arc Arc; kpeter@814: kpeter@814: private: kpeter@814: kpeter@814: const CostMap &_cost_map; kpeter@814: kpeter@814: public: kpeter@814: kpeter@814: ///\e kpeter@814: ResidualCostMap(const CostMap &cost_map) : _cost_map(cost_map) {} kpeter@814: kpeter@814: ///\e kpeter@814: Cost operator[](const Arc &e) const { kpeter@814: return ResDigraph::forward(e) ? _cost_map[e] : -_cost_map[e]; kpeter@814: } kpeter@814: kpeter@814: }; //class ResidualCostMap kpeter@814: kpeter@814: /// \brief Map adaptor class for handling reduced arc costs. kpeter@814: /// kpeter@814: /// Map adaptor class for handling reduced arc costs. kpeter@814: class ReducedCostMap : public MapBase kpeter@814: { kpeter@814: private: kpeter@814: kpeter@814: const Digraph &_gr; kpeter@814: const CostMap &_cost_map; kpeter@814: const PotentialMap &_pot_map; kpeter@814: kpeter@814: public: kpeter@814: kpeter@814: ///\e kpeter@814: ReducedCostMap( const Digraph &gr, kpeter@814: const CostMap &cost_map, kpeter@814: const PotentialMap &pot_map ) : kpeter@814: _gr(gr), _cost_map(cost_map), _pot_map(pot_map) {} kpeter@814: kpeter@814: ///\e kpeter@814: inline Cost operator[](const Arc &e) const { kpeter@814: return _cost_map[e] + _pot_map[_gr.source(e)] kpeter@814: - _pot_map[_gr.target(e)]; kpeter@814: } kpeter@814: kpeter@814: }; //class ReducedCostMap kpeter@814: kpeter@814: struct BFOperationTraits { kpeter@814: static double zero() { return 0; } kpeter@814: kpeter@814: static double infinity() { kpeter@814: return std::numeric_limits::infinity(); kpeter@814: } kpeter@814: kpeter@814: static double plus(const double& left, const double& right) { kpeter@814: return left + right; kpeter@814: } kpeter@814: kpeter@814: static bool less(const double& left, const double& right) { kpeter@814: return left + 1e-6 < right; kpeter@814: } kpeter@814: }; // class BFOperationTraits kpeter@814: kpeter@814: private: kpeter@814: kpeter@814: // The digraph the algorithm runs on kpeter@814: const Digraph &_graph; kpeter@814: // The original lower bound map kpeter@814: const LowerMap *_lower; kpeter@814: // The modified capacity map kpeter@814: CapacityArcMap _capacity; kpeter@814: // The original cost map kpeter@814: const CostMap &_cost; kpeter@814: // The modified supply map kpeter@814: SupplyNodeMap _supply; kpeter@814: bool _valid_supply; kpeter@814: kpeter@814: // Arc map of the current flow kpeter@814: FlowMap *_flow; kpeter@814: bool _local_flow; kpeter@814: // Node map of the current potentials kpeter@814: PotentialMap *_potential; kpeter@814: bool _local_potential; kpeter@814: kpeter@814: // The residual digraph kpeter@814: ResDigraph *_res_graph; kpeter@814: // The residual cost map kpeter@814: ResidualCostMap _res_cost; kpeter@814: kpeter@814: public: kpeter@814: kpeter@814: /// \brief General constructor (with lower bounds). kpeter@814: /// kpeter@814: /// General constructor (with lower bounds). kpeter@814: /// kpeter@814: /// \param digraph The digraph the algorithm runs on. kpeter@814: /// \param lower The lower bounds of the arcs. kpeter@814: /// \param capacity The capacities (upper bounds) of the arcs. kpeter@814: /// \param cost The cost (length) values of the arcs. kpeter@814: /// \param supply The supply values of the nodes (signed). kpeter@814: CancelAndTighten( const Digraph &digraph, kpeter@814: const LowerMap &lower, kpeter@814: const CapacityMap &capacity, kpeter@814: const CostMap &cost, kpeter@814: const SupplyMap &supply ) : kpeter@814: _graph(digraph), _lower(&lower), _capacity(digraph), _cost(cost), kpeter@814: _supply(digraph), _flow(NULL), _local_flow(false), kpeter@814: _potential(NULL), _local_potential(false), kpeter@814: _res_graph(NULL), _res_cost(_cost) kpeter@814: { kpeter@814: // Check the sum of supply values kpeter@814: Supply sum = 0; kpeter@814: for (NodeIt n(_graph); n != INVALID; ++n) { kpeter@814: _supply[n] = supply[n]; kpeter@814: sum += _supply[n]; kpeter@814: } kpeter@814: _valid_supply = sum == 0; kpeter@814: kpeter@814: // Remove non-zero lower bounds kpeter@814: for (ArcIt e(_graph); e != INVALID; ++e) { kpeter@814: _capacity[e] = capacity[e]; kpeter@814: if (lower[e] != 0) { kpeter@814: _capacity[e] -= lower[e]; kpeter@814: _supply[_graph.source(e)] -= lower[e]; kpeter@814: _supply[_graph.target(e)] += lower[e]; kpeter@814: } kpeter@814: } kpeter@814: } kpeter@814: /* kpeter@814: /// \brief General constructor (without lower bounds). kpeter@814: /// kpeter@814: /// General constructor (without lower bounds). kpeter@814: /// kpeter@814: /// \param digraph The digraph the algorithm runs on. kpeter@814: /// \param capacity The capacities (upper bounds) of the arcs. kpeter@814: /// \param cost The cost (length) values of the arcs. kpeter@814: /// \param supply The supply values of the nodes (signed). kpeter@814: CancelAndTighten( const Digraph &digraph, kpeter@814: const CapacityMap &capacity, kpeter@814: const CostMap &cost, kpeter@814: const SupplyMap &supply ) : kpeter@814: _graph(digraph), _lower(NULL), _capacity(capacity), _cost(cost), kpeter@814: _supply(supply), _flow(NULL), _local_flow(false), kpeter@814: _potential(NULL), _local_potential(false), kpeter@814: _res_graph(NULL), _res_cost(_cost) kpeter@814: { kpeter@814: // Check the sum of supply values kpeter@814: Supply sum = 0; kpeter@814: for (NodeIt n(_graph); n != INVALID; ++n) sum += _supply[n]; kpeter@814: _valid_supply = sum == 0; kpeter@814: } kpeter@814: kpeter@814: /// \brief Simple constructor (with lower bounds). kpeter@814: /// kpeter@814: /// Simple constructor (with lower bounds). kpeter@814: /// kpeter@814: /// \param digraph The digraph the algorithm runs on. kpeter@814: /// \param lower The lower bounds of the arcs. kpeter@814: /// \param capacity The capacities (upper bounds) of the arcs. kpeter@814: /// \param cost The cost (length) values of the arcs. kpeter@814: /// \param s The source node. kpeter@814: /// \param t The target node. kpeter@814: /// \param flow_value The required amount of flow from node \c s kpeter@814: /// to node \c t (i.e. the supply of \c s and the demand of \c t). kpeter@814: CancelAndTighten( const Digraph &digraph, kpeter@814: const LowerMap &lower, kpeter@814: const CapacityMap &capacity, kpeter@814: const CostMap &cost, kpeter@814: Node s, Node t, kpeter@814: Supply flow_value ) : kpeter@814: _graph(digraph), _lower(&lower), _capacity(capacity), _cost(cost), kpeter@814: _supply(digraph, 0), _flow(NULL), _local_flow(false), kpeter@814: _potential(NULL), _local_potential(false), kpeter@814: _res_graph(NULL), _res_cost(_cost) kpeter@814: { kpeter@814: // Remove non-zero lower bounds kpeter@814: _supply[s] = flow_value; kpeter@814: _supply[t] = -flow_value; kpeter@814: for (ArcIt e(_graph); e != INVALID; ++e) { kpeter@814: if (lower[e] != 0) { kpeter@814: _capacity[e] -= lower[e]; kpeter@814: _supply[_graph.source(e)] -= lower[e]; kpeter@814: _supply[_graph.target(e)] += lower[e]; kpeter@814: } kpeter@814: } kpeter@814: _valid_supply = true; kpeter@814: } kpeter@814: kpeter@814: /// \brief Simple constructor (without lower bounds). kpeter@814: /// kpeter@814: /// Simple constructor (without lower bounds). kpeter@814: /// kpeter@814: /// \param digraph The digraph the algorithm runs on. kpeter@814: /// \param capacity The capacities (upper bounds) of the arcs. kpeter@814: /// \param cost The cost (length) values of the arcs. kpeter@814: /// \param s The source node. kpeter@814: /// \param t The target node. kpeter@814: /// \param flow_value The required amount of flow from node \c s kpeter@814: /// to node \c t (i.e. the supply of \c s and the demand of \c t). kpeter@814: CancelAndTighten( const Digraph &digraph, kpeter@814: const CapacityMap &capacity, kpeter@814: const CostMap &cost, kpeter@814: Node s, Node t, kpeter@814: Supply flow_value ) : kpeter@814: _graph(digraph), _lower(NULL), _capacity(capacity), _cost(cost), kpeter@814: _supply(digraph, 0), _flow(NULL), _local_flow(false), kpeter@814: _potential(NULL), _local_potential(false), kpeter@814: _res_graph(NULL), _res_cost(_cost) kpeter@814: { kpeter@814: _supply[s] = flow_value; kpeter@814: _supply[t] = -flow_value; kpeter@814: _valid_supply = true; kpeter@814: } kpeter@814: */ kpeter@814: /// Destructor. kpeter@814: ~CancelAndTighten() { kpeter@814: if (_local_flow) delete _flow; kpeter@814: if (_local_potential) delete _potential; kpeter@814: delete _res_graph; kpeter@814: } kpeter@814: kpeter@814: /// \brief Set the flow map. kpeter@814: /// kpeter@814: /// Set the flow map. kpeter@814: /// kpeter@814: /// \return \c (*this) kpeter@814: CancelAndTighten& flowMap(FlowMap &map) { kpeter@814: if (_local_flow) { kpeter@814: delete _flow; kpeter@814: _local_flow = false; kpeter@814: } kpeter@814: _flow = ↦ kpeter@814: return *this; kpeter@814: } kpeter@814: kpeter@814: /// \brief Set the potential map. kpeter@814: /// kpeter@814: /// Set the potential map. kpeter@814: /// kpeter@814: /// \return \c (*this) kpeter@814: CancelAndTighten& potentialMap(PotentialMap &map) { kpeter@814: if (_local_potential) { kpeter@814: delete _potential; kpeter@814: _local_potential = false; kpeter@814: } kpeter@814: _potential = ↦ kpeter@814: return *this; kpeter@814: } kpeter@814: kpeter@814: /// \name Execution control kpeter@814: kpeter@814: /// @{ kpeter@814: kpeter@814: /// \brief Run the algorithm. kpeter@814: /// kpeter@814: /// Run the algorithm. kpeter@814: /// kpeter@814: /// \return \c true if a feasible flow can be found. kpeter@814: bool run() { kpeter@814: return init() && start(); kpeter@814: } kpeter@814: kpeter@814: /// @} kpeter@814: kpeter@814: /// \name Query Functions kpeter@814: /// The result of the algorithm can be obtained using these kpeter@814: /// functions.\n kpeter@814: /// \ref lemon::CancelAndTighten::run() "run()" must be called before kpeter@814: /// using them. kpeter@814: kpeter@814: /// @{ kpeter@814: kpeter@814: /// \brief Return a const reference to the arc map storing the kpeter@814: /// found flow. kpeter@814: /// kpeter@814: /// Return a const reference to the arc map storing the found flow. kpeter@814: /// kpeter@814: /// \pre \ref run() must be called before using this function. kpeter@814: const FlowMap& flowMap() const { kpeter@814: return *_flow; kpeter@814: } kpeter@814: kpeter@814: /// \brief Return a const reference to the node map storing the kpeter@814: /// found potentials (the dual solution). kpeter@814: /// kpeter@814: /// Return a const reference to the node map storing the found kpeter@814: /// potentials (the dual solution). kpeter@814: /// kpeter@814: /// \pre \ref run() must be called before using this function. kpeter@814: const PotentialMap& potentialMap() const { kpeter@814: return *_potential; kpeter@814: } kpeter@814: kpeter@814: /// \brief Return the flow on the given arc. kpeter@814: /// kpeter@814: /// Return the flow on the given arc. kpeter@814: /// kpeter@814: /// \pre \ref run() must be called before using this function. kpeter@814: Capacity flow(const Arc& arc) const { kpeter@814: return (*_flow)[arc]; kpeter@814: } kpeter@814: kpeter@814: /// \brief Return the potential of the given node. kpeter@814: /// kpeter@814: /// Return the potential of the given node. kpeter@814: /// kpeter@814: /// \pre \ref run() must be called before using this function. kpeter@814: Cost potential(const Node& node) const { kpeter@814: return (*_potential)[node]; kpeter@814: } kpeter@814: kpeter@814: /// \brief Return the total cost of the found flow. kpeter@814: /// kpeter@814: /// Return the total cost of the found flow. The complexity of the kpeter@814: /// function is \f$ O(e) \f$. kpeter@814: /// kpeter@814: /// \pre \ref run() must be called before using this function. kpeter@814: Cost totalCost() const { kpeter@814: Cost c = 0; kpeter@814: for (ArcIt e(_graph); e != INVALID; ++e) kpeter@814: c += (*_flow)[e] * _cost[e]; kpeter@814: return c; kpeter@814: } kpeter@814: kpeter@814: /// @} kpeter@814: kpeter@814: private: kpeter@814: kpeter@814: /// Initialize the algorithm. kpeter@814: bool init() { kpeter@814: if (!_valid_supply) return false; kpeter@814: kpeter@814: // Initialize flow and potential maps kpeter@814: if (!_flow) { kpeter@814: _flow = new FlowMap(_graph); kpeter@814: _local_flow = true; kpeter@814: } kpeter@814: if (!_potential) { kpeter@814: _potential = new PotentialMap(_graph); kpeter@814: _local_potential = true; kpeter@814: } kpeter@814: kpeter@814: _res_graph = new ResDigraph(_graph, _capacity, *_flow); kpeter@814: kpeter@814: // Find a feasible flow using Circulation kpeter@814: Circulation< Digraph, ConstMap, kpeter@814: CapacityArcMap, SupplyMap > kpeter@814: circulation( _graph, constMap(Capacity(0)), kpeter@814: _capacity, _supply ); kpeter@814: return circulation.flowMap(*_flow).run(); kpeter@814: } kpeter@814: kpeter@814: bool start() { kpeter@814: const double LIMIT_FACTOR = 0.01; kpeter@814: const int MIN_LIMIT = 3; kpeter@814: kpeter@814: typedef typename Digraph::template NodeMap FloatPotentialMap; kpeter@814: typedef typename Digraph::template NodeMap LevelMap; kpeter@814: typedef typename Digraph::template NodeMap BoolNodeMap; kpeter@814: typedef typename Digraph::template NodeMap PredNodeMap; kpeter@814: typedef typename Digraph::template NodeMap PredArcMap; kpeter@814: typedef typename ResDigraph::template ArcMap ResShiftCostMap; kpeter@814: FloatPotentialMap pi(_graph); kpeter@814: LevelMap level(_graph); kpeter@814: BoolNodeMap reached(_graph); kpeter@814: BoolNodeMap processed(_graph); kpeter@814: PredNodeMap pred_node(_graph); kpeter@814: PredArcMap pred_arc(_graph); kpeter@814: int node_num = countNodes(_graph); kpeter@814: typedef std::pair pair; kpeter@814: std::vector stack(node_num); kpeter@814: std::vector proc_vector(node_num); kpeter@814: ResShiftCostMap shift_cost(*_res_graph); kpeter@814: kpeter@814: Tolerance tol; kpeter@814: tol.epsilon(1e-6); kpeter@814: kpeter@814: Timer t1, t2, t3; kpeter@814: t1.reset(); kpeter@814: t2.reset(); kpeter@814: t3.reset(); kpeter@814: kpeter@814: // Initialize epsilon and the node potentials kpeter@814: double epsilon = 0; kpeter@814: for (ArcIt e(_graph); e != INVALID; ++e) { kpeter@814: if (_capacity[e] - (*_flow)[e] > 0 && _cost[e] < -epsilon) kpeter@814: epsilon = -_cost[e]; kpeter@814: else if ((*_flow)[e] > 0 && _cost[e] > epsilon) kpeter@814: epsilon = _cost[e]; kpeter@814: } kpeter@814: for (NodeIt v(_graph); v != INVALID; ++v) { kpeter@814: pi[v] = 0; kpeter@814: } kpeter@814: kpeter@814: // Start phases kpeter@814: int limit = int(LIMIT_FACTOR * node_num); kpeter@814: if (limit < MIN_LIMIT) limit = MIN_LIMIT; kpeter@814: int iter = limit; kpeter@814: while (epsilon * node_num >= 1) { kpeter@814: t1.start(); kpeter@814: // Find and cancel cycles in the admissible digraph using DFS kpeter@814: for (NodeIt n(_graph); n != INVALID; ++n) { kpeter@814: reached[n] = false; kpeter@814: processed[n] = false; kpeter@814: } kpeter@814: int stack_head = -1; kpeter@814: int proc_head = -1; kpeter@814: kpeter@814: for (NodeIt start(_graph); start != INVALID; ++start) { kpeter@814: if (reached[start]) continue; kpeter@814: kpeter@814: // New start node kpeter@814: reached[start] = true; kpeter@814: pred_arc[start] = INVALID; kpeter@814: pred_node[start] = INVALID; kpeter@814: kpeter@814: // Find the first admissible residual outgoing arc kpeter@814: double p = pi[start]; kpeter@814: Arc e; kpeter@814: _graph.firstOut(e, start); kpeter@814: while ( e != INVALID && (_capacity[e] - (*_flow)[e] == 0 || kpeter@814: !tol.negative(_cost[e] + p - pi[_graph.target(e)])) ) kpeter@814: _graph.nextOut(e); kpeter@814: if (e != INVALID) { kpeter@814: stack[++stack_head] = pair(e, true); kpeter@814: goto next_step_1; kpeter@814: } kpeter@814: _graph.firstIn(e, start); kpeter@814: while ( e != INVALID && ((*_flow)[e] == 0 || kpeter@814: !tol.negative(-_cost[e] + p - pi[_graph.source(e)])) ) kpeter@814: _graph.nextIn(e); kpeter@814: if (e != INVALID) { kpeter@814: stack[++stack_head] = pair(e, false); kpeter@814: goto next_step_1; kpeter@814: } kpeter@814: processed[start] = true; kpeter@814: proc_vector[++proc_head] = start; kpeter@814: continue; kpeter@814: next_step_1: kpeter@814: kpeter@814: while (stack_head >= 0) { kpeter@814: Arc se = stack[stack_head].first; kpeter@814: bool sf = stack[stack_head].second; kpeter@814: Node u, v; kpeter@814: if (sf) { kpeter@814: u = _graph.source(se); kpeter@814: v = _graph.target(se); kpeter@814: } else { kpeter@814: u = _graph.target(se); kpeter@814: v = _graph.source(se); kpeter@814: } kpeter@814: kpeter@814: if (!reached[v]) { kpeter@814: // A new node is reached kpeter@814: reached[v] = true; kpeter@814: pred_node[v] = u; kpeter@814: pred_arc[v] = se; kpeter@814: // Find the first admissible residual outgoing arc kpeter@814: double p = pi[v]; kpeter@814: Arc e; kpeter@814: _graph.firstOut(e, v); kpeter@814: while ( e != INVALID && (_capacity[e] - (*_flow)[e] == 0 || kpeter@814: !tol.negative(_cost[e] + p - pi[_graph.target(e)])) ) kpeter@814: _graph.nextOut(e); kpeter@814: if (e != INVALID) { kpeter@814: stack[++stack_head] = pair(e, true); kpeter@814: goto next_step_2; kpeter@814: } kpeter@814: _graph.firstIn(e, v); kpeter@814: while ( e != INVALID && ((*_flow)[e] == 0 || kpeter@814: !tol.negative(-_cost[e] + p - pi[_graph.source(e)])) ) kpeter@814: _graph.nextIn(e); kpeter@814: stack[++stack_head] = pair(e, false); kpeter@814: next_step_2: ; kpeter@814: } else { kpeter@814: if (!processed[v]) { kpeter@814: // A cycle is found kpeter@814: Node n, w = u; kpeter@814: Capacity d, delta = sf ? _capacity[se] - (*_flow)[se] : kpeter@814: (*_flow)[se]; kpeter@814: for (n = u; n != v; n = pred_node[n]) { kpeter@814: d = _graph.target(pred_arc[n]) == n ? kpeter@814: _capacity[pred_arc[n]] - (*_flow)[pred_arc[n]] : kpeter@814: (*_flow)[pred_arc[n]]; kpeter@814: if (d <= delta) { kpeter@814: delta = d; kpeter@814: w = pred_node[n]; kpeter@814: } kpeter@814: } kpeter@814: kpeter@814: /* kpeter@814: std::cout << "CYCLE FOUND: "; kpeter@814: if (sf) kpeter@814: std::cout << _cost[se] + pi[_graph.source(se)] - pi[_graph.target(se)]; kpeter@814: else kpeter@814: std::cout << _graph.id(se) << ":" << -(_cost[se] + pi[_graph.source(se)] - pi[_graph.target(se)]); kpeter@814: for (n = u; n != v; n = pred_node[n]) { kpeter@814: if (_graph.target(pred_arc[n]) == n) kpeter@814: std::cout << " " << _cost[pred_arc[n]] + pi[_graph.source(pred_arc[n])] - pi[_graph.target(pred_arc[n])]; kpeter@814: else kpeter@814: std::cout << " " << -(_cost[pred_arc[n]] + pi[_graph.source(pred_arc[n])] - pi[_graph.target(pred_arc[n])]); kpeter@814: } kpeter@814: std::cout << "\n"; kpeter@814: */ kpeter@814: // Augment along the cycle kpeter@814: (*_flow)[se] = sf ? (*_flow)[se] + delta : kpeter@814: (*_flow)[se] - delta; kpeter@814: for (n = u; n != v; n = pred_node[n]) { kpeter@814: if (_graph.target(pred_arc[n]) == n) kpeter@814: (*_flow)[pred_arc[n]] += delta; kpeter@814: else kpeter@814: (*_flow)[pred_arc[n]] -= delta; kpeter@814: } kpeter@814: for (n = u; stack_head > 0 && n != w; n = pred_node[n]) { kpeter@814: --stack_head; kpeter@814: reached[n] = false; kpeter@814: } kpeter@814: u = w; kpeter@814: } kpeter@814: v = u; kpeter@814: kpeter@814: // Find the next admissible residual outgoing arc kpeter@814: double p = pi[v]; kpeter@814: Arc e = stack[stack_head].first; kpeter@814: if (!stack[stack_head].second) { kpeter@814: _graph.nextIn(e); kpeter@814: goto in_arc_3; kpeter@814: } kpeter@814: _graph.nextOut(e); kpeter@814: while ( e != INVALID && (_capacity[e] - (*_flow)[e] == 0 || kpeter@814: !tol.negative(_cost[e] + p - pi[_graph.target(e)])) ) kpeter@814: _graph.nextOut(e); kpeter@814: if (e != INVALID) { kpeter@814: stack[stack_head] = pair(e, true); kpeter@814: goto next_step_3; kpeter@814: } kpeter@814: _graph.firstIn(e, v); kpeter@814: in_arc_3: kpeter@814: while ( e != INVALID && ((*_flow)[e] == 0 || kpeter@814: !tol.negative(-_cost[e] + p - pi[_graph.source(e)])) ) kpeter@814: _graph.nextIn(e); kpeter@814: stack[stack_head] = pair(e, false); kpeter@814: next_step_3: ; kpeter@814: } kpeter@814: kpeter@814: while (stack_head >= 0 && stack[stack_head].first == INVALID) { kpeter@814: processed[v] = true; kpeter@814: proc_vector[++proc_head] = v; kpeter@814: if (--stack_head >= 0) { kpeter@814: v = stack[stack_head].second ? kpeter@814: _graph.source(stack[stack_head].first) : kpeter@814: _graph.target(stack[stack_head].first); kpeter@814: // Find the next admissible residual outgoing arc kpeter@814: double p = pi[v]; kpeter@814: Arc e = stack[stack_head].first; kpeter@814: if (!stack[stack_head].second) { kpeter@814: _graph.nextIn(e); kpeter@814: goto in_arc_4; kpeter@814: } kpeter@814: _graph.nextOut(e); kpeter@814: while ( e != INVALID && (_capacity[e] - (*_flow)[e] == 0 || kpeter@814: !tol.negative(_cost[e] + p - pi[_graph.target(e)])) ) kpeter@814: _graph.nextOut(e); kpeter@814: if (e != INVALID) { kpeter@814: stack[stack_head] = pair(e, true); kpeter@814: goto next_step_4; kpeter@814: } kpeter@814: _graph.firstIn(e, v); kpeter@814: in_arc_4: kpeter@814: while ( e != INVALID && ((*_flow)[e] == 0 || kpeter@814: !tol.negative(-_cost[e] + p - pi[_graph.source(e)])) ) kpeter@814: _graph.nextIn(e); kpeter@814: stack[stack_head] = pair(e, false); kpeter@814: next_step_4: ; kpeter@814: } kpeter@814: } kpeter@814: } kpeter@814: } kpeter@814: t1.stop(); kpeter@814: kpeter@814: // Tighten potentials and epsilon kpeter@814: if (--iter > 0) { kpeter@814: // Compute levels kpeter@814: t2.start(); kpeter@814: for (int i = proc_head; i >= 0; --i) { kpeter@814: Node v = proc_vector[i]; kpeter@814: double p = pi[v]; kpeter@814: int l = 0; kpeter@814: for (InArcIt e(_graph, v); e != INVALID; ++e) { kpeter@814: Node u = _graph.source(e); kpeter@814: if ( _capacity[e] - (*_flow)[e] > 0 && kpeter@814: tol.negative(_cost[e] + pi[u] - p) && kpeter@814: level[u] + 1 > l ) l = level[u] + 1; kpeter@814: } kpeter@814: for (OutArcIt e(_graph, v); e != INVALID; ++e) { kpeter@814: Node u = _graph.target(e); kpeter@814: if ( (*_flow)[e] > 0 && kpeter@814: tol.negative(-_cost[e] + pi[u] - p) && kpeter@814: level[u] + 1 > l ) l = level[u] + 1; kpeter@814: } kpeter@814: level[v] = l; kpeter@814: } kpeter@814: kpeter@814: // Modify potentials kpeter@814: double p, q = -1; kpeter@814: for (ArcIt e(_graph); e != INVALID; ++e) { kpeter@814: Node u = _graph.source(e); kpeter@814: Node v = _graph.target(e); kpeter@814: if (_capacity[e] - (*_flow)[e] > 0 && level[u] - level[v] > 0) { kpeter@814: p = (_cost[e] + pi[u] - pi[v] + epsilon) / kpeter@814: (level[u] - level[v] + 1); kpeter@814: if (q < 0 || p < q) q = p; kpeter@814: } kpeter@814: else if ((*_flow)[e] > 0 && level[v] - level[u] > 0) { kpeter@814: p = (-_cost[e] - pi[u] + pi[v] + epsilon) / kpeter@814: (level[v] - level[u] + 1); kpeter@814: if (q < 0 || p < q) q = p; kpeter@814: } kpeter@814: } kpeter@814: for (NodeIt v(_graph); v != INVALID; ++v) { kpeter@814: pi[v] -= q * level[v]; kpeter@814: } kpeter@814: kpeter@814: // Modify epsilon kpeter@814: epsilon = 0; kpeter@814: for (ArcIt e(_graph); e != INVALID; ++e) { kpeter@814: double curr = _cost[e] + pi[_graph.source(e)] kpeter@814: - pi[_graph.target(e)]; kpeter@814: if (_capacity[e] - (*_flow)[e] > 0 && curr < -epsilon) kpeter@814: epsilon = -curr; kpeter@814: else if ((*_flow)[e] > 0 && curr > epsilon) kpeter@814: epsilon = curr; kpeter@814: } kpeter@814: t2.stop(); kpeter@814: } else { kpeter@814: // Set epsilon to the minimum cycle mean kpeter@814: t3.start(); kpeter@814: kpeter@814: /**/ kpeter@814: StaticDigraph static_graph; kpeter@814: typename ResDigraph::template NodeMap node_ref(*_res_graph); kpeter@814: typename ResDigraph::template ArcMap arc_ref(*_res_graph); kpeter@814: static_graph.build(*_res_graph, node_ref, arc_ref); kpeter@814: typename StaticDigraph::template NodeMap static_pi(static_graph); kpeter@814: typename StaticDigraph::template ArcMap static_cost(static_graph); kpeter@814: kpeter@814: for (typename ResDigraph::ArcIt e(*_res_graph); e != INVALID; ++e) kpeter@814: static_cost[arc_ref[e]] = _res_cost[e]; kpeter@814: kpeter@814: Howard > kpeter@814: mmc(static_graph, static_cost); kpeter@814: mmc.findMinMean(); kpeter@814: epsilon = -mmc.cycleMean(); kpeter@814: /**/ kpeter@814: kpeter@814: /* kpeter@814: Howard mmc(*_res_graph, _res_cost); kpeter@814: mmc.findMinMean(); kpeter@814: epsilon = -mmc.cycleMean(); kpeter@814: */ kpeter@814: kpeter@814: // Compute feasible potentials for the current epsilon kpeter@814: for (typename StaticDigraph::ArcIt e(static_graph); e != INVALID; ++e) kpeter@814: static_cost[e] += epsilon; kpeter@814: typename BellmanFord >:: kpeter@814: template SetDistMap >:: kpeter@814: template SetOperationTraits::Create kpeter@814: bf(static_graph, static_cost); kpeter@814: bf.distMap(static_pi).init(0); kpeter@814: bf.start(); kpeter@814: for (NodeIt n(_graph); n != INVALID; ++n) kpeter@814: pi[n] = static_pi[node_ref[n]]; kpeter@814: kpeter@814: /* kpeter@814: for (typename ResDigraph::ArcIt e(*_res_graph); e != INVALID; ++e) kpeter@814: shift_cost[e] = _res_cost[e] + epsilon; kpeter@814: typename BellmanFord:: kpeter@814: template SetDistMap:: kpeter@814: template SetOperationTraits::Create kpeter@814: bf(*_res_graph, shift_cost); kpeter@814: bf.distMap(pi).init(0); kpeter@814: bf.start(); kpeter@814: */ kpeter@814: kpeter@814: iter = limit; kpeter@814: t3.stop(); kpeter@814: } kpeter@814: } kpeter@814: kpeter@814: // std::cout << t1.realTime() << " " << t2.realTime() << " " << t3.realTime() << "\n"; kpeter@814: kpeter@814: // Handle non-zero lower bounds kpeter@814: if (_lower) { kpeter@814: for (ArcIt e(_graph); e != INVALID; ++e) kpeter@814: (*_flow)[e] += (*_lower)[e]; kpeter@814: } kpeter@814: return true; kpeter@814: } kpeter@814: kpeter@814: }; //class CancelAndTighten kpeter@814: kpeter@814: ///@} kpeter@814: kpeter@814: } //namespace lemon kpeter@814: kpeter@814: #endif //LEMON_CANCEL_AND_TIGHTEN_H