|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Classes | |
| struct | Arithmetic |
| Arithmetic of the policy evaluation, specialised by exactness. More... | |
| struct | Arithmetic< Cost_Type, false > |
Floating-point arithmetic: long double with a relative tolerance in the comparison of biases. More... | |
| struct | 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 | Exact_Integer |
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... | |
| class | Solver |
| Policy iteration over the out-adjacency of a graph in which every node has at least one outgoing arc. More... | |
Functions | |
| template<typename Cost_Type > | |
| size_t | 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 | 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 | 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 | 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 * | 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 > | 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 > | 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. | |
Variables | |
| template<typename Cost_Type > | |
| constexpr bool | 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 | iteration_slack = 64 |
| Iterations allowed beyond one per node before Karp takes over. | |
| constexpr size_t | default_iterations = std::numeric_limits<size_t>::max() |
Value of max_iterations that selects the default limit, V + 64. | |
| 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).
Used when the exact verification is not possible: the policy itself comes from rounded arithmetic, but the choice among its cycles does not.
| [in] | adj | Out-adjacency with floating-point weights. |
| [in] | policy | A policy over adj. |
| [in] | n | Number of nodes. |
n if an exact sum overflowed. Definition at line 1005 of file Howard_Min_Mean_Cycle.H.
References Aleph::negative_cycles_detail::Exact_Sum< T >::add_multiple(), Aleph::negative_cycles_detail::Exact_Sum< T >::clear(), Aleph::negative_cycles_detail::Exact_Sum< T >::sign(), Aleph::sum(), Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::tgt, and Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::weight.
Referenced by minimum_mean_cycle().
| 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).
| [in] | adj | Out-adjacency the policy refers to. |
| [in] | policy | For each node, the position in adj of its arc. |
| [in] | start | Node to start from. |
Definition at line 884 of file Howard_Min_Mean_Cycle.H.
References Aleph::Array< T >::size(), Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::start, and Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::tgt.
Referenced by minimum_mean_cycle(), and try_exact().
|
inlinenoexcept |
Words for a fallback reason, for error messages.
| [in] | reason | The reason. |
Definition at line 1067 of file Howard_Min_Mean_Cycle.H.
References Aleph::Arithmetic_Overflow, Aleph::Cost_Out_Of_Range, Aleph::Iteration_Limit, and Aleph::None.
Referenced by karp_by_component().
| 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.
ig must be its own cyclic core: every arc joins two nodes of the same strongly connected component, and every node has an arc. The nodes reachable from a node are then exactly those of its component, so a forward search from each node not yet labelled labels the components, numbered by their first node. Each component gets its own Karp table, built and freed in turn. Neither the graph, nor the distance accessor, nor the arc filter is read again, and the nodes outside the core cost nothing.
The answer is the component of smallest mean, compared exactly for integral costs where 128-bit integers exist; a tie goes to the one whose witness vertex comes first in ig. The witness, however, is taken from a component of that mean whose cycle cost fits Cost_Type when there is one, so a component that does not win, or whose cost does not fit while another of the same mean does, never makes the call throw. The mean does not depend on the witness, so With_Witness false gives the same minimum_mean without the table of predecessors and without extracting the cycle.
With a memory limit, the bytes of the tables of every component are computed first (karp_table_bytes(), as karp_solve() reserves them), and if the largest exceeds the limit nothing is run.
| With_Witness | Whether to keep predecessors and extract the witness. |
| [in] | ig | Cyclic core (a snapshot that is its own core included). |
| [in] | adj | Out-adjacency of ig. |
| [in] | reason | Why Howard falls back, for the message of a refusal. |
| [in] | max_bytes | Limit of the bytes of the tables of one component; std::numeric_limits<size_t>::max() for none. |
With_Witness; otherwise has_cycle and minimum_mean only. | std::length_error | If the tables of a component need more than max_bytes bytes. |
| std::overflow_error | If, with With_Witness, the cost of no minimum-mean witness the components gave fits Cost_Type; or a checked integer addition overflows (see karp_minimum_mean_cycle()). |
| std::domain_error | If a floating-point accumulation is not finite. |
Definition at line 1129 of file Howard_Min_Mean_Cycle.H.
References ah_domain_error_if, ah_length_error_if, ah_overflow_error_if, Aleph::and, Aleph::Array< T >::append(), Aleph::DynList< T >::append(), Aleph::negative_cycles_detail::Indexed_Graph< GT, Cost_Type >::arcs, Aleph::Min_Mean_Cycle_Result< GT, Cost_Type >::cycle_arcs, Aleph::Min_Mean_Cycle_Result< GT, Cost_Type >::cycle_length, Aleph::Min_Mean_Cycle_Result< GT, Cost_Type >::cycle_nodes, Aleph::Min_Mean_Cycle_Result< GT, Cost_Type >::cycle_total_cost, fallback_reason_text(), Aleph::Array< T >::get_last(), Aleph::Min_Mean_Cycle_Result< GT, Cost_Type >::has_cycle, Aleph::Array< T >::is_empty(), Aleph::min_mean_cycle_detail::karp_sums(), Aleph::mean(), Aleph::min_mean_cycle_detail::mean_order(), Aleph::Min_Mean_Cycle_Result< GT, Cost_Type >::minimum_mean, Aleph::negative_cycles_detail::Indexed_Graph< GT, Cost_Type >::nodes, Aleph::Array< T >::remove_last(), Aleph::Array< T >::reserve(), root(), Aleph::Array< T >::size(), Aleph::size(), Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::start, Aleph::sum(), Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::tgt, and Aleph::Min_Mean_Cycle_Result< GT, Cost_Type >::witness_node.
| 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.
With With_Witness false (howard_minimum_mean_cycle_value()) the lists of nodes and arcs of the cycle are not built, and the fallback runs Karp without predecessors. has_cycle, minimum_mean, iterations, used_karp and numeric_quality are the same as with the witness, except that the cost of the cycle does not have to fit Cost_Type when the fallback answers (see karp_by_component()).
| With_Witness | Whether to build the witness. |
| [in] | g | Graph (must be directed). |
| [in] | distance | Arc cost accessor. |
| [in] | sa | Arc filter. |
| [in] | max_iterations | Improvements allowed, or default_iterations. |
| [in] | max_fallback_bytes | Limit of the bytes of Karp's tables in the fallback (see the file documentation); none by default. |
howard_minimum_mean_cycle(). | std::length_error | If the fallback runs and its tables need more than max_fallback_bytes bytes. |
Definition at line 1319 of file Howard_Min_Mean_Cycle.H.
References Aleph::and, Aleph::Array< T >::append(), arcs, Aleph::negative_cycles_detail::Indexed_Graph< GT, Cost_Type >::arcs, Aleph::Arithmetic_Overflow, best_cycle_exactly(), Aleph::howard_detail::Solver< Cost_Type >::best_node(), Aleph::negative_cycles_detail::build_out_adjacency(), Aleph::Cost_Out_Of_Range, Aleph::negative_cycles_detail::cycle_cost(), cycle_entry(), Aleph::negative_cycles_detail::cyclic_core(), default_iterations, Aleph::Exact, Aleph::negative_cycles_detail::index_graph(), Aleph::Iteration_Limit, Aleph::howard_detail::Solver< Cost_Type >::Iteration_Limit, iteration_slack, Aleph::negative_cycles_detail::Indexed_Graph< GT, Cost_Type >::nodes, Aleph::howard_detail::Solver< Cost_Type >::Optimal, Aleph::howard_detail::Solver< Cost_Type >::policy(), root(), Aleph::Rounded, Aleph::Array< T >::size(), Aleph::howard_detail::Solver< Cost_Type >::solve(), and verify_exactly().
| 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.
Every weight is an odd integer times a power of two; dividing all of them by the smallest such power gives integers that represent them exactly, so the iteration that follows decides with the exact weights.
| [in] | adj | Out-adjacency with floating-point weights. |
| [in] | n | Number of nodes. |
| [in] | limit | Improvements allowed. |
| [in] | low | Exponent of the lowest set bit of each weight (any value for a zero weight). |
| [in] | low_min | Exponent of the lowest set bit over all the weights. |
| [in,out] | policy | Starting policy; receives the optimal one. |
| [out] | first | A node of an optimal cycle of the final policy. |
| [out] | iterations | Improvements applied. |
Int or the limit was reached; nothing is written then. Definition at line 917 of file Howard_Min_Mean_Cycle.H.
References Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::arc, Aleph::howard_detail::Solver< Cost_Type >::best_node(), cycle_entry(), Aleph::low(), Aleph::negative_cycles_detail::odd_significand(), Aleph::howard_detail::Solver< Cost_Type >::policy(), Aleph::Array< T >::size(), Aleph::howard_detail::Solver< Cost_Type >::solve(), Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::start, Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::tgt, w, and Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::weight.
| 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.
| [in] | adj | Out-adjacency with floating-point weights. |
| [in] | n | Number of nodes. |
| [in] | limit | Improvements allowed. |
| [in,out] | policy | The policy of the floating-point iteration; receives the exactly optimal one. |
| [out] | first | A node of an optimal cycle. |
| [out] | iterations | Improvements applied by the exact iteration. |
Definition at line 965 of file Howard_Min_Mean_Cycle.H.
References Aleph::and, Aleph::negative_cycles_detail::bit_span(), Aleph::bits(), Aleph::low(), Aleph::Array< T >::size(), and Aleph::negative_cycles_detail::Out_Adjacency< Cost_Type >::weight.
Referenced by minimum_mean_cycle().
|
inlineconstexpr |
Value of max_iterations that selects the default limit, V + 64.
Definition at line 874 of file Howard_Min_Mean_Cycle.H.
Referenced by Aleph::howard_minimum_mean_cycle(), Aleph::howard_minimum_mean_cycle_value(), minimum_mean_cycle(), TEST(), TEST(), and TEST().
|
inlineconstexpr |
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.
Definition at line 266 of file Howard_Min_Mean_Cycle.H.
|
inlineconstexpr |
Iterations allowed beyond one per node before Karp takes over.
Definition at line 871 of file Howard_Min_Mean_Cycle.H.
Referenced by minimum_mean_cycle().