File size: 3,248 Bytes
ec5ee81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from __future__ import annotations

from dataclasses import dataclass

from coflow5.control.a1_controller import A1FlowController
from coflow5.control.max_pressure import (
    CooperativeMaxPressureController,
    MaxPressureObservation,
    MovementPressure,
)
from coflow5.control.safety import DeterministicSafetyMask, controlled_cross_plan
from coflow5.control.situation_aware import (
    A1SituationAwareMaxPressureController,
    SituationAwareMaxPressureResult,
)
from coflow5.messaging.board import MessageTransport
from coflow5.sumo_adapter.signal_executor import ImmutableSafetyLog, SignalExecutorRegistry


class _ProbeTrafficLight:
    def __init__(self) -> None:
        self.state = "GrGr"
        self.writes: list[tuple[str, str]] = []

    def setRedYellowGreenState(self, signal_id: str, state: str) -> None:
        self.state = state
        self.writes.append((signal_id, state))

    def getRedYellowGreenState(self, signal_id: str) -> str:
        return self.state


class _ProbeConnection:
    def __init__(self) -> None:
        self.trafficlight = _ProbeTrafficLight()


@dataclass(frozen=True)
class ForecastFallbackProbe:
    result: SituationAwareMaxPressureResult
    a1_signal_write_count: int
    a4_signal_write_count: int
    safety_event_count: int
    command_count: int


def run_forecast_fallback_probe(

    *, transport: MessageTransport, run_id: str, scenario_hash: str,

    signal_id: str = "J0", simulation_time: float = 20.0,

) -> ForecastFallbackProbe:
    """Execute one legal in-memory A1 decision for Row 10 fallback evidence."""
    connection = _ProbeConnection()
    log = ImmutableSafetyLog()
    mask = DeterministicSafetyMask(controlled_cross_plan())
    executor = SignalExecutorRegistry().acquire(
        signal_id=signal_id,
        connection=connection,
        safety_mask=mask,
        initial_phase_id="NS_GREEN",
        initial_phase_entered_at=0.0,
        run_id=run_id,
        scenario_hash=scenario_hash,
        log=log,
    )
    local = CooperativeMaxPressureController(
        a1=A1FlowController(executor),
        safety_mask=mask,
        run_id=run_id,
        scenario_hash=scenario_hash,
        signal_id=signal_id,
    )
    result = A1SituationAwareMaxPressureController(
        controller=local,
        transport=transport,
        run_id=run_id,
        scenario_hash=scenario_hash,
        signal_id=signal_id,
    ).decide(
        proposal_id=f"a4-fallback-{simulation_time:g}",
        observation=MaxPressureObservation(
            simulation_time=simulation_time,
            current_phase_id="NS_GREEN",
            phase_entered_at=0.0,
            movements={
                "north": MovementPressure(1, 0, 20),
                "south": MovementPressure(1, 0, 20),
                "east": MovementPressure(5, 0, 20),
                "west": MovementPressure(5, 0, 20),
            },
        ),
    )
    return ForecastFallbackProbe(
        result=result,
        a1_signal_write_count=len(connection.trafficlight.writes),
        a4_signal_write_count=0,
        safety_event_count=len(log.events),
        command_count=len(log.commands),
    )