--orders: route several connection orders in parallel, keep the best
The default order is always one of the candidates, so the result is never worse than a plain run; ties go to the earlier candidate.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
"""--orders: route several connection orders and keep the best."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from freeroute.cli import main
|
||||
from freeroute.route.orders import best_of_orders, candidate_seeds, route_once, score
|
||||
|
||||
FIXTURES = Path(__file__).resolve().parent.parent / "dsn" / "fixtures"
|
||||
# Greedy (no rip-up), the default order strands a net here and seeds 1 to 3 do not.
|
||||
RIPUP = (FIXTURES / "ripup_needed.dsn").read_text()
|
||||
GREEDY = {"rip_up": False}
|
||||
|
||||
|
||||
def test_the_default_order_is_always_a_candidate_without_a_seed():
|
||||
assert candidate_seeds(1) == [None]
|
||||
assert candidate_seeds(4) == [None, 0, 1, 2]
|
||||
assert candidate_seeds(3, seed=7) == [7, 8, 9]
|
||||
|
||||
|
||||
def test_keeps_the_order_that_leaves_the_fewest_unrouted():
|
||||
assert score(route_once(RIPUP, "grid", GREEDY, None)) == (1, 0)
|
||||
result, kept, tried = best_of_orders(RIPUP, "grid", GREEDY, orders=3, jobs=2)
|
||||
assert kept == 1
|
||||
assert score(result) == (0, 0)
|
||||
assert tried == [(None, (1, 0)), (0, (1, 0)), (1, (0, 0))]
|
||||
|
||||
|
||||
def test_ties_go_to_the_earlier_candidate():
|
||||
# every order routes this board fully, so the default order is kept
|
||||
_, kept, tried = best_of_orders(RIPUP, "grid", {}, orders=3, jobs=3)
|
||||
assert kept is None
|
||||
assert all(s == (0, 0) for _, s in tried)
|
||||
|
||||
|
||||
def test_cli_orders_writes_the_best_session(tmp_path, capsys):
|
||||
out = tmp_path / "best.ses"
|
||||
rc = main([str(FIXTURES / "ripup_needed.dsn"), "--no-rip-up", "--orders", "3", "-do", str(out)])
|
||||
assert rc == 0
|
||||
err = capsys.readouterr().err
|
||||
assert "tried 3 orders, kept --seed 1" in err
|
||||
assert "not routed" not in err
|
||||
assert out.read_text().startswith("(session")
|
||||
Reference in New Issue
Block a user