Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::howard_detail Namespace Reference

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.
 

Function Documentation

◆ best_cycle_exactly()

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).

Used when the exact verification is not possible: the policy itself comes from rounded arithmetic, but the choice among its cycles does not.

Parameters
[in]adjOut-adjacency with floating-point weights.
[in]policyA policy over adj.
[in]nNumber of nodes.
Returns
A node of the chosen cycle, or 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().

◆ cycle_entry()

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).

Parameters
[in]adjOut-adjacency the policy refers to.
[in]policyFor each node, the position in adj of its arc.
[in]startNode to start from.
Returns
A node of the cycle.

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().

◆ fallback_reason_text()

const char * Aleph::howard_detail::fallback_reason_text ( const Howard_Fallback_Reason  reason)
inlinenoexcept

Words for a fallback reason, for error messages.

Parameters
[in]reasonThe reason.
Returns
A short lowercase description.

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().

◆ karp_by_component()

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.

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.

Template Parameters
With_WitnessWhether to keep predecessors and extract the witness.
Parameters
[in]igCyclic core (a snapshot that is its own core included).
[in]adjOut-adjacency of ig.
[in]reasonWhy Howard falls back, for the message of a refusal.
[in]max_bytesLimit of the bytes of the tables of one component; std::numeric_limits<size_t>::max() for none.
Returns
Karp's answer, with its witness in the nodes and arcs of the graph if With_Witness; otherwise has_cycle and minimum_mean only.
Exceptions
std::length_errorIf the tables of a component need more than max_bytes bytes.
std::overflow_errorIf, 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_errorIf a floating-point accumulation is not finite.
Note
\(O(V + E + \sum_c V_c E_c)\) time and \(O(V + E + \max_c V_c^2)\) space, where \(V_c\) and \(E_c\) are the nodes and arcs of component \(c\).

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.

◆ minimum_mean_cycle()

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.

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()).

Template Parameters
With_WitnessWhether to build the witness.
Parameters
[in]gGraph (must be directed).
[in]distanceArc cost accessor.
[in]saArc filter.
[in]max_iterationsImprovements allowed, or default_iterations.
[in]max_fallback_bytesLimit of the bytes of Karp's tables in the fallback (see the file documentation); none by default.
Returns
The result of howard_minimum_mean_cycle().
Exceptions
std::length_errorIf 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().

◆ try_exact()

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.

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.

Parameters
[in]adjOut-adjacency with floating-point weights.
[in]nNumber of nodes.
[in]limitImprovements allowed.
[in]lowExponent of the lowest set bit of each weight (any value for a zero weight).
[in]low_minExponent of the lowest set bit over all the weights.
[in,out]policyStarting policy; receives the optimal one.
[out]firstA node of an optimal cycle of the final policy.
[out]iterationsImprovements applied.
Returns
False if an intermediate value did not fit 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.

◆ verify_exactly()

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.

Parameters
[in]adjOut-adjacency with floating-point weights.
[in]nNumber of nodes.
[in]limitImprovements allowed.
[in,out]policyThe policy of the floating-point iteration; receives the exactly optimal one.
[out]firstA node of an optimal cycle.
[out]iterationsImprovements applied by the exact iteration.
Returns
True if the result is now exact; false (and nothing changed) if the weights span too many binary orders of magnitude.

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().

Variable Documentation

◆ default_iterations

constexpr size_t Aleph::howard_detail::default_iterations = std::numeric_limits<size_t>::max()
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().

◆ exact_arithmetic_v

template<typename Cost_Type >
constexpr bool Aleph::howard_detail::exact_arithmetic_v
inlineconstexpr
Initial value:
= std::is_integral_v<Cost_Type> and
std::numeric_limits<Cost_Type>::max() <= std::numeric_limits<long long>::max()
and
Check uniqueness with explicit hash + equality functors.

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.

◆ iteration_slack

constexpr size_t Aleph::howard_detail::iteration_slack = 64
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().