Skip to content

Add parameter access and copy support to Scenario#3158

Closed
navneetkumaryadav207001 wants to merge 19 commits intomesa:mainfrom
navneetkumaryadav207001:main
Closed

Add parameter access and copy support to Scenario#3158
navneetkumaryadav207001 wants to merge 19 commits intomesa:mainfrom
navneetkumaryadav207001:main

Conversation

@navneetkumaryadav207001
Copy link
Copy Markdown

@navneetkumaryadav207001 navneetkumaryadav207001 commented Jan 17, 2026

Summary

Adds two utility methods to the Scenario base class: get_parameters() for accessing scenario parameters without metadata, and copy() for creating modified scenario variants.

Motive

Working with experiment sweeps, logging, and reproducibility often requires separating scenario parameters from internal metadata and creating slightly modified copies of scenarios. This change provides a simple, consistent API for both use cases.

Implementation

  • Added get_parameters() to return scenario attributes excluding model and _scenario_id.
  • Added copy(**updates) to create a new Scenario instance with optional parameter updates.
  • The copied scenario:
    • gets a new _scenario_id
    • does not retain the model reference
    • shares the same RNG object by reference (documented).

Usage Examples

params = scenario.get_parameters()

new_scenario = scenario.copy(alpha=0.5, beta=10)

solves #3159.

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +1.4% [+0.5%, +2.3%] 🔵 -2.9% [-3.0%, -2.7%]
BoltzmannWealth large 🔵 +1.0% [+0.4%, +1.6%] 🔵 -0.2% [-2.2%, +2.0%]
Schelling small 🔵 +0.1% [-0.1%, +0.2%] 🔵 +0.0% [-0.1%, +0.1%]
Schelling large 🔵 -0.7% [-1.0%, -0.5%] 🔵 -2.1% [-2.8%, -1.4%]
WolfSheep small 🔵 -1.7% [-1.9%, -1.4%] 🔵 +0.1% [-0.1%, +0.2%]
WolfSheep large 🔵 -1.3% [-2.3%, -0.3%] 🔵 -2.4% [-3.4%, -1.4%]
BoidFlockers small 🔵 -1.1% [-1.5%, -0.8%] 🔵 +0.0% [-0.2%, +0.2%]
BoidFlockers large 🔵 -0.3% [-0.9%, +0.2%] 🔵 -0.0% [-0.2%, +0.1%]

Comment on lines +83 to +87
def get_parameters(self):
"""Get scenario parameters excluding metadata."""
excluded = {"model", "_scenario_id"}
return {k: v for k, v in self.__dict__.items() if k not in excluded}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant with the already existing to_dict method.

params.pop("model", None) # Don't copy model reference
params.pop("_scenario_id", None) # Create new scenario with new ID

return self.__class__(**params)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is needed either because you have to_dict.

Can you give me an example of when you would need this?

@quaquel quaquel added the enhancement Release notes label label Jan 17, 2026
codebreaker32 and others added 15 commits January 17, 2026 15:57
Add convenience property to access model.scenario directly from agents.

This follows the same pattern as the existing random and rng properties, making scenario parameters easier to access within agent code.
mesa#3163)

* Optimise create_agents by replacing 'ListLike' approach

* Optimised tuple slicing overhead

* Add test for fast path

---------

Co-authored-by: Jan Kwakkel <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants