simprocesd.model package

Subpackages

Submodules

simprocesd.model.resource_manager module

class simprocesd.model.resource_manager.ReservedResources(resource_manager, reserved_resources)

Bases: object

Tracks reserved resources from a request.

Use .release() function to release all reserved resources tracked by this ReservedResources instance.

release(resources=None)

Release resources back into the pool of available resources.

Parameters

resources (Dictionary, default=None) – A Dictionary where each entry specifies what resource to release and the amount to be released, resource_name:amount If None then it will release all the resources reserved here.

Raises
  • ValueError – If trying to release an amount of a resources that is more than is still reserved.

  • RuntimeError – When trying to release resources after the simulation was reset.

property reserved_resources

Dictionary of the resources reserved and their amounts.

class simprocesd.model.resource_manager.ResourceManager

Bases: object

Provides a pool of limited resources which can be reserved by callers and released back to the resource pool.

Callers can try to reserve resources immediately or they can provide a callback function to be invoked when the requested resources are available.

add_resources(resource_name, amount)

Add new unreserved resources or reduce the pool of available resources.

Parameters
  • resource_name (str) – Resource type identifier.

  • amount (float) – How much of the specified resource type to add or remove if the value is negative.

Raises

ValueError – When trying to release more of a resource than is currently unreserved.

property available_resources

Return a dictionary of unreserved resources.

initialize(env)

Prepare ResourceManager for simulation and reset attributes to starting values.

Called when simulation starts for the first time or when it is restarted.

Parameters

env (Environment) – Environment used in the simulation.

reserve_resources(request)

Try to reserve one or more types of resources.

If the entire request cannot be completed then no resources will be reserved.

Parameters

request (Dictionary) – A dictionary where each entry specifies what resource is requested and the amount requested, resource_name:amount.

Returns

  • ReservedResources with requested resources if successful.

  • Calling .release() on the returned object will make the reserved

  • resources available again.

  • Returns None if requested resources could not be reserved.

reserve_resources_with_callback(request, callback)

Set the callback function to be called when the request can be fulfilled.

The caller can use .reserve_resources within the callback to ensure that the available resources are not reserved elsewhere.

Parameters
  • request (Dictionary) – A Dictionary where each entry specifies what resource is requested and the amount requested, resource_name:amount.

  • callback (function) – Function to be invoked when there are sufficient available resources to fulfill the request. The functions needs to accept one parameter which will be the copy of the request.

simprocesd.model.simulation module

class simprocesd.model.simulation.Environment(name='environment', simulation_data_storage_type=DataStorageType.NONE, resource_manager=None)

Bases: object

Simulation environment.

Responsible for creating, prioritizing, executing, and otherwise managing Events.

Note

Environment is automatically created by System and generally should not be created manually.

Parameters
  • name (str, default='environment') – Environment name.

  • simulation_data_storage_type (DataStorageType, default=DataStorageType.NONE) – How to store <simulation_data>. Does not currently support DataStorageType.FILE

now

Current time of the simulation, starts at 0.

Type

float

simulation_data

Stored datapoints added with Environment.add_datapoint

Type

list

add_datapoint(list_label, sub_label, datapoint)

Record a new datapoint/item in the appropriate list.

Calling env.simulation_data[list_label][sub_label] will return a list of all datapoint that were created using the same labels.

Parameters
  • label (object) – Primary label. Usually a string describing the datapoint.

  • sub_label (object) – Secondary label. Usually a string specifying the source of data like an Asset name.

  • datapoint (object) – New datapoint that will be added to the list using list.append(datapoint). Can be a single object or a tuple.

cancel_matching_events(asset_id=None)

Find scheduled Events with matching parameters and mark them as cancelled.

Parameters

asset_id (int, optional) – If set, will only match events with the same asset_id

is_simulation_in_progress()

Indicates whether a simulation is in progress or not.

Return type

True if the simulation is currently in progress, otherwise False.

property now
pause_matching_events(asset_id=None)

Find scheduled Events with matching parameters and mark them as Paused. Paused Events can be resumed with environment.unpause_matching_events

Parameters

asset_id (int, optional) – If set, will only match events with the same asset_id

reset()

Reset the Environment to its initial state.

This will clear out all scheduled and paused events and reset the simulation_data table. To preserve the old data save a reference to simulation_data before calling reset, the reference can then be used to access old data.

run(simulation_duration, trace=False)

Simulate the system for a limited duration.

Will repeatedly execute a scheduled Event with the highest priority until specified simulation time passes.

Parameters
  • simulation_duration (float) – For how long to run the simulation. Measured in simulation time.

  • trace (bool, default=False) – If True then executed Events will be recorded and exported to a file at: ‘~/Downloads/{environment.name}_trace.json’

schedule_event(time, asset_id, action, event_type=EventType.OTHER_LOW_PRIORITY, message='')

Schedule an Event to be executed at a later simulation time.

Parameters
  • time (float) – Simulation time of when to perform the action. Cannot be lower than the current simulation time (Environment.now).

  • asset_id (int) – ID of the actor Asset. Should follow the rule that if the actor Asset were to shutdown then this event should be paused/cancelled.

  • action (function) – Action to be executed, a function with no arguments.

  • event_type (float, default=EventType.OTHER_LOW_PRIORITY) – Priority of the event, recommended to use EventType enum.

  • message (str, default='') – Any message to be associated with this Event. Useful as debugging information.

step()

Execute a scheduled Event with the highest priority.

unpause_matching_events(asset_id=None)

Find paused Events with matching parameters and unpause them.

The Events to be unpaused will be scheduled for their original time plus the duration of their pause.

Parameters

asset_id (int, optional) – If set, will only match events with the same asset_id

class simprocesd.model.simulation.Event(time, asset_id, action, event_type, message='')

Bases: object

Class used for scheduling actions within the simulation.

Higher Event priority is determined by:
1. lower <time>
2. if there is a tie, then higher <event_type>
3. if there is a tie, then a lower random weight
4. if there is a tie, then lower <asset_id>
Parameters
  • time (float) – Simulation time when the event will be executed.

  • asset_id (int) – ID of the Asset who the action belongs to.

  • action (function) – Action to be executed, a function with no arguments.

  • event_type (float) – Priority of the event, recommended to use EventType enum.

  • message (str, default='') – Any message to be associated with this Event. Useful as debugging information.

execute()

Calls the event’s action unless the event is marked as cancelled or as executed.

class simprocesd.model.simulation.EventType(value)

Bases: IntEnum

Events in the order of lowest to highest priority.

EventType priority makes a difference when multiple events are scheduled to happen at the exact same time, in those cases events with higher priority are executed first

To give a custom priority to an event you can specify a value that is slightly higher or lower than another event. For example: “event_type = EventType.FAIL - 0.1”. An event with this event_type will be executed with a slightly lower priority than EventType.FAIL

FAIL = 5
FINISH_PROCESSING = 8
FINISH_WORK = 10
OTHER_HIGH_PRIORITY = 11
OTHER_LOW_PRIORITY = 2
PASS_PART = 7
RELEASE_RESERVED_RESOURCES = 6
RESTORE = 9
SENSOR = 4
START_WORK = 3
TERMINATE = 1

simprocesd.model.system module

class simprocesd.model.system.System(simulation_data_storage_type=DataStorageType.MEMORY, resource_manager=None)

Bases: object

A required class that helps setup and run simulations.

System handles Asset initialization and manages the Environment object to provide an easy to use interface for running simulations.

Creating a new System sets it as the active System replacing another active System if there is one. Only the active system can run simulations.

System object needs to be created before any Asset because Assets will automatically try to register themselves with the active System.

Parameters
  • simulation_data_storage_type (DataStorageType, default=DataStorageType.MEMORY) – How to store <simulation_data>. Does not currently support DataStorageType.FILE

  • resource_manager (ResourceManager, default=None) – A ResourceManager instance to use for this simulation. If None then the default ResourceManager will be used.

static add_asset(new_asset)

Register an Asset with the active System.

Automatically called by newly created Assets. If the simulation is already in progress then the new_asset will be initialized immediately.

Note: System will keep a reference to this Asset so transitory assets should not be added here or they will remain loaded into memory. For example, Parts are not added here and parts are initialized by the Source that created them.

Parameters

new_asset (Asset) – Asset to be registered.

Raises

RuntimeError – System object must be created before non-transitory Assets.

find_assets(name=None, id_=None, type_=None, subtype=None)

Search through Assets registered with the System.

If multiple arguments are provided then it will return only the Assets that match all of the arguments.

Parameters
  • name (str, optional) – Filter Assets by name.

  • id (int, optional) – Filter Assets by ID.

  • type (type, optional) – Filter Assets by class type, checks immediate class only.

  • subtype (type, optional) – Filter Assets by class type, checks all of the Asset’s types using isinstance()

Returns

A list of Assets that match the provided arguments.

Return type

list

get_net_value_of_assets()

Calculate the net value of all Assets which are registered with the System.

Returns

Net value of Assets.

Return type

float

property resource_manager

ResourceManager instance.

simulate(simulation_duration, reset=True, trace=False, print_summary=True)

Run the simulation for the specified duration.

Ensures objects are initialized or re-initialized as needed.

Parameters
  • simulation_duration (float) – For how long to run the simulation. Measured in simulation time.

  • reset (bool, default=True) – If True then the simulation will be restarted from time zero and states of all Devices and other Assets will be reset to initial states. If False then the simulation will continue from the current simulation time.

  • trace (bool, default=False) – If True then the executed events will be recorded and the trace will be exported to a file at ‘~/Downloads/environment_trace.json’

  • print_summary (bool, default=True) – If True a brief summary will be printed at the end of the simulation.

property simulation_data

Stored datapoints added with Environment.add_datapoint(list_label, sub_label, datapoint)

To retrieving a list of datapoints call:
system.simulation_data[list_label][sub_label]

Module contents