agent_state
agentevac.agents.agent_state
Agent runtime state management for the AgentEvac wildfire evacuation simulator.
This module defines the per-agent in-memory state that persists across decision rounds.
All live agent states are stored in the global AGENT_STATES dict, which is keyed by
vehicle ID. The main simulation loop (agentevac.simulation.main) creates or retrieves state via
ensure_agent_state() before running the belief-update and departure-decision pipeline.
Psychological profile parameters stored in each agent's profile dict:
- theta_trust : Weight given to social (neighbor) signals vs. own observations [0, 1].
Higher values mean the agent trusts peer messages more.
- theta_r : Risk threshold; agent departs if p_danger > theta_r [0, 1].
- theta_u : Urgency threshold; agent departs if the urgency term falls below
this value (see departure_model.py) [0, 1].
- gamma : Discount factor controlling urgency decay over time (≈ 0.99).
Each elapsed second reduces urgency by a factor of gamma.
- lambda_e : Exposure weight in the route utility function (≥ 0).
Larger values make agents more averse to hazardous routes.
- lambda_t : Travel-time weight in the route utility function (≥ 0).
Larger values make agents prefer shorter travel times.
- neighbor_window_s : Recency window for local neighborhood departure observations.
- social_recent_weight : Weight on recent neighbor departures when computing
social departure pressure.
- social_total_weight : Weight on cumulative departed neighbors when computing
social departure pressure.
- social_trigger : Pressure threshold that can trigger social departure.
- social_min_danger : Minimum danger floor required before social departure
pressure can trigger departure.
AgentRuntimeState
dataclass
Holds all mutable per-agent state across decision rounds.
Attributes:
| Name | Type | Description |
|---|---|---|
agent_id |
str
|
Unique vehicle identifier matching the SUMO vehicle ID. |
created_sim_t_s |
float
|
Simulation time (seconds) when this agent was first registered. |
last_sim_t_s |
float
|
Simulation time of the most recent state update. |
profile |
Dict[str, float]
|
Immutable-ish psychological parameters (theta_trust, theta_r, etc.).
Populated by |
belief |
Dict[str, Any]
|
Current Bayesian belief distribution {p_safe, p_risky, p_danger} plus derived fields (entropy, entropy_norm, uncertainty_bucket). |
psychology |
Dict[str, Any]
|
Scalar summaries derived from the belief (perceived_risk, confidence). |
signal_history |
List[Dict[str, Any]]
|
Bounded list of recent environment signals (noisy margin observations). |
social_history |
List[Dict[str, Any]]
|
Bounded list of recent social signals derived from inbox messages. |
decision_history |
List[Dict[str, Any]]
|
Bounded list of past decision records (predeparture + routing).
Passed to the LLM as |
observation_history |
List[Dict[str, Any]]
|
Bounded list of system-generated local neighborhood observations. |
institutional_history |
List[Dict[str, Any]]
|
Bounded list of institutional snapshots (forecast + annotated
menu) pushed each decision round. Used by
|
has_departed |
bool
|
True once the vehicle has been added to the SUMO simulation. |
Source code in agentevac/agents/agent_state.py
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 | |
append_decision_history(state, decision, *, max_items=32)
Append a decision record to the agent's decision history.
Decision records are passed to the LLM as agent_self_history so the model can
avoid repeating previously ineffective choices. The larger default cap (32) relative
to signal/social histories reflects the higher value of decision context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
AgentRuntimeState
|
The agent whose history to update. |
required |
decision
|
Dict[str, Any]
|
The decision record (predeparture or routing) to append. |
required |
max_items
|
int
|
Maximum number of decision records to retain. |
32
|
Source code in agentevac/agents/agent_state.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
append_institutional_history(state, snapshot, *, max_items=16)
Append an institutional snapshot to the agent's institutional history.
Each snapshot contains the forecast and annotated destination/route menu
produced for that decision round. Used by apply_institutional_delay
to serve stale official information when INFO_DELAY_S > 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
AgentRuntimeState
|
The agent whose history to update. |
required |
snapshot
|
Dict[str, Any]
|
Dict with |
required |
max_items
|
int
|
Maximum number of snapshots to retain. |
16
|
Source code in agentevac/agents/agent_state.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
append_observation_history(state, observation, *, max_items=16)
Append a system-generated observation to the agent's observation history.
These observations are simulator-authored facts, such as nearby household departure updates. They are kept separately from peer-authored social messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
AgentRuntimeState
|
The agent whose history to update. |
required |
observation
|
Dict[str, Any]
|
The observation dict to append. |
required |
max_items
|
int
|
Maximum number of observation records to retain. |
16
|
Source code in agentevac/agents/agent_state.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | |
append_signal_history(state, signal, *, max_items=16)
Append an environment signal record to the agent's signal history.
The history is bounded to max_items entries (default 16).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
AgentRuntimeState
|
The agent whose history to update. |
required |
signal
|
Dict[str, Any]
|
The environment signal dict produced by |
required |
max_items
|
int
|
Maximum number of signal records to retain. |
16
|
Source code in agentevac/agents/agent_state.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
append_social_history(state, signal, *, max_items=16)
Append a social signal record to the agent's social history.
Social signals are derived from inbox messages by information_model.build_social_signal.
Keeping a bounded history supports future analysis of how peer influence evolved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
AgentRuntimeState
|
The agent whose history to update. |
required |
signal
|
Dict[str, Any]
|
The social signal dict produced by |
required |
max_items
|
int
|
Maximum number of social signal records to retain. |
16
|
Source code in agentevac/agents/agent_state.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
ensure_agent_state(agent_id, sim_t_s, *, default_theta_trust=0.5, default_theta_r=0.45, default_theta_u=0.3, default_gamma=0.995, default_lambda_e=1.0, default_lambda_t=0.1, default_neighbor_window_s=120.0, default_social_recent_weight=0.7, default_social_total_weight=0.3, default_social_trigger=0.5, default_social_min_danger=0.15)
Retrieve an existing agent state or create a new one with default parameters.
On first call for a given agent_id, initializes the belief distribution to a
uniform prior (maximum entropy, uncertainty_bucket="High") and sets the
psychology scalars to neutral values. On subsequent calls, only updates
last_sim_t_s and back-fills any missing profile keys via setdefault.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Vehicle ID used as the state registry key. |
required |
sim_t_s
|
float
|
Current simulation time in seconds. |
required |
default_theta_trust
|
float
|
Initial social-signal trust weight (see module docstring). |
0.5
|
default_theta_r
|
float
|
Initial risk-departure threshold. |
0.45
|
default_theta_u
|
float
|
Initial urgency-departure threshold. |
0.3
|
default_gamma
|
float
|
Per-second urgency discount factor. |
0.995
|
default_lambda_e
|
float
|
Initial exposure weight for route utility scoring. |
1.0
|
default_lambda_t
|
float
|
Initial travel-time weight for route utility scoring. |
0.1
|
default_neighbor_window_s
|
float
|
Window for recent neighborhood departure observations. |
120.0
|
default_social_recent_weight
|
float
|
Weight on recent departures in social pressure. |
0.7
|
default_social_total_weight
|
float
|
Weight on cumulative departures in social pressure. |
0.3
|
default_social_trigger
|
float
|
Pressure threshold for socially triggered departure. |
0.5
|
default_social_min_danger
|
float
|
Minimum danger probability required before social departure pressure can trigger departure. |
0.15
|
Returns:
| Type | Description |
|---|---|
AgentRuntimeState
|
The (possibly newly created) |
Source code in agentevac/agents/agent_state.py
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 | |
sample_profile_params(agent_id, means, spreads, bounds)
Sample per-agent profile parameters from truncated normal distributions.
Each parameter is drawn from N(mean, spread) and clipped to [lo, hi].
When spread <= 0 the mean is returned unchanged (no heterogeneity).
A deterministic RNG seeded by agent_id ensures that the same agent always
receives the same profile regardless of which code path creates it first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Vehicle ID used to seed the per-agent RNG. |
required |
means
|
Dict[str, float]
|
Dict of parameter names to population means. |
required |
spreads
|
Dict[str, float]
|
Dict of parameter names to population standard deviations. Missing keys or values <= 0 disable sampling for that parameter. |
required |
bounds
|
Dict[str, Tuple[float, float]]
|
Dict of parameter names to |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, float]
|
A dict of sampled parameter values, one per key in |
Source code in agentevac/agents/agent_state.py
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 | |
snapshot_agent_state(state)
Serialize an AgentRuntimeState to a plain dict for logging or replay.
The returned dict is JSON-serializable and contains shallow copies of all mutable
sub-structures. Used by replay.record_agent_cognition to capture a point-in-time
snapshot of the agent's internal state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
AgentRuntimeState
|
The agent state to serialize. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A JSON-serializable dict representation of the agent state. |
Source code in agentevac/agents/agent_state.py
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | |