whab13's picture
initial: procthor engine + parallel launcher + setup + README
bb54e71 verified
Raw
History Blame Contribute Delete
4.38 kB
"""Action sequence → phase spans + per-phase turn anchor.
Port of the segmentation algorithm used by `whab13/temporal-navigation`:
> Major-segment boundaries are derived from the action sequence (rock-solid):
> a route is a series of forward-runs separated by SUSTAINED turns (>=2 consecutive
> same-direction turns). Short single-step turns are course corrections kept
> inside a segment. This yields ~3-6 segments matching the instruction's clause
> count, not the ~20 micro-turns a naive split would give.
Inputs:
actions: list[int] -1=stop, 1=forward, 2=left-turn, 3=right-turn
Outputs:
spans: list[(start, end)] half-open ranges into `actions`
kinds: list[str] one of 'forward' | 'left' | 'right' | 'stop'
(per-phase alignment anchor)
"""
from __future__ import annotations
from typing import List, Tuple
FORWARD = 1
LEFT = 2
RIGHT = 3
STOP = -1
def _runs(actions: List[int]):
"""Yield (value, start, end) for each maximal run of equal values in `actions`."""
if not actions: return
cur = actions[0]; start = 0
for i in range(1, len(actions)):
if actions[i] != cur:
yield cur, start, i
cur = actions[i]; start = i
yield cur, start, len(actions)
def segment_actions(actions: List[int],
sustained_turn_min: int = 2,
) -> Tuple[List[Tuple[int, int]], List[str]]:
"""Returns (spans, kinds).
Algorithm:
1. Walk the action sequence left-to-right.
2. Phase boundaries are placed at the START of a SUSTAINED-turn run
(>= `sustained_turn_min` consecutive turns of the same direction).
3. Single-step or short turns get absorbed into the surrounding forward
phase. (They're course corrections, not the operator's intended turn.)
4. The phase kind = the dominant action in that phase:
- if the phase contains a sustained-turn run, kind = direction of that run
- else kind = 'forward'
- if the entire phase is only STOPs, kind = 'stop'
"""
if not actions:
return [], []
# 1) Identify sustained-turn runs by index
sustained_turn_starts = []
for v, s, e in _runs(actions):
if v in (LEFT, RIGHT) and (e - s) >= sustained_turn_min:
sustained_turn_starts.append((s, e, v))
# 2) Build phase boundaries
boundaries = [0]
for s, e, _ in sustained_turn_starts:
if s not in boundaries:
boundaries.append(s)
# The phase that holds the turn ends WHERE the turn ends — next phase starts after.
if e not in boundaries and e < len(actions):
boundaries.append(e)
if boundaries[-1] != len(actions):
boundaries.append(len(actions))
boundaries = sorted(set(boundaries))
spans: List[Tuple[int, int]] = []
for i in range(len(boundaries) - 1):
s, e = boundaries[i], boundaries[i + 1]
if e > s: spans.append((s, e))
# 3) Classify each phase
kinds: List[str] = []
for s, e in spans:
sub = actions[s:e]
if not sub:
kinds.append("stop"); continue
# If a sustained-turn run is entirely inside [s, e), use its direction
kind = None
for ts, te, tv in sustained_turn_starts:
if s <= ts < te <= e:
kind = "left" if tv == LEFT else "right"
break
if kind is None:
# No sustained turn in this phase
if all(a == STOP for a in sub):
kind = "stop"
else:
kind = "forward"
kinds.append(kind)
return spans, kinds
def _self_test():
# Mirror R2R example episode 1803 vibe:
# mostly forward with a sustained left turn 4-5 in, then a sustained turn later.
actions = [-1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 2, 2, 2, 2, 2, 2, 1, 3, 1, 2, 1, 3,
1, 2, 2, 2, 1, 1, 3, 1, 2, 2, 1, 1, 1, 2, 1]
spans, kinds = segment_actions(actions)
print(f"n_phases={len(spans)}")
for s, k in zip(spans, kinds):
print(f" {s} kind={k} actions={actions[s[0]:s[1]]}")
assert len(spans) >= 3, "should detect multiple phases"
assert "left" in kinds and "forward" in kinds, "should detect a left phase and a forward phase"
print("self-test OK")
if __name__ == "__main__":
_self_test()