smif.controller package¶
Implements procedures and tools to run a system-of-systems model Provides methods for copying a sample project into a local directory and for or running a system-of-systems model.
Example
Copy a project folder:
>>> copy_project_folder('/projects/smif/')
Run a single system-of-systems model:
>>> execute_model_run('energy_supply_demand', store)
Use the SubProcessRunScheduler to run a system-of-systems model:
>>> my_scheduler = SubProcessRunScheduler()
>>> my_scheduler.add(
'energy_supply_demand',
{
'directory': '/projects/smif'
}
)
>>> my_scheduler.status('energy_supply_demand')
{
'message': 'running'
}
>>> my_scheduler.status('energy_supply_demand')
{
"err": "",
"output": "20102010\n2015\n2015\nModel run '20170918_energy_water' complete\n",
"status": "done"
}
- class smif.controller.ModelRunner(warm_start=False)[source]¶
Bases:
objectThe ModelRunner orchestrates the simulation of a SoSModel over decision iterations and timesteps as provided by a DecisionManager.
- build_job_graph(model_run, bundle)[source]¶
Build a job graph
Build and return the job graph for an entire bundle, including before_model_run jobs when the models were not yet initialised.
Constraints: - Bundle must have keys: ‘decision_iterations’ and ‘timesteps’ - Running a bundle runs each (decision iteration, timestep) pair specified by the
combinations of decision iterations and timesteps
(decision iteration, timestep) pairs must be unique over an entire model run
In a single bundle, timesteps must be a consecutive subset of the model horizon timesteps
The first timestep in each decision iteration of a bundle is either: - the first timestep in the model horizon and initialised from the model run starting
point with scenario data and initial-timestep interventions only
or another timestep, picking up from where some previous (timestep, decision iteration) left off, which is explicitly included in the bundle.
If a bundle’s timesteps start from a timestep after the first one in the model horizon, the bundle must provide ‘decision_links’, and bundle must start from the very next timestep available in the model horizon.
Jobs need to be able to identify a point to pick up from, namely the (timestep, decision iteration) which identifies the immediately preceding simulation state.
E.g. request running first two timesteps:
{ 'decision_iterations': [0, 1], 'timesteps': [0, 1] }
Run first two timesteps again, with an updated decision:
{ 'decision_iterations': [1, 2], 'timesteps': [0, 1] }
Results meet decision requirements, so run next two timesteps, linking this bundle’s decision iterations to previous decision iterations:
{ 'decision_iterations': [3, 4], 'timesteps': [2, 3], 'decision_links': {3: 1, 4: 2} }
- Parameters:
model_run (
smif.controller.modelrun.ModelRunbundle:dict)- Returns:
different operations and timesteps
- Return type:
networkx.GraphA populated job graph with edges showing dependencies between
- solve_model(model_run, job_scheduler, store, dry_run=False)[source]¶
Solve a ModelRun
This method steps through the model horizon, building a job graph and submitting this to the scheduler at each decision loop.
- Parameters:
model_run (
smif.controller.modelrun.ModelRun)store (
smif.data_layer.Store)
- smif.controller.copy_project_folder(directory)[source]¶
Creates folder structure in the target directory
- Parameters:
directory – Location where the sample project should be copied to
- smif.controller.execute_decision_step(model_run_id, decision, store)[source]¶
Request the next set of decisions from the decision manager, including the bundle of timesteps and decision iterations to simulate next.
- smif.controller.execute_model_before_step(model_run_id, model_name, store, dry_run=False)[source]¶
Runs model initialisation
This method
- smif.controller.execute_model_run(model_run_ids, store, warm=False, dry=False)[source]¶
Runs the model run
- Parameters:
modelrun_ids (list) – Modelrun ids that should be executed sequentially
- smif.controller.execute_model_step(model_run_id, model_name, timestep, decision, store, dry_run=False)[source]¶
Runs a single step of a model run
This method is designed to be the single place where smif actually calls wrapped models.
Loading model wrapper implementations (via ModelLoader) is also deferred to this method, so that the smif scheduler environment need not have all the python dependencies of all wrapped models.
For example, in a scheduled containerised environment, the model run can be configured and set running (smif run modelrun_name) in one environment, and individual models can run (smif run modelrun_name –model model_name –timestep 2050) in their own environments.
Subpackages¶
Submodules¶
smif.controller.build module¶
- smif.controller.build.build_model_run(model_run_config)[source]¶
Builds the model run
- Parameters:
model_run_config (dict) – A valid model run configuration dict with objects
- Return type:
smif.controller.modelrun.ModelRun
- smif.controller.build.get_scenario_models(scenarios, handler)[source]¶
Read in ScenarioModels
- Parameters:
scenarios (dict)
handler (smif.data_layer.Store)
- Return type:
smif.controller.execute_run module¶
Execute a model run - discover and schedule all steps in order
smif.controller.execute_step module¶
Execute a single step directly
- smif.controller.execute_step.execute_decision_step(model_run_id, decision, store)[source]¶
Request the next set of decisions from the decision manager, including the bundle of timesteps and decision iterations to simulate next.
- smif.controller.execute_step.execute_model_before_step(model_run_id, model_name, store, dry_run=False)[source]¶
Runs model initialisation
This method
- smif.controller.execute_step.execute_model_step(model_run_id, model_name, timestep, decision, store, dry_run=False)[source]¶
Runs a single step of a model run
This method is designed to be the single place where smif actually calls wrapped models.
Loading model wrapper implementations (via ModelLoader) is also deferred to this method, so that the smif scheduler environment need not have all the python dependencies of all wrapped models.
For example, in a scheduled containerised environment, the model run can be configured and set running (smif run modelrun_name) in one environment, and individual models can run (smif run modelrun_name –model model_name –timestep 2050) in their own environments.
smif.controller.modelrun module¶
The Model Run collects scenarios, timesteps, narratives, and model collection into a package which can be built and passed to the ModelRunner to run.
The ModelRunner is responsible for running a ModelRun, including passing in the correct data to the model between timesteps and calling to the DecisionManager to obtain decisions.
ModeRun has attributes: - id - description - sosmodel - timesteps - scenarios - narratives - strategy - status
- class smif.controller.modelrun.ModelRun[source]¶
Bases:
objectCollects timesteps, scenarios , narratives and a SosModel together
- timestamp¶
An ISO8601 compatible timestamp of model run creation time
- Type:
- sos_model¶
The contained SosModel
- logger¶
- Type:
- as_dict()[source]¶
Serialises
smif.controller.modelrun.ModelRunReturns a dictionary definition of a ModelRun which is equivalent to that required by from_dict to construct a new model run
- Return type:
- classmethod from_dict(config)[source]¶
Create a
smif.controller.modelrun.ModelRunfrom a dictionary
- property model_horizon¶
Returns the list of timesteps
- Returns:
A list of timesteps, distinct and sorted in ascending order
- Return type:
- class smif.controller.modelrun.ModelRunner(warm_start=False)[source]¶
Bases:
objectThe ModelRunner orchestrates the simulation of a SoSModel over decision iterations and timesteps as provided by a DecisionManager.
- build_job_graph(model_run, bundle)[source]¶
Build a job graph
Build and return the job graph for an entire bundle, including before_model_run jobs when the models were not yet initialised.
Constraints: - Bundle must have keys: ‘decision_iterations’ and ‘timesteps’ - Running a bundle runs each (decision iteration, timestep) pair specified by the
combinations of decision iterations and timesteps
(decision iteration, timestep) pairs must be unique over an entire model run
In a single bundle, timesteps must be a consecutive subset of the model horizon timesteps
The first timestep in each decision iteration of a bundle is either: - the first timestep in the model horizon and initialised from the model run starting
point with scenario data and initial-timestep interventions only
or another timestep, picking up from where some previous (timestep, decision iteration) left off, which is explicitly included in the bundle.
If a bundle’s timesteps start from a timestep after the first one in the model horizon, the bundle must provide ‘decision_links’, and bundle must start from the very next timestep available in the model horizon.
Jobs need to be able to identify a point to pick up from, namely the (timestep, decision iteration) which identifies the immediately preceding simulation state.
E.g. request running first two timesteps:
{ 'decision_iterations': [0, 1], 'timesteps': [0, 1] }
Run first two timesteps again, with an updated decision:
{ 'decision_iterations': [1, 2], 'timesteps': [0, 1] }
Results meet decision requirements, so run next two timesteps, linking this bundle’s decision iterations to previous decision iterations:
{ 'decision_iterations': [3, 4], 'timesteps': [2, 3], 'decision_links': {3: 1, 4: 2} }
- Parameters:
model_run (
smif.controller.modelrun.ModelRunbundle:dict)- Returns:
different operations and timesteps
- Return type:
networkx.GraphA populated job graph with edges showing dependencies between
- solve_model(model_run, job_scheduler, store, dry_run=False)[source]¶
Solve a ModelRun
This method steps through the model horizon, building a job graph and submitting this to the scheduler at each decision loop.
- Parameters:
model_run (
smif.controller.modelrun.ModelRun)store (
smif.data_layer.Store)