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 the reserved resources tracked by this ReservedResources instance.

merge(reserved_resources)

Merge the resources reserved by another ReservedResources object into this one.

Parameters:

reserved_resources (ReservedResources) – ReservedResources object whose reserved resources will be merged into this. The provided reserved_resources object will be set to have no reserve resources.

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 an amount to be released that is >= 0. For example: {‘resourceA’: 10, ‘resourceB’: 1} 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 or trying to release a negative amount of a resource.

property reserved_resources

Dictionary of the resources reserved and their amounts.

class simprocesd.model.resource_manager.ResourceManager

Bases: object

Provides pools of limited resources which can be reserved by callers and released back.

An attempt to reserve resources can be made immediately or a callback function to be registered to be invoked when the requested resources are available.

add_resources(resource_name, amount)

Add or reduce maximum resource capacity.

Note

Reducing resource capacity below current usage will not force those resources to be released therefore allowing resource usage to remain above the new maximum.

Parameters:
  • resource_name (str) – Resource type identifier.

  • amount (int, float) – How much of the specified resource type to add. If the value is negative then then maximum pool of that resource will be reduced.

Raises:

ValueError – When trying to reduce a resource by more than the maximum capacity of that resource.

get_resource_capacity(resource_name)

Get total capacity of a resource (reserved + unreserved).

Parameters:

resource_name (str) – Resource type identifier.

Returns:

What is the total amount of the specified resource, this includes both reserved and unreserved resources.

Return type:

float

get_resource_usage(resource_name)

Get how much of a resource is currently reserved/in-use.

Parameters:

resource_name (str) – Resource type identifier.

Returns:

How much of the specified resource is currently reserved.

Return type:

float

initialize(env)

Prepare ResourceManager for simulation.

Called when simulation starts for the first time.

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 an amount requested that is >= 0. For example: {‘resourceA’: 10, ‘resourceB’: 1}

Returns:

ReservedResources with reserved resources if successful. Returns None if the request could not be fulfilled.

Return type:

ReservedResources

reserve_resources_with_callback(request, callback)

Register a callback function to be invoked when the request can be fulfilled.

Resources will not be automatically reserved when the callback is invoked. The caller can use the reserve_resources function within the callback to immediately reserve those resources.

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

  • callback (function(resource_manager, request)) – Function to be invoked when there are sufficient available resources to fulfill the request. The callback needs to accept two parameter: - the resource manager - copy of the request Dictionary

simprocesd.model.simulation module

class simprocesd.model.simulation.Environment(name='environment', 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.

  • resource_manager (ResourceManager, default=None) – Instance of a ResourceManager that will be used by Assets.

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

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(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:

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.

property env

Simulation’s Environment instance.

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, trace=False, print_summary=True)

Run the simulation for the specified duration.

Ensures Assets are initialized when called for the first time.

Parameters:
  • simulation_duration (float) – For how long to run the simulation. Measured in 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.

static simulate_multiple_times(simulation, number_of_simulations, max_processes=0, *args, **kwargs)

Run a simulation multiple times using multiple processes.

Simulation function is executed multiple times with a new System instance each time. The simulation function needs to initialize the model and run the simulation by calling system_parameter.simulate(…)

Note

It is recommended to familiarize yourself with Python’s multi-process code execution such as each process having a separate copy of static variables. Each process may run multiple simulations, one after another.

Parameters:
  • simulation (function) – Function that sets up the model and runs it. The function will be called with 2+ parameter: - System instance to be used for this simulation. - integer index of the simulation - additional arguments, see *args, **kwargs

  • number_of_simulations (int > 0) – Number of times to run <simulation>.

  • max_processes (int, default=0) – Maximum number of processes to create for running simultaneous simulations. If 0, then simulations will run one after another in the current thread (and the current process). If None, then default to the number of processors on the machine

  • *args – Additional arguments will be passed to the simulation function.

  • **kwargs – Additional arguments will be passed to the simulation function.

Returns:

A list of System objects that were used to run each simulation. List length will equal <iterations>.

Return type:

list

Raises:

AttributeError – If you see error like: Can’t pickle local object ‘simulation.<locals>.function’ It means you defined an anonymous function (e.g. function within a function) in your code that the multiprocess setup does not support.

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