nesting in a RepeatActivity#
This notebook provides an example of OpenCLSim’s capability to nest various processes in a RepeatActivity.
0. Import libraries#
import datetime, time
import simpy
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 time needs to match the available weather data)
simulation_start = 0
my_env = simpy.Environment(initial_time=simulation_start)
2. Define object classes#
In this example we won’t use object classes
3. Create objects#
3.1. Create site object(s)#
No site objects are created
3.2. Create vessel object(s)#
No vessel objects are created
3.3 Create activity/activities#
# initialise registry
registry = {}
a = model.BasicActivity(
env= my_env,
name= "a",
ID= "a",
registry= registry,
duration= 1,
)
a2 = model.BasicActivity(
env= my_env,
name= "a2",
ID= "a2",
registry= registry,
duration= 1,
)
c = model.BasicActivity(
env= my_env,
name= "c",
ID= "c",
registry= registry,
duration= 1,
)
Ra=model.RepeatActivity(
env= my_env,
name= "Ra",
ID= "Ra",
registry= registry,
sub_processes= [a, a2],
repetitions=3
)
Sc=model.SequentialActivity(
env= my_env,
name= "Sc",
ID= "Sc",
registry= registry,
sub_processes= [c, Ra],
)
b = model.BasicActivity(
env= my_env,
name= "b",
ID= "b",
registry= registry,
duration= 1.5,
start_event=[
{
"name": "a",
"type": "activity",
"state": "done"
}
]
)
Rb=model.RepeatActivity(
env= my_env,
name= "Rb",
ID= "Rb",
registry= registry,
sub_processes= [b],
repetitions=3
)
4. Register processes and run simpy#
model.register_processes([Sc,Rb])
my_env.run()
5. Inspect results#
5.1 Inspect logs#
display(pd.DataFrame(Sc.log))
display(pd.DataFrame(c.log))
display(pd.DataFrame(Ra.log))
display(pd.DataFrame(a.log))
display(pd.DataFrame(a2.log))
display(pd.DataFrame(b.log))
Timestamp | ActivityID | ActivityState | ObjectState | ActivityLabel | |
---|---|---|---|---|---|
0 | 1970-01-01 00:00:00 | Sc | START | {} | {} |
1 | 1970-01-01 00:00:00 | Sc | START | {} | {'type': 'subprocess', 'ref': 'c'} |
2 | 1970-01-01 00:00:01 | Sc | STOP | {} | {'type': 'subprocess', 'ref': 'c'} |
3 | 1970-01-01 00:00:01 | Sc | START | {} | {'type': 'subprocess', 'ref': 'Ra'} |
4 | 1970-01-01 00:00:07 | Sc | STOP | {} | {'type': 'subprocess', 'ref': 'Ra'} |
5 | 1970-01-01 00:00:07 | Sc | STOP | {} | {} |
Timestamp | ActivityID | ActivityState | ObjectState | ActivityLabel | |
---|---|---|---|---|---|
0 | 1970-01-01 00:00:00 | c | START | {} | {} |
1 | 1970-01-01 00:00:01 | c | STOP | {} | {} |
Timestamp | ActivityID | ActivityState | ObjectState | ActivityLabel | |
---|---|---|---|---|---|
0 | 1970-01-01 00:00:01 | Ra | START | {} | {} |
1 | 1970-01-01 00:00:01 | Ra | START | {} | {'type': 'subprocess', 'ref': 'a'} |
2 | 1970-01-01 00:00:02 | Ra | STOP | {} | {'type': 'subprocess', 'ref': 'a'} |
3 | 1970-01-01 00:00:02 | Ra | START | {} | {'type': 'subprocess', 'ref': 'a2'} |
4 | 1970-01-01 00:00:03 | Ra | STOP | {} | {'type': 'subprocess', 'ref': 'a2'} |
5 | 1970-01-01 00:00:03 | Ra | START | {} | {'type': 'subprocess', 'ref': 'a'} |
6 | 1970-01-01 00:00:04 | Ra | STOP | {} | {'type': 'subprocess', 'ref': 'a'} |
7 | 1970-01-01 00:00:04 | Ra | START | {} | {'type': 'subprocess', 'ref': 'a2'} |
8 | 1970-01-01 00:00:05 | Ra | STOP | {} | {'type': 'subprocess', 'ref': 'a2'} |
9 | 1970-01-01 00:00:05 | Ra | START | {} | {'type': 'subprocess', 'ref': 'a'} |
10 | 1970-01-01 00:00:06 | Ra | STOP | {} | {'type': 'subprocess', 'ref': 'a'} |
11 | 1970-01-01 00:00:06 | Ra | START | {} | {'type': 'subprocess', 'ref': 'a2'} |
12 | 1970-01-01 00:00:07 | Ra | STOP | {} | {'type': 'subprocess', 'ref': 'a2'} |
13 | 1970-01-01 00:00:07 | Ra | STOP | {} | {} |
Timestamp | ActivityID | ActivityState | ObjectState | ActivityLabel | |
---|---|---|---|---|---|
0 | 1970-01-01 00:00:01 | a | START | {} | {} |
1 | 1970-01-01 00:00:02 | a | STOP | {} | {} |
2 | 1970-01-01 00:00:03 | a | START | {} | {} |
3 | 1970-01-01 00:00:04 | a | STOP | {} | {} |
4 | 1970-01-01 00:00:05 | a | START | {} | {} |
5 | 1970-01-01 00:00:06 | a | STOP | {} | {} |
Timestamp | ActivityID | ActivityState | ObjectState | ActivityLabel | |
---|---|---|---|---|---|
0 | 1970-01-01 00:00:02 | a2 | START | {} | {} |
1 | 1970-01-01 00:00:03 | a2 | STOP | {} | {} |
2 | 1970-01-01 00:00:04 | a2 | START | {} | {} |
3 | 1970-01-01 00:00:05 | a2 | STOP | {} | {} |
4 | 1970-01-01 00:00:06 | a2 | START | {} | {} |
5 | 1970-01-01 00:00:07 | a2 | STOP | {} | {} |
Timestamp | ActivityID | ActivityState | ObjectState | ActivityLabel | |
---|---|---|---|---|---|
0 | 1970-01-01 00:00:00.000 | b | WAIT_START | {} | {} |
1 | 1970-01-01 00:00:02.000 | b | WAIT_STOP | {} | {} |
2 | 1970-01-01 00:00:02.000 | b | START | {} | {} |
3 | 1970-01-01 00:00:03.500 | b | STOP | {} | {} |
4 | 1970-01-01 00:00:03.500 | b | WAIT_START | {} | {} |
5 | 1970-01-01 00:00:04.000 | b | WAIT_STOP | {} | {} |
6 | 1970-01-01 00:00:04.000 | b | START | {} | {} |
7 | 1970-01-01 00:00:05.500 | b | STOP | {} | {} |
8 | 1970-01-01 00:00:05.500 | b | WAIT_START | {} | {} |
9 | 1970-01-01 00:00:06.000 | b | WAIT_STOP | {} | {} |
10 | 1970-01-01 00:00:06.000 | b | START | {} | {} |
11 | 1970-01-01 00:00:07.500 | b | STOP | {} | {} |
5.2 Visualise gantt charts#
plot.get_gantt_chart([Sc,c,Ra,a,a2,b])