alpar@956
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
kpeter@805
|
2 |
*
|
alpar@956
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
kpeter@805
|
4 |
*
|
alpar@956
|
5 |
* Copyright (C) 2003-2010
|
kpeter@805
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
kpeter@805
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
kpeter@805
|
8 |
*
|
kpeter@805
|
9 |
* Permission to use, modify and distribute this software is granted
|
kpeter@805
|
10 |
* provided that this copyright notice appears in all copies. For
|
kpeter@805
|
11 |
* precise terms see the accompanying LICENSE file.
|
kpeter@805
|
12 |
*
|
kpeter@805
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
kpeter@805
|
14 |
* express or implied, and with no claim as to its suitability for any
|
kpeter@805
|
15 |
* purpose.
|
kpeter@805
|
16 |
*
|
kpeter@805
|
17 |
*/
|
kpeter@805
|
18 |
|
kpeter@942
|
19 |
#ifndef LEMON_HOWARD_MMC_H
|
kpeter@942
|
20 |
#define LEMON_HOWARD_MMC_H
|
kpeter@805
|
21 |
|
kpeter@815
|
22 |
/// \ingroup min_mean_cycle
|
kpeter@805
|
23 |
///
|
kpeter@805
|
24 |
/// \file
|
kpeter@805
|
25 |
/// \brief Howard's algorithm for finding a minimum mean cycle.
|
kpeter@805
|
26 |
|
kpeter@805
|
27 |
#include <vector>
|
kpeter@810
|
28 |
#include <limits>
|
kpeter@805
|
29 |
#include <lemon/core.h>
|
kpeter@805
|
30 |
#include <lemon/path.h>
|
kpeter@805
|
31 |
#include <lemon/tolerance.h>
|
kpeter@805
|
32 |
#include <lemon/connectivity.h>
|
kpeter@805
|
33 |
|
kpeter@805
|
34 |
namespace lemon {
|
kpeter@805
|
35 |
|
kpeter@942
|
36 |
/// \brief Default traits class of HowardMmc class.
|
kpeter@808
|
37 |
///
|
kpeter@942
|
38 |
/// Default traits class of HowardMmc class.
|
kpeter@808
|
39 |
/// \tparam GR The type of the digraph.
|
kpeter@942
|
40 |
/// \tparam CM The type of the cost map.
|
kpeter@808
|
41 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
|
kpeter@808
|
42 |
#ifdef DOXYGEN
|
kpeter@942
|
43 |
template <typename GR, typename CM>
|
kpeter@808
|
44 |
#else
|
kpeter@942
|
45 |
template <typename GR, typename CM,
|
kpeter@942
|
46 |
bool integer = std::numeric_limits<typename CM::Value>::is_integer>
|
kpeter@808
|
47 |
#endif
|
kpeter@942
|
48 |
struct HowardMmcDefaultTraits
|
kpeter@808
|
49 |
{
|
kpeter@808
|
50 |
/// The type of the digraph
|
kpeter@808
|
51 |
typedef GR Digraph;
|
kpeter@942
|
52 |
/// The type of the cost map
|
kpeter@942
|
53 |
typedef CM CostMap;
|
kpeter@942
|
54 |
/// The type of the arc costs
|
kpeter@942
|
55 |
typedef typename CostMap::Value Cost;
|
kpeter@808
|
56 |
|
kpeter@942
|
57 |
/// \brief The large cost type used for internal computations
|
kpeter@808
|
58 |
///
|
kpeter@942
|
59 |
/// The large cost type used for internal computations.
|
kpeter@942
|
60 |
/// It is \c long \c long if the \c Cost type is integer,
|
kpeter@808
|
61 |
/// otherwise it is \c double.
|
kpeter@942
|
62 |
/// \c Cost must be convertible to \c LargeCost.
|
kpeter@942
|
63 |
typedef double LargeCost;
|
kpeter@808
|
64 |
|
kpeter@808
|
65 |
/// The tolerance type used for internal computations
|
kpeter@942
|
66 |
typedef lemon::Tolerance<LargeCost> Tolerance;
|
kpeter@808
|
67 |
|
kpeter@808
|
68 |
/// \brief The path type of the found cycles
|
kpeter@808
|
69 |
///
|
kpeter@808
|
70 |
/// The path type of the found cycles.
|
kpeter@808
|
71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept
|
kpeter@808
|
72 |
/// and it must have an \c addBack() function.
|
kpeter@808
|
73 |
typedef lemon::Path<Digraph> Path;
|
kpeter@808
|
74 |
};
|
kpeter@808
|
75 |
|
kpeter@942
|
76 |
// Default traits class for integer cost types
|
kpeter@942
|
77 |
template <typename GR, typename CM>
|
kpeter@942
|
78 |
struct HowardMmcDefaultTraits<GR, CM, true>
|
kpeter@808
|
79 |
{
|
kpeter@808
|
80 |
typedef GR Digraph;
|
kpeter@942
|
81 |
typedef CM CostMap;
|
kpeter@942
|
82 |
typedef typename CostMap::Value Cost;
|
kpeter@808
|
83 |
#ifdef LEMON_HAVE_LONG_LONG
|
kpeter@942
|
84 |
typedef long long LargeCost;
|
kpeter@808
|
85 |
#else
|
kpeter@942
|
86 |
typedef long LargeCost;
|
kpeter@808
|
87 |
#endif
|
kpeter@942
|
88 |
typedef lemon::Tolerance<LargeCost> Tolerance;
|
kpeter@808
|
89 |
typedef lemon::Path<Digraph> Path;
|
kpeter@808
|
90 |
};
|
kpeter@808
|
91 |
|
kpeter@808
|
92 |
|
kpeter@815
|
93 |
/// \addtogroup min_mean_cycle
|
kpeter@805
|
94 |
/// @{
|
kpeter@805
|
95 |
|
kpeter@805
|
96 |
/// \brief Implementation of Howard's algorithm for finding a minimum
|
kpeter@805
|
97 |
/// mean cycle.
|
kpeter@805
|
98 |
///
|
kpeter@811
|
99 |
/// This class implements Howard's policy iteration algorithm for finding
|
kpeter@942
|
100 |
/// a directed cycle of minimum mean cost in a digraph
|
kpeter@1164
|
101 |
/// \ref dasdan98minmeancycle, \ref dasdan04experimental.
|
kpeter@815
|
102 |
/// This class provides the most efficient algorithm for the
|
kpeter@815
|
103 |
/// minimum mean cycle problem, though the best known theoretical
|
kpeter@815
|
104 |
/// bound on its running time is exponential.
|
kpeter@805
|
105 |
///
|
kpeter@805
|
106 |
/// \tparam GR The type of the digraph the algorithm runs on.
|
kpeter@942
|
107 |
/// \tparam CM The type of the cost map. The default
|
kpeter@805
|
108 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
|
kpeter@891
|
109 |
/// \tparam TR The traits class that defines various types used by the
|
kpeter@942
|
110 |
/// algorithm. By default, it is \ref HowardMmcDefaultTraits
|
kpeter@942
|
111 |
/// "HowardMmcDefaultTraits<GR, CM>".
|
kpeter@891
|
112 |
/// In most cases, this parameter should not be set directly,
|
kpeter@891
|
113 |
/// consider to use the named template parameters instead.
|
kpeter@805
|
114 |
#ifdef DOXYGEN
|
kpeter@942
|
115 |
template <typename GR, typename CM, typename TR>
|
kpeter@805
|
116 |
#else
|
kpeter@805
|
117 |
template < typename GR,
|
kpeter@942
|
118 |
typename CM = typename GR::template ArcMap<int>,
|
kpeter@942
|
119 |
typename TR = HowardMmcDefaultTraits<GR, CM> >
|
kpeter@805
|
120 |
#endif
|
kpeter@942
|
121 |
class HowardMmc
|
kpeter@805
|
122 |
{
|
kpeter@805
|
123 |
public:
|
alpar@956
|
124 |
|
kpeter@808
|
125 |
/// The type of the digraph
|
kpeter@808
|
126 |
typedef typename TR::Digraph Digraph;
|
kpeter@942
|
127 |
/// The type of the cost map
|
kpeter@942
|
128 |
typedef typename TR::CostMap CostMap;
|
kpeter@942
|
129 |
/// The type of the arc costs
|
kpeter@942
|
130 |
typedef typename TR::Cost Cost;
|
kpeter@808
|
131 |
|
kpeter@942
|
132 |
/// \brief The large cost type
|
kpeter@808
|
133 |
///
|
kpeter@942
|
134 |
/// The large cost type used for internal computations.
|
kpeter@942
|
135 |
/// By default, it is \c long \c long if the \c Cost type is integer,
|
kpeter@808
|
136 |
/// otherwise it is \c double.
|
kpeter@942
|
137 |
typedef typename TR::LargeCost LargeCost;
|
kpeter@808
|
138 |
|
kpeter@808
|
139 |
/// The tolerance type
|
kpeter@808
|
140 |
typedef typename TR::Tolerance Tolerance;
|
kpeter@808
|
141 |
|
kpeter@808
|
142 |
/// \brief The path type of the found cycles
|
kpeter@808
|
143 |
///
|
kpeter@808
|
144 |
/// The path type of the found cycles.
|
kpeter@942
|
145 |
/// Using the \ref HowardMmcDefaultTraits "default traits class",
|
kpeter@808
|
146 |
/// it is \ref lemon::Path "Path<Digraph>".
|
kpeter@808
|
147 |
typedef typename TR::Path Path;
|
kpeter@808
|
148 |
|
kpeter@942
|
149 |
/// The \ref HowardMmcDefaultTraits "traits class" of the algorithm
|
kpeter@808
|
150 |
typedef TR Traits;
|
kpeter@805
|
151 |
|
kpeter@1178
|
152 |
/// \brief Constants for the causes of search termination.
|
kpeter@1178
|
153 |
///
|
kpeter@1178
|
154 |
/// Enum type containing constants for the different causes of search
|
kpeter@1178
|
155 |
/// termination. The \ref findCycleMean() function returns one of
|
kpeter@1178
|
156 |
/// these values.
|
kpeter@1178
|
157 |
enum TerminationCause {
|
kpeter@1178
|
158 |
|
kpeter@1178
|
159 |
/// No directed cycle can be found in the digraph.
|
kpeter@1178
|
160 |
NO_CYCLE = 0,
|
kpeter@1178
|
161 |
|
kpeter@1178
|
162 |
/// Optimal solution (minimum cycle mean) is found.
|
kpeter@1178
|
163 |
OPTIMAL = 1,
|
kpeter@1178
|
164 |
|
kpeter@1178
|
165 |
/// The iteration count limit is reached.
|
kpeter@1178
|
166 |
ITERATION_LIMIT
|
kpeter@1178
|
167 |
};
|
kpeter@1178
|
168 |
|
kpeter@805
|
169 |
private:
|
kpeter@805
|
170 |
|
kpeter@805
|
171 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
|
alpar@956
|
172 |
|
kpeter@805
|
173 |
// The digraph the algorithm runs on
|
kpeter@805
|
174 |
const Digraph &_gr;
|
kpeter@942
|
175 |
// The cost of the arcs
|
kpeter@942
|
176 |
const CostMap &_cost;
|
kpeter@805
|
177 |
|
kpeter@807
|
178 |
// Data for the found cycles
|
kpeter@807
|
179 |
bool _curr_found, _best_found;
|
kpeter@942
|
180 |
LargeCost _curr_cost, _best_cost;
|
kpeter@807
|
181 |
int _curr_size, _best_size;
|
kpeter@807
|
182 |
Node _curr_node, _best_node;
|
kpeter@807
|
183 |
|
kpeter@805
|
184 |
Path *_cycle_path;
|
kpeter@807
|
185 |
bool _local_path;
|
kpeter@805
|
186 |
|
kpeter@807
|
187 |
// Internal data used by the algorithm
|
kpeter@807
|
188 |
typename Digraph::template NodeMap<Arc> _policy;
|
kpeter@807
|
189 |
typename Digraph::template NodeMap<bool> _reached;
|
kpeter@807
|
190 |
typename Digraph::template NodeMap<int> _level;
|
kpeter@942
|
191 |
typename Digraph::template NodeMap<LargeCost> _dist;
|
kpeter@805
|
192 |
|
kpeter@807
|
193 |
// Data for storing the strongly connected components
|
kpeter@807
|
194 |
int _comp_num;
|
kpeter@805
|
195 |
typename Digraph::template NodeMap<int> _comp;
|
kpeter@807
|
196 |
std::vector<std::vector<Node> > _comp_nodes;
|
kpeter@807
|
197 |
std::vector<Node>* _nodes;
|
kpeter@807
|
198 |
typename Digraph::template NodeMap<std::vector<Arc> > _in_arcs;
|
alpar@956
|
199 |
|
kpeter@807
|
200 |
// Queue used for BFS search
|
kpeter@807
|
201 |
std::vector<Node> _queue;
|
kpeter@807
|
202 |
int _qfront, _qback;
|
kpeter@808
|
203 |
|
kpeter@808
|
204 |
Tolerance _tolerance;
|
alpar@956
|
205 |
|
kpeter@814
|
206 |
// Infinite constant
|
kpeter@942
|
207 |
const LargeCost INF;
|
kpeter@814
|
208 |
|
kpeter@808
|
209 |
public:
|
alpar@956
|
210 |
|
kpeter@808
|
211 |
/// \name Named Template Parameters
|
kpeter@808
|
212 |
/// @{
|
kpeter@808
|
213 |
|
kpeter@808
|
214 |
template <typename T>
|
kpeter@942
|
215 |
struct SetLargeCostTraits : public Traits {
|
kpeter@942
|
216 |
typedef T LargeCost;
|
kpeter@808
|
217 |
typedef lemon::Tolerance<T> Tolerance;
|
kpeter@808
|
218 |
};
|
kpeter@808
|
219 |
|
kpeter@808
|
220 |
/// \brief \ref named-templ-param "Named parameter" for setting
|
kpeter@942
|
221 |
/// \c LargeCost type.
|
kpeter@808
|
222 |
///
|
kpeter@942
|
223 |
/// \ref named-templ-param "Named parameter" for setting \c LargeCost
|
kpeter@808
|
224 |
/// type. It is used for internal computations in the algorithm.
|
kpeter@808
|
225 |
template <typename T>
|
kpeter@942
|
226 |
struct SetLargeCost
|
kpeter@942
|
227 |
: public HowardMmc<GR, CM, SetLargeCostTraits<T> > {
|
kpeter@942
|
228 |
typedef HowardMmc<GR, CM, SetLargeCostTraits<T> > Create;
|
kpeter@808
|
229 |
};
|
kpeter@808
|
230 |
|
kpeter@808
|
231 |
template <typename T>
|
kpeter@808
|
232 |
struct SetPathTraits : public Traits {
|
kpeter@808
|
233 |
typedef T Path;
|
kpeter@808
|
234 |
};
|
kpeter@808
|
235 |
|
kpeter@808
|
236 |
/// \brief \ref named-templ-param "Named parameter" for setting
|
kpeter@808
|
237 |
/// \c %Path type.
|
kpeter@808
|
238 |
///
|
kpeter@808
|
239 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path
|
kpeter@808
|
240 |
/// type of the found cycles.
|
kpeter@808
|
241 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept
|
kpeter@808
|
242 |
/// and it must have an \c addBack() function.
|
kpeter@808
|
243 |
template <typename T>
|
kpeter@808
|
244 |
struct SetPath
|
kpeter@942
|
245 |
: public HowardMmc<GR, CM, SetPathTraits<T> > {
|
kpeter@942
|
246 |
typedef HowardMmc<GR, CM, SetPathTraits<T> > Create;
|
kpeter@808
|
247 |
};
|
alpar@956
|
248 |
|
kpeter@808
|
249 |
/// @}
|
kpeter@805
|
250 |
|
kpeter@941
|
251 |
protected:
|
kpeter@941
|
252 |
|
kpeter@942
|
253 |
HowardMmc() {}
|
kpeter@941
|
254 |
|
kpeter@805
|
255 |
public:
|
kpeter@805
|
256 |
|
kpeter@805
|
257 |
/// \brief Constructor.
|
kpeter@805
|
258 |
///
|
kpeter@805
|
259 |
/// The constructor of the class.
|
kpeter@805
|
260 |
///
|
kpeter@805
|
261 |
/// \param digraph The digraph the algorithm runs on.
|
kpeter@942
|
262 |
/// \param cost The costs of the arcs.
|
kpeter@942
|
263 |
HowardMmc( const Digraph &digraph,
|
kpeter@942
|
264 |
const CostMap &cost ) :
|
kpeter@942
|
265 |
_gr(digraph), _cost(cost), _best_found(false),
|
kpeter@942
|
266 |
_best_cost(0), _best_size(1), _cycle_path(NULL), _local_path(false),
|
kpeter@807
|
267 |
_policy(digraph), _reached(digraph), _level(digraph), _dist(digraph),
|
kpeter@814
|
268 |
_comp(digraph), _in_arcs(digraph),
|
kpeter@942
|
269 |
INF(std::numeric_limits<LargeCost>::has_infinity ?
|
kpeter@942
|
270 |
std::numeric_limits<LargeCost>::infinity() :
|
kpeter@942
|
271 |
std::numeric_limits<LargeCost>::max())
|
kpeter@805
|
272 |
{}
|
kpeter@805
|
273 |
|
kpeter@805
|
274 |
/// Destructor.
|
kpeter@942
|
275 |
~HowardMmc() {
|
kpeter@805
|
276 |
if (_local_path) delete _cycle_path;
|
kpeter@805
|
277 |
}
|
kpeter@805
|
278 |
|
kpeter@805
|
279 |
/// \brief Set the path structure for storing the found cycle.
|
kpeter@805
|
280 |
///
|
kpeter@805
|
281 |
/// This function sets an external path structure for storing the
|
kpeter@805
|
282 |
/// found cycle.
|
kpeter@805
|
283 |
///
|
kpeter@805
|
284 |
/// If you don't call this function before calling \ref run() or
|
kpeter@942
|
285 |
/// \ref findCycleMean(), it will allocate a local \ref Path "path"
|
kpeter@805
|
286 |
/// structure. The destuctor deallocates this automatically
|
kpeter@805
|
287 |
/// allocated object, of course.
|
kpeter@805
|
288 |
///
|
kpeter@805
|
289 |
/// \note The algorithm calls only the \ref lemon::Path::addBack()
|
kpeter@805
|
290 |
/// "addBack()" function of the given path structure.
|
kpeter@805
|
291 |
///
|
kpeter@805
|
292 |
/// \return <tt>(*this)</tt>
|
kpeter@942
|
293 |
HowardMmc& cycle(Path &path) {
|
kpeter@805
|
294 |
if (_local_path) {
|
kpeter@805
|
295 |
delete _cycle_path;
|
kpeter@805
|
296 |
_local_path = false;
|
kpeter@805
|
297 |
}
|
kpeter@805
|
298 |
_cycle_path = &path;
|
kpeter@805
|
299 |
return *this;
|
kpeter@805
|
300 |
}
|
kpeter@805
|
301 |
|
kpeter@816
|
302 |
/// \brief Set the tolerance used by the algorithm.
|
kpeter@816
|
303 |
///
|
kpeter@816
|
304 |
/// This function sets the tolerance object used by the algorithm.
|
kpeter@816
|
305 |
///
|
kpeter@816
|
306 |
/// \return <tt>(*this)</tt>
|
kpeter@942
|
307 |
HowardMmc& tolerance(const Tolerance& tolerance) {
|
kpeter@816
|
308 |
_tolerance = tolerance;
|
kpeter@816
|
309 |
return *this;
|
kpeter@816
|
310 |
}
|
kpeter@816
|
311 |
|
kpeter@816
|
312 |
/// \brief Return a const reference to the tolerance.
|
kpeter@816
|
313 |
///
|
kpeter@816
|
314 |
/// This function returns a const reference to the tolerance object
|
kpeter@816
|
315 |
/// used by the algorithm.
|
kpeter@816
|
316 |
const Tolerance& tolerance() const {
|
kpeter@816
|
317 |
return _tolerance;
|
kpeter@816
|
318 |
}
|
kpeter@816
|
319 |
|
kpeter@805
|
320 |
/// \name Execution control
|
kpeter@805
|
321 |
/// The simplest way to execute the algorithm is to call the \ref run()
|
kpeter@805
|
322 |
/// function.\n
|
kpeter@942
|
323 |
/// If you only need the minimum mean cost, you may call
|
kpeter@942
|
324 |
/// \ref findCycleMean().
|
kpeter@805
|
325 |
|
kpeter@805
|
326 |
/// @{
|
kpeter@805
|
327 |
|
kpeter@805
|
328 |
/// \brief Run the algorithm.
|
kpeter@805
|
329 |
///
|
kpeter@805
|
330 |
/// This function runs the algorithm.
|
kpeter@806
|
331 |
/// It can be called more than once (e.g. if the underlying digraph
|
kpeter@942
|
332 |
/// and/or the arc costs have been modified).
|
kpeter@805
|
333 |
///
|
kpeter@805
|
334 |
/// \return \c true if a directed cycle exists in the digraph.
|
kpeter@805
|
335 |
///
|
kpeter@806
|
336 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
|
kpeter@805
|
337 |
/// \code
|
kpeter@942
|
338 |
/// return mmc.findCycleMean() && mmc.findCycle();
|
kpeter@805
|
339 |
/// \endcode
|
kpeter@805
|
340 |
bool run() {
|
kpeter@942
|
341 |
return findCycleMean() && findCycle();
|
kpeter@805
|
342 |
}
|
kpeter@805
|
343 |
|
kpeter@1178
|
344 |
/// \brief Find the minimum cycle mean (or an upper bound).
|
kpeter@805
|
345 |
///
|
kpeter@942
|
346 |
/// This function finds the minimum mean cost of the directed
|
kpeter@1178
|
347 |
/// cycles in the digraph (or an upper bound for it).
|
kpeter@805
|
348 |
///
|
kpeter@1178
|
349 |
/// By default, the function finds the exact minimum cycle mean,
|
kpeter@1178
|
350 |
/// but an optional limit can also be specified for the number of
|
kpeter@1178
|
351 |
/// iterations performed during the search process.
|
kpeter@1178
|
352 |
/// The return value indicates if the optimal solution is found
|
kpeter@1178
|
353 |
/// or the iteration limit is reached. In the latter case, an
|
kpeter@1178
|
354 |
/// approximate solution is provided, which corresponds to a directed
|
kpeter@1178
|
355 |
/// cycle whose mean cost is relatively small, but not necessarily
|
kpeter@1178
|
356 |
/// minimal.
|
kpeter@1178
|
357 |
///
|
kpeter@1178
|
358 |
/// \param limit The maximum allowed number of iterations during
|
kpeter@1178
|
359 |
/// the search process. Its default value implies that the algorithm
|
kpeter@1178
|
360 |
/// runs until it finds the exact optimal solution.
|
kpeter@1178
|
361 |
///
|
kpeter@1178
|
362 |
/// \return The termination cause of the search process.
|
kpeter@1178
|
363 |
/// For more information, see \ref TerminationCause.
|
kpeter@1178
|
364 |
TerminationCause findCycleMean(int limit = std::numeric_limits<int>::max()) {
|
kpeter@807
|
365 |
// Initialize and find strongly connected components
|
kpeter@807
|
366 |
init();
|
kpeter@807
|
367 |
findComponents();
|
alpar@956
|
368 |
|
kpeter@806
|
369 |
// Find the minimum cycle mean in the components
|
kpeter@1178
|
370 |
int iter_count = 0;
|
kpeter@1178
|
371 |
bool iter_limit_reached = false;
|
kpeter@805
|
372 |
for (int comp = 0; comp < _comp_num; ++comp) {
|
kpeter@807
|
373 |
// Find the minimum mean cycle in the current component
|
kpeter@807
|
374 |
if (!buildPolicyGraph(comp)) continue;
|
kpeter@805
|
375 |
while (true) {
|
kpeter@1178
|
376 |
if (++iter_count > limit) {
|
kpeter@1178
|
377 |
iter_limit_reached = true;
|
kpeter@1178
|
378 |
break;
|
kpeter@1178
|
379 |
}
|
kpeter@807
|
380 |
findPolicyCycle();
|
kpeter@805
|
381 |
if (!computeNodeDistances()) break;
|
kpeter@805
|
382 |
}
|
kpeter@1178
|
383 |
|
kpeter@807
|
384 |
// Update the best cycle (global minimum mean cycle)
|
kpeter@814
|
385 |
if ( _curr_found && (!_best_found ||
|
kpeter@942
|
386 |
_curr_cost * _best_size < _best_cost * _curr_size) ) {
|
kpeter@807
|
387 |
_best_found = true;
|
kpeter@942
|
388 |
_best_cost = _curr_cost;
|
kpeter@807
|
389 |
_best_size = _curr_size;
|
kpeter@807
|
390 |
_best_node = _curr_node;
|
kpeter@807
|
391 |
}
|
kpeter@1178
|
392 |
|
kpeter@1178
|
393 |
if (iter_limit_reached) break;
|
kpeter@805
|
394 |
}
|
kpeter@1178
|
395 |
|
kpeter@1178
|
396 |
if (iter_limit_reached) {
|
kpeter@1178
|
397 |
return ITERATION_LIMIT;
|
kpeter@1178
|
398 |
} else {
|
kpeter@1178
|
399 |
return _best_found ? OPTIMAL : NO_CYCLE;
|
kpeter@1178
|
400 |
}
|
kpeter@805
|
401 |
}
|
kpeter@805
|
402 |
|
kpeter@805
|
403 |
/// \brief Find a minimum mean directed cycle.
|
kpeter@805
|
404 |
///
|
kpeter@942
|
405 |
/// This function finds a directed cycle of minimum mean cost
|
kpeter@942
|
406 |
/// in the digraph using the data computed by findCycleMean().
|
kpeter@805
|
407 |
///
|
kpeter@805
|
408 |
/// \return \c true if a directed cycle exists in the digraph.
|
kpeter@805
|
409 |
///
|
kpeter@942
|
410 |
/// \pre \ref findCycleMean() must be called before using this function.
|
kpeter@805
|
411 |
bool findCycle() {
|
kpeter@807
|
412 |
if (!_best_found) return false;
|
kpeter@807
|
413 |
_cycle_path->addBack(_policy[_best_node]);
|
kpeter@807
|
414 |
for ( Node v = _best_node;
|
kpeter@807
|
415 |
(v = _gr.target(_policy[v])) != _best_node; ) {
|
kpeter@805
|
416 |
_cycle_path->addBack(_policy[v]);
|
kpeter@805
|
417 |
}
|
kpeter@805
|
418 |
return true;
|
kpeter@805
|
419 |
}
|
kpeter@805
|
420 |
|
kpeter@805
|
421 |
/// @}
|
kpeter@805
|
422 |
|
kpeter@805
|
423 |
/// \name Query Functions
|
kpeter@806
|
424 |
/// The results of the algorithm can be obtained using these
|
kpeter@805
|
425 |
/// functions.\n
|
kpeter@805
|
426 |
/// The algorithm should be executed before using them.
|
kpeter@805
|
427 |
|
kpeter@805
|
428 |
/// @{
|
kpeter@805
|
429 |
|
kpeter@942
|
430 |
/// \brief Return the total cost of the found cycle.
|
kpeter@805
|
431 |
///
|
kpeter@942
|
432 |
/// This function returns the total cost of the found cycle.
|
kpeter@805
|
433 |
///
|
kpeter@942
|
434 |
/// \pre \ref run() or \ref findCycleMean() must be called before
|
kpeter@805
|
435 |
/// using this function.
|
kpeter@942
|
436 |
Cost cycleCost() const {
|
kpeter@942
|
437 |
return static_cast<Cost>(_best_cost);
|
kpeter@805
|
438 |
}
|
kpeter@805
|
439 |
|
kpeter@805
|
440 |
/// \brief Return the number of arcs on the found cycle.
|
kpeter@805
|
441 |
///
|
kpeter@805
|
442 |
/// This function returns the number of arcs on the found cycle.
|
kpeter@805
|
443 |
///
|
kpeter@942
|
444 |
/// \pre \ref run() or \ref findCycleMean() must be called before
|
kpeter@805
|
445 |
/// using this function.
|
kpeter@942
|
446 |
int cycleSize() const {
|
kpeter@807
|
447 |
return _best_size;
|
kpeter@805
|
448 |
}
|
kpeter@805
|
449 |
|
kpeter@942
|
450 |
/// \brief Return the mean cost of the found cycle.
|
kpeter@805
|
451 |
///
|
kpeter@942
|
452 |
/// This function returns the mean cost of the found cycle.
|
kpeter@805
|
453 |
///
|
kpeter@807
|
454 |
/// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
|
kpeter@805
|
455 |
/// following code.
|
kpeter@805
|
456 |
/// \code
|
kpeter@942
|
457 |
/// return static_cast<double>(alg.cycleCost()) / alg.cycleSize();
|
kpeter@805
|
458 |
/// \endcode
|
kpeter@805
|
459 |
///
|
kpeter@942
|
460 |
/// \pre \ref run() or \ref findCycleMean() must be called before
|
kpeter@805
|
461 |
/// using this function.
|
kpeter@805
|
462 |
double cycleMean() const {
|
kpeter@942
|
463 |
return static_cast<double>(_best_cost) / _best_size;
|
kpeter@805
|
464 |
}
|
kpeter@805
|
465 |
|
kpeter@805
|
466 |
/// \brief Return the found cycle.
|
kpeter@805
|
467 |
///
|
kpeter@805
|
468 |
/// This function returns a const reference to the path structure
|
kpeter@805
|
469 |
/// storing the found cycle.
|
kpeter@805
|
470 |
///
|
kpeter@805
|
471 |
/// \pre \ref run() or \ref findCycle() must be called before using
|
kpeter@805
|
472 |
/// this function.
|
kpeter@805
|
473 |
const Path& cycle() const {
|
kpeter@805
|
474 |
return *_cycle_path;
|
kpeter@805
|
475 |
}
|
kpeter@805
|
476 |
|
kpeter@805
|
477 |
///@}
|
kpeter@805
|
478 |
|
kpeter@805
|
479 |
private:
|
kpeter@805
|
480 |
|
kpeter@807
|
481 |
// Initialize
|
kpeter@807
|
482 |
void init() {
|
kpeter@807
|
483 |
if (!_cycle_path) {
|
kpeter@807
|
484 |
_local_path = true;
|
kpeter@807
|
485 |
_cycle_path = new Path;
|
kpeter@805
|
486 |
}
|
kpeter@807
|
487 |
_queue.resize(countNodes(_gr));
|
kpeter@807
|
488 |
_best_found = false;
|
kpeter@942
|
489 |
_best_cost = 0;
|
kpeter@807
|
490 |
_best_size = 1;
|
kpeter@807
|
491 |
_cycle_path->clear();
|
kpeter@807
|
492 |
}
|
alpar@956
|
493 |
|
kpeter@807
|
494 |
// Find strongly connected components and initialize _comp_nodes
|
kpeter@807
|
495 |
// and _in_arcs
|
kpeter@807
|
496 |
void findComponents() {
|
kpeter@807
|
497 |
_comp_num = stronglyConnectedComponents(_gr, _comp);
|
kpeter@807
|
498 |
_comp_nodes.resize(_comp_num);
|
kpeter@807
|
499 |
if (_comp_num == 1) {
|
kpeter@807
|
500 |
_comp_nodes[0].clear();
|
kpeter@807
|
501 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
kpeter@807
|
502 |
_comp_nodes[0].push_back(n);
|
kpeter@807
|
503 |
_in_arcs[n].clear();
|
kpeter@807
|
504 |
for (InArcIt a(_gr, n); a != INVALID; ++a) {
|
kpeter@807
|
505 |
_in_arcs[n].push_back(a);
|
kpeter@807
|
506 |
}
|
kpeter@807
|
507 |
}
|
kpeter@807
|
508 |
} else {
|
kpeter@807
|
509 |
for (int i = 0; i < _comp_num; ++i)
|
kpeter@807
|
510 |
_comp_nodes[i].clear();
|
kpeter@807
|
511 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
kpeter@807
|
512 |
int k = _comp[n];
|
kpeter@807
|
513 |
_comp_nodes[k].push_back(n);
|
kpeter@807
|
514 |
_in_arcs[n].clear();
|
kpeter@807
|
515 |
for (InArcIt a(_gr, n); a != INVALID; ++a) {
|
kpeter@807
|
516 |
if (_comp[_gr.source(a)] == k) _in_arcs[n].push_back(a);
|
kpeter@807
|
517 |
}
|
kpeter@807
|
518 |
}
|
kpeter@805
|
519 |
}
|
kpeter@807
|
520 |
}
|
kpeter@807
|
521 |
|
kpeter@807
|
522 |
// Build the policy graph in the given strongly connected component
|
kpeter@807
|
523 |
// (the out-degree of every node is 1)
|
kpeter@807
|
524 |
bool buildPolicyGraph(int comp) {
|
kpeter@807
|
525 |
_nodes = &(_comp_nodes[comp]);
|
kpeter@807
|
526 |
if (_nodes->size() < 1 ||
|
kpeter@807
|
527 |
(_nodes->size() == 1 && _in_arcs[(*_nodes)[0]].size() == 0)) {
|
kpeter@807
|
528 |
return false;
|
kpeter@805
|
529 |
}
|
kpeter@807
|
530 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
kpeter@814
|
531 |
_dist[(*_nodes)[i]] = INF;
|
kpeter@807
|
532 |
}
|
kpeter@807
|
533 |
Node u, v;
|
kpeter@807
|
534 |
Arc e;
|
kpeter@807
|
535 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
kpeter@807
|
536 |
v = (*_nodes)[i];
|
kpeter@807
|
537 |
for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
|
kpeter@807
|
538 |
e = _in_arcs[v][j];
|
kpeter@807
|
539 |
u = _gr.source(e);
|
kpeter@942
|
540 |
if (_cost[e] < _dist[u]) {
|
kpeter@942
|
541 |
_dist[u] = _cost[e];
|
kpeter@807
|
542 |
_policy[u] = e;
|
kpeter@807
|
543 |
}
|
kpeter@805
|
544 |
}
|
kpeter@805
|
545 |
}
|
kpeter@805
|
546 |
return true;
|
kpeter@805
|
547 |
}
|
kpeter@805
|
548 |
|
kpeter@807
|
549 |
// Find the minimum mean cycle in the policy graph
|
kpeter@807
|
550 |
void findPolicyCycle() {
|
kpeter@807
|
551 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
kpeter@807
|
552 |
_level[(*_nodes)[i]] = -1;
|
kpeter@807
|
553 |
}
|
kpeter@942
|
554 |
LargeCost ccost;
|
kpeter@805
|
555 |
int csize;
|
kpeter@805
|
556 |
Node u, v;
|
kpeter@807
|
557 |
_curr_found = false;
|
kpeter@807
|
558 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
kpeter@807
|
559 |
u = (*_nodes)[i];
|
kpeter@807
|
560 |
if (_level[u] >= 0) continue;
|
kpeter@807
|
561 |
for (; _level[u] < 0; u = _gr.target(_policy[u])) {
|
kpeter@807
|
562 |
_level[u] = i;
|
kpeter@807
|
563 |
}
|
kpeter@807
|
564 |
if (_level[u] == i) {
|
kpeter@807
|
565 |
// A cycle is found
|
kpeter@942
|
566 |
ccost = _cost[_policy[u]];
|
kpeter@807
|
567 |
csize = 1;
|
kpeter@807
|
568 |
for (v = u; (v = _gr.target(_policy[v])) != u; ) {
|
kpeter@942
|
569 |
ccost += _cost[_policy[v]];
|
kpeter@807
|
570 |
++csize;
|
kpeter@805
|
571 |
}
|
kpeter@807
|
572 |
if ( !_curr_found ||
|
kpeter@942
|
573 |
(ccost * _curr_size < _curr_cost * csize) ) {
|
kpeter@807
|
574 |
_curr_found = true;
|
kpeter@942
|
575 |
_curr_cost = ccost;
|
kpeter@807
|
576 |
_curr_size = csize;
|
kpeter@807
|
577 |
_curr_node = u;
|
kpeter@805
|
578 |
}
|
kpeter@805
|
579 |
}
|
kpeter@805
|
580 |
}
|
kpeter@805
|
581 |
}
|
kpeter@805
|
582 |
|
kpeter@807
|
583 |
// Contract the policy graph and compute node distances
|
kpeter@805
|
584 |
bool computeNodeDistances() {
|
kpeter@807
|
585 |
// Find the component of the main cycle and compute node distances
|
kpeter@807
|
586 |
// using reverse BFS
|
kpeter@807
|
587 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
kpeter@807
|
588 |
_reached[(*_nodes)[i]] = false;
|
kpeter@807
|
589 |
}
|
kpeter@807
|
590 |
_qfront = _qback = 0;
|
kpeter@807
|
591 |
_queue[0] = _curr_node;
|
kpeter@807
|
592 |
_reached[_curr_node] = true;
|
kpeter@807
|
593 |
_dist[_curr_node] = 0;
|
kpeter@805
|
594 |
Node u, v;
|
kpeter@807
|
595 |
Arc e;
|
kpeter@807
|
596 |
while (_qfront <= _qback) {
|
kpeter@807
|
597 |
v = _queue[_qfront++];
|
kpeter@807
|
598 |
for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
|
kpeter@807
|
599 |
e = _in_arcs[v][j];
|
kpeter@805
|
600 |
u = _gr.source(e);
|
kpeter@807
|
601 |
if (_policy[u] == e && !_reached[u]) {
|
kpeter@807
|
602 |
_reached[u] = true;
|
kpeter@942
|
603 |
_dist[u] = _dist[v] + _cost[e] * _curr_size - _curr_cost;
|
kpeter@807
|
604 |
_queue[++_qback] = u;
|
kpeter@805
|
605 |
}
|
kpeter@805
|
606 |
}
|
kpeter@805
|
607 |
}
|
kpeter@807
|
608 |
|
kpeter@807
|
609 |
// Connect all other nodes to this component and compute node
|
kpeter@807
|
610 |
// distances using reverse BFS
|
kpeter@807
|
611 |
_qfront = 0;
|
kpeter@807
|
612 |
while (_qback < int(_nodes->size())-1) {
|
kpeter@807
|
613 |
v = _queue[_qfront++];
|
kpeter@807
|
614 |
for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
|
kpeter@807
|
615 |
e = _in_arcs[v][j];
|
kpeter@807
|
616 |
u = _gr.source(e);
|
kpeter@807
|
617 |
if (!_reached[u]) {
|
kpeter@807
|
618 |
_reached[u] = true;
|
kpeter@807
|
619 |
_policy[u] = e;
|
kpeter@942
|
620 |
_dist[u] = _dist[v] + _cost[e] * _curr_size - _curr_cost;
|
kpeter@807
|
621 |
_queue[++_qback] = u;
|
kpeter@807
|
622 |
}
|
kpeter@807
|
623 |
}
|
kpeter@807
|
624 |
}
|
kpeter@807
|
625 |
|
kpeter@807
|
626 |
// Improve node distances
|
kpeter@805
|
627 |
bool improved = false;
|
kpeter@807
|
628 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
kpeter@807
|
629 |
v = (*_nodes)[i];
|
kpeter@807
|
630 |
for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
|
kpeter@807
|
631 |
e = _in_arcs[v][j];
|
kpeter@807
|
632 |
u = _gr.source(e);
|
kpeter@942
|
633 |
LargeCost delta = _dist[v] + _cost[e] * _curr_size - _curr_cost;
|
kpeter@808
|
634 |
if (_tolerance.less(delta, _dist[u])) {
|
kpeter@807
|
635 |
_dist[u] = delta;
|
kpeter@807
|
636 |
_policy[u] = e;
|
kpeter@807
|
637 |
improved = true;
|
kpeter@807
|
638 |
}
|
kpeter@805
|
639 |
}
|
kpeter@805
|
640 |
}
|
kpeter@805
|
641 |
return improved;
|
kpeter@805
|
642 |
}
|
kpeter@805
|
643 |
|
kpeter@942
|
644 |
}; //class HowardMmc
|
kpeter@805
|
645 |
|
kpeter@805
|
646 |
///@}
|
kpeter@805
|
647 |
|
kpeter@805
|
648 |
} //namespace lemon
|
kpeter@805
|
649 |
|
kpeter@942
|
650 |
#endif //LEMON_HOWARD_MMC_H
|