[Lemon-commits] Peter Kovacs: Separate types for flow and cost v...
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/9ad8d2122b50
changeset: 639:9ad8d2122b50
user: Peter Kovacs <kpeter [at] inf.elte.hu>
date: Fri Apr 03 13:46:16 2009 +0200
description:
Separate types for flow and cost values in NetworkSimplex (#234)
diffstat:
lemon/network_simplex.h | 146 +++++++++++++++++++++++++-----------------------
test/min_cost_flow_test.cc | 24 ++++---
2 files changed, 90 insertions(+), 80 deletions(-)
diffs (truncated from 513 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
@@ -49,24 +49,28 @@
/// in LEMON for the minimum cost flow problem.
///
/// \tparam GR The digraph type the algorithm runs on.
- /// \tparam V The value type used in the algorithm.
- /// By default it is \c int.
+ /// \tparam F The value type used for flow amounts, capacity bounds
+ /// and supply values in the algorithm. By default it is \c int.
+ /// \tparam C The value type used for costs and potentials in the
+ /// algorithm. By default it is the same as \c F.
///
- /// \warning The value type must be a signed integer type.
+ /// \warning Both value types must be signed integer types.
///
/// \note %NetworkSimplex provides five different pivot rule
/// implementations. For more information see \ref PivotRule.
- template <typename GR, typename V = int>
+ template <typename GR, typename F = int, typename C = F>
class NetworkSimplex
{
public:
- /// The value type of the algorithm
- typedef V Value;
+ /// The flow type of the algorithm
+ typedef F Flow;
+ /// The cost type of the algorithm
+ typedef C Cost;
/// The type of the flow map
- typedef typename GR::template ArcMap<Value> FlowMap;
+ typedef typename GR::template ArcMap<Flow> FlowMap;
/// The type of the potential map
- typedef typename GR::template NodeMap<Value> PotentialMap;
+ typedef typename GR::template NodeMap<Cost> PotentialMap;
public:
@@ -117,14 +121,16 @@
TEMPLATE_DIGRAPH_TYPEDEFS(GR);
- typedef typename GR::template ArcMap<Value> ValueArcMap;
- typedef typename GR::template NodeMap<Value> ValueNodeMap;
+ typedef typename GR::template ArcMap<Flow> FlowArcMap;
+ typedef typename GR::template ArcMap<Cost> CostArcMap;
+ typedef typename GR::template NodeMap<Flow> FlowNodeMap;
typedef std::vector<Arc> ArcVector;
typedef std::vector<Node> NodeVector;
typedef std::vector<int> IntVector;
typedef std::vector<bool> BoolVector;
- typedef std::vector<Value> ValueVector;
+ typedef std::vector<Flow> FlowVector;
+ typedef std::vector<Cost> CostVector;
// State constants for arcs
enum ArcStateEnum {
@@ -141,13 +147,13 @@
int _arc_num;
// Parameters of the problem
- ValueArcMap *_plower;
- ValueArcMap *_pupper;
- ValueArcMap *_pcost;
- ValueNodeMap *_psupply;
+ FlowArcMap *_plower;
+ FlowArcMap *_pupper;
+ CostArcMap *_pcost;
+ FlowNodeMap *_psupply;
bool _pstsup;
Node _psource, _ptarget;
- Value _pstflow;
+ Flow _pstflow;
// Result maps
FlowMap *_flow_map;
@@ -162,11 +168,11 @@
IntVector _target;
// Node and arc data
- ValueVector _cap;
- ValueVector _cost;
- ValueVector _supply;
- ValueVector _flow;
- ValueVector _pi;
+ FlowVector _cap;
+ CostVector _cost;
+ FlowVector _supply;
+ FlowVector _flow;
+ CostVector _pi;
// Data for storing the spanning tree structure
IntVector _parent;
@@ -184,7 +190,7 @@
int in_arc, join, u_in, v_in, u_out, v_out;
int first, second, right, last;
int stem, par_stem, new_stem;
- Value delta;
+ Flow delta;
private:
@@ -196,9 +202,9 @@
// References to the NetworkSimplex class
const IntVector &_source;
const IntVector &_target;
- const ValueVector &_cost;
+ const CostVector &_cost;
const IntVector &_state;
- const ValueVector &_pi;
+ const CostVector &_pi;
int &_in_arc;
int _arc_num;
@@ -216,7 +222,7 @@
// Find next entering arc
bool findEnteringArc() {
- Value c;
+ Cost c;
for (int e = _next_arc; e < _arc_num; ++e) {
c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
if (c < 0) {
@@ -247,9 +253,9 @@
// References to the NetworkSimplex class
const IntVector &_source;
const IntVector &_target;
- const ValueVector &_cost;
+ const CostVector &_cost;
const IntVector &_state;
- const ValueVector &_pi;
+ const CostVector &_pi;
int &_in_arc;
int _arc_num;
@@ -264,7 +270,7 @@
// Find next entering arc
bool findEnteringArc() {
- Value c, min = 0;
+ Cost c, min = 0;
for (int e = 0; e < _arc_num; ++e) {
c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]);
if (c < min) {
@@ -286,9 +292,9 @@
// References to the NetworkSimplex class
const IntVector &_source;
const IntVector &_target;
- const ValueVector &_cost;
+ const CostVector &_cost;
const IntVector &_state;
- const ValueVector &_pi;
+ const CostVector &_pi;
int &_in_arc;
int _arc_num;
@@ -314,7 +320,7 @@
// Find next entering arc
bool findEnteringArc() {
- Value c, min = 0;
+ Cost c, min = 0;
int cnt = _block_size;
int e, min_arc = _next_arc;
for (e = _next_arc; e < _arc_num; ++e) {
@@ -358,9 +364,9 @@
// References to the NetworkSimplex class
const IntVector &_source;
const IntVector &_target;
- const ValueVector &_cost;
+ const CostVector &_cost;
const IntVector &_state;
- const ValueVector &_pi;
+ const CostVector &_pi;
int &_in_arc;
int _arc_num;
@@ -394,7 +400,7 @@
/// Find next entering arc
bool findEnteringArc() {
- Value min, c;
+ Cost min, c;
int e, min_arc = _next_arc;
if (_curr_length > 0 && _minor_count < _minor_limit) {
// Minor iteration: select the best eligible arc from the
@@ -463,9 +469,9 @@
// References to the NetworkSimplex class
const IntVector &_source;
const IntVector &_target;
- const ValueVector &_cost;
+ const CostVector &_cost;
const IntVector &_state;
- const ValueVector &_pi;
+ const CostVector &_pi;
int &_in_arc;
int _arc_num;
@@ -473,15 +479,15 @@
int _block_size, _head_length, _curr_length;
int _next_arc;
IntVector _candidates;
- ValueVector _cand_cost;
+ CostVector _cand_cost;
// Functor class to compare arcs during sort of the candidate list
class SortFunc
{
private:
- const ValueVector &_map;
+ const CostVector &_map;
public:
- SortFunc(const ValueVector &map) : _map(map) {}
+ SortFunc(const CostVector &map) : _map(map) {}
bool operator()(int left, int right) {
return _map[left] > _map[right];
}
@@ -590,9 +596,12 @@
_local_flow(false), _local_potential(false),
_node_id(graph)
{
- LEMON_ASSERT(std::numeric_limits<Value>::is_integer &&
- std::numeric_limits<Value>::is_signed,
- "The value type of NetworkSimplex must be a signed integer");
+ LEMON_ASSERT(std::numeric_limits<Flow>::is_integer &&
+ std::numeric_limits<Flow>::is_signed,
+ "The flow type of NetworkSimplex must be signed integer");
+ LEMON_ASSERT(std::numeric_limits<Cost>::is_integer &&
+ std::numeric_limits<Cost>::is_signed,
+ "The cost type of NetworkSimplex must be signed integer");
}
/// Destructor.
@@ -609,14 +618,14 @@
/// on all arcs.
///
/// \param map An arc map storing the lower bounds.
- /// Its \c Value type must be convertible to the \c Value type
+ /// Its \c Value type must be convertible to the \c Flow type
/// of the algorithm.
///
/// \return <tt>(*this)</tt>
template <typename LOWER>
NetworkSimplex& lowerMap(const LOWER& map) {
delete _plower;
- _plower = new ValueArcMap(_graph);
+ _plower = new FlowArcMap(_graph);
for (ArcIt a(_graph); a != INVALID; ++a) {
(*_plower)[a] = map[a];
}
@@ -629,17 +638,17 @@
/// If none of the functions \ref upperMap(), \ref capacityMap()
/// and \ref boundMaps() is used before calling \ref run(),
/// the upper bounds (capacities) will be set to
- /// \c std::numeric_limits<Value>::max() on all arcs.
+ /// \c std::numeric_limits<Flow>::max() on all arcs.
///
/// \param map An arc map storing the upper bounds.
- /// Its \c Value type must be convertible to the \c Value type
+ /// Its \c Value type must be convertible to the \c Flow type
/// of the algorithm.
///
/// \return <tt>(*this)</tt>
template<typename UPPER>
NetworkSimplex& upperMap(const UPPER& map) {
delete _pupper;
- _pupper = new ValueArcMap(_graph);
+ _pupper = new FlowArcMap(_graph);
for (ArcIt a(_graph); a != INVALID; ++a) {
(*_pupper)[a] = map[a];
}
@@ -666,13 +675,13 @@
/// If none of the functions \ref upperMap(), \ref capacityMap()
/// and \ref boundMaps() is used before calling \ref run(),
/// the upper bounds (capacities) will be set to
- /// \c std::numeric_limits<Value>::max() on all arcs.
+ /// \c std::numeric_limits<Flow>::max() on all arcs.
///
/// \param lower An arc map storing the lower bounds.
/// \param upper An arc map storing the upper bounds.
///
/// The \c Value type of the maps must be convertible to the
- /// \c Value type of the algorithm.
+ /// \c Flow type of the algorithm.
///
/// \note This function is just a shortcut of calling \ref lowerMap()
/// and \ref upperMap() separately.
@@ -690,14 +699,14 @@
/// will be set to \c 1 on all arcs.
///
/// \param map An arc map storing the costs.
- /// Its \c Value type must be convertible to the \c Value type
+ /// Its \c Value type must be convertible to the \c Cost type
/// of the algorithm.
///
/// \return <tt>(*this)</tt>
template<typename COST>
More information about the Lemon-commits
mailing list