1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2010 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CAPACITY_SCALING_H |
20 | 20 |
#define LEMON_CAPACITY_SCALING_H |
21 | 21 |
|
22 | 22 |
/// \ingroup min_cost_flow_algs |
23 | 23 |
/// |
24 | 24 |
/// \file |
25 | 25 |
/// \brief Capacity Scaling algorithm for finding a minimum cost flow. |
26 | 26 |
|
27 | 27 |
#include <vector> |
28 | 28 |
#include <limits> |
29 | 29 |
#include <lemon/core.h> |
30 | 30 |
#include <lemon/bin_heap.h> |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
/// \brief Default traits class of CapacityScaling algorithm. |
35 | 35 |
/// |
36 | 36 |
/// Default traits class of CapacityScaling algorithm. |
37 | 37 |
/// \tparam GR Digraph type. |
38 | 38 |
/// \tparam V The number type used for flow amounts, capacity bounds |
39 | 39 |
/// and supply values. By default it is \c int. |
40 | 40 |
/// \tparam C The number type used for costs and potentials. |
41 | 41 |
/// By default it is the same as \c V. |
42 | 42 |
template <typename GR, typename V = int, typename C = V> |
43 | 43 |
struct CapacityScalingDefaultTraits |
44 | 44 |
{ |
45 | 45 |
/// The type of the digraph |
46 | 46 |
typedef GR Digraph; |
47 | 47 |
/// The type of the flow amounts, capacity bounds and supply values |
48 | 48 |
typedef V Value; |
49 | 49 |
/// The type of the arc costs |
50 | 50 |
typedef C Cost; |
51 | 51 |
|
52 | 52 |
/// \brief The type of the heap used for internal Dijkstra computations. |
53 | 53 |
/// |
54 | 54 |
/// The type of the heap used for internal Dijkstra computations. |
55 | 55 |
/// It must conform to the \ref lemon::concepts::Heap "Heap" concept, |
56 | 56 |
/// its priority type must be \c Cost and its cross reference type |
57 | 57 |
/// must be \ref RangeMap "RangeMap<int>". |
58 | 58 |
typedef BinHeap<Cost, RangeMap<int> > Heap; |
59 | 59 |
}; |
60 | 60 |
|
61 | 61 |
/// \addtogroup min_cost_flow_algs |
62 | 62 |
/// @{ |
63 | 63 |
|
64 | 64 |
/// \brief Implementation of the Capacity Scaling algorithm for |
65 | 65 |
/// finding a \ref min_cost_flow "minimum cost flow". |
66 | 66 |
/// |
67 | 67 |
/// \ref CapacityScaling implements the capacity scaling version |
68 | 68 |
/// of the successive shortest path algorithm for finding a |
69 | 69 |
/// \ref min_cost_flow "minimum cost flow" \ref amo93networkflows, |
70 | 70 |
/// \ref edmondskarp72theoretical. It is an efficient dual |
71 | 71 |
/// solution method. |
72 | 72 |
/// |
73 | 73 |
/// Most of the parameters of the problem (except for the digraph) |
74 | 74 |
/// can be given using separate functions, and the algorithm can be |
75 | 75 |
/// executed using the \ref run() function. If some parameters are not |
76 | 76 |
/// specified, then default values will be used. |
77 | 77 |
/// |
78 | 78 |
/// \tparam GR The digraph type the algorithm runs on. |
79 | 79 |
/// \tparam V The number type used for flow amounts, capacity bounds |
80 | 80 |
/// and supply values in the algorithm. By default, it is \c int. |
81 | 81 |
/// \tparam C The number type used for costs and potentials in the |
82 | 82 |
/// algorithm. By default, it is the same as \c V. |
83 | 83 |
/// \tparam TR The traits class that defines various types used by the |
84 | 84 |
/// algorithm. By default, it is \ref CapacityScalingDefaultTraits |
85 | 85 |
/// "CapacityScalingDefaultTraits<GR, V, C>". |
86 | 86 |
/// In most cases, this parameter should not be set directly, |
87 | 87 |
/// consider to use the named template parameters instead. |
88 | 88 |
/// |
89 | 89 |
/// \warning Both \c V and \c C must be signed number types. |
90 |
/// \warning All input data (capacities, supply values, and costs) must |
|
91 |
/// be integer. |
|
90 |
/// \warning Capacity bounds and supply values must be integer, but |
|
91 |
/// arc costs can be arbitrary real numbers. |
|
92 | 92 |
/// \warning This algorithm does not support negative costs for |
93 | 93 |
/// arcs having infinite upper bound. |
94 | 94 |
#ifdef DOXYGEN |
95 | 95 |
template <typename GR, typename V, typename C, typename TR> |
96 | 96 |
#else |
97 | 97 |
template < typename GR, typename V = int, typename C = V, |
98 | 98 |
typename TR = CapacityScalingDefaultTraits<GR, V, C> > |
99 | 99 |
#endif |
100 | 100 |
class CapacityScaling |
101 | 101 |
{ |
102 | 102 |
public: |
103 | 103 |
|
104 | 104 |
/// The type of the digraph |
105 | 105 |
typedef typename TR::Digraph Digraph; |
106 | 106 |
/// The type of the flow amounts, capacity bounds and supply values |
107 | 107 |
typedef typename TR::Value Value; |
108 | 108 |
/// The type of the arc costs |
109 | 109 |
typedef typename TR::Cost Cost; |
110 | 110 |
|
111 | 111 |
/// The type of the heap used for internal Dijkstra computations |
112 | 112 |
typedef typename TR::Heap Heap; |
113 | 113 |
|
114 | 114 |
/// The \ref CapacityScalingDefaultTraits "traits class" of the algorithm |
115 | 115 |
typedef TR Traits; |
116 | 116 |
|
117 | 117 |
public: |
118 | 118 |
|
119 | 119 |
/// \brief Problem type constants for the \c run() function. |
120 | 120 |
/// |
121 | 121 |
/// Enum type containing the problem type constants that can be |
122 | 122 |
/// returned by the \ref run() function of the algorithm. |
123 | 123 |
enum ProblemType { |
124 | 124 |
/// The problem has no feasible solution (flow). |
125 | 125 |
INFEASIBLE, |
126 | 126 |
/// The problem has optimal solution (i.e. it is feasible and |
127 | 127 |
/// bounded), and the algorithm has found optimal flow and node |
128 | 128 |
/// potentials (primal and dual solutions). |
129 | 129 |
OPTIMAL, |
130 | 130 |
/// The digraph contains an arc of negative cost and infinite |
131 | 131 |
/// upper bound. It means that the objective function is unbounded |
132 | 132 |
/// on that arc, however, note that it could actually be bounded |
133 | 133 |
/// over the feasible flows, but this algroithm cannot handle |
134 | 134 |
/// these cases. |
135 | 135 |
UNBOUNDED |
136 | 136 |
}; |
137 | 137 |
|
138 | 138 |
private: |
139 | 139 |
|
140 | 140 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
141 | 141 |
|
142 | 142 |
typedef std::vector<int> IntVector; |
143 | 143 |
typedef std::vector<Value> ValueVector; |
144 | 144 |
typedef std::vector<Cost> CostVector; |
145 | 145 |
typedef std::vector<char> BoolVector; |
146 | 146 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
147 | 147 |
|
148 | 148 |
private: |
149 | 149 |
|
150 | 150 |
// Data related to the underlying digraph |
151 | 151 |
const GR &_graph; |
152 | 152 |
int _node_num; |
153 | 153 |
int _arc_num; |
154 | 154 |
int _res_arc_num; |
155 | 155 |
int _root; |
156 | 156 |
|
157 | 157 |
// Parameters of the problem |
158 | 158 |
bool _have_lower; |
159 | 159 |
Value _sum_supply; |
160 | 160 |
|
161 | 161 |
// Data structures for storing the digraph |
162 | 162 |
IntNodeMap _node_id; |
163 | 163 |
IntArcMap _arc_idf; |
164 | 164 |
IntArcMap _arc_idb; |
165 | 165 |
IntVector _first_out; |
166 | 166 |
BoolVector _forward; |
167 | 167 |
IntVector _source; |
168 | 168 |
IntVector _target; |
169 | 169 |
IntVector _reverse; |
170 | 170 |
|
171 | 171 |
// Node and arc data |
172 | 172 |
ValueVector _lower; |
173 | 173 |
ValueVector _upper; |
174 | 174 |
CostVector _cost; |
175 | 175 |
ValueVector _supply; |
176 | 176 |
|
177 | 177 |
ValueVector _res_cap; |
178 | 178 |
CostVector _pi; |
179 | 179 |
ValueVector _excess; |
180 | 180 |
IntVector _excess_nodes; |
181 | 181 |
IntVector _deficit_nodes; |
182 | 182 |
|
183 | 183 |
Value _delta; |
184 | 184 |
int _factor; |
185 | 185 |
IntVector _pred; |
186 | 186 |
|
187 | 187 |
public: |
0 comments (0 inline)