|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +import netgraph_core as ngc |
| 6 | + |
| 7 | + |
| 8 | +def _graph_overflow_with_alternative(): |
| 9 | + kmax = np.iinfo(np.int64).max |
| 10 | + src = np.array([0, 1, 0], dtype=np.int32) |
| 11 | + dst = np.array([1, 2, 2], dtype=np.int32) |
| 12 | + cap = np.array([1.0, 1.0, 1.0], dtype=np.float64) |
| 13 | + cost = np.array([kmax - 5, 10, 100], dtype=np.int64) |
| 14 | + ext = np.arange(3, dtype=np.int64) |
| 15 | + return ngc.StrictMultiDiGraph.from_arrays(3, src, dst, cap, cost, ext) |
| 16 | + |
| 17 | + |
| 18 | +def _graph_overflow_single_path(): |
| 19 | + kmax = np.iinfo(np.int64).max |
| 20 | + src = np.array([0, 1], dtype=np.int32) |
| 21 | + dst = np.array([1, 2], dtype=np.int32) |
| 22 | + cap = np.array([1.0, 1.0], dtype=np.float64) |
| 23 | + cost = np.array([kmax - 5, 10], dtype=np.int64) |
| 24 | + ext = np.arange(2, dtype=np.int64) |
| 25 | + return ngc.StrictMultiDiGraph.from_arrays(3, src, dst, cap, cost, ext) |
| 26 | + |
| 27 | + |
| 28 | +def _graph_zero_cost_cycle(): |
| 29 | + src = np.array([0, 1, 2, 2, 1], dtype=np.int32) |
| 30 | + dst = np.array([1, 2, 1, 3, 3], dtype=np.int32) |
| 31 | + cap = np.array([1.0, 1.0, 1.0, 1.0, 1.0], dtype=np.float64) |
| 32 | + cost = np.array([0, 0, 0, 1, 1], dtype=np.int64) |
| 33 | + ext = np.arange(5, dtype=np.int64) |
| 34 | + return ngc.StrictMultiDiGraph.from_arrays(4, src, dst, cap, cost, ext) |
| 35 | + |
| 36 | + |
| 37 | +def test_spf_overflow_does_not_wrap_negative(algs, to_handle): |
| 38 | + g = _graph_overflow_with_alternative() |
| 39 | + gh = to_handle(g) |
| 40 | + dist, _ = algs.spf(gh, 0, dtype="int64") |
| 41 | + assert int(dist[2]) == 100 |
| 42 | + |
| 43 | + |
| 44 | +def test_ksp_overflow_saturates_finite_cost(algs, to_handle): |
| 45 | + g = _graph_overflow_single_path() |
| 46 | + gh = to_handle(g) |
| 47 | + items = algs.ksp(gh, 0, 2, k=1, dtype="int64") |
| 48 | + assert len(items) == 1 |
| 49 | + kmax = np.iinfo(np.int64).max |
| 50 | + assert int(items[0][0][2]) == int(kmax - 1) |
| 51 | + |
| 52 | + |
| 53 | +def test_max_flow_summary_costs_no_negative_wrap(algs, to_handle): |
| 54 | + g = _graph_overflow_single_path() |
| 55 | + gh = to_handle(g) |
| 56 | + flow, summary = algs.max_flow(gh, 0, 2) |
| 57 | + assert flow == 1.0 |
| 58 | + assert int(summary.costs[0]) == int(np.iinfo(np.int64).max - 1) |
| 59 | + |
| 60 | + |
| 61 | +def test_flow_policy_max_cost_factor_handles_large_cost(algs, to_handle): |
| 62 | + g = _graph_overflow_single_path() |
| 63 | + gh = to_handle(g) |
| 64 | + fg = ngc.FlowGraph(g) |
| 65 | + cfg = ngc.FlowPolicyConfig(max_flow_count=1, max_path_cost_factor=2.0) |
| 66 | + policy = ngc.FlowPolicy(algs, gh, cfg) |
| 67 | + placed, left = policy.place_demand(fg, 0, 2, 0, 1.0) |
| 68 | + assert placed == 1.0 |
| 69 | + assert left == 0.0 |
| 70 | + |
| 71 | + |
| 72 | +def test_resolve_to_paths_terminates_on_zero_cost_cycle(algs, to_handle): |
| 73 | + g = _graph_zero_cost_cycle() |
| 74 | + gh = to_handle(g) |
| 75 | + _, dag = algs.spf(gh, 0, dst=3, multipath=True, dtype="int64") |
| 76 | + paths = dag.resolve_to_paths(0, 3) |
| 77 | + assert len(paths) == 2 |
| 78 | + for path in paths: |
| 79 | + nodes = [int(n) for n, _ in path] |
| 80 | + assert len(nodes) == len(set(nodes)) |
| 81 | + |
| 82 | + |
| 83 | +def test_equal_balanced_max_flow_zero_cost_cycle(algs, to_handle): |
| 84 | + g = _graph_zero_cost_cycle() |
| 85 | + gh = to_handle(g) |
| 86 | + flow_prop, _ = algs.max_flow( |
| 87 | + gh, |
| 88 | + 0, |
| 89 | + 3, |
| 90 | + flow_placement=ngc.FlowPlacement.PROPORTIONAL, |
| 91 | + shortest_path=False, |
| 92 | + require_capacity=True, |
| 93 | + ) |
| 94 | + flow_eb, _ = algs.max_flow( |
| 95 | + gh, |
| 96 | + 0, |
| 97 | + 3, |
| 98 | + flow_placement=ngc.FlowPlacement.EQUAL_BALANCED, |
| 99 | + shortest_path=False, |
| 100 | + require_capacity=True, |
| 101 | + ) |
| 102 | + assert flow_prop == 1.0 |
| 103 | + assert flow_eb == 1.0 |
0 commit comments