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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//! Scenario replay: timed rider spawns with pass/fail conditions.
use crate::config::SimConfig;
use crate::dispatch::DispatchStrategy;
use crate::error::SimError;
use crate::metrics::Metrics;
use crate::sim::Simulation;
use crate::stop::StopId;
use serde::{Deserialize, Serialize};
/// A timed rider spawn event within a scenario.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimedSpawn {
/// Tick at which to spawn this rider.
pub tick: u64,
/// Origin stop for the rider.
pub origin: StopId,
/// Destination stop for the rider.
pub destination: StopId,
/// Weight of the rider.
pub weight: f64,
}
/// A pass/fail condition for scenario evaluation.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Condition {
/// Average wait time must be below this value (ticks).
AvgWaitBelow(f64),
/// Maximum wait time must be below this value (ticks).
MaxWaitBelow(u64),
/// Throughput must be above this value (riders per window).
ThroughputAbove(u64),
/// All riders must be delivered by this tick.
AllDeliveredByTick(u64),
/// Abandonment rate must be below this value (0.0 - 1.0).
AbandonmentRateBelow(f64),
}
/// A complete scenario: config + timed spawns + success conditions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Scenario {
/// Human-readable scenario name.
pub name: String,
/// Simulation configuration.
pub config: SimConfig,
/// Timed rider spawn events.
pub spawns: Vec<TimedSpawn>,
/// Pass/fail conditions for evaluation.
pub conditions: Vec<Condition>,
/// Max ticks to run before declaring timeout.
pub max_ticks: u64,
}
/// Result of evaluating a single condition.
#[derive(Debug, Clone)]
pub struct ConditionResult {
/// The condition that was evaluated.
pub condition: Condition,
/// Whether the condition passed.
pub passed: bool,
/// The actual observed value.
pub actual_value: f64,
}
/// Result of running a complete scenario.
#[derive(Debug, Clone)]
pub struct ScenarioResult {
/// Scenario name.
pub name: String,
/// Whether all conditions passed.
pub passed: bool,
/// Number of ticks run.
pub ticks_run: u64,
/// Per-condition results.
pub conditions: Vec<ConditionResult>,
/// Final simulation metrics.
pub metrics: Metrics,
}
/// Runs a scenario to completion and evaluates conditions.
pub struct ScenarioRunner {
/// The underlying simulation.
sim: Simulation,
/// Timed spawn events.
spawns: Vec<TimedSpawn>,
/// Index of the next spawn to process.
spawn_cursor: usize,
/// Pass/fail conditions.
conditions: Vec<Condition>,
/// Maximum ticks before timeout.
max_ticks: u64,
/// Scenario name.
name: String,
}
impl ScenarioRunner {
/// Create a new runner from a scenario definition and dispatch strategy.
///
/// Returns `Err` if the scenario's config is invalid.
///
/// # Errors
///
/// Returns [`SimError::InvalidConfig`] if the scenario's simulation config is invalid.
pub fn new(
scenario: Scenario,
dispatch: impl DispatchStrategy + 'static,
) -> Result<Self, SimError> {
let sim = Simulation::new(&scenario.config, dispatch)?;
Ok(Self {
sim,
spawns: scenario.spawns,
spawn_cursor: 0,
conditions: scenario.conditions,
max_ticks: scenario.max_ticks,
name: scenario.name,
})
}
/// Access the underlying simulation.
#[must_use]
pub const fn sim(&self) -> &Simulation {
&self.sim
}
/// Run one tick: spawn scheduled riders, then tick simulation.
pub fn tick(&mut self) {
// Spawn any riders scheduled for this tick.
while self.spawn_cursor < self.spawns.len()
&& self.spawns[self.spawn_cursor].tick <= self.sim.current_tick()
{
let spawn = &self.spawns[self.spawn_cursor];
// Deliberately ignore spawn errors: scenario files may reference stops
// that were removed or disabled during the scenario run. Silently
// skipping invalid spawns is the correct replay behavior.
let _ = self
.sim
.spawn_rider(spawn.origin, spawn.destination, spawn.weight);
self.spawn_cursor += 1;
}
self.sim.step();
}
/// Run to completion (all riders delivered or `max_ticks` reached).
pub fn run_to_completion(&mut self) -> ScenarioResult {
use crate::components::RiderPhase;
for _ in 0..self.max_ticks {
self.tick();
// Check if all spawns have happened and all riders are done.
if self.spawn_cursor >= self.spawns.len() {
let all_done =
self.sim.world().iter_riders().all(|(_, r)| {
matches!(r.phase, RiderPhase::Arrived | RiderPhase::Abandoned)
});
if all_done {
break;
}
}
}
self.evaluate()
}
/// Evaluate conditions against current metrics.
#[must_use]
pub fn evaluate(&self) -> ScenarioResult {
let metrics = self.sim.metrics().clone();
let condition_results: Vec<ConditionResult> = self
.conditions
.iter()
.map(|cond| evaluate_condition(cond, &metrics, self.sim.current_tick()))
.collect();
let passed = condition_results.iter().all(|r| r.passed);
ScenarioResult {
name: self.name.clone(),
passed,
ticks_run: self.sim.current_tick(),
conditions: condition_results,
metrics,
}
}
}
/// Evaluate a single condition against metrics and the current tick.
fn evaluate_condition(
condition: &Condition,
metrics: &Metrics,
current_tick: u64,
) -> ConditionResult {
match condition {
Condition::AvgWaitBelow(threshold) => ConditionResult {
condition: condition.clone(),
passed: metrics.avg_wait_time() < *threshold,
actual_value: metrics.avg_wait_time(),
},
Condition::MaxWaitBelow(threshold) => ConditionResult {
condition: condition.clone(),
passed: metrics.max_wait_time() < *threshold,
actual_value: metrics.max_wait_time() as f64,
},
Condition::ThroughputAbove(threshold) => ConditionResult {
condition: condition.clone(),
passed: metrics.throughput() > *threshold,
actual_value: metrics.throughput() as f64,
},
Condition::AllDeliveredByTick(deadline) => ConditionResult {
condition: condition.clone(),
passed: current_tick <= *deadline
&& metrics.total_delivered() == metrics.total_spawned(),
actual_value: current_tick as f64,
},
Condition::AbandonmentRateBelow(threshold) => ConditionResult {
condition: condition.clone(),
passed: metrics.abandonment_rate() < *threshold,
actual_value: metrics.abandonment_rate(),
},
}
}