smif.model package¶
Implements a composite scenario/sector model/system-of-systems model
Begin by declaring the atomic units (scenarios and sector models) which make up
a the composite system-of-systems model and then add these to the composite.
Declare dependencies by using the add_dependency() method, passing in a
reference to the source model object, and a pointer to the model output
and sink parameter name for the destination model.
Run the model by calling the simulate() method, passing in a dictionary
containing data for any free hanging model inputs, not linked through a
dependency. A fully defined SosModel should have no hanging model inputs, and
can therefore be called using simulate() with no arguments.
Responsibility for passing required data to the contained models lies with the calling class. This means data is only ever passed one layer down. This simplifies the interface, and allows as little or as much hiding of data, dependencies and model inputs as required.
Example
A very simple example with just one scenario:
>>> elec_scenario = ScenarioModel('scenario')
>>> elec_scenario.add_output('demand', 'national', 'annual', 'GWh')
>>> sos_model = SosModel('simple')
>>> sos_model.add_model(elec_scenario)
>>> sos_model.simulate(2010)
{'scenario': {'demand': array([123])}}
A more comprehensive example with one scenario and one scenario model:
>>> elec_scenario = ScenarioModel('scenario')
>>> elec_scenario.add_output('demand', 'national', 'annual', 'GWh')
>>> class EnergyModel(SectorModel):
... def extract_obj(self):
... pass
... def simulate(self, timestep, data):
... return {self.name: {'cost': data['input'] * 2}}
...
>>> energy_model = EnergyModel('model')
>>> energy_model.add_input('input', 'national', 'annual', 'GWh')
>>> energy_model.add_dependency(elec_scenario, 'demand', 'input', lambda x: x)
>>> sos_model = SosModel('sos')
>>> sos_model.add_model(elec_scenario)
>>> sos_model.add_model(energy_model)
>>> sos_model.simulate(2010)
{'model': {'cost': array([[246]])}, 'scenario': {'demand': array([[123]])}}
- class smif.model.Model(name)[source]¶
Bases:
objectAbstract class represents the interface used to implement the model classes SectorModel and ScenarioModel.
- Parameters:
name (str)
- add_input(spec)[source]¶
Add an input
- Parameters:
spec (smif.metadata.Spec)
- add_output(spec)[source]¶
Add an output
- Parameters:
spec (smif.metadata.Spec)
- add_parameter(spec)[source]¶
Add a parameter
- Parameters:
spec (smif.metadata.Spec)
- property inputs¶
All model inputs defined at this layer
- Returns:
dict of {input_name
- Return type:
smif.metadata.Spec}
- property outputs¶
All model outputs defined at this layer
- Returns:
dict of {output_name
- Return type:
smif.metadata.Spec}
- property parameters¶
Model parameters
- Returns:
dict of {parameter_name
- Return type:
smif.data_layer.data_array.DataArray}
- simulate(data)[source]¶
Override to implement the generation of model results
Generate
resultsfortimestepusingdata- Parameters:
data (smif.data_layer.DataHandle) – Access state, parameter values, dependency inputs.
- class smif.model.ModelOperation(*values)[source]¶
Bases:
EnumEnumerate that describes the possible operations on Models
- BEFORE_MODEL_RUN = 'before_model_run'¶
- SIMULATE = 'simulate'¶
- class smif.model.ScenarioModel(name)[source]¶
Bases:
ModelRepresents exogenous scenario data
- Parameters:
name (str) – The name of this scenario (scenario set/abstract scenario/scenario group) - like sector model name
- class smif.model.SectorModel(name)[source]¶
Bases:
ModelA representation of the sector model with inputs and outputs
Implement this class to enable integration of the wrapped simulation model into a system-of-system model.
- Parameters:
name (str) – The unique name of the sector model
Notes
Implement the various abstract functions throughout the class to provide an interface to the simulation model, which can then be called upon by the framework.
The key methods in the SectorModel class which must be overridden are:
SectorModel.extract_obj()
An implementation may also override:
- before_model_run(data)[source]¶
Implement this method to conduct pre-model run tasks
- Parameters:
data (smif.data_layer.DataHandle) – Access parameter values (before any model is run, no dependency input data or state is guaranteed to be available) Access decision/system state (i.e. initial_conditions)
- simulate(data)[source]¶
Implement this method to run the model
- Parameters:
data (smif.data_layer.DataHandle) – Access state, parameter values, dependency inputs, results and interventions
Notes
See docs on
DataHandlefor details of how to access inputs, parameters and state and how to set results.intervalshould reference an id from the interval set corresponding to the output parameter, as specified in model configuration
regionshould reference a region name from the region set corresponding to the output parameter, as specified in model configuration
To obtain simulation model data in this method, use the methods such as:
parameter_value = data.get_parameter('my_parameter_name')
Other useful methods are
get_base_timestep_data(),get_previous_timestep_data(),get_parameter(),get_data(),get_parameters()andget_results().get_state()returns a list of intervention dict for the current timestepget_current_interventions()returns a list of dict where each dict is an intervention
- class smif.model.SosModel(name)[source]¶
Bases:
objectConsists of the collection of models joined via dependencies
- Parameters:
name (str) – The unique name of the SosModel
- add_dependency(source_model, source_output_name, sink_model, sink_input_name, timestep=RelativeTimestep.CURRENT)[source]¶
Adds a dependency to this system-of-systems model
- Parameters:
source_model (smif.model.model.Model) – A reference to the source ~smif.model.model.Model object
source_output_name (string) – The name of the model_output defined in the source_model
source_model – A reference to the source ~smif.model.model.Model object
sink_name (string) – The name of a model_input defined in this object
timestep (smif.metadata.RelativeTimestep, optional) – The relative timestep of the dependency, defaults to CURRENT, may be PREVIOUS.
- add_model(model)[source]¶
Adds a model to the system-of-systems model
- Parameters:
model (
smif.model.model.Model)
- add_narrative(narrative)[source]¶
Add a narrative to the system-of-systems model
- Parameters:
narrative (dict)
- check_dependencies()[source]¶
For each contained model, compare dependency list against list of available models
- property dependencies¶
Dependency connections between models within this system-of-systems model
- Return type:
iterable[smif.model.dependency.Dependency]
- property free_inputs¶
Returns the free inputs to models which are not yet linked to a dependency.
- Returns:
dict of {(model_name, input_name)
- Return type:
smif.metadata.Spec}
- property model_dependencies¶
Dependency connections between models within this system-of-systems model
- Return type:
iterable[smif.model.dependency.Dependency]
- property models¶
The models contained within this system-of-systems model
- Return type:
- property outputs¶
All model outputs provided by models contained within this SosModel
- Returns:
dict of {(model_name, output_name)
- Return type:
smif.metadata.Spec}
- property scenario_dependencies¶
Dependency connections between models within this system-of-systems model
- Return type:
iterable[smif.model.dependency.Dependency]
- property scenario_models¶
Scenario model objects contained in the SosModel
- Returns:
A list of scenario model objects
- Return type:
Submodules¶
smif.model.dependency module¶
Dependencies represent the flow of data explicitly, with reference to the source model output and sink model input.
A Dependency is an edge in the model process graph. No processing happens on dependency edges, only on the Model nodes.
A Dependency can have a timestep specified - by default this is the current timestep, but a system-of-systems model may also want to specify that data should be provided from the previous timestep or a base timestep.
- class smif.model.dependency.Dependency(source_model, source, sink_model, sink, timestep=RelativeTimestep.CURRENT)[source]¶
Bases:
objectLink a model input to a data source
- Parameters:
source_model (smif.model.Model) – The source model object
source (smif.metadata.Spec) – The source output description
sink_model (smif.model.Model) – The sink model object
sink (smif.metadata.Spec) – The sink input description
timestep (smif.metadata.RelativeTimestep, optional) – The relative timestep of the dependency, defaults to CURRENT
smif.model.model module¶
Model abstract class
- class smif.model.model.Model(name)[source]¶
Bases:
objectAbstract class represents the interface used to implement the model classes SectorModel and ScenarioModel.
- Parameters:
name (str)
- add_input(spec)[source]¶
Add an input
- Parameters:
spec (smif.metadata.Spec)
- add_output(spec)[source]¶
Add an output
- Parameters:
spec (smif.metadata.Spec)
- add_parameter(spec)[source]¶
Add a parameter
- Parameters:
spec (smif.metadata.Spec)
- property inputs¶
All model inputs defined at this layer
- Returns:
dict of {input_name
- Return type:
smif.metadata.Spec}
- property outputs¶
All model outputs defined at this layer
- Returns:
dict of {output_name
- Return type:
smif.metadata.Spec}
- property parameters¶
Model parameters
- Returns:
dict of {parameter_name
- Return type:
smif.data_layer.data_array.DataArray}
- simulate(data)[source]¶
Override to implement the generation of model results
Generate
resultsfortimestepusingdata- Parameters:
data (smif.data_layer.DataHandle) – Access state, parameter values, dependency inputs.
- class smif.model.model.ModelOperation(*values)[source]¶
Bases:
EnumEnumerate that describes the possible operations on Models
- BEFORE_MODEL_RUN = 'before_model_run'¶
- SIMULATE = 'simulate'¶
smif.model.sector_model module¶
This module acts as a bridge to the sector models from the controller
The SectorModel exposes several key methods for running wrapped
sector models. To add a sector model to an instance of the framework,
first implement SectorModel.
Key Functions¶
This class performs several key functions which ease the integration of sector models into the system-of-systems framework.
The user must implement the various abstract functions throughout the class to
provide an interface to the sector model, which can be called upon by the
framework. From the model’s perspective, SectorModel provides a bridge
from the sector-specific problem representation to the general representation
which allows reasoning across infrastructure systems.
The key functions include:
converting input/outputs to/from geographies/temporal resolutions
converting control vectors from the decision layer of the framework, to asset Interventions specific to the sector model
returning scalar/vector values to the framework to enable measurements of performance, particularly for the purposes of optimisation and rule-based approaches
- class smif.model.sector_model.SectorModel(name)[source]¶
Bases:
ModelA representation of the sector model with inputs and outputs
Implement this class to enable integration of the wrapped simulation model into a system-of-system model.
- Parameters:
name (str) – The unique name of the sector model
Notes
Implement the various abstract functions throughout the class to provide an interface to the simulation model, which can then be called upon by the framework.
The key methods in the SectorModel class which must be overridden are:
SectorModel.extract_obj()
An implementation may also override:
- before_model_run(data)[source]¶
Implement this method to conduct pre-model run tasks
- Parameters:
data (smif.data_layer.DataHandle) – Access parameter values (before any model is run, no dependency input data or state is guaranteed to be available) Access decision/system state (i.e. initial_conditions)
- simulate(data)[source]¶
Implement this method to run the model
- Parameters:
data (smif.data_layer.DataHandle) – Access state, parameter values, dependency inputs, results and interventions
Notes
See docs on
DataHandlefor details of how to access inputs, parameters and state and how to set results.intervalshould reference an id from the interval set corresponding to the output parameter, as specified in model configuration
regionshould reference a region name from the region set corresponding to the output parameter, as specified in model configuration
To obtain simulation model data in this method, use the methods such as:
parameter_value = data.get_parameter('my_parameter_name')
Other useful methods are
get_base_timestep_data(),get_previous_timestep_data(),get_parameter(),get_data(),get_parameters()andget_results().get_state()returns a list of intervention dict for the current timestepget_current_interventions()returns a list of dict where each dict is an intervention
smif.model.sos_model module¶
This module coordinates the software components that make up the integration framework.
A system of systems model contains simulation and scenario models, and the dependencies between the models.
- class smif.model.sos_model.SosModel(name)[source]¶
Bases:
objectConsists of the collection of models joined via dependencies
- Parameters:
name (str) – The unique name of the SosModel
- add_dependency(source_model, source_output_name, sink_model, sink_input_name, timestep=RelativeTimestep.CURRENT)[source]¶
Adds a dependency to this system-of-systems model
- Parameters:
source_model (smif.model.model.Model) – A reference to the source ~smif.model.model.Model object
source_output_name (string) – The name of the model_output defined in the source_model
source_model – A reference to the source ~smif.model.model.Model object
sink_name (string) – The name of a model_input defined in this object
timestep (smif.metadata.RelativeTimestep, optional) – The relative timestep of the dependency, defaults to CURRENT, may be PREVIOUS.
- add_model(model)[source]¶
Adds a model to the system-of-systems model
- Parameters:
model (
smif.model.model.Model)
- add_narrative(narrative)[source]¶
Add a narrative to the system-of-systems model
- Parameters:
narrative (dict)
- check_dependencies()[source]¶
For each contained model, compare dependency list against list of available models
- property dependencies¶
Dependency connections between models within this system-of-systems model
- Return type:
iterable[smif.model.dependency.Dependency]
- property free_inputs¶
Returns the free inputs to models which are not yet linked to a dependency.
- Returns:
dict of {(model_name, input_name)
- Return type:
smif.metadata.Spec}
- property model_dependencies¶
Dependency connections between models within this system-of-systems model
- Return type:
iterable[smif.model.dependency.Dependency]
- property models¶
The models contained within this system-of-systems model
- Return type:
- property outputs¶
All model outputs provided by models contained within this SosModel
- Returns:
dict of {(model_name, output_name)
- Return type:
smif.metadata.Spec}
- property scenario_dependencies¶
Dependency connections between models within this system-of-systems model
- Return type:
iterable[smif.model.dependency.Dependency]
- property scenario_models¶
Scenario model objects contained in the SosModel
- Returns:
A list of scenario model objects
- Return type: