Store predecessor links for column S-bend sequences
This commit is contained in:
parent
253ea1f9c9
commit
e0efd04181
2 changed files with 43 additions and 15 deletions
|
|
@ -67,6 +67,15 @@ class _BendSequence:
|
||||||
cost: float
|
cost: float
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, slots=True, repr=False, eq=False)
|
||||||
|
class _BendState:
|
||||||
|
offset: int
|
||||||
|
previous: _BendState | None
|
||||||
|
span: int
|
||||||
|
cost: float
|
||||||
|
length: int
|
||||||
|
|
||||||
|
|
||||||
def _columns(problem: RoutingProblem, start: int, finish: int) -> tuple[_Column, ...]:
|
def _columns(problem: RoutingProblem, start: int, finish: int) -> tuple[_Column, ...]:
|
||||||
# A bounding rectangle only reserves extra space in this candidate
|
# A bounding rectangle only reserves extra space in this candidate
|
||||||
# planner. Capacity proofs still require the actual rectangular obstacles.
|
# planner. Capacity proofs still require the actual rectangular obstacles.
|
||||||
|
|
@ -314,7 +323,7 @@ def _bend_sequences(
|
||||||
for d, span in spans.items()
|
for d, span in spans.items()
|
||||||
if span <= max_usable
|
if span <= max_usable
|
||||||
]
|
]
|
||||||
sequences = {0: (_BendSequence((), 0, 0.0),)}
|
sequences = {0: (_BendState(0, None, 0, 0.0, 0),)}
|
||||||
upcoming = sorted(offset for offset, _, _ in bends)
|
upcoming = sorted(offset for offset, _, _ in bends)
|
||||||
stride = gcd(*upcoming) or 1
|
stride = gcd(*upcoming) or 1
|
||||||
queued = set(upcoming)
|
queued = set(upcoming)
|
||||||
|
|
@ -329,19 +338,20 @@ def _bend_sequences(
|
||||||
height = heappop(upcoming)
|
height = heappop(upcoming)
|
||||||
queued.remove(height)
|
queued.remove(height)
|
||||||
candidates = [
|
candidates = [
|
||||||
_BendSequence((offset, *prefix.offsets), span + prefix.span, cost + prefix.cost)
|
_BendState(offset, prefix, span + prefix.span, cost + prefix.cost, prefix.length + 1)
|
||||||
for offset, span, cost in bends
|
for offset, span, cost in bends
|
||||||
for prefix in sequences.get(height - offset, ())
|
for prefix in sequences.get(height - offset, ())
|
||||||
if span + prefix.span <= max_usable
|
if span + prefix.span <= max_usable
|
||||||
]
|
]
|
||||||
|
# For each leading offset and span, at most one predecessor survives.
|
||||||
candidates.sort(
|
candidates.sort(
|
||||||
key=lambda candidate: (
|
key=lambda candidate: (
|
||||||
candidate.span,
|
candidate.span,
|
||||||
candidate.cost if weighted else len(candidate.offsets),
|
candidate.cost if weighted else candidate.length,
|
||||||
tuple(-d for d in candidate.offsets),
|
-candidate.offset,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
frontier: list[_BendSequence] = []
|
frontier: list[_BendState] = []
|
||||||
for candidate in candidates:
|
for candidate in candidates:
|
||||||
if not frontier or (weighted and candidate.cost < frontier[-1].cost - 1e-9):
|
if not frontier or (weighted and candidate.cost < frontier[-1].cost - 1e-9):
|
||||||
frontier.append(candidate)
|
frontier.append(candidate)
|
||||||
|
|
@ -353,13 +363,16 @@ def _bend_sequences(
|
||||||
if span <= remaining and next_height not in queued:
|
if span <= remaining and next_height not in queued:
|
||||||
heappush(upcoming, next_height)
|
heappush(upcoming, next_height)
|
||||||
queued.add(next_height)
|
queued.add(next_height)
|
||||||
viable = [candidate for candidate in sequences.get(magnitude, ()) if candidate.span <= usable]
|
best = next((state for state in reversed(sequences.get(magnitude, ())) if state.span <= usable), None)
|
||||||
if not viable:
|
if best is None:
|
||||||
return None
|
return None
|
||||||
best = viable[-1]
|
sign = -1 if delta < 0 else 1
|
||||||
if delta < 0:
|
offsets = []
|
||||||
return _BendSequence(tuple(-offset for offset in best.offsets), best.span, best.cost)
|
state = best
|
||||||
return best
|
while state.previous is not None:
|
||||||
|
offsets.append(sign * state.offset)
|
||||||
|
state = state.previous
|
||||||
|
return _BendSequence(tuple(offsets), best.span, best.cost)
|
||||||
|
|
||||||
return choose
|
return choose
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -77,17 +77,17 @@ def test_sbend_sequences_use_the_common_offset_stride() -> None:
|
||||||
|
|
||||||
def test_sbend_sequence_only_builds_reachable_shifts_within_the_maximum_window(monkeypatch) -> None:
|
def test_sbend_sequence_only_builds_reachable_shifts_within_the_maximum_window(monkeypatch) -> None:
|
||||||
max_usable = 399_891
|
max_usable = 399_891
|
||||||
make_sequence = _columns._BendSequence
|
make_state = _columns._BendState
|
||||||
built = 0
|
built = 0
|
||||||
|
|
||||||
def count_sequence(offsets, span, cost):
|
def count_state(offset, previous, span, cost, length):
|
||||||
nonlocal built
|
nonlocal built
|
||||||
built += 1
|
built += 1
|
||||||
assert span <= max_usable
|
assert span <= max_usable
|
||||||
assert built <= 81
|
assert built <= 81
|
||||||
return make_sequence(offsets, span, cost)
|
return make_state(offset, previous, span, cost, length)
|
||||||
|
|
||||||
monkeypatch.setattr(_columns, "_BendSequence", count_sequence)
|
monkeypatch.setattr(_columns, "_BendState", count_state)
|
||||||
choose = _columns._bend_sequences(
|
choose = _columns._bend_sequences(
|
||||||
{20: 5000, 125_002: 375_006}, 312_505, LENGTH_COST, max_usable=max_usable, weighted=False, sbend_penalty=0,
|
{20: 5000, 125_002: 375_006}, 312_505, LENGTH_COST, max_usable=max_usable, weighted=False, sbend_penalty=0,
|
||||||
)
|
)
|
||||||
|
|
@ -98,6 +98,21 @@ def test_sbend_sequence_only_builds_reachable_shifts_within_the_maximum_window(m
|
||||||
assert sequence.span == 375_006
|
assert sequence.span == 375_006
|
||||||
|
|
||||||
|
|
||||||
|
def test_sbend_table_reconstructs_only_the_chosen_sequence(monkeypatch) -> None:
|
||||||
|
make_sequence = _columns._BendSequence
|
||||||
|
|
||||||
|
def build_sequence(offsets, span, cost):
|
||||||
|
assert offsets == (-180_002,)
|
||||||
|
return make_sequence(offsets, span, cost)
|
||||||
|
|
||||||
|
monkeypatch.setattr(_columns, "_BendSequence", build_sequence)
|
||||||
|
choose = _columns._bend_sequences(
|
||||||
|
{20: 6000, 180_002: 540_006}, 450_005, LENGTH_COST, max_usable=65_000_000, weighted=False, sbend_penalty=0,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert choose(-180_002, 65_000_000).span == 540_006
|
||||||
|
|
||||||
|
|
||||||
def test_weighted_sbend_sequence_keeps_both_short_and_low_cost_choices() -> None:
|
def test_weighted_sbend_sequence_keeps_both_short_and_low_cost_choices() -> None:
|
||||||
weights = {"straight": 1.0, "sbend": 1.0}
|
weights = {"straight": 1.0, "sbend": 1.0}
|
||||||
choose = _columns._bend_sequences({2: 14, 10: 30}, 25, weights, max_usable=70, weighted=True, sbend_penalty=0)
|
choose = _columns._bend_sequences({2: 14, 10: 30}, 25, weights, max_usable=70, weighted=True, sbend_penalty=0)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue