45# include <gtest/gtest.h>
73 Rational() { mpq_init(q_); }
74 Rational(
const Rational & other) { mpq_init(q_); mpq_set(q_, other.q_); }
75 Rational & operator=(
const Rational & other) { mpq_set(q_, other.q_);
return *
this; }
76 ~Rational() { mpq_clear(q_); }
81 static Rational of(
const T x)
87 const T f = std::frexp(std::fabs(x), &e);
88 constexpr int digits = std::numeric_limits<T>::digits;
89 T m = std::ldexp(f, digits);
90 std::vector<unsigned long> chunks;
91 const T base =
T(4294967296.0);
94 const T low = std::fmod(
m, base);
95 chunks.push_back(
static_cast<unsigned long>(
low));
100 for (
size_t i = chunks.size(); i-- > 0; )
102 mpz_mul_2exp(z, z, 32);
103 mpz_add_ui(z, z, chunks[i]);
109 const int exponent = e - digits;
111 mpq_mul_2exp(
r.q_,
r.q_,
static_cast<mp_bitcnt_t
>(exponent));
113 mpq_div_2exp(
r.q_,
r.q_,
static_cast<mp_bitcnt_t
>(-exponent));
117 Rational & operator+=(
const Rational & other) { mpq_add(q_, q_, other.q_);
return *
this; }
119 friend Rational
operator-(
const Rational & a,
const Rational & b)
122 mpq_sub(
r.q_, a.q_, b.q_);
126 Rational times(
const size_t factor)
const
129 mpq_set_ui(f.q_,
static_cast<unsigned long>(factor), 1);
131 mpq_mul(
r.q_, q_, f.q_);
135 friend int compare(
const Rational & a,
const Rational & b) {
return mpq_cmp(a.q_, b.q_); }
136 int sign()
const {
return mpq_sgn(q_); }
140 bool mean_less(
const Rational & a,
const size_t la,
const Rational & b,
const size_t lb)
142 return compare(a.times(lb), b.times(la)) < 0;
145 bool mean_equal(
const Rational & a,
const size_t la,
const Rational & b,
const size_t lb)
147 return compare(a.times(lb), b.times(la)) == 0;
152 template <
typename T>
153 bool within_one_ulp(
const T computed,
const Rational & exact)
155 if (computed ==
T(0))
156 return exact.sign() == 0;
157 if ((computed <
T(0) ? -1 : 1) != exact.sign())
159 const T up = std::nextafter(computed, std::numeric_limits<T>::infinity());
160 const T down = std::nextafter(computed, -std::numeric_limits<T>::infinity());
161 return compare(Rational::of(down), exact) <= 0
and compare(exact, Rational::of(up)) <= 0;
167 template <
typename T>
171 std::vector<std::tuple<size_t, size_t, T>> edges;
172 std::vector<bool> hidden;
175 enum class Family { Moderate, Ties, Subnormal, Three_Scales, Market };
177 template <
typename T>
178 T draw_weight(
const Family family, std::mt19937_64 &
rng)
180 const auto small = [&
rng](
const int range) {
return static_cast<int>(
rng() % (2 *
range + 1)) -
range; };
183 case Family::Moderate:
184 return std::ldexp(
static_cast<T>(small(50)), small(8));
186 return static_cast<T>(small(3));
187 case Family::Subnormal:
188 return static_cast<T>(small(50)) * std::numeric_limits<T>::denorm_min()
189 *
static_cast<T>(1 << (
rng() % 8));
190 case Family::Three_Scales:
193 constexpr int gap = std::numeric_limits<T>::digits + 6;
194 const int scale =
static_cast<int>(
rng() % 3) - 1;
195 return std::ldexp(
static_cast<T>(small(7)), scale * gap);
200 std::uniform_real_distribution<double> lp(-3.0, 3.0);
201 return static_cast<T>(lp(
rng) - lp(
rng) - std::log(1.0 - 0.001));
207 template <
typename T>
208 Instance<T> make_instance(
const Family family, std::mt19937_64 &
rng)
211 inst.n = 1 +
rng() % 6;
212 const size_t m =
rng() % (inst.n * inst.n + 3);
215 for (
size_t i = 0; i <
m; ++i)
217 size_t u =
rng() % inst.n;
218 size_t v =
rng() % inst.n;
219 if (split
and (u < inst.n / 2) != (v < inst.n / 2)
and u > v)
221 inst.edges.emplace_back(u, v, draw_weight<T>(family,
rng));
224 if (family == Family::Three_Scales
and inst.n >= 4
and rng() % 2 == 0)
228 constexpr int gap = std::numeric_limits<T>::digits + 6;
229 const T big = std::ldexp(
T(1), gap);
230 const T unit =
T(1 +
rng() % 5);
231 inst.edges.emplace_back(0, 1, big);
232 inst.edges.emplace_back(1, 2, unit);
233 inst.edges.emplace_back(2, 3, -big);
234 inst.edges.emplace_back(3, 0, -unit);
235 for (
int k = 0;
k < 4; ++
k)
236 inst.hidden.push_back(
false);
241 template <
typename T>
244 template <
typename T>
248 std::vector<typename Graph_T<T>::Node *>
nodes;
249 std::vector<typename Graph_T<T>::Arc *>
arcs;
250 std::set<typename Graph_T<T>::Arc *> hidden;
255 template <
typename T>
256 void build(
const Instance<T> & inst, Built<T> & b,
const std::vector<size_t> & node_order,
257 const std::vector<size_t> & arc_order,
const int scale = 0)
259 b.nodes.assign(inst.n,
nullptr);
260 for (
const size_t i : node_order)
261 b.
nodes[i] = b.g.insert_node(static_cast<int>(i));
262 b.arcs.assign(inst.edges.size(),
nullptr);
263 for (
const size_t k : arc_order)
265 const auto & [u, v,
w] = inst.edges[
k];
266 b.arcs[
k] = b.g.insert_arc(b.nodes[u], b.nodes[v], std::ldexp(
w, scale));
268 b.hidden.insert(b.arcs[
k]);
272 template <
typename T>
273 void build(
const Instance<T> & inst, Built<T> & b)
275 std::vector<size_t>
nodes(inst.n);
276 std::vector<size_t>
arcs(inst.edges.size());
277 for (
size_t i = 0; i <
nodes.size(); ++i)
279 for (
size_t i = 0; i <
arcs.size(); ++i)
284 template <
typename T>
287 const std::set<typename Graph_T<T>::Arc *> * hidden =
nullptr;
289 bool operator()(
typename Graph_T<T>::Arc * arc)
const
291 return hidden ==
nullptr or hidden->count(arc) == 0;
300 bool any_cycle =
false;
301 bool any_bounded =
false;
303 size_t best_total_length = 0;
304 Rational best_mean_total;
305 size_t best_mean_length = 1;
308 template <
typename T>
309 Oracle
oracle(
const Instance<T> & inst,
const size_t bound)
312 std::vector<bool> on_path(inst.n,
false);
313 std::vector<Rational> weights;
314 for (
const auto & e : inst.edges)
315 weights.push_back(Rational::of(
std::get<2>(e)));
317 std::function<void(
size_t,
size_t,
const Rational &,
size_t)>
dfs =
318 [&](
const size_t start,
const size_t u,
const Rational & total,
const size_t length)
320 for (
size_t k = 0;
k < inst.edges.size(); ++
k)
322 const auto & [a, b,
w] = inst.edges[
k];
323 if (a != u or inst.hidden[
k])
329 const size_t len = length + 1;
330 if (not o.any_cycle or
mean_less(t, len, o.best_mean_total, o.best_mean_length))
332 o.best_mean_total = t;
333 o.best_mean_length = len;
336 if (len <= bound
and (not o.any_bounded or compare(t, o.best_total) < 0
337 or (compare(t, o.best_total) == 0
and len < o.best_total_length)))
340 o.best_total_length = len;
341 o.any_bounded =
true;
345 if (b < start or on_path[b])
348 dfs(start, b, t, length + 1);
352 for (
size_t s = 0; s < inst.n; ++s)
355 dfs(s, s, Rational(), 0);
363 template <
typename T>
364 bool exact_witness(
const Built<T> & b,
const DynList<
typename Graph_T<T>::Arc *> &
arcs,
365 Rational & total,
size_t & length)
367 std::set<typename Graph_T<T>::Node *> seen;
368 typename Graph_T<T>::Node * first =
nullptr;
369 typename Graph_T<T>::Node *
current =
nullptr;
372 for (
auto it =
arcs.get_it(); it.has_curr(); it.next_ne())
374 auto * arc = it.get_curr();
375 if (b.hidden.count(arc) != 0)
377 auto * src = b.g.get_src_node(arc);
378 if (first ==
nullptr)
380 if (src != current or not seen.insert(src).second)
382 current = b.g.get_tgt_node(arc);
383 total += Rational::of(arc->get_info());
393 template <
typename T>
394 void check_short(
const Instance<T> & inst,
const Built<T> & b,
const size_t bound)
396 using G = Graph_T<T>;
397 const Oracle o =
oracle(inst, bound);
398 const auto r = most_negative_cycle_up_to_3<G, Dft_Dist<G>, Hide_Set<T>>(
399 b.g, bound,
Dft_Dist<G>(), Hide_Set<T>{&b.hidden});
400 ASSERT_EQ(
r.has_cycle, o.any_bounded);
406 ASSERT_TRUE(exact_witness(b,
r.cycle_arcs, total, length));
407 ASSERT_EQ(length,
r.length);
408 ASSERT_EQ(length, o.best_total_length);
409 ASSERT_EQ(compare(total, o.best_total), 0);
410 ASSERT_TRUE(within_one_ulp(
r.total_cost, total));
411 ASSERT_EQ(
r.is_negative(), total.sign() < 0);
414 template <
typename T>
415 void check_bounded(
const Instance<T> & inst,
const Built<T> & b,
const size_t bound,
416 const char * family,
const size_t trial)
418 using G = Graph_T<T>;
419 const Oracle o =
oracle(inst, bound);
420 const auto r = most_negative_cycle_bounded<G, Dft_Dist<G>, Hide_Set<T>>(
421 b.g, bound,
Dft_Dist<G>(), Hide_Set<T>{&b.hidden});
422 ASSERT_EQ(
r.has_cycle, o.any_bounded) << family <<
" trial=" << trial <<
" L=" << bound;
428 ASSERT_TRUE(exact_witness(b,
r.cycle_arcs, total, length)) << family <<
" trial=" << trial;
429 ASSERT_EQ(length,
r.length) << family <<
" trial=" << trial;
430 ASSERT_LE(length, bound) << family <<
" trial=" << trial;
431 ASSERT_TRUE(within_one_ulp(
r.total_cost, total)) << family <<
" trial=" << trial;
432 ASSERT_EQ(
r.is_negative(), total.sign() < 0) << family <<
" trial=" << trial;
435 ASSERT_GE(
r.optimality_gap,
T(0)) << family <<
" trial=" << trial;
436 ASSERT_LE(compare(total - o.best_total, Rational::of(
r.optimality_gap)), 0)
437 << family <<
" trial=" << trial <<
" L=" << bound;
438 ASSERT_EQ(
r.is_exact,
r.optimality_gap ==
T(0)) << family <<
" trial=" << trial;
440 ASSERT_EQ(compare(total, o.best_total), 0) << family <<
" trial=" << trial;
443 template <
typename T>
444 void check_howard(
const Instance<T> & inst,
const Built<T> & b,
const char * family,
447 using G = Graph_T<T>;
448 const Oracle o =
oracle(inst, inst.n);
449 const auto h = howard_minimum_mean_cycle<G, Dft_Dist<G>, Hide_Set<T>>(
451 ASSERT_EQ(
h.has_cycle, o.any_cycle) << family <<
" trial=" << trial;
452 if (not
h.has_cycle or
h.used_karp)
457 ASSERT_TRUE(exact_witness(b,
h.cycle_arcs, total, length)) << family <<
" trial=" << trial;
458 ASSERT_EQ(length,
h.cycle_length) << family <<
" trial=" << trial;
459 ASSERT_TRUE(within_one_ulp(
h.cycle_total_cost, total)) << family <<
" trial=" << trial;
460 if (
h.numeric_quality == Cycle_Numeric_Quality::Exact)
461 ASSERT_TRUE(mean_equal(total, length, o.best_mean_total, o.best_mean_length))
462 << family <<
" trial=" << trial;
465 template <
typename T>
466 void check_enumeration(
const Instance<T> & inst,
const Built<T> & b,
const char * family,
469 using G = Graph_T<T>;
470 const Oracle o =
oracle(inst, inst.n);
471 const auto cycles = find_disjoint_negative_cycles<G, Dft_Dist<G>, Hide_Set<T>>(
472 b.g, 3, Negative_Cycle_Exclusion::All_Arcs,
Dft_Dist<G>(), Hide_Set<T>{&b.hidden});
473 std::set<typename G::Arc *> used;
474 for (
auto it = cycles.get_it(); it.has_curr(); it.next_ne())
476 const auto & item = it.get_curr();
477 const auto arcs = item.cycle.arcs();
480 ASSERT_TRUE(exact_witness(b,
arcs, total, length)) << family <<
" trial=" << trial;
481 ASSERT_EQ(length, item.length) << family <<
" trial=" << trial;
482 ASSERT_LT(total.sign(), 0) << family <<
" trial=" << trial;
483 ASSERT_TRUE(within_one_ulp(item.total_cost, total)) << family <<
" trial=" << trial;
484 for (
auto ai =
arcs.get_it(); ai.has_curr(); ai.next_ne())
485 ASSERT_TRUE(used.insert(ai.get_curr()).second) << family <<
" trial=" << trial;
489 if (not o.any_cycle or o.best_mean_total.sign() >= 0)
490 ASSERT_TRUE(cycles.is_empty()) << family <<
" trial=" << trial;
493 const std::pair<Family, const char *> families[] = {
494 {Family::Moderate,
"moderate"},
495 {Family::Ties,
"ties"},
496 {Family::Subnormal,
"subnormal"},
497 {Family::Three_Scales,
"three-scales"},
498 {Family::Market,
"market"},
501 template <
typename T>
502 void run_differential(
const unsigned long long seed,
const size_t trials)
505 for (
const auto & [family, name] : families)
506 for (size_t trial = 0; trial < trials; ++trial)
508 const Instance<T> inst = make_instance<T>(family,
rng);
511 for (
const size_t bound : {size_t(1), size_t(2), size_t(3), inst.n})
512 check_bounded(inst, b, bound, name, trial);
513 check_howard(inst, b, name, trial);
514 check_enumeration(inst, b, name, trial);
515 if (::testing::Test::HasFatalFailure())
521 template <
typename T>
522 void run_short_differential(
const unsigned long long seed)
525 for (
const auto & [family, name] : families)
526 for (size_t trial = 0; trial < 200; ++trial)
528 SCOPED_TRACE(::testing::Message() << name <<
" trial=" << trial);
529 const Instance<T> inst = make_instance<T>(family,
rng);
532 for (
size_t bound = 1; bound <= 3; ++bound)
534 SCOPED_TRACE(::testing::Message() <<
"L=" << bound);
535 check_short(inst, b, bound);
536 if (::testing::Test::HasFatalFailure())
548TEST(NegativeCyclesNumericTest, DoubleWeightsAgreeWithTheExactOracle)
550 run_differential<double>(0x5EED0001ULL, 400);
554TEST(NegativeCyclesNumericTest, FloatWeightsAgreeWithTheExactOracle)
556 run_differential<float>(0x5EED0002ULL, 300);
560TEST(NegativeCyclesNumericTest, LongDoubleWeightsAgreeWithTheExactOracle)
562 run_differential<long double>(0x5EED0003ULL, 300);
566TEST(NegativeCyclesNumericTest, ShortDoubleCyclesAgreeWithTheExactOracle)
568 run_short_differential<double>(0xD40001ULL);
572TEST(NegativeCyclesNumericTest, ShortFloatCyclesAgreeWithTheExactOracle)
574 run_short_differential<float>(0xD40002ULL);
578TEST(NegativeCyclesNumericTest, ShortLongDoubleCyclesAgreeWithTheExactOracle)
580 run_short_differential<long double>(0xD40003ULL);
584TEST(NegativeCyclesNumericTest, ShortOptimaSurvivePermutationAndExactScaling)
586 std::mt19937_64
rng(0xD45CA1E);
587 for (
const auto family : {Family::Moderate, Family::Ties, Family::Three_Scales, Family::Market})
588 for (
size_t trial = 0; trial < 200; ++trial)
590 SCOPED_TRACE(::testing::Message() <<
"family=" <<
int(family) <<
" trial=" << trial);
591 const Instance<double> inst = make_instance<double>(family,
rng);
592 std::vector<size_t>
nodes(inst.n),
arcs(inst.edges.size());
593 for (
size_t i = 0; i <
nodes.size(); ++i)
595 for (
size_t i = 0; i <
arcs.size(); ++i)
597 for (
const int scale : {-7, 0, 9})
602 for (
auto & edge : scaled.edges)
603 std::get<2>(edge) = std::ldexp(std::get<2>(edge), scale);
606 for (
size_t bound = 1; bound <= 3; ++bound)
608 SCOPED_TRACE(::testing::Message() <<
"scale=" << scale <<
" L=" << bound);
609 check_short(scaled, b, bound);
610 if (::testing::Test::HasFatalFailure())
626TEST(NegativeCyclesNumericTest, BoundedSearchIsInvariantUnderScalingByPowersOfTwo)
628 std::mt19937_64
rng(0x5CA1E);
629 for (
const auto family : {Family::Moderate, Family::Ties, Family::Three_Scales, Family::Market})
630 for (
size_t trial = 0; trial < 300; ++trial)
632 const Instance<double> inst = make_instance<double>(family,
rng);
633 std::vector<size_t>
nodes(inst.n);
634 std::vector<size_t>
arcs(inst.edges.size());
635 for (
size_t i = 0; i <
nodes.size(); ++i)
637 for (
size_t i = 0; i <
arcs.size(); ++i)
639 for (
const int scale : {-7, 9})
642 Built<double> scaled;
645 using G = Graph_T<double>;
646 const size_t bound = std::max<size_t>(1, inst.n);
647 const auto a = most_negative_cycle_bounded<G, Dft_Dist<G>, Hide_Set<double>>(
648 plain.g, bound,
Dft_Dist<G>(), Hide_Set<double>{&plain.hidden});
649 const auto b = most_negative_cycle_bounded<G, Dft_Dist<G>, Hide_Set<double>>(
650 scaled.g, bound,
Dft_Dist<G>(), Hide_Set<double>{&scaled.hidden});
651 ASSERT_EQ(a.has_cycle, b.has_cycle) <<
"trial=" << trial;
654 ASSERT_EQ(b.total_cost, std::ldexp(a.total_cost, scale)) <<
"trial=" << trial;
655 ASSERT_EQ(b.optimality_gap, std::ldexp(a.optimality_gap, scale)) <<
"trial=" << trial;
656 ASSERT_EQ(a.is_exact, b.is_exact) <<
"trial=" << trial;
657 ASSERT_EQ(a.matches_relaxed_bound, b.matches_relaxed_bound) <<
"trial=" << trial;
658 ASSERT_EQ(a.length, b.length) <<
"trial=" << trial;
668TEST(NegativeCyclesNumericTest, CertifiedResultsDoNotDependOnArcOrNodeOrder)
670 std::mt19937_64
rng(0x0D3E5);
671 for (
const auto family : {Family::Moderate, Family::Ties, Family::Three_Scales, Family::Market})
672 for (
size_t trial = 0; trial < 200; ++trial)
674 const Instance<double> inst = make_instance<double>(family,
rng);
675 const Oracle o =
oracle(inst, inst.n);
676 std::vector<size_t>
nodes(inst.n);
677 std::vector<size_t>
arcs(inst.edges.size());
678 for (
size_t i = 0; i <
nodes.size(); ++i)
680 for (
size_t i = 0; i <
arcs.size(); ++i)
688 using G = Graph_T<double>;
690 const auto r = most_negative_cycle_bounded<G, Dft_Dist<G>, Hide_Set<double>>(
691 b.g, std::max<size_t>(1, inst.n),
Dft_Dist<G>(), Hide_Set<double>{&b.hidden});
696 ASSERT_TRUE(exact_witness(b,
r.cycle_arcs, total, length));
697 ASSERT_EQ(compare(total, o.best_total), 0) <<
"trial=" << trial;
700 const auto h = howard_minimum_mean_cycle<G, Dft_Dist<G>, Hide_Set<double>>(
702 if (
h.has_cycle
and not
h.used_karp
and h.numeric_quality == Cycle_Numeric_Quality::Exact)
706 ASSERT_TRUE(exact_witness(b,
h.cycle_arcs, total, length));
707 ASSERT_TRUE(mean_equal(total, length, o.best_mean_total, o.best_mean_length))
708 <<
"trial=" << trial;
715TEST(NegativeCyclesNumericTest, ExactUpperBoundBeyondTheRangeIsInfinity)
721 const double max = std::numeric_limits<double>::max();
723 Exact_Sum<double> above;
724 ASSERT_TRUE(above.add(
max));
725 ASSERT_TRUE(above.add(1.0));
726 EXPECT_EQ(above.upper(), std::numeric_limits<double>::infinity());
730 Exact_Sum<double> at;
731 ASSERT_TRUE(at.add(
max));
732 EXPECT_EQ(at.upper(),
max);
734 Exact_Sum<double> just_above_one;
735 ASSERT_TRUE(just_above_one.add(1.0));
736 ASSERT_TRUE(just_above_one.add(0x1p-60));
737 EXPECT_EQ(just_above_one.upper(), std::nextafter(1.0, 2.0));
739 Exact_Sum<double> below_minus_max;
740 ASSERT_TRUE(below_minus_max.add(-
max));
741 ASSERT_TRUE(below_minus_max.add(-1.0));
742 EXPECT_EQ(below_minus_max.upper(), -
max);
749 using Component = Exact_Sum<double>::Component;
750 const Component component_max = std::numeric_limits<Component>::max();
751 Exact_Sum<double> broken;
752 ASSERT_TRUE(broken.add(-component_max));
753 ASSERT_FALSE(broken.add(-component_max));
754 EXPECT_EQ(broken.upper(), std::numeric_limits<double>::infinity());
758TEST(NegativeCyclesNumericTest, ExactSumsNearTheLargestFiniteValueStayExact)
767 const double max = std::numeric_limits<double>::max();
768 const double a = 0x1p968;
769 const double c = -(0x1p1022 - 0x1p970);
772 ASSERT_TRUE(s.add(a));
773 ASSERT_TRUE(s.add(
max));
774 ASSERT_TRUE(s.add(c));
775 EXPECT_EQ(s.sign(), 1);
776 EXPECT_EQ(s.rounded(), 0x1.8p1023);
779 ASSERT_TRUE(t.add(-0x1p1023));
780 ASSERT_TRUE(t.add(-0x3p970));
781 ASSERT_TRUE(t.add(
max));
782 EXPECT_EQ(t.sign(), 1);
783 EXPECT_EQ(t.rounded(), 0x1.ffffffffffffbp1022);
786 ASSERT_TRUE(f.add(0x1p101f));
787 ASSERT_TRUE(f.add(std::numeric_limits<float>::max()));
788 ASSERT_TRUE(f.add(-(0x1p126f - 0x1p102f)));
789 EXPECT_EQ(f.sign(), 1);
790 EXPECT_EQ(f.rounded(), 0x1.7ffffep127f);
793 using G = Graph_T<double>;
803 ASSERT_TRUE(u3.has_cycle);
804 EXPECT_TRUE(u3.is_negative());
805 EXPECT_EQ(u3.total_cost, -0x1.8p1023);
808 ASSERT_TRUE(bounded.has_cycle);
809 EXPECT_EQ(bounded.total_cost, -0x1.8p1023);
812 ASSERT_EQ(found.size(), 1u);
813 EXPECT_EQ(found.get_first().total_cost, -0x1.8p1023);
816 ASSERT_TRUE(
h.has_cycle);
817 EXPECT_EQ(
h.cycle_total_cost, -0x1.8p1023);
818 EXPECT_LT(
h.minimum_mean, 0.0L);
822TEST(NegativeCyclesNumericTest, TotalsBeyondTheRangeThrowWhateverTheEvaluationMethod)
828 using G = Graph_T<double>;
829 const double max = std::numeric_limits<double>::max();
Minimum mean cycle by Howard's policy iteration.
Enumeration of negative cycles and bounded-length most negative cycle in directed graphs.
Default distance accessor for arc weights.
Generic directed graph (digraph) wrapper template.
Doubly-linked list (defined in tpl_dynList.H).
Graph implemented with double-linked adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Exact sum of floating-point numbers.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
DynList< Negative_Cycle_Item< GT, typename Distance::Distance_Type > > find_disjoint_negative_cycles(const GT &g, const size_t max_cycles, const Negative_Cycle_Exclusion exclusion=Negative_Cycle_Exclusion::Min_Weight_Arc, Distance distance=Distance(), SA sa=SA())
Find up to max_cycles negative cycles, excluding arcs of each cycle found before searching for the ne...
Bounded_Cycle_Result< GT, typename Distance::Distance_Type > most_negative_cycle_bounded(const GT &g, const size_t max_length, Distance distance=Distance(), SA sa=SA())
Most negative simple cycle of at most max_length arcs, ranked by total cost (not by mean cost as Karp...
Short_Cycle_Result< GT, typename Distance::Distance_Type > most_negative_cycle_up_to_3(const GT &g, const size_t max_length=3, Distance distance=Distance(), SA sa=SA())
Minimum-total-cost simple cycle with at most one, two or three arcs.
Howard_Mean_Cycle_Result< GT, typename Distance::Distance_Type > 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.
decltype(auto) current(It &it) noexcept(current_is_noexcept< It >)
Current item of an iterator that is positioned on one.
void build(Tree_Node< Key > *troot, Convert &&convert, typename GT::Node *groot, Args &&... args)
DFSResult< typename Domain::Distance > dfs(Domain &domain, typename Domain::State &state, SearchPath< typename Domain::Move > &path, const typename Domain::Distance g, const typename Domain::Distance threshold, const size_t depth, IDAStarResult< Solution, typename Domain::Distance > &result, OnSolution &on_solution)
Core recursive DFS used by IDA* for a single threshold pass.
bool mean_less(const Acc c1, const size_t l1, const Acc c2, const size_t l2)
Whether the mean c1 / l1 is smaller than c2 / l2.
Main namespace for Aleph-w library functions.
Container2< typename Container1::Item_Type > filter(Container1 &container, Operation &operation)
Filter elements that satisfy operation.
static long & low(typename GT::Node *p)
Internal helper: low-link value stored directly in NODE_COOKIE(p).
std::decay_t< typename HeadC::Item_Type > T
Gen_MultiPolynomial< C, M > operator-(const C &s, const Gen_MultiPolynomial< C, M > &p)
Scalar - polynomial.
and
Check uniqueness with explicit hash + equality functors.
auto shuffle(const C< T > &c)
Randomly shuffle a sequence.
std::vector< std::string > & split(const std::string &s, const char delim, std::vector< std::string > &elems)
Split a std::string by a single delimiter character.
Container< T > range(const T start, const T end, const T step=1)
Generate a range of values [start, end] with a given step.
Arc of graph implemented with double-linked adjacency lists.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Generic graph and digraph implementations.