|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Minimum mean cycle by Howard's policy iteration. More...
#include <ah-graph-concepts.H>#include <cmath>#include <cstddef>#include <limits>#include <numeric>#include <type_traits>#include <utility>#include <ah-errors.H>#include <Min_Mean_Cycle.H>#include <Negative_Cycles.H>#include <shortest_path_common.H>#include <tpl_array.H>#include <tpl_graph.H>Go to the source code of this file.
Classes | |
| struct | Aleph::Howard_Mean_Cycle_Result< GT, Cost_Type > |
Result of howard_minimum_mean_cycle(). More... | |
| struct | Aleph::howard_detail::Exact_Integer< Cost_Type > |
Integer type, and its limits, of the exact arithmetic for Cost_Type: long long, or int128_t for unsigned long long and 128-bit costs. More... | |
| struct | Aleph::howard_detail::Arithmetic< Cost_Type, true > |
Exact arithmetic for integral costs: Eta is a reduced fraction and the bias of a node is scaled by the denominator of its Eta. More... | |
| struct | Aleph::howard_detail::Arithmetic< Cost_Type, true >::Eta |
Reduced fraction num / den with den > 0: the representation is canonical, so two equal means have equal fields. More... | |
| struct | Aleph::howard_detail::Arithmetic< Cost_Type, false > |
Floating-point arithmetic: long double with a relative tolerance in the comparison of biases. More... | |
| class | Aleph::howard_detail::Solver< Cost_Type > |
| Policy iteration over the out-adjacency of a graph in which every node has at least one outgoing arc. More... | |
Namespaces | |
| namespace | Aleph |
| Main namespace for Aleph-w library functions. | |
| namespace | Aleph::howard_detail |
Macros | |
| #define | ALEPH_HOWARD_INT128 0 |
| Whether the exact verification of floating-point results can use 128-bit integers. | |
Enumerations | |
| enum class | Aleph::Howard_Fallback_Reason { Aleph::Howard_Fallback_Reason::None , Aleph::Howard_Fallback_Reason::Iteration_Limit , Aleph::Howard_Fallback_Reason::Arithmetic_Overflow , Aleph::Howard_Fallback_Reason::Cost_Out_Of_Range } |
Why howard_minimum_mean_cycle() handed the problem over to Karp's algorithm (see the documentation of Howard_Min_Mean_Cycle.H). More... | |
Functions | |
| template<typename Cost_Type > | |
| size_t | Aleph::howard_detail::cycle_entry (const negative_cycles_detail::Out_Adjacency< Cost_Type > &adj, const Array< size_t > &policy, const size_t start) |
Node where the walk that follows policy from start closes its cycle (the first node of that cycle the walk meets). | |
| template<typename Int , typename Cost_Type > | |
| bool | Aleph::howard_detail::try_exact (const negative_cycles_detail::Out_Adjacency< Cost_Type > &adj, const size_t n, const size_t limit, const Array< int > &low, const int low_min, Array< size_t > &policy, size_t &first, size_t &iterations) |
Re-solve exactly, from policy, with the floating-point weights of adj scaled to integers of type Int. | |
| template<typename Cost_Type > | |
| bool | Aleph::howard_detail::verify_exactly (const negative_cycles_detail::Out_Adjacency< Cost_Type > &adj, const size_t n, const size_t limit, Array< size_t > &policy, size_t &first, size_t &iterations) |
Verify, and finish if needed, a floating-point result with exact arithmetic: try_exact() with 64-bit integers, or 128-bit ones where they exist, if the scaled weights fit. | |
| template<typename Cost_Type > | |
| size_t | Aleph::howard_detail::best_cycle_exactly (const negative_cycles_detail::Out_Adjacency< Cost_Type > &adj, const Array< size_t > &policy, const size_t n) |
Cycle of minimum mean among the cycles of policy, with means compared exactly (Exact_Sum, cross-multiplied by the lengths). | |
| const char * | Aleph::howard_detail::fallback_reason_text (const Howard_Fallback_Reason reason) noexcept |
| Words for a fallback reason, for error messages. | |
| template<bool With_Witness, AlephGraph GT, typename Cost_Type > | |
| Min_Mean_Cycle_Result< GT, Cost_Type > | Aleph::howard_detail::karp_by_component (const negative_cycles_detail::Indexed_Graph< GT, Cost_Type > &ig, const negative_cycles_detail::Out_Adjacency< Cost_Type > &adj, const Howard_Fallback_Reason reason, const size_t max_bytes) |
| Karp's algorithm on every strongly connected component of a cyclic core, from the weights already read. | |
| template<AlephGraph GT, ArcDistance< GT > Distance, ArcFilter< GT > SA, bool With_Witness = true> | |
| Howard_Mean_Cycle_Result< GT, typename Distance::Distance_Type > | Aleph::howard_detail::minimum_mean_cycle (const GT &g, Distance distance, SA sa, const size_t max_iterations, const size_t max_fallback_bytes=std::numeric_limits< size_t >::max()) |
Body of howard_minimum_mean_cycle() with an explicit iteration limit, so that the fallback to Karp can be exercised. | |
| template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>> | |
| Howard_Mean_Cycle_Result< GT, typename Distance::Distance_Type > | Aleph::howard_minimum_mean_cycle (const GT &g, Distance distance=Distance(), SA sa=SA(), const size_t max_fallback_bytes=std::numeric_limits< size_t >::max()) |
| Minimum mean cycle by Howard's policy iteration. | |
| template<AlephGraph GT, ArcDistance< GT > Distance = Dft_Dist<GT>, ArcFilter< GT > SA = Dft_Show_Arc<GT>> | |
| Min_Mean_Cycle_Value_Result | Aleph::howard_minimum_mean_cycle_value (const GT &g, Distance distance=Distance(), SA sa=SA(), const size_t max_fallback_bytes=std::numeric_limits< size_t >::max()) |
| Minimum mean cycle value by Howard's policy iteration, without the witness. | |
Variables | |
| template<typename Cost_Type > | |
| constexpr bool | Aleph::howard_detail::exact_arithmetic_v |
Integral costs that fit a long long use exact arithmetic, and so do unsigned long long and 128-bit integers where 128-bit integers exist. | |
| constexpr size_t | Aleph::howard_detail::iteration_slack = 64 |
| Iterations allowed beyond one per node before Karp takes over. | |
| constexpr size_t | Aleph::howard_detail::default_iterations = std::numeric_limits<size_t>::max() |
Value of max_iterations that selects the default limit, V + 64. | |
Minimum mean cycle by Howard's policy iteration.
Solves the same problem as karp_minimum_mean_cycle() (Min_Mean_Cycle.H): the cycle \(C\) of a directed weighted graph that minimizes \(w(C) / |C|\). It is a faster and leaner backend, not a different criterion: it ranks cycles by mean, not by total cost, so for arbitrage the cycle to execute still comes from Negative_Cycles.H.
A policy picks one outgoing arc for every node, so it defines a functional graph: every component has exactly one cycle, with trees hanging from it. Evaluating a policy gives each node the mean eta of the cycle it reaches and a bias x (the cost of the path to the cycle, relative to eta per arc). Improving a policy first moves every node to a successor with a smaller eta; when none exists it moves nodes to a successor with the same eta and a smaller bias. When nothing changes the policy is optimal and the smallest eta is the minimum mean (Cochet-Terrasson, Cohen, Gaubert, McGettrick and Quadrat, 1998; Dasdan, 2004).
Only the cyclic core of the graph takes part (the arcs inside strongly connected components, see Negative_Cycles.H), so every node has an outgoing arc inside its own component.
Each iteration takes \(O(V + E)\) time and the working space is \(O(V + E)\), against \(O(V E)\) time and \(O(V^2)\) space for Karp. The exact verification of floating-point results costs one more iteration, in integer arithmetic, plus \(O(E)\) to scale the weights (a fifth to a third more time on the market graphs of the benchmark). No polynomial bound on the number of iterations is known (as for the simplex method) and constructed instances need many, but on graphs that occur in practice it is small. The function therefore stops after V + 64 iterations and returns Karp's answer instead, so it never runs much longer than Karp.
That fallback (also taken when the exact arithmetic overflows, see below) runs Karp's algorithm, the same table as karp_minimum_mean_cycle(), on each strongly connected component of the cyclic core in turn, from the snapshot already taken: neither the distance accessor nor the arc filter is called again, and the nodes outside the core cost nothing. It takes \(O(\sum_c V_c E_c)\) time and \(O(\max_c V_c^2)\) memory, where \(V_c\) and \(E_c\) count the nodes and arcs of component \(c\): Karp's O(V E) and O(V^2) only when one component holds the whole graph. howard_minimum_mean_cycle_value() runs it without the table of predecessors (8 bytes per entry, next to the sums of 8 or 16) and without extracting a witness.
fallback_reason says why it ran (Howard_Fallback_Reason): the iteration limit; an overflow of the arithmetic of the policy evaluation (a cycle sum or a scaled bias beyond the 64- or 128-bit integers of the exact arithmetic, or not finite in long double); or an exact total cost of the optimal cycle that a narrow cost type, such as int, cannot hold.
Karp's tables for a component of \(V_c\) nodes have \((V_c + 1) V_c\) entries, of 8 to 24 bytes (the sum, and a predecessor position with the witness), and Array reserves a power of two of them: from kilobytes to gigabytes. The optional last argument max_fallback_bytes bounds them. Before building any table, the fallback computes the bytes of the largest one and, above the limit, throws std::length_error (these functions throw it for no other reason), with the bytes, the size of the component and the reason of the fallback in the message. The limit counts only those tables, not the \(O(V + E)\) memory of the snapshot and of Howard's iteration. 0 refuses any fallback; the default, std::numeric_limits<size_t>::max(), sets no limit.
long long, and unsigned long long where 128-bit integers exist) are handled exactly: eta is a reduced fraction and the bias is scaled by its denominator, so every comparison is one between integers and the reported cycle has the exact minimum mean cycle_total_cost / cycle_length. The scaled bias can exceed the range of long long before Karp's sums do; the function then returns Karp's answer (Arithmetic_Overflow). So it does when the exact total of the optimal cycle does not fit a narrow cost type such as int (Cost_Out_Of_Range).long double, with a relative tolerance in the bias comparison that protects the iteration from oscillating on rounding noise. That arithmetic alone is not reliable when weights of very different magnitudes cancel (a ring 2^100, 1, -2^100, -2, of exact mean -1/4, sums to -2 and looks better than a self-loop of -0.375), nor for improvements below the absolute floor of the tolerance, 64 * epsilon(long double) (with self-loops of weight 0 and arcs 0 -> 1 of 2^-60 and 1 -> 0 of -2^-59 the iteration stops at a loop of mean 0 instead of the cycle of mean -2^-61). So the result is then finished with exact arithmetic: every weight is an odd integer times a power of two, and dividing them all by the smallest such power gives integers that represent them exactly; when those fit 64-bit integers, or 128-bit ones where the compiler has them, the iteration resumes from its policy in that exact arithmetic (usually confirming it in one evaluation), and the answer is the exact optimum (numeric_quality is Exact). When the weights span too many binary orders of magnitude for that (more than about 126), the policy stays the rounded one, but the cycle is still chosen among its cycles with exact sums, and numeric_quality is Rounded. The reported cost is the exact sum of the weights of the cycle, rounded once.witness_node is the first node of its cycle, also after the fallback; Karp's is the vertex its walk ends at. When several cycles share the minimum mean the two functions may return different ones.karp_minimum_mean_cycle()), except when its sums are checked long long (no 128-bit integers, as with MSVC) or the cost type's own (unsigned long long, 128-bit costs): there a sum equal to the maximum of the type reads as unreachable, an arc of that cost is ignored, and the fallback inherits that.V costs in its table, even on acyclic graphs, but in long long for narrower types and in 128-bit integers where n * max|w| could leave long long, so for integer costs of at most 64 bits it throws only when the cost of the cycle it reports does not fit the cost type, as Howard. Without 128-bit integers its sums are checked long long and it can throw where Howard answers; the fallback, whose sums follow only walks inside one component, can then also overflow where Karp on the whole graph would not, where an arc entering the component keeps the whole-graph sums low (rarely; the opposite is far more common).minimum_mean is a long double, so with large integer costs its last bits depend on the width of that type. Howard rounds the exact quotient cycle_total_cost / cycle_length: once if long double has a 64-bit mantissa or more (x86, AArch64 Linux). Where it is a plain double (Apple Silicon, MSVC) a total beyond 2^53 is rounded when converted as well. Karp rounds the conversion of two sums and of their difference before dividing, so its value can drift from Howard's, and from the exact mean, by a few rounding errors of those sums (epsilon * V * max|cost|). To compare means exactly, use cycle_total_cost and cycle_length.negative_cycles_detail namespace of Negative_Cycles.H.Definition in file Howard_Min_Mean_Cycle.H.
| #define ALEPH_HOWARD_INT128 0 |
Whether the exact verification of floating-point results can use 128-bit integers.
GCC and Clang provide them (__SIZEOF_INT128__), but clang-cl, which also defines that macro, lacks the runtime support for 128-bit division on Windows (there is no compiler-rt there), so it is left out.
Definition at line 251 of file Howard_Min_Mean_Cycle.H.