model.ShiftAmountActivity#

This notebook shows the workings of model.ShiftAmountActivity. This activity uses a processor to transfer a specified number of objects from an origin resource, which must have a container, to a destination resource, which also must have a container. In this case it shifts payload from a from_site to vessel01.

NB: The model.ShiftAmountActivity checks the possible amount of objects which can be transferred, based on the number of objects available in the origin, the number of objects which can be stored in the destination and the number of objects requested to be transferred. If the number of actually to be transferred objects is zero than an exception is raised. These cases have to be prevented by using appropriate events.

0. Import libraries#

import datetime, time
import simpy

import shapely.geometry
import pandas as pd

import openclsim.core as core
import openclsim.model as model
import openclsim.plot as plot

1. Initialise simpy environment#

# setup environment
simulation_start = 0
my_env = simpy.Environment(initial_time=simulation_start)

2. Define object classes#

# create a Site object based on desired mixin classes
Site = type(
    "Site",
    (
        core.Identifiable,
        core.Log,
        core.Locatable,
        core.HasContainer,
        core.HasResource,
    ),
    {},
)

# create a TransportProcessingResource object based on desired mixin classes
TransportProcessingResource = type(
    "TransportProcessingResource",
    (
        core.ContainerDependentMovable,
        core.HasResource,
        core.Processor,
        core.Identifiable,
        core.Log,
    ),
    {},
)

3. Create objects#

3.1. Create site object(s)#

# prepare input data for from_site
location_from_site = shapely.geometry.Point(4.18055556, 52.18664444)
data_from_site = {"env": my_env,
                "name": "from_site",
                "geometry": location_from_site,
                "capacity": 100,
                "level": 100
               }
# instantiate from_site 
from_site = Site(**data_from_site)

3.2. Create vessel object(s)#

# prepare input data for vessel_01
data_vessel01 = {"env": my_env,
                 "name": "vessel01",
                 "geometry": location_from_site, 
                 "capacity": 5,
                 "compute_v": lambda x: 10
               }
# instantiate vessel_01 
vessel01 = TransportProcessingResource(**data_vessel01)

3.3 Create activity/activities#

# initialise registry
registry = {}
shift_amount_activity_data = model.ShiftAmountActivity(
    env=my_env,
    name="Shift amount activity",
    registry=registry,
    processor=vessel01,
    origin=from_site,
    destination=vessel01,
    amount=100,
    duration=60,
)

4. Register processes and run simpy#

model.register_processes([shift_amount_activity_data])
my_env.run()

5. Inspect results#

5.1 Inspect logs#

We can now inspect the logs. The model now shifted cargo from the from_site onto vessel01.

display(plot.get_log_dataframe(shift_amount_activity_data, [shift_amount_activity_data]))
Activity Timestamp ActivityState
0 Shift amount activity 1970-01-01 00:00:00 START
1 Shift amount activity 1970-01-01 00:01:00 STOP
display(plot.get_log_dataframe(from_site, [shift_amount_activity_data]))
Activity Timestamp ActivityState container level geometry
0 Shift amount activity 1970-01-01 00:00:00 START 100.0 POINT (4.18055556 52.18664444)
1 Shift amount activity 1970-01-01 00:01:00 STOP 95.0 POINT (4.18055556 52.18664444)
display(plot.get_log_dataframe(vessel01, [shift_amount_activity_data]))
Activity Timestamp ActivityState container level geometry
0 Shift amount activity 1970-01-01 00:00:00 START 0.0 POINT (4.18055556 52.18664444)
1 Shift amount activity 1970-01-01 00:01:00 STOP 5.0 POINT (4.18055556 52.18664444)

Observe that an amount has been shifted from from_site to vessel01. There was no movement.