Skip to content

Make Engine instances reusable#106

Open
SCiarella wants to merge 1 commit intomainfrom
issue_21
Open

Make Engine instances reusable#106
SCiarella wants to merge 1 commit intomainfrom
issue_21

Conversation

@SCiarella
Copy link
Copy Markdown
Collaborator

Closes #21 (and #20).

This PR separates engine construction from runtime setup so the same Engine instance can be reused across multiple parameter sets without re-instantiating the full model stack each time.

The main goal is to support workflows where parameters are updated repeatedly, especially in differentiable and optimization use cases, while avoiding the current need to deep-copy the parameter provider before every run.

Changes

  • Added a reusable setup(... to Engine
  • run setup when Engine is initialize
  • add a _reset_runtime_state method so an engine instance can be safely reused
  • define our custom _finish_cropsimulation to avoid calling clear_override() that would pop away variables from the Engine
  • Use our new VariableKiosk to support external states the main engine
  • Simplified EngineTestHelper so it relies on the same engine setup logic
  • Updated differentiable tests to instantiate EngineTestHelper once and call setup(...) for each forward pass, so that we can avoid the combo of pop + deepcopy
  • Fixed existing tests
  • added new tests to cover new methods

So now we basically go from this (before):

for params_dict in parameter_sets:
    crop_model_params_provider = copy.deepcopy(parameters)

    for name, value in params_dict.items():
        crop_model_params_provider.set_override(name, value, check=False)

    model = Engine(
        crop_model_params_provider,
        weather_data,
        agromanagement_data,
        config=config,
    )
    model.run_till_terminate()
    results = model.get_output()

to this (after):

model = Engine(config=config)

for params_dict in parameter_sets:
    for name, value in params_dict.items():
        parameters.set_override(name, value, check=False)

    model.setup(parameters, weather_data, agromanagement_data)
    model.run_till_terminate()
    results = model.get_output()

@SCiarella SCiarella requested a review from SarahAlidoost March 27, 2026 08:30
@SCiarella SCiarella marked this pull request as ready for review March 27, 2026 08:34
@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
11.9% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Task] Running a model with different parameter sets

1 participant