41# include <gtest/gtest.h>
68 template <
typename Acc>
78 return total *
l < t * length;
85 template <
typename Acc =
long long,
class Built>
88 using GT =
decltype(built.g);
89 const size_t n = built.
nodes.size();
90 std::vector<bool> visited(n,
false);
93 std::function<
void(
size_t,
size_t,
Acc,
long long)>
dfs =
94 [&](
const size_t start,
const size_t u,
const Acc total,
const long long len)
96 for (
Node_Arc_Iterator<std::remove_cv_t<std::remove_reference_t<GT>>> it(built.nodes[u]);
99 const size_t v =
static_cast<size_t>(it.get_tgt_node()->get_info());
100 const Acc w = it.get_current_arc_ne()->get_info();
103 if (
not best.exists
or (total +
w) * best.length < best.total * (len + 1))
107 if (v < start
or visited[v])
110 dfs(start, v, total +
w, len + 1);
115 for (
size_t s = 0; s < n; ++s)
128 template <
class GT,
typename Cost>
133 return r.cycle_length == 0
and r.cycle_nodes.is_empty()
and r.cycle_arcs.is_empty();
135 if (
r.cycle_length == 0
or r.cycle_nodes.size() !=
r.cycle_length + 1
136 or r.cycle_arcs.size() !=
r.cycle_length)
139 std::set<typename GT::Node *> seen;
140 auto node_it =
r.cycle_nodes.get_it();
145 long double sum = 0.0L;
157 if (curr != first
or node_it.has_curr()
or r.witness_node != first)
161 const long double mean =
sum /
static_cast<long double>(
r.cycle_length);
162 if constexpr (std::is_floating_point_v<Cost>)
163 return std::fabs(mean -
r.minimum_mean) <= 1e-9L * (1.0L + std::fabs(mean))
164 and std::fabs(sum -
static_cast<long double>(
r.cycle_total_cost))
165 <= 1e-9L * (1.0L + std::fabs(sum));
167 return mean ==
r.minimum_mean
and sum ==
static_cast<long double>(
r.cycle_total_cost);
189 auto built =
build_graph(4, {{0, 1, -5}, {1, 2, -5}, {0, 2, -9}, {2, 3, -1}});
198 auto built =
build_graph(3, {{0, 0, -2}, {0, 1, 4}, {1, 2, 4}, {2, 0, 4}});
205 EXPECT_EQ(
r.numeric_quality, Cycle_Numeric_Quality::Exact);
214 {2, 3, 1}, {3, 4, 1}, {4, 2, 1},
215 {5, 6, 2}, {6, 5, 2}, {1, 2, 9}});
226 auto built =
build_graph(3, {{0, 1, 2}, {1, 2, -7}, {2, 0, 1}});
231 EXPECT_NEAR(
static_cast<double>(
r.minimum_mean), -4.0 / 3.0, 1e-15);
238 auto built =
build_graph(4, {{0, 1, -4}, {1, 0, 1}, {2, 3, -2}, {3, 2, -2}});
256 3, {{0, 1, 0.5}, {1, 2, -1.25}, {2, 0, 0.25}, {2, 2, 0.1}});
260 EXPECT_NEAR(
static_cast<double>(
r.minimum_mean), -0.5 / 3.0, 1e-12);
265 EXPECT_EQ(
r.numeric_quality, Cycle_Numeric_Quality::Exact);
279 inf_graph.insert_arc(a, b, std::numeric_limits<double>::infinity());
285 nan_graph.insert_arc(c, c, std::numeric_limits<double>::quiet_NaN());
299 const long long M = std::numeric_limits<long long>::max();
300 auto built =
build_graph(2, {{0, 1, M - 1}, {1, 0, 2}});
309 3, {{0, 1, 3}, {1, 2, -5}, {2, 0, 1}, {1, 1, 4}});
336 template <
class Result>
337 long double mean_of(
const Result &
r)
339 return r.has_cycle ?
r.minimum_mean : std::numeric_limits<long double>::infinity();
346 auto built =
build_graph(3, {{0, 1, 2}, {1, 2, -7}, {2, 0, 1}});
361 std::vector<Edge_Def> edges = {{0, 1, -1}, {1, 2, -1}, {2, 0, -1}};
373 EXPECT_EQ(karp.minimum_mean, howard.minimum_mean);
380 const long long M = std::numeric_limits<long long>::max();
388 const long long lowest = std::numeric_limits<long long>::min();
406 4, {{2, 2, 653666155}, {2, 2, 585777688}, {1, 3, 641209630}, {3, 0, 491799673},
407 {3, 3, -1900485882}});
417 EXPECT_EQ(karp.cycle_total_cost, -1900485882);
422 4, {{0, 1, 2000000000}, {1, 2, 2000000000}, {2, 3, -2000000000}});
429 auto built =
build_graph(3, {{0, 1, 1}, {1, 2, -3}, {2, 0, 1}, {0, 2, 7}, {2, 2, 4}});
445 auto built =
build_graph(4, {{0, 1, 1}, {1, 2, -3}, {2, 0, 1}, {2, 3, -2}, {3, 2, 1}});
462 std::mt19937_64
rng(0x4F5741524431ULL);
463 std::uniform_int_distribution<int>
n_dist(1, 7);
464 std::bernoulli_distribution
has_edge(0.4);
465 std::bernoulli_distribution has_loop(0.12);
466 std::uniform_int_distribution<int>
weight_dist(-6, 9);
471 const size_t n =
static_cast<size_t>(
n_dist(
rng));
472 std::vector<Edge_Def> edges;
473 for (
size_t u = 0; u < n; ++u)
474 for (
size_t v = 0; v < n; ++v)
477 edges.emplace_back(u, v,
static_cast<long long>(
weight_dist(
rng)));
479 edges.emplace_back(u, v,
static_cast<long long>(
weight_dist(
rng)));
491 if (
not howard.has_cycle)
496 ASSERT_EQ(howard.cycle_total_cost *
oracle.length,
oracle.total *
static_cast<long long>(howard.cycle_length))
497 <<
"trial=" <<
trial;
498 ASSERT_EQ(howard.minimum_mean, karp.minimum_mean) <<
"trial=" <<
trial;
506 std::mt19937_64
rng(0xB16B00B5ULL);
507 std::uniform_int_distribution<int>
n_dist(8, 60);
508 std::uniform_int_distribution<int>
weight_dist(-40, 90);
512 const size_t n =
static_cast<size_t>(
n_dist(
rng));
513 const size_t m = n +
static_cast<size_t>(
rng() % (4 * n));
514 std::vector<Edge_Def> edges;
515 for (
size_t i = 0; i <
m; ++i)
517 const size_t u =
rng() % n;
518 const size_t v =
rng() % n;
519 edges.emplace_back(u, v,
static_cast<long long>(
weight_dist(
rng)));
528 if (
not howard.has_cycle)
530 ASSERT_EQ(howard.minimum_mean, karp.minimum_mean) <<
"trial=" <<
trial;
538 std::mt19937_64
rng(0xF10A7ULL);
539 std::uniform_int_distribution<int>
n_dist(2, 40);
540 std::uniform_real_distribution<double>
weight_dist(-1.0, 2.0);
544 const size_t n =
static_cast<size_t>(
n_dist(
rng));
545 const size_t m = n +
static_cast<size_t>(
rng() % (3 * n));
546 std::vector<std::tuple<size_t, size_t, double>> edges;
547 for (
size_t i = 0; i <
m; ++i)
549 const size_t u =
rng() % n;
550 const size_t v =
rng() % n;
560 if (
not howard.has_cycle)
562 ASSERT_NEAR(
static_cast<double>(howard.minimum_mean),
static_cast<double>(karp.minimum_mean), 1e-12)
563 <<
"trial=" <<
trial;
575 std::mt19937_64
rng(0x5A11A11ULL);
576 std::uniform_real_distribution<double> weight(-0.001, 0.003);
580 const size_t n = 3 +
rng() % 28;
581 const size_t m = n +
rng() % (3 * n);
582 std::vector<std::tuple<size_t, size_t, double>> edges;
583 for (
size_t i = 0; i <
m; ++i)
585 const size_t u =
rng() % n;
586 const size_t v =
rng() % n;
587 edges.emplace_back(u, v, weight(
rng));
595 if (
not howard.has_cycle)
597 ASSERT_NEAR(
static_cast<double>(howard.minimum_mean),
static_cast<double>(karp.minimum_mean), 1e-12)
598 <<
"trial=" <<
trial;
607 std::mt19937_64
rng(0xDEC1A1ULL);
608 const double menu[] = {0.1, 0.2, 0.3, 0.7, -0.1, 0.4};
612 const size_t n = 3 +
rng() % 12;
613 const size_t m = n +
rng() % (3 * n);
614 std::vector<std::tuple<size_t, size_t, double>> edges;
615 for (
size_t i = 0; i <
m; ++i)
617 const size_t u =
rng() % n;
618 const size_t v =
rng() % n;
619 edges.emplace_back(u, v,
menu[
rng() % 6]);
627 if (howard.has_cycle)
628 ASSERT_NEAR(
static_cast<double>(howard.minimum_mean),
static_cast<double>(karp.minimum_mean), 1e-12)
629 <<
"trial=" <<
trial;
638 auto built =
build_graph(6, {{0, 1, 1}, {1, 2, 1}, {2, 0, 1},
639 {3, 4, -1}, {4, 3, 1},
659 auto built =
build_graph(8, {{5, 0, -4}, {4, 7, 7}, {0, 1, 2}, {1, 2, 3}, {0, 7, 3}, {7, 2, 1},
660 {7, 5, 4}, {3, 7, 1}, {1, 1, 0}, {7, 3, -5}, {2, 0, 5}, {1, 2, 5},
661 {7, 0, -3}, {7, 7, 4}, {0, 4, 2}, {5, 5, 5}, {1, 0, -3}, {7, 2, 7},
662 {4, 4, -2}, {0, 4, 6}, {0, 3, 3}});
687 5, {{2, 3, 0.0}, {3, 4, -1
e20}, {3, 0, 1.0}, {0, 2, 0.0}, {1, 2, 3.0}, {4, 1, 1
e20}});
700 auto built =
build_graph(4, {{0, 1, 1}, {1, 0, 1}, {0, 2, 2}, {2, 3, -9}, {3, 0, 2}, {1, 2, 5}});
713 EXPECT_EQ(normal.numeric_quality, Cycle_Numeric_Quality::Exact);
729 const long long big = 3100000000000000000LL;
730 auto built =
build_graph(3, {{0, 1, big}, {1, 2, -big}, {2, 0, 200000000000000000LL}});
736 EXPECT_EQ(howard.minimum_mean, karp.minimum_mean);
737 EXPECT_EQ(howard.minimum_mean, 200000000000000000.0L / 3.0L);
747 auto built =
build_graph(4, {{0, 1, 1}, {1, 0, 1}, {0, 2, 2}, {2, 3, -9}, {3, 0, 2}, {1, 2, 5}});
751 const auto r = howard_detail::minimum_mean_cycle<Graph, Counting_Dist, Counting_Filter>(
769 const auto same = [](
const auto & full,
const auto &
value) -> ::testing::AssertionResult
771 if (full.has_cycle !=
value.has_cycle)
772 return ::testing::AssertionFailure() <<
"has_cycle";
773 if (
not full.has_cycle)
774 return ::testing::AssertionSuccess();
775 if (full.minimum_mean !=
value.minimum_mean
776 or std::signbit(full.minimum_mean) != std::signbit(
value.minimum_mean))
777 return ::testing::AssertionFailure()
778 <<
"minimum_mean " << full.minimum_mean <<
" against " <<
value.minimum_mean;
779 if (full.iterations !=
value.iterations
or full.used_karp !=
value.used_karp
780 or full.fallback_reason !=
value.fallback_reason
781 or full.numeric_quality !=
value.numeric_quality)
782 return ::testing::AssertionFailure() <<
"how the answer was obtained";
783 if (full.used_karp != (full.fallback_reason != Howard_Fallback_Reason::None))
784 return ::testing::AssertionFailure() <<
"used_karp disagrees with fallback_reason";
786 or value.witness_node !=
nullptr)
787 return ::testing::AssertionFailure() <<
"the value-only result built a witness";
788 return ::testing::AssertionSuccess();
791 std::mt19937_64
rng(0xC2);
796 const size_t n = 2 +
rng() % 8;
797 const size_t m = n +
rng() % (2 * n);
798 std::vector<Edge_Def>
iedges;
799 std::vector<Edge_Def>
bedges;
800 std::vector<std::tuple<size_t, size_t, double>>
fedges;
801 for (
size_t i = 0; i <
m; ++i)
803 const size_t u =
rng() % n;
804 const size_t v =
rng() % n;
805 const long long w =
static_cast<long long>(
rng() % 41) - 20;
806 const long long big = (1LL << 60) +
static_cast<long long>(
rng() % (1ULL << 61));
807 const double unit = std::uniform_real_distribution<double>(-1.0, 1.0)(
rng);
808 const int exponent =
static_cast<int>(
rng() % 40) - 20;
810 bedges.emplace_back(u, v,
rng() % 2 ? big : -big);
811 fedges.emplace_back(u, v, std::ldexp(
unit, exponent));
821 for (
auto * built : {&
ib, &
bb})
824 try { full = howard_detail::minimum_mean_cycle<Graph, D, S>(built->g, D(), S(), limit); }
825 catch (
const std::overflow_error &) {
continue; }
826 const auto value = howard_detail::minimum_mean_cycle<Graph, D, S, false>(
827 built->g, D(), S(), limit);
835 const auto ffull = howard_detail::minimum_mean_cycle<Float_Graph, FD, FS>(
fb.g,
FD(),
FS(), limit);
837 howard_detail::minimum_mean_cycle<Float_Graph, FD, FS, false>(
fb.g,
FD(),
FS(), limit);
857 auto built =
build_graph(4, {{0, 1, 1}, {1, 0, 1}, {0, 2, 2}, {2, 3, -9}, {3, 0, 2}, {1, 2, 5}});
863 const auto r = howard_detail::minimum_mean_cycle<Graph, Counting_Dist, Counting_Filter, false>(
881 const long long max = std::numeric_limits<long long>::max();
885# if ALEPH_KARP_INT128
904 auto limit_graph =
build_graph(4, {{0, 1, 1}, {1, 0, 1}, {0, 2, 2}, {2, 3, -9}, {3, 0, 2}, {1, 2, 5}});
907 EXPECT_EQ(normal.fallback_reason, Howard_Fallback_Reason::None);
910 const auto limited = howard_detail::minimum_mean_cycle<Graph, D, S>(
limit_graph.g, D(), S(), 0);
912 EXPECT_EQ(
limited.fallback_reason, Howard_Fallback_Reason::Iteration_Limit);
915 const long long big = 3100000000000000000LL;
919 EXPECT_EQ(overflowed.fallback_reason, Howard_Fallback_Reason::Arithmetic_Overflow);
928 const int imax = std::numeric_limits<int>::max();
931 const auto value = howard_detail::minimum_mean_cycle<Int_Graph, ID, IS, false>(
935 EXPECT_EQ(
value.fallback_reason, Howard_Fallback_Reason::Cost_Out_Of_Range);
949 auto built =
build_graph(7, {{6, 1, 5062036987607531343LL}, {4, 3, -3695868834463239656LL},
950 {4, 1, -3661910840776953621LL}, {2, 6, 5139072827235855353LL},
951 {6, 2, -2377556606531334447LL}, {1, 6, -3191812457526854301LL},
952 {6, 5, -2872654956802585972LL}, {0, 4, -4120068231194526993LL},
953 {5, 2, -6893328469635551422LL}, {1, 5, -3920853593594695630LL},
954 {0, 2, -5174462936561685175LL}, {6, 1, 5725667193745753167LL},
955 {5, 6, 4628323756336527655LL}, {3, 5, 5325742771487410443LL}});
960 EXPECT_EQ(
r.fallback_reason, Howard_Fallback_Reason::None);
961 EXPECT_EQ(
r.numeric_quality, Cycle_Numeric_Quality::Exact);
962 EXPECT_EQ(
r.cycle_total_cost, -4626910599202282041LL);
965# if ALEPH_KARP_INT128
984 const long long big = 3100000000000000000LL;
986 {{0, 1, big}, {1, 2, -big}, {2, 0, 1}, {3, 4, -1}, {4, 5, -1}, {5, 3, -2}});
993# if ALEPH_KARP_INT128
1020 EXPECT_EQ(
limited.fallback_reason, Howard_Fallback_Reason::Arithmetic_Overflow);
1030 unlimited.minimum_mean) <<
"limit=" << limit;
1050 FAIL() <<
"the limit was ignored";
1052 catch (
const std::length_error & e)
1054 const std::string what = e.what();
1055 EXPECT_NE(what.find(
"arithmetic overflow"), std::string::npos) << what;
1057 EXPECT_NE(what.find(
"component of 3 nodes"), std::string::npos) << what;
1058 EXPECT_NE(what.find(
"limit of 100 bytes"), std::string::npos) << what;
1063 auto plain =
build_graph(4, {{0, 1, 1}, {1, 0, 1}, {0, 2, 2}, {2, 3, -9}, {3, 0, 2}, {1, 2, 5}});
1070 (
void) howard_detail::minimum_mean_cycle<Graph, D, S>(plain.g, D(), S(), 0, 0);
1071 FAIL() <<
"a limit of 0 let the fallback run";
1073 catch (
const std::length_error & e)
1075 EXPECT_NE(std::string(e.what()).find(
"iteration limit"), std::string::npos) << e.what();
1087 const long long big = 3100000000000000000LL;
1088 std::vector<std::tuple<size_t, size_t, long long>> edges = {{0, 1, big}, {1, 2, -big}, {2, 0, 1}};
1089 for (
size_t i = 0; i < 40; ++i)
1090 edges.emplace_back(3 + i, 3 + (i + 1) % 40,
static_cast<long long>(i) - 20);
1098# if ALEPH_KARP_INT128
1105 const long long max = std::numeric_limits<long long>::max();
1106 auto built =
build_graph(3, {{0, 1,
max}, {1, 0, 1}, {2, 2, -1}});
1110 EXPECT_EQ(
r.fallback_reason, Howard_Fallback_Reason::Arithmetic_Overflow);
1123 using U =
unsigned long long;
1125 const U m = std::numeric_limits<U>::max();
1127 6, {{4, 2, 6}, {1, 5, 7}, {3, 4, 0}, {5, 0, 7}, {5, 4, 3}, {5, 0, 1}, {2, 3,
m - 2},
1128 {4, 1, 1}, {3, 2,
m}, {3, 3, 7}, {0, 5,
m - 2}, {3, 4, 6}, {4, 2, 1}});
1132 EXPECT_EQ(
r.numeric_quality, Cycle_Numeric_Quality::Exact);
1140 auto large =
build_graph_generic<U_Graph, U>(3, {{0, 1,
m}, {1, 0,
m}, {2, 2,
m - 1}});
1156 5, {{2, 1, -32767}, {4, 3, 32761}, {1, 2, -32761}, {2, 4, -32768},
1157 {3, 2, -32768}, {3, 3, -32764}, {4, 1, 32760}});
1163 EXPECT_EQ(
r.fallback_reason, Howard_Fallback_Reason::Cost_Out_Of_Range);
1172 const long long a = 1LL << 60;
1176 for (
size_t i = 0; i < 6; ++i)
1177 six.emplace_back(i, (i + 1) % 6, i == 5 ? a + 1 : a);
1178 for (
size_t i = 0; i < 7; ++i)
1179 seven.emplace_back(6 + i, 6 + (i + 1) % 7, i == 6 ? a + 1 : a);
1182 edges.insert(edges.end(),
rest.begin(),
rest.end());
1204 const size_t isolated = 2000;
1205 std::vector<Edge_Def> edges;
1206 for (
size_t base : {0u, 4u})
1207 for (
const auto &[u, v,
w] : std::vector<Edge_Def>{{0, 1, 1}, {1, 0, 1}, {0, 2, 2},
1208 {2, 3, -9}, {3, 0, 2}, {1, 2, 5}})
1209 edges.emplace_back(base + u, base + v,
w);
1210 edges.emplace_back(8, 9, -1);
1211 edges.emplace_back(9, 8, -1);
1212 edges.emplace_back(9, 0, -100);
1213 edges.emplace_back(3, 4, -100);
1214 edges.emplace_back(10, 8, -100);
1226 for (
auto it =
r.cycle_nodes.get_it(); it.has_curr(); it.next_ne())
1227 EXPECT_LT(it.get_curr()->get_info(), 4);
1242 std::mt19937_64
rng(0xC4);
1246 const size_t n = 2 +
rng() % 8;
1247 const size_t m = n +
rng() % (2 * n);
1248 std::vector<Edge_Def>
iedges;
1249 std::vector<std::tuple<size_t, size_t, double>>
fedges;
1250 for (
size_t i = 0; i <
m; ++i)
1252 const size_t u =
rng() % n;
1253 const size_t v =
rng() % n;
1254 const long long w =
static_cast<long long>(
rng() % 41) - 20;
1255 const double unit = std::uniform_real_distribution<double>(-1.0, 1.0)(
rng);
1256 const int exponent =
static_cast<int>(
rng() % 40) - 20;
1258 fedges.emplace_back(u, v, std::ldexp(
unit, exponent));
1262 const auto ir = howard_detail::minimum_mean_cycle<Graph, Dft_Dist<Graph>,
Dft_Show_Arc<Graph>>(
1264 ASSERT_EQ(ir.used_karp, ir.fallback_reason != Howard_Fallback_Reason::None) <<
"trial=" <<
trial;
1268 ASSERT_EQ(ir.fallback_reason, Howard_Fallback_Reason::Iteration_Limit) <<
"trial=" <<
trial;
1277 ASSERT_EQ(
fr.used_karp,
fr.fallback_reason != Howard_Fallback_Reason::None) <<
"trial=" <<
trial;
1281 ASSERT_EQ(
fr.fallback_reason, Howard_Fallback_Reason::Iteration_Limit) <<
"trial=" <<
trial;
1283 ASSERT_EQ(
fr.numeric_quality, Cycle_Numeric_Quality::Rounded);
1297 auto built =
build_graph(6, {{3, 4, 2412223573619837724LL}, {2, 3, 2422047310724953863LL},
1298 {1, 2, 3179646298365416052LL}, {0, 1, -3127445719569793886LL},
1299 {1, 2, 3008898773151771847LL}, {4, 4, 3004633816298768677LL},
1300 {0, 5, 2726043725430506698LL}, {3, 5, 1938808504231304943LL},
1301 {4, 1, 3287975327571625454LL}, {1, 2, 2527433209753059253LL},
1302 {1, 1, 1921533484238488548LL}, {5, 3, 1325352911222347034LL},
1303 {2, 3, 2519116059040766616LL}, {2, 2, 1472205099507841752LL},
1304 {2, 2, 3345130543762169200LL}, {1, 4, 1779708353840524248LL},
1305 {1, 5, 3214578466714837685LL}});
1310# if ALEPH_KARP_INT128
1339 for (
const int e : {50, 100, 200})
1341 const double big = std::ldexp(1.0, e);
1343 5, {{0, 1, big}, {1, 2, 1.0}, {2, 3, -big}, {3, 0, -2.0}, {4, 4, -0.375}});
1349 EXPECT_EQ(
r.minimum_mean, -0.375L) <<
"e=" << e;
1350 EXPECT_EQ(
r.cycle_total_cost, -0.375) <<
"e=" << e;
1355# if ALEPH_HOWARD_INT128
1361 : Cycle_Numeric_Quality::Rounded) <<
"e=" << e;
1374 const double big = std::ldexp(1.0, 122);
1376 5, {{0, 1, big}, {1, 2, 1.0}, {2, 3, -big}, {3, 0, -1.875}, {4, 4, -0.375}});
1381 EXPECT_EQ(
r.numeric_quality, Cycle_Numeric_Quality::Rounded);
1395 for (
const int exponent : {-60, 0})
1397 const double d = std::ldexp(1.0, exponent);
1399 2, {{0, 0, 0.0}, {1, 1, 0.0}, {0, 1, d}, {1, 0, -2 * d}});
1404 EXPECT_EQ(
r.numeric_quality, Cycle_Numeric_Quality::Exact) <<
"exponent=" << exponent;
1406 EXPECT_EQ(
r.minimum_mean, -
static_cast<long double>(d) / 2) <<
"exponent=" << exponent;
1407 EXPECT_EQ(
r.cycle_total_cost, -d) <<
"exponent=" << exponent;
1408 EXPECT_EQ(
r.cycle_length, 2u) <<
"exponent=" << exponent;
1420 const double big = std::ldexp(1.0, 100);
1421 const double tiny = std::ldexp(1.0, -100);
1422 const std::vector<double>
ring = {big, 1.0,
tiny, -1.0, -big, -
tiny};
1426 std::vector<std::tuple<size_t, size_t, double>> edges;
1427 for (
size_t i = 0; i <
ring.size(); ++i)
1435 EXPECT_EQ(
r.numeric_quality, Cycle_Numeric_Quality::Rounded) <<
"rotation=" <<
rotation;
1457 using Mean_Limits = std::numeric_limits<long double>;
1458 std::mt19937_64
rng(0x0F3EF10ULL);
1461 const size_t n = 2 +
rng() % 5;
1462 const long long big = (
trial % 2 == 0) ? 1000000000000000000LL : 2000000000000000000LL;
1463 std::vector<Edge_Def> edges;
1464 const size_t m = n +
rng() % (2 * n);
1465 for (
size_t i = 0; i <
m; ++i)
1467 const size_t u =
rng() % n;
1468 const size_t v =
rng() % n;
1469 const long long w =
static_cast<long long>(
rng() %
static_cast<unsigned long long>(2 * big)) - big;
1470 edges.emplace_back(u, v,
w);
1479 catch (
const std::overflow_error &)
1486# if ALEPH_HOWARD_INT128
1497 <<
"trial=" <<
trial;
1511 const long double tolerance =
1512 Mean_Limits::digits >= 64
1514 : 8.0L * Mean_Limits::epsilon() *
static_cast<long double>(n)
1515 *
static_cast<long double>(big);
1517 <<
"trial=" <<
trial;
1528 const long long hi = std::numeric_limits<long long>::max();
1529 const long long lo = std::numeric_limits<long long>::min();
1531 EXPECT_EQ(Arith::compare_fractions(1, 3, 2, 6), 0);
1532 EXPECT_EQ(Arith::compare_fractions(-1, 3, 1, 3), -1);
1533 EXPECT_EQ(Arith::compare_fractions(7, 2, 10, 3), 1);
1534 EXPECT_EQ(Arith::compare_fractions(hi, 1, hi - 1, 1), 1);
1535 EXPECT_EQ(Arith::compare_fractions(lo, 1, lo + 1, 1), -1);
1536 EXPECT_EQ(Arith::compare_fractions(lo, 3, lo, 2), 1);
1537 EXPECT_EQ(Arith::compare_fractions(hi, hi, 1, 1), 0);
1538 EXPECT_EQ(Arith::compare_fractions(hi - 1, hi, hi - 2, hi - 1), 1);
1540# if ALEPH_HOWARD_INT128
1541 std::mt19937_64
rng(0xC0FFEE);
1542 for (
size_t i = 0; i < 200000; ++i)
1546 const unsigned long long r =
rng();
1549 case 0:
return static_cast<long long>(
r);
1550 case 1:
return static_cast<long long>(
r % 2000) - 1000;
1551 case 2:
return static_cast<long long>(
r >> (
rng() % 63)) * (
rng() % 2 ? 1 : -1);
1552 default:
return (
rng() % 2) ? hi -
static_cast<long long>(
r % 5) : lo +
static_cast<long long>(
r % 5);
1557 long long b = draw();
1560 return b < 0 ? -b : (b == 0 ? 1 : b);
1562 const long long a1 = draw(), a2 = draw(), b1 =
positive(), b2 =
positive();
1565 const int expected = lhs < rhs ? -1 : (lhs > rhs ? 1 : 0);
1567 << a1 <<
"/" << b1 <<
" vs " << a2 <<
"/" << b2;
1575 const auto e = Arith::make_eta(-6, 4);
1578 EXPECT_TRUE(Arith::equal(Arith::make_eta(2, 4), Arith::make_eta(1, 2)));
1579 EXPECT_FALSE(Arith::equal(Arith::make_eta(1, 3), Arith::make_eta(1, 2)));
1580 EXPECT_TRUE(Arith::less(Arith::make_eta(1, 3), Arith::make_eta(1, 2)));
1581 EXPECT_FALSE(Arith::less(Arith::make_eta(1, 2), Arith::make_eta(1, 2)));
1582 const auto zero = Arith::make_eta(0, 7);
1585 const auto extreme = Arith::make_eta(std::numeric_limits<long long>::min(), 2);
1592 const long long hi = std::numeric_limits<long long>::max();
1594 EXPECT_FALSE(Arith::step(0, Arith::Eta{std::numeric_limits<long long>::min(), 1}, 0,
out));
1595 EXPECT_FALSE(Arith::step(0, Arith::Eta{0, 1}, hi,
out)
and Arith::step(1, Arith::Eta{0, 1}, hi,
out));
1599# if ALEPH_HOWARD_INT128
1604 using Wide = howard_detail::int128_t;
1606 const Wide
two_125 = Wide(1) << 125;
1619 const auto e = Ar::make_eta(-(Wide(3) << 100), 6);
1632 std::vector<Edge_Def> edges;
1633 for (
size_t u = 0; u < 40; ++u)
1635 edges.emplace_back(u, (u + 1) % 40, 1 +
static_cast<long long>(u % 5));
1636 edges.emplace_back(u, (u * 7 + 3) % 40, 2 -
static_cast<long long>(u % 3));
1642 constexpr size_t threads = 8;
1643 std::vector<int> failures(threads, 0);
1644 std::vector<std::thread> pool;
1645 for (
size_t t = 0; t < threads; ++t)
1646 pool.emplace_back([&, t]
1648 for (size_t i = 0; i < 40; ++i)
1650 const auto r = howard_minimum_mean_cycle(built.g);
1651 if (r.minimum_mean != reference.minimum_mean or r.cycle_arcs != reference.cycle_arcs
1652 or r.iterations != reference.iterations)
1656 for (
auto &
th : pool)
1658 for (
size_t t = 0; t < threads; ++t)
1659 EXPECT_EQ(failures[t], 0) <<
"thread " << t;
Minimum mean cycle by Howard's policy iteration.
Minimum mean cycle in directed graphs (Karp algorithm).
size_t size_t int32_t value
size_t size_t int32_t * out
Default distance accessor for arc weights.
Generic directed graph (digraph) wrapper template.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Minimal std::expected-style result type for C++20.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Container< Node * > nodes() const
Return a container with all the nodes of the graph.
__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)
Min_Mean_Cycle_Value_Result karp_minimum_mean_cycle_value(const GT &g, Distance distance=Distance(), SA sa=SA())
Compute only the minimum mean value (without witness walk).
Min_Mean_Cycle_Result< GT, typename Distance::Distance_Type > karp_minimum_mean_cycle(const GT &g, Distance distance=Distance(), SA sa=SA())
Compute minimum mean cycle by Karp's algorithm.
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.
Min_Mean_Cycle_Value_Result 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.
constexpr size_t default_iterations
Value of max_iterations that selects the default limit, V + 64.
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.
Main namespace for Aleph-w library functions.
bool less_than(const T &op1, const T &op2, Compare &cmp)
Determines if op1 is less than op2 using a comparison operator.
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).
bool exists(Container &container, Operation &operation)
Return true if at least one element satisfies a predicate.
and
Check uniqueness with explicit hash + equality functors.
auto mean(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute the arithmetic mean.
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Built_Graph_T< Graph > build_graph(const size_t n, const std::vector< Edge_Def > &edges)
Graph types and probe helpers shared by negative_cycles_test.cc and bounded_negative_cycle_test....
Default filter for filtered iterators on arcs.
Result of howard_minimum_mean_cycle().
bool used_karp
True if Karp's algorithm produced the answer: fallback_reason is not None.
Result of minimum mean cycle computation.
size_t cycle_length
Number of arcs in witness cycle.
Cost_Type cycle_total_cost
Weight sum of witness cycle.
bool has_cycle
True if at least one directed cycle exists.
bool has_cycle
True if at least one directed cycle exists.
Filtered iterator of adjacent arcs of a node.
Arithmetic of the policy evaluation, specialised by exactness.
Integer type, and its limits, of the exact arithmetic for Cost_Type: long long, or int128_t for unsig...
void plant(Built_Graph_T< GT > &built)
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Array-based graph implementation.
Generic graph and digraph implementations.