Calibrator
- class Melodie.Calibrator(config: Config, scenario_cls: Type[Scenario] | None, model_cls: Type[Model] | None, data_loader_cls: Type[DataLoader] = None, processors: int = 1, parallel_mode: Literal['process', 'thread'] = 'process')
Bases:
BaseModellingManagerThe
Calibratoruses a genetic algorithm to tune scenario parameters.It aims to find the parameter set that minimizes the “distance” between a model’s output and a predefined target (e.g., empirical data).
- Parameters:
config – The project
Configobject.scenario_cls – The
Scenariosubclass for the model.model_cls – The
Modelsubclass for the model.data_loader_cls – The
DataLoadersubclass for the model.processors – The number of processor cores to use for parallel computation of the genetic algorithm.
parallel_mode – The parallelization mode.
"process"(default) uses subprocess-based parallelism, suitable for all Python versions."thread"uses thread-based parallelism, which is recommended for Python 3.13+ (free-threaded/No-GIL builds) for better performance.
- setup()
A hook for setting up the Calibrator.
This method should be overridden in a subclass to define which scenario properties to calibrate using
add_scenario_calibrating_property().
- collect_data()
(Optional) A hook to define which agent and environment properties to record.
This is not required for calibration itself but is useful for saving detailed simulation data during the calibration process. Use
add_environment_property()to register properties.
- generate_scenarios() List[Scenario]
Generate scenarios from the
CalibratorScenariostable.- Returns:
A list of
Scenarioobjects.
- get_params_scenarios() List
Load the genetic algorithm parameters from the
CalibratorParamsScenariostable.- Returns:
A list of dictionaries, where each dictionary contains the GA parameters for one calibration run.
- run()
The main entry point for starting the calibration process.
- run_once_new(scenario: Scenario, calibration_params: GACalibratorParams)
(Internal) Run a single calibration path.
- target_function(model: Model) float | int
(Internal) A wrapper for the user-defined distance function.
- distance(model: Model) float
The distance function to be minimized by the calibrator.
This method must be overridden in a subclass. It should take a
Modelobject (representing the final state of a simulation run) and return a single float value representing the “distance” or error between the model’s output and the desired target.- Parameters:
model – The
Modelobject after a simulation run.- Returns:
A float representing the distance.
- add_scenario_calibrating_property(prop: str)
Register a scenario property to be calibrated.
The specified property will be tuned by the genetic algorithm within the bounds defined in the
CalibratorParamsScenariostable.- Parameters:
prop – The name of the scenario property to calibrate.
- add_environment_property(prop: str)
Register an environment property to be recorded during calibration.
This allows for saving the values of specific environment properties for each simulation run within the calibration process.
- Parameters:
prop – The name of the environment property to record.