[Lemon-commits] Peter Kovacs: Rework the interface of NetworkSim...
Lemon HG
hg at lemon.cs.elte.hu
Tue Apr 21 16:33:31 CEST 2009
details: http://lemon.cs.elte.hu/hg/lemon/rev/5232721b3f14
changeset: 637:5232721b3f14
user: Peter Kovacs <kpeter [at] inf.elte.hu>
date: Wed Mar 25 15:58:44 2009 +0100
description:
Rework the interface of NetworkSimplex (#234)
The parameters of the problem can be set with separate functions
instead of different constructors.
diffstat:
lemon/network_simplex.h | 746 ++++++++++++++++++++++++++--------------------
test/min_cost_flow_test.cc | 296 ++++--------------
tools/dimacs-solver.cc | 5 +-
3 files changed, 489 insertions(+), 558 deletions(-)
diffs (truncated from 1496 to 300 lines):
diff --git a/lemon/network_simplex.h b/lemon/network_simplex.h
--- a/lemon/network_simplex.h
+++ b/lemon/network_simplex.h
@@ -22,7 +22,7 @@
/// \ingroup min_cost_flow
///
/// \file
-/// \brief Network simplex algorithm for finding a minimum cost flow.
+/// \brief Network Simplex algorithm for finding a minimum cost flow.
#include <vector>
#include <limits>
@@ -36,80 +36,89 @@
/// \addtogroup min_cost_flow
/// @{
- /// \brief Implementation of the primal network simplex algorithm
+ /// \brief Implementation of the primal Network Simplex algorithm
/// for finding a \ref min_cost_flow "minimum cost flow".
///
- /// \ref NetworkSimplex implements the primal network simplex algorithm
+ /// \ref NetworkSimplex implements the primal Network Simplex algorithm
/// for finding a \ref min_cost_flow "minimum cost flow".
///
- /// \tparam Digraph The digraph type the algorithm runs on.
- /// \tparam LowerMap The type of the lower bound map.
- /// \tparam CapacityMap The type of the capacity (upper bound) map.
- /// \tparam CostMap The type of the cost (length) map.
- /// \tparam SupplyMap The type of the supply map.
+ /// \tparam GR The digraph type the algorithm runs on.
+ /// \tparam V The value type used in the algorithm.
+ /// By default it is \c int.
///
- /// \warning
- /// - Arc capacities and costs should be \e non-negative \e integers.
- /// - Supply values should be \e signed \e integers.
- /// - The value types of the maps should be convertible to each other.
- /// - \c CostMap::Value must be signed type.
+ /// \warning \c V must be a signed integer type.
///
- /// \note \ref NetworkSimplex provides five different pivot rule
- /// implementations that significantly affect the efficiency of the
- /// algorithm.
- /// By default "Block Search" pivot rule is used, which proved to be
- /// by far the most efficient according to our benchmark tests.
- /// However another pivot rule can be selected using \ref run()
- /// function with the proper parameter.
-#ifdef DOXYGEN
- template < typename Digraph,
- typename LowerMap,
- typename CapacityMap,
- typename CostMap,
- typename SupplyMap >
-
-#else
- template < typename Digraph,
- typename LowerMap = typename Digraph::template ArcMap<int>,
- typename CapacityMap = typename Digraph::template ArcMap<int>,
- typename CostMap = typename Digraph::template ArcMap<int>,
- typename SupplyMap = typename Digraph::template NodeMap<int> >
-#endif
+ /// \note %NetworkSimplex provides five different pivot rule
+ /// implementations. For more information see \ref PivotRule.
+ template <typename GR, typename V = int>
class NetworkSimplex
{
- TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
+ public:
- typedef typename CapacityMap::Value Capacity;
- typedef typename CostMap::Value Cost;
- typedef typename SupplyMap::Value Supply;
+ /// The value type of the algorithm
+ typedef V Value;
+ /// The type of the flow map
+ typedef typename GR::template ArcMap<Value> FlowMap;
+ /// The type of the potential map
+ typedef typename GR::template NodeMap<Value> PotentialMap;
+
+ public:
+
+ /// \brief Enum type for selecting the pivot rule.
+ ///
+ /// Enum type for selecting the pivot rule for the \ref run()
+ /// function.
+ ///
+ /// \ref NetworkSimplex provides five different pivot rule
+ /// implementations that significantly affect the running time
+ /// of the algorithm.
+ /// By default \ref BLOCK_SEARCH "Block Search" is used, which
+ /// proved to be the most efficient and the most robust on various
+ /// test inputs according to our benchmark tests.
+ /// However another pivot rule can be selected using the \ref run()
+ /// function with the proper parameter.
+ enum PivotRule {
+
+ /// The First Eligible pivot rule.
+ /// The next eligible arc is selected in a wraparound fashion
+ /// in every iteration.
+ FIRST_ELIGIBLE,
+
+ /// The Best Eligible pivot rule.
+ /// The best eligible arc is selected in every iteration.
+ BEST_ELIGIBLE,
+
+ /// The Block Search pivot rule.
+ /// A specified number of arcs are examined in every iteration
+ /// in a wraparound fashion and the best eligible arc is selected
+ /// from this block.
+ BLOCK_SEARCH,
+
+ /// The Candidate List pivot rule.
+ /// In a major iteration a candidate list is built from eligible arcs
+ /// in a wraparound fashion and in the following minor iterations
+ /// the best eligible arc is selected from this list.
+ CANDIDATE_LIST,
+
+ /// The Altering Candidate List pivot rule.
+ /// It is a modified version of the Candidate List method.
+ /// It keeps only the several best eligible arcs from the former
+ /// candidate list and extends this list in every iteration.
+ ALTERING_LIST
+ };
+
+ private:
+
+ TEMPLATE_DIGRAPH_TYPEDEFS(GR);
+
+ typedef typename GR::template ArcMap<Value> ValueArcMap;
+ typedef typename GR::template NodeMap<Value> ValueNodeMap;
typedef std::vector<Arc> ArcVector;
typedef std::vector<Node> NodeVector;
typedef std::vector<int> IntVector;
typedef std::vector<bool> BoolVector;
- typedef std::vector<Capacity> CapacityVector;
- typedef std::vector<Cost> CostVector;
- typedef std::vector<Supply> SupplyVector;
-
- public:
-
- /// The type of the flow map
- typedef typename Digraph::template ArcMap<Capacity> FlowMap;
- /// The type of the potential map
- typedef typename Digraph::template NodeMap<Cost> PotentialMap;
-
- public:
-
- /// Enum type for selecting the pivot rule used by \ref run()
- enum PivotRuleEnum {
- FIRST_ELIGIBLE_PIVOT,
- BEST_ELIGIBLE_PIVOT,
- BLOCK_SEARCH_PIVOT,
- CANDIDATE_LIST_PIVOT,
- ALTERING_LIST_PIVOT
- };
-
- private:
+ typedef std::vector<Value> ValueVector;
// State constants for arcs
enum ArcStateEnum {
@@ -120,15 +129,19 @@
private:
- // References for the original data
- const Digraph &_graph;
- const LowerMap *_orig_lower;
- const CapacityMap &_orig_cap;
- const CostMap &_orig_cost;
- const SupplyMap *_orig_supply;
- Node _orig_source;
- Node _orig_target;
- Capacity _orig_flow_value;
+ // Data related to the underlying digraph
+ const GR &_graph;
+ int _node_num;
+ int _arc_num;
+
+ // Parameters of the problem
+ ValueArcMap *_plower;
+ ValueArcMap *_pupper;
+ ValueArcMap *_pcost;
+ ValueNodeMap *_psupply;
+ bool _pstsup;
+ Node _psource, _ptarget;
+ Value _pstflow;
// Result maps
FlowMap *_flow_map;
@@ -136,22 +149,18 @@
bool _local_flow;
bool _local_potential;
- // The number of nodes and arcs in the original graph
- int _node_num;
- int _arc_num;
-
- // Data structures for storing the graph
+ // Data structures for storing the digraph
IntNodeMap _node_id;
ArcVector _arc_ref;
IntVector _source;
IntVector _target;
- // Node and arc maps
- CapacityVector _cap;
- CostVector _cost;
- CostVector _supply;
- CapacityVector _flow;
- CostVector _pi;
+ // Node and arc data
+ ValueVector _cap;
+ ValueVector _cost;
+ ValueVector _supply;
+ ValueVector _flow;
+ ValueVector _pi;
// Data for storing the spanning tree structure
IntVector _parent;
@@ -169,17 +178,11 @@
int in_arc, join, u_in, v_in, u_out, v_out;
int first, second, right, last;
int stem, par_stem, new_stem;
- Capacity delta;
+ Value delta;
private:
- /// \brief Implementation of the "First Eligible" pivot rule for the
- /// \ref NetworkSimplex "network simplex" algorithm.
- ///
- /// This class implements the "First Eligible" pivot rule
- /// for the \ref NetworkSimplex "network simplex" algorithm.
- ///
- /// For more information see \ref NetworkSimplex::run().
+ // Implementation of the First Eligible pivot rule
class FirstEligiblePivotRule
{
private:
@@ -187,9 +190,9 @@
// References to the NetworkSimplex class
const IntVector &_source;
const IntVector &_target;
- const CostVector &_cost;
+ const ValueVector &_cost;
const IntVector &_state;
- const CostVector &_pi;
+ const ValueVector &_pi;
int &_in_arc;
int _arc_num;
@@ -198,16 +201,16 @@
public:
- /// Constructor
+ // Constructor
FirstEligiblePivotRule(NetworkSimplex &ns) :
_source(ns._source), _target(ns._target),
_cost(ns._cost), _state(ns._state), _pi(ns._pi),
_in_arc(ns.in_arc), _arc_num(ns._arc_num), _next_arc(0)
{}
- /// Find next entering arc
+ // Find next entering arc
bool findEnteringArc() {
- Cost c;
+ Value c;
for (int e = _next_arc; e < _arc_num; ++e) {
c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
if (c < 0) {
@@ -230,13 +233,7 @@
}; //class FirstEligiblePivotRule
- /// \brief Implementation of the "Best Eligible" pivot rule for the
- /// \ref NetworkSimplex "network simplex" algorithm.
- ///
- /// This class implements the "Best Eligible" pivot rule
- /// for the \ref NetworkSimplex "network simplex" algorithm.
- ///
- /// For more information see \ref NetworkSimplex::run().
+ // Implementation of the Best Eligible pivot rule
class BestEligiblePivotRule
{
private:
@@ -244,24 +241,24 @@
// References to the NetworkSimplex class
const IntVector &_source;
const IntVector &_target;
- const CostVector &_cost;
+ const ValueVector &_cost;
const IntVector &_state;
- const CostVector &_pi;
+ const ValueVector &_pi;
int &_in_arc;
int _arc_num;
More information about the Lemon-commits
mailing list