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
│ └── ...
└── results/
└── <MODEL>_v<NV>_c<NC>.parquet ← series cache
Main public entry points¶
| Function | Returns | Description |
|---|---|---|
simulation_from_config(config_path) |
None |
Full pipeline: generate → apply rules → save per-iteration Parquet files under sim_result/ |
simulation_series_from_config(config_path) |
SimulationTotalResult |
Same as above but returns results in memory; caches series under results/ |
simulation_instance(gen_code, n_v, n_c, rule_codes, ...) |
SimulationSeriesResult |
Run one (model, voters, candidates) combination; supports incremental rule addition from cache |
generate_data(config_path) |
list[str] |
Generate (or reuse cached) profiles only, no rules applied |
obtain_data_instance(model, n_v, n_c, ...) |
DataInstance |
Load a single cached profile or generate and persist it |
run_rules_on_instance(data_instance, rule_codes) |
SimulationStepResult |
Apply a list of rules to a single DataInstance |
simulation_step(profile, rule_codes) |
SimulationStepResult |
Apply a list of rules to an svvamp Profile directly |
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 x n_voters x n_candidates x 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, reload=False)
¶
Load a cached profile or generate + persist it.
If the parquet file already exists AND reload is False, the profile is loaded from disk; otherwise it is (re-)generated with the given seed 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 (combined with iteration for uniqueness). |
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
|
reload
|
bool
|
When True, ignore any existing cached file and regenerate from the seed. Use this whenever the seed changes to ensure the new seed is actually applied. |
False
|
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, *, reload=False, 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
|
reload
|
bool
|
When |
False
|
compute_metrics
|
bool
|
Whether to compute :class: |
True
|
Source code in src/vote_simulation/simulation/simulation.py
281 282 283 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 | |
simulation_instance(gen_code, n_v, n_c, rule_codes, n_iteration=1000, seed=161, base_path='data', reload=False, show_progress=True, *, extra_params=None, 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
|
extra_params
|
dict[str, object] | None
|
Optional dict of extra generator parameters (e.g.
|
None
|
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
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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | |
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.
Séquentiel — le travail est CPU-bound Python (svvamp) ; le GIL empêche
toute parallélisation réelle via ThreadPoolExecutor.
En mode UI (Streamlit), le tqdm externe est intercepté par _PatchedTqdm
pour mettre à jour la barre de progression sans toucher à la hot loop.
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_series_from_config_2(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
|