Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
259 changes: 259 additions & 0 deletions notebooks/32_openclsim_opentnsim.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "bf80dcb5",
"metadata": {},
"outputs": [],
"source": [
"import datetime\n",
"import time\n",
"\n",
"import simpy\n",
"import pandas as pd\n",
"import shapely\n",
"\n",
"import openclsim\n",
"import openclsim.model\n",
"import opentnsim\n",
"import networkx as nx\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "3e4a3b86",
"metadata": {},
"outputs": [],
"source": [
"# setup environment\n",
"simulation_start = 0\n",
"my_env = simpy.Environment(initial_time=simulation_start)\n",
"graph = nx.Graph()\n",
"my_env.graph = graph"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "3be50ba8",
"metadata": {},
"outputs": [],
"source": [
"# create a Site object based on desired mixin classes\n",
"Site = type(\n",
" \"Site\",\n",
" (\n",
" openclsim.core.Identifiable,\n",
" openclsim.core.Log,\n",
" openclsim.core.Locatable,\n",
" openclsim.core.HasContainer,\n",
" openclsim.core.HasResource,\n",
" ),\n",
" {},\n",
")\n",
"\n",
"# create a TransportProcessingResource object based on desired mixin classes\n",
"TransportProcessingResource = type(\n",
" \"TransportProcessingResource\",\n",
" (\n",
" opentnsim.core.Movable,\n",
" openclsim.core.Identifiable,\n",
" openclsim.core.Log,\n",
" openclsim.core.HasResource,\n",
" openclsim.core.HasContainer,\n",
" openclsim.core.Processor,\n",
" # capture extra metadata to make sure we don't have leftover arguments\n",
" opentnsim.core.ExtraMetadata\n",
" ),\n",
" {},\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b2ee2d9a",
"metadata": {},
"outputs": [],
"source": [
"# prepare input data for from_site (note: in this example we only need the start location)\n",
"location_from_site = shapely.geometry.Point(4.18055556, 52.18664444)\n",
"\n",
"# prepare input data for to_site\n",
"location_to_site = shapely.geometry.Point(4.25222222, 52.11428333)\n",
"data_to_site = {\n",
" \"env\": my_env,\n",
" \"name\": \"to_site\",\n",
" \"geometry\": location_to_site,\n",
" \"capacity\": 100,\n",
" \"level\": 100\n",
"}\n",
"# instantiate to_site \n",
"to_site = Site(**data_to_site)\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "5bba2225",
"metadata": {},
"outputs": [],
"source": [
"# create a location based graph (with geometry as nodes)\n",
"my_env.graph.add_node(location_from_site, **{\"geometry\": location_from_site})\n",
"my_env.graph.add_node(location_to_site, **{\"geometry\": location_to_site})\n",
"my_env.graph.add_edge(location_from_site, location_to_site)\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "bed66786",
"metadata": {},
"outputs": [],
"source": [
"# prepare input data for vessel_01\n",
"data_vessel01 = {\n",
" \"env\": my_env,\n",
" \"name\": \"vessel01\",\n",
" \"geometry\": location_from_site, \n",
" \"capacity\": 5,\n",
" \"v\": 10,\n",
" \"route\": [location_from_site, location_to_site]\n",
"}\n",
"# instantiate vessel_01 \n",
"vessel01 = TransportProcessingResource(**data_vessel01)\n",
"assert not vessel01.metadata"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "34f963b3",
"metadata": {},
"outputs": [],
"source": [
"# initialise registry\n",
"registry = {}"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "88cabb7c",
"metadata": {},
"outputs": [],
"source": [
"activity = openclsim.model.MoveActivity(\n",
" env=my_env,\n",
" name=\"Move activity\",\n",
" registry=registry,\n",
" mover=vessel01,\n",
" destination=to_site,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "485057d1",
"metadata": {},
"outputs": [],
"source": [
"# initate the simpy processes defined in the 'move activity' and run simpy\n",
"openclsim.model.register_processes([activity])\n",
"my_env.run()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "44e6f1a7",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Activity</th>\n",
" <th>Timestamp</th>\n",
" <th>ActivityState</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>74edfe04-f803-4508-aca0-869c02e835ca</td>\n",
" <td>1970-01-01 00:00:00.000000</td>\n",
" <td>START</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>74edfe04-f803-4508-aca0-869c02e835ca</td>\n",
" <td>1970-01-01 00:15:42.824591</td>\n",
" <td>STOP</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Activity Timestamp \\\n",
"0 74edfe04-f803-4508-aca0-869c02e835ca 1970-01-01 00:00:00.000000 \n",
"1 74edfe04-f803-4508-aca0-869c02e835ca 1970-01-01 00:15:42.824591 \n",
"\n",
" ActivityState \n",
"0 START \n",
"1 STOP "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(openclsim.plot.get_log_dataframe(activity))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading