Simulation workflow¶
This page explains how the simulation pipeline is structured and where to look depending on what you want to change.
End-to-end flow¶
The full workflow is:
- load a TOML configuration,
- validate the requested simulation mode,
- generate or reuse election profiles,
- apply every requested voting rule,
- save one result file per iteration.
Directory layout¶
<output_base>/
├── gen/
│ └── <MODEL>_v<NV>_c<NC>/
│ ├── iter_0001.parquet
│ └── ...
└── sim_result/
└── <MODEL>_v<NV>_c<NC>/
├── iter_0001.parquet
└── ...
Legacy single-file and batch modes write their outputs under sim/.
Main public entry points¶
obtain_data_instance()retrieve (from storage or generation)run_rules_on_instance()apply SRC on DataInstance-
`
-
generate_data()for generation only simulation_full()for the full pipelinesimulation()for one fileobtain_data_instance()for the cache-or-generate step
Configuration reference¶
Configuration loading for vote simulations.
SimulationConfig
dataclass
¶
Validated simulation configuration.
Source code in src/vote_simulation/simulation/configuration.py
load_simulation_config(config_path=DEFAULT_CONFIG_PATH)
¶
Load and validate a simulation config file.
Source code in src/vote_simulation/simulation/configuration.py
Simulation engine reference¶
Core simulation engine.
Workflow¶
- Read the TOML configuration.
- For each generative model × (n_voters, n_candidates) × iteration: a. Check if the generated profile already exists on disk → load it. b. If not, generate it via the generator registry and persist it.
- Apply every rule to each profile and collect winners.
- Persist the simulation results to
sim_result/.
The directory layout follows::
<output_base>/
gen/<MODEL>_v<NV>_c<NC>/
iter_0001.parquet
…
sim_result/<MODEL>_v<NV>_c<NC>/
iter_0001.parquet
…
generate_data(config_path, show_progress=True)
¶
Generate (or retrieve cached) profiles for every combination defined in the config.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of file paths of generated/cached parquet files. |
Source code in src/vote_simulation/simulation/simulation.py
obtain_data_instance(model, n_v, n_c, *, iteration=0, seed=161, base_path='data', extra_params=None)
¶
Load a cached profile or generate + persist it.
If the parquet file already exists the profile is loaded from disk; otherwise it is generated and saved for future reuse.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Generative model code (e.g. "UNI", "IC"). |
required |
n_v
|
int
|
Number of voters. |
required |
n_c
|
int
|
Number of candidates. |
required |
iteration
|
int
|
Iteration index. |
0
|
seed
|
int
|
Random seed for generation (will be combined with iteration index for variability). |
161
|
base_path
|
str
|
Root folder for generated data (see config.output_base_path). |
'data'
|
extra_params
|
dict[str, object] | None
|
Optional dict of extra parameters to pass to the generator (per-model). |
None
|
Source code in src/vote_simulation/simulation/simulation.py
run_rules_on_instance(data_instance, rule_codes, config=None, *, compute_metrics=True)
¶
Apply every rule and collect winners into a SimulationStepResult.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_instance
|
DataInstance
|
The profile data to run the rules on. |
required |
rule_codes
|
list[str]
|
List of rule codes to apply (e.g. ["RV", "MJ", "AP_T"]). |
required |
config
|
ResultConfig | None
|
Optional :class: |
None
|
compute_metrics
|
bool
|
Whether to compute :class: |
True
|
Source code in src/vote_simulation/simulation/simulation.py
sim(file_path, rule_code)
¶
Execute a single rule on a single file
²
Source code in src/vote_simulation/simulation/simulation.py
simulation_from_config(config_path, show_progress=True, *, compute_metrics=True)
¶
Full pipeline: generate profiles, apply rules, save results.
For every (model, n_voters, n_candidates, iteration) combination:
1. Obtain (generate or load) the profile.
2. Run all requested rules.
3. Save the result in sim_result/<MODEL>_v<NV>_c<NC>/iter_XXXX.parquet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
str
|
Path to the TOML configuration file (see docs for the template). |
required |
show_progress
|
bool
|
Whether to display a progress bar. |
True
|
compute_metrics
|
bool
|
Whether to compute :class: |
True
|
Source code in src/vote_simulation/simulation/simulation.py
simulation_instance(gen_code, n_v, n_c, rule_codes, n_iteration=1000, seed=161, base_path='data', reload=False, show_progress=True, *, compute_metrics=True)
¶
Run the workflow on a single (model, voters, candidates) instance.
Each step receives a :class:ResultConfig so that the series
automatically aggregates the simulation context.
Cache logic:
1. Checks for a cached result at <base_path>/results/<base_label>.parquet
(where base_label excludes rules).
2. If found with matching step count and same base parameters:
- If rules are identical: returns cached series (no recomputation).
- If rules differ: loads cached series and applies new rules incrementally.
3. If not found or stale: recomputes from scratch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gen_code
|
str
|
Generative model code (list can be found in doc). |
required |
n_v
|
int
|
Number of voters. |
required |
n_c
|
int
|
Number of candidates. |
required |
rule_codes
|
list[str]
|
List of rule codes to apply. |
required |
n_iteration
|
int
|
Number of iterations. Defaults to 1000. |
1000
|
seed
|
int
|
Seed for reproducibility. Defaults to 161. |
161
|
base_path
|
str
|
Root folder for generated data. Defaults to |
'data'
|
reload
|
bool
|
Force re-computation (ignore cache). Defaults to False. |
False
|
show_progress
|
bool
|
Whether to display progress bars. Defaults to True. |
True
|
compute_metrics
|
bool
|
Whether to compute :class: |
True
|
Returns:
SimulationSeriesResult with attached :attr:config including all rules.
Source code in src/vote_simulation/simulation/simulation.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | |
simulation_series_from_config(config_path, reload=False, *, compute_metrics=True)
¶
Run simulation instances for every combination in the config.
Iterates over each (model, n_voters, n_candidates) triplet defined
in the TOML configuration, delegates to :func:simulation_instance,
and collects all resulting series into a :class:SimulationTotalResult.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
str
|
Path to the TOML configuration file. |
required |
reload
|
bool
|
Force re-computation (ignore cache). Defaults to False. |
False
|
compute_metrics
|
bool
|
Whether to compute :class: |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
SimulationTotalResult
|
class: |
SimulationTotalResult
|
|
Source code in src/vote_simulation/simulation/simulation.py
simulation_step(profile, rule_codes, config=None, *, compute_metrics=True)
¶
Run a single profile through all rules and return a :class:SimulationStepResult.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profile
|
Profile
|
The profile data to run the rules on. |
required |
candidates
|
List of candidate names. |
required | |
rule_codes
|
list[str]
|
List of rule codes to apply (e.g. ["RV", "MJ", "AP_T"]). |
required |
config
|
ResultConfig | None
|
Optional :class: |
None
|
compute_metrics
|
bool
|
Whether to compute :class: |
True
|