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