Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
bench_negative_cycles.cc File Reference

Benchmark and result validator for negative cycle detection: Bellman_Ford (Bellman_Ford.H) against find_disjoint_negative_cycles() and most_negative_cycle_bounded() (Negative_Cycles.H), with Karp's minimum mean cycle (Min_Mean_Cycle.H) as a reference. More...

#include <algorithm>
#include <charconv>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <memory>
#include <new>
#include <random>
#include <string>
#include <system_error>
#include <tuple>
#include <vector>
#include <Bellman_Ford.H>
#include <Howard_Min_Mean_Cycle.H>
#include <Min_Mean_Cycle.H>
#include <Negative_Cycles.H>
#include <tpl_array.H>
#include <tpl_dynList.H>
#include <tpl_dynMapTree.H>
#include <tpl_dynSetTree.H>
#include <tpl_graph.H>
#include "../ca/bench_support.H"
Include dependency graph for bench_negative_cycles.cc:

Go to the source code of this file.

Macros

#define BENCH_NOINLINE
 

Functions

void * operator new (const std::size_t size)
 
void * operator new[] (const std::size_t size)
 
void operator delete (void *p) noexcept
 
void operator delete[] (void *p) noexcept
 
void operator delete (void *p, std::size_t) noexcept
 
void operator delete[] (void *p, std::size_t) noexcept
 
int main (const int argc, char *argv[])
 

Detailed Description

Benchmark and result validator for negative cycle detection: Bellman_Ford (Bellman_Ford.H) against find_disjoint_negative_cycles() and most_negative_cycle_bounded() (Negative_Cycles.H), with Karp's minimum mean cycle (Min_Mean_Cycle.H) as a reference.

Usage:

bench_negative_cycles [num_assets] [samples] [seed] [--check-only] [--only=<text>]
bool check(GT &g)
ValueArg< size_t > seed
Definition testHash.C:53
  • num_assets in [19, 20000] (default 300);
  • samples timed runs per measurement, in [1, 100000] (default 31; fewer than 30 are flagged as exploratory);
  • seed in [0, 4294967295] (default 42);
  • --check-only runs the validation pass only (no timing);
  • --only=<text> keeps the graph families whose name contains <text>.

Every argument must be a complete decimal integer in its range; anything else is rejected before a graph is built (exit code 2).

Graph families
market-arbitrage and market-clean model a crypto market: n assets, four hub assets traded against every other one, plus random pairs, each pair traded both ways with weight -log(rate * (1 - fee)) taken from consistent prices, so market-clean has no negative cycle and market-arbitrage has five seeded triangles (as parallel arcs: the graphs are multigraphs). The other families stress the structure the bounded search is expected to exploit: market-arbitrage-reversed (same arcs, reverse insertion order), market-isolated-nodes (as many isolated vertices as assets), dag (acyclic, negative arcs), components (disconnected strongly connected components, one in three holding a negative triangle), dense (complete digraph on at most 120 assets), market-reweighted (fixed topology, eight weight sets applied round-robin between the timed samples), loop-isolated (one negative self-loop among 50 isolated vertices per asset, at most 20000; the bounded variants run whatever its size), scc-pair-isolated (the family of audit H6: arcs 0 -> 1 and 1 -> 0 of cost 0 and a self-loop of -1 on node 1, among isolated vertices, 10 per asset, at most 4000) and scc-pairs (disjoint copies of that pair, at most 300 vertices).
Long walks in small components
Besides L = 3, 6, 12, the bounded search runs with L = V, as long as any simple cycle, on scc-pair-isolated, scc-pairs and components (up to 400 vertices). Every length up to L has a closed walk from each node of a strongly connected component, and the search reconstructs and decomposes one per length: about V_c * L^2 work for V_c nodes in components, which dominates when the components are small and L is not (audit H6). On the last two families that grows as the cube of V, hence their limits.
Validation (untimed, before and apart from the measurements)
Each variant is run once per weight set and its result is checked against the ground truth known by construction. A reported cycle must be closed, simple, made of consecutive arcs of the graph and of strictly negative cost (the reported cost must also match the arc sum); at most the requested number of cycles, pairwise distinct, and pairwise arc-disjoint under All_Arcs; at most L arcs for the bounded search. A negative cycle must be found where one was seeded and none may be reported where none exists. Results of the new algorithms that fail a check make the program exit with code 1. The cert column of the bounded search is reported, not validated: exact (proven optimal: a zero optimality_gap), bound (the relaxed bound was met in the rounded arithmetic of the search, which proves nothing by itself; the market families use double weights) or no-cert. most_negative_cycle_up_to_3(g, 3) is measured separately against the DP with the same bound. It enumerates simple cycles exactly and shows exact; the validator checks that label by enumerating every loop, opposite pair and triangle of the graph on its own (up to the rounding of long double sums). Its shortest-tie rule is tested against exhaustive integer and GMP rational oracles in the test suite.

Bellman_Ford gives its witness by the arcs of its graph of predecessors: on multigraphs, the parallel arc that it relaxed. Up to 7.3.2 it rebuilt the cycle from its nodes, and on multigraphs it could return one whose cost was not negative, as on n = 19 with seeds 19 and 42, which are always validated, and dense. Any invalid witness, a missed or a false detection is an error.

Before any instance, the validators themselves are fed known results: the cheaper of two loops must pass the short-cycle check and the costlier one must not, and an invalid Bellman-Ford witness must be an error. A validator that misjudges them also makes the program exit with code 1.

Timing protocol
An untimed warmup (at least 2 runs, then until 30 ms have elapsed, at most 200 runs), then samples timed runs; the table shows the median, the 90th percentile (nearest rank) and the best time. Weight changes of market-reweighted happen outside the timed region. The allocation columns count calls to the global operator new during one untimed run. The bounded variants are skipped above 1000 vertices (their worst case is O(V * L * (V + E))), except for loop-isolated and scc-pair-isolated, whose cyclic cores have one and two nodes; so are the Karp variants, whose table takes O(V^2) memory (about 9 bytes per entry for the value and 25 with the witness, measured: 15 000 vertices would need 2 to 6 GB).
Mean-cycle rows
karp_minimum_mean_cycle_value(), karp_minimum_mean_cycle() and their Howard counterparts (Howard_Min_Mean_Cycle.H) solve a different problem: the cycle of minimum mean weight, not the cheapest one. Howard is checked for a simple witness and, where Karp fits in memory, for Karp's mean; its cert column shows the policy iterations (it=7) or karp if the answer had to come from Karp. Both witnesses are simple cycles (Karp's walk is decomposed into simple cycles, see Min_Mean_Cycle.H), and both validators check that, along with consecutive arcs and the reported total, length and mean.
Reusable-topology estimate
Every instance also prints a block that estimates what find_disjoint(g, 1) would cost if its snapshot (index_graph) were kept between calls and only the weights were re-read. That is not a library API: the enumeration cannot run on an existing snapshot. The block times the call as it is today, its snapshot, and a loop that re-reads the weight of every arc of a kept snapshot, and reports call - snapshot + refresh. It assumes that the rest of the call does not depend on how the snapshot was obtained and that a real API would add no work of its own, so it is an optimistic bound. The validation checks that a kept snapshot, refreshed after each weight set of market-reweighted, equals a new snapshot arc by arc (so the refresh loop does the work a reusable topology would have to do).

Definition in file bench_negative_cycles.cc.

Macro Definition Documentation

◆ BENCH_NOINLINE

#define BENCH_NOINLINE

Definition at line 185 of file bench_negative_cycles.cc.

Function Documentation

◆ main()

int main ( const int  argc,
char *  argv[] 
)

Definition at line 1680 of file bench_negative_cycles.cc.

References Aleph::and, seed, and usage().

◆ operator delete() [1/2]

void operator delete ( void *  p)
noexcept

Definition at line 202 of file bench_negative_cycles.cc.

◆ operator delete() [2/2]

void operator delete ( void *  p,
std::size_t   
)
noexcept

Definition at line 204 of file bench_negative_cycles.cc.

◆ operator delete[]() [1/2]

void operator delete[] ( void *  p)
noexcept

Definition at line 203 of file bench_negative_cycles.cc.

◆ operator delete[]() [2/2]

void operator delete[] ( void *  p,
std::size_t   
)
noexcept

Definition at line 205 of file bench_negative_cycles.cc.

◆ operator new()

void * operator new ( const std::size_t  size)

Definition at line 188 of file bench_negative_cycles.cc.

References Aleph::size().

◆ operator new[]()

void * operator new[] ( const std::size_t  size)

Definition at line 201 of file bench_negative_cycles.cc.

References Aleph::size().