gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Fix the doc in CapacityScaling: cost can be real numbers (#261)
0 1 0
default
1 file changed with 2 insertions and 2 deletions:
↑ Collapse diff ↑
Ignore white space 512 line context
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:
188 188

	
189 189
    /// \brief Constant for infinite upper bounds (capacities).
190 190
    ///
191 191
    /// Constant for infinite upper bounds (capacities).
192 192
    /// It is \c std::numeric_limits<Value>::infinity() if available,
193 193
    /// \c std::numeric_limits<Value>::max() otherwise.
194 194
    const Value INF;
195 195

	
196 196
  private:
197 197

	
198 198
    // Special implementation of the Dijkstra algorithm for finding
199 199
    // shortest paths in the residual network of the digraph with
200 200
    // respect to the reduced arc costs and modifying the node
201 201
    // potentials according to the found distance labels.
202 202
    class ResidualDijkstra
203 203
    {
204 204
    private:
205 205

	
206 206
      int _node_num;
207 207
      bool _geq;
208 208
      const IntVector &_first_out;
209 209
      const IntVector &_target;
210 210
      const CostVector &_cost;
211 211
      const ValueVector &_res_cap;
212 212
      const ValueVector &_excess;
213 213
      CostVector &_pi;
214 214
      IntVector &_pred;
215 215

	
216 216
      IntVector _proc_nodes;
217 217
      CostVector _dist;
218 218

	
219 219
    public:
220 220

	
221 221
      ResidualDijkstra(CapacityScaling& cs) :
222 222
        _node_num(cs._node_num), _geq(cs._sum_supply < 0),
223 223
        _first_out(cs._first_out), _target(cs._target), _cost(cs._cost),
224 224
        _res_cap(cs._res_cap), _excess(cs._excess), _pi(cs._pi),
225 225
        _pred(cs._pred), _dist(cs._node_num)
226 226
      {}
227 227

	
228 228
      int run(int s, Value delta = 1) {
229 229
        RangeMap<int> heap_cross_ref(_node_num, Heap::PRE_HEAP);
230 230
        Heap heap(heap_cross_ref);
231 231
        heap.push(s, 0);
232 232
        _pred[s] = -1;
233 233
        _proc_nodes.clear();
234 234

	
235 235
        // Process nodes
236 236
        while (!heap.empty() && _excess[heap.top()] > -delta) {
237 237
          int u = heap.top(), v;
238 238
          Cost d = heap.prio() + _pi[u], dn;
239 239
          _dist[u] = heap.prio();
240 240
          _proc_nodes.push_back(u);
241 241
          heap.pop();
242 242

	
243 243
          // Traverse outgoing residual arcs
244 244
          int last_out = _geq ? _first_out[u+1] : _first_out[u+1] - 1;
245 245
          for (int a = _first_out[u]; a != last_out; ++a) {
246 246
            if (_res_cap[a] < delta) continue;
247 247
            v = _target[a];
248 248
            switch (heap.state(v)) {
249 249
              case Heap::PRE_HEAP:
250 250
                heap.push(v, d + _cost[a] - _pi[v]);
251 251
                _pred[v] = a;
252 252
                break;
253 253
              case Heap::IN_HEAP:
254 254
                dn = d + _cost[a] - _pi[v];
255 255
                if (dn < heap[v]) {
256 256
                  heap.decrease(v, dn);
257 257
                  _pred[v] = a;
258 258
                }
259 259
                break;
260 260
              case Heap::POST_HEAP:
261 261
                break;
262 262
            }
263 263
          }
264 264
        }
265 265
        if (heap.empty()) return -1;
266 266

	
267 267
        // Update potentials of processed nodes
268 268
        int t = heap.top();
269 269
        Cost dt = heap.prio();
270 270
        for (int i = 0; i < int(_proc_nodes.size()); ++i) {
271 271
          _pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - dt;
272 272
        }
273 273

	
274 274
        return t;
275 275
      }
276 276

	
277 277
    }; //class ResidualDijkstra
278 278

	
279 279
  public:
280 280

	
281 281
    /// \name Named Template Parameters
282 282
    /// @{
283 283

	
284 284
    template <typename T>
285 285
    struct SetHeapTraits : public Traits {
286 286
      typedef T Heap;
287 287
    };
288 288

	
289 289
    /// \brief \ref named-templ-param "Named parameter" for setting
290 290
    /// \c Heap type.
291 291
    ///
292 292
    /// \ref named-templ-param "Named parameter" for setting \c Heap
293 293
    /// type, which is used for internal Dijkstra computations.
294 294
    /// It must conform to the \ref lemon::concepts::Heap "Heap" concept,
295 295
    /// its priority type must be \c Cost and its cross reference type
296 296
    /// must be \ref RangeMap "RangeMap<int>".
297 297
    template <typename T>
298 298
    struct SetHeap
299 299
      : public CapacityScaling<GR, V, C, SetHeapTraits<T> > {
300 300
      typedef  CapacityScaling<GR, V, C, SetHeapTraits<T> > Create;
301 301
    };
302 302

	
303 303
    /// @}
304 304

	
305 305
  protected:
306 306

	
307 307
    CapacityScaling() {}
308 308

	
309 309
  public:
310 310

	
311 311
    /// \brief Constructor.
312 312
    ///
313 313
    /// The constructor of the class.
314 314
    ///
315 315
    /// \param graph The digraph the algorithm runs on.
316 316
    CapacityScaling(const GR& graph) :
317 317
      _graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph),
318 318
      INF(std::numeric_limits<Value>::has_infinity ?
319 319
          std::numeric_limits<Value>::infinity() :
320 320
          std::numeric_limits<Value>::max())
321 321
    {
322 322
      // Check the number types
323 323
      LEMON_ASSERT(std::numeric_limits<Value>::is_signed,
324 324
        "The flow type of CapacityScaling must be signed");
325 325
      LEMON_ASSERT(std::numeric_limits<Cost>::is_signed,
326 326
        "The cost type of CapacityScaling must be signed");
327 327

	
328 328
      // Reset data structures
329 329
      reset();
330 330
    }
331 331

	
332 332
    /// \name Parameters
333 333
    /// The parameters of the algorithm can be specified using these
334 334
    /// functions.
335 335

	
336 336
    /// @{
337 337

	
338 338
    /// \brief Set the lower bounds on the arcs.
339 339
    ///
340 340
    /// This function sets the lower bounds on the arcs.
341 341
    /// If it is not used before calling \ref run(), the lower bounds
342 342
    /// will be set to zero on all arcs.
343 343
    ///
344 344
    /// \param map An arc map storing the lower bounds.
345 345
    /// Its \c Value type must be convertible to the \c Value type
346 346
    /// of the algorithm.
347 347
    ///
0 comments (0 inline)