Skip to content

Commit

Permalink
Merge pull request #147 from TUDelft-CITG/feature/openclsim-routable-…
Browse files Browse the repository at this point in the history
…test

OpenCLSim routing using path and route
  • Loading branch information
Fedor Baart committed Mar 19, 2024
2 parents d966163 + efea262 commit e8ee298
Show file tree
Hide file tree
Showing 33 changed files with 1,285 additions and 80 deletions.
68 changes: 38 additions & 30 deletions notebooks/32_openclsim_opentnsim.ipynb
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d7c55f61-8f73-4f3f-9241-8992bb5aa60e",
"metadata": {},
"source": [
"# Combining OpenCLSim and OpenTNSim\n",
"\n",
"This notebook shows how to combine elements from OpenCLSim and OpenTNSim. You need OpenTNSim>=1.3.0 and OpenCLSim>=1.6 for this notebook to work. Since that version the core components (`Locatable`, `Identifiable`, `SimpyObject`) of opentnsim are referring to the versions from OpenCLSim. \n",
"\n",
"In this notebook we show how to use the \"move\" functionality of OpenTNSim with the activity workflow from OpenCLSim.\n",
"OpenCLSim does not allow sailing over graphs by default. On the other hand, OpenTNSim does not have the concept of activities."
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"id": "bf80dcb5",
"metadata": {},
"outputs": [],
Expand All @@ -25,7 +38,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"id": "3e4a3b86",
"metadata": {},
"outputs": [],
Expand All @@ -39,7 +52,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"id": "3be50ba8",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -76,7 +89,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"id": "b2ee2d9a",
"metadata": {},
"outputs": [],
Expand All @@ -99,20 +112,20 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"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"
"my_env.graph.add_edge(location_from_site, location_to_site)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"id": "bed66786",
"metadata": {},
"outputs": [],
Expand All @@ -133,7 +146,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"id": "34f963b3",
"metadata": {},
"outputs": [],
Expand All @@ -144,7 +157,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"id": "88cabb7c",
"metadata": {},
"outputs": [],
Expand All @@ -160,23 +173,10 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"id": "485057d1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"mover vessel01 destination to_site\n",
"getting location from to_site\n",
"moving to POINT (4.25222222 52.11428333)\n",
"moving over (<POINT (4.181 52.187)>, <POINT (4.252 52.114)>)\n",
"mover vessel01 is now at POINT (4.25222222 52.11428333)\n",
"mover vessel01 is now at POINT (4.25222222 52.11428333)\n"
]
}
],
"outputs": [],
"source": [
"# initate the simpy processes defined in the 'move activity' and run simpy\n",
"openclsim.model.register_processes([activity])\n",
Expand All @@ -185,7 +185,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"id": "44e6f1a7",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -218,13 +218,13 @@
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>7017847c-7178-4b59-bc7d-95ae3bf5ba85</td>\n",
" <td>adbf9f81-36fd-4032-9c30-2a94d12eda58</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>7017847c-7178-4b59-bc7d-95ae3bf5ba85</td>\n",
" <td>adbf9f81-36fd-4032-9c30-2a94d12eda58</td>\n",
" <td>1970-01-01 00:15:42.824591</td>\n",
" <td>STOP</td>\n",
" </tr>\n",
Expand All @@ -234,8 +234,8 @@
],
"text/plain": [
" Activity Timestamp \\\n",
"0 7017847c-7178-4b59-bc7d-95ae3bf5ba85 1970-01-01 00:00:00.000000 \n",
"1 7017847c-7178-4b59-bc7d-95ae3bf5ba85 1970-01-01 00:15:42.824591 \n",
"0 adbf9f81-36fd-4032-9c30-2a94d12eda58 1970-01-01 00:00:00.000000 \n",
"1 adbf9f81-36fd-4032-9c30-2a94d12eda58 1970-01-01 00:15:42.824591 \n",
"\n",
" ActivityState \n",
"0 START \n",
Expand All @@ -249,6 +249,14 @@
"source": [
"display(openclsim.plot.get_log_dataframe(activity))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c98fe858-e9c4-406f-ae8a-454b3850363f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -267,7 +275,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
"version": "3.11.6"
}
},
"nbformat": 4,
Expand Down
75 changes: 51 additions & 24 deletions notebooks/32_openclsim_opentnsim_moving.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"source": [
"# Combining OpenCLSim and OpenTNSim\n",
"\n",
"This notebook shows how to combine elements from OpenCLSim and OpenTNSim. You need OpenTNSim>=1.3.0 and OpenCLSim>=1.6 for this notebook to work. Since that version the core components (locatable, identifiable, simpyobject) of opentnsim are referring to the versions from OpenCLSim. \n",
"This notebook shows how to combine elements from OpenCLSim and OpenTNSim. You need OpenTNSim>=1.3.0 and OpenCLSim>=1.6 for this notebook to work. Since that version the core components (`Locatable`, `Identifiable`, `SimpyObject`) of opentnsim are referring to the versions from OpenCLSim. \n",
"\n",
"In this notebook we show how to use the \"move\" functionality of OpenTNSim with the activity workflow from OpenCLSim.\n",
"OpenCLSim does not allow sailing over graphs by default. OpenTNSim does not have the concept of activities. \n"
"OpenCLSim does not allow sailing over graphs by default. On the other hand, OpenTNSim does not have the concept of activities."
]
},
{
Expand All @@ -30,7 +30,12 @@
"import openclsim\n",
"import openclsim.model\n",
"import opentnsim\n",
"import networkx as nx\n"
"import networkx as nx\n",
"\n",
"import pyproj\n",
"\n",
"# Earth, to compute spherical distances\n",
"WGS84 = pyproj.Geod(ellps=\"WGS84\")"
]
},
{
Expand Down Expand Up @@ -90,7 +95,7 @@
"metadata": {},
"source": [
"## Create the points and geometries\n",
"In OpenTNSim nodes are points, you can sail over a linestring between points. Here we add an extra point in between the form and to location. That way you can sail more complex routes than just great circles. "
"In OpenTNSim nodes are points, you can sail over a linestring between points. Here we add an extra point in between the `location_from_site` and the `location_to_site`. That way you can sail more complex routes than just great circles. "
]
},
{
Expand Down Expand Up @@ -122,6 +127,7 @@
"\n",
"# create a geometry in between\n",
"intermediate_point = shapely.geometry.Point(4.2, 52.14)\n",
"\n",
"# Line of 3 points\n",
"edge_geometry = shapely.geometry.LineString([location_from_site, intermediate_point, location_to_site])\n",
"edge_geometry"
Expand Down Expand Up @@ -198,7 +204,7 @@
"metadata": {},
"source": [
"## Define the vessel\n",
"Here we start to integrate OpenCLSim and OpenTNSim. We make a composed type of Movable and the OpenCLSim components that allow to move and transport goods (container, processor). "
"Here we start to integrate OpenCLSim and OpenTNSim. We make a composed type of `Movable` and the OpenCLSim components that allow to move and transport goods (`HasContainer`, `Processor`). "
]
},
{
Expand Down Expand Up @@ -232,7 +238,7 @@
"metadata": {},
"source": [
"## Instantiate the vessel\n",
"We can now make the vessel. Compared to a normal OpenCLSim route we have an extra route property."
"We can now make the vessel. Compared to a normal OpenCLSim route we have an extra `route` property."
]
},
{
Expand Down Expand Up @@ -297,18 +303,7 @@
"execution_count": 11,
"id": "485057d1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"mover vessel-01 destination to_site\n",
"passing edge from_site to_site\n",
"mover vessel-01 is now at POINT (4.25222222 52.11428333)\n",
"mover vessel-01 is now at POINT (4.25222222 52.11428333)\n"
]
}
],
"outputs": [],
"source": [
"# initate the simpy processes defined in the 'move activity' and run simpy\n",
"openclsim.model.register_processes([activity])\n",
Expand All @@ -321,7 +316,7 @@
"metadata": {},
"source": [
"## Show the results\n",
"We can now display how long it took. It should take more than 15min. "
"We can now display how long it took."
]
},
{
Expand Down Expand Up @@ -359,13 +354,13 @@
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>4260f129-7451-44cd-baa8-89373a5c8521</td>\n",
" <td>6c3e62da-1229-42c1-b1da-035a7743920a</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>4260f129-7451-44cd-baa8-89373a5c8521</td>\n",
" <td>6c3e62da-1229-42c1-b1da-035a7743920a</td>\n",
" <td>1970-01-01 00:16:33.819868</td>\n",
" <td>STOP</td>\n",
" </tr>\n",
Expand All @@ -375,8 +370,8 @@
],
"text/plain": [
" Activity Timestamp \\\n",
"0 4260f129-7451-44cd-baa8-89373a5c8521 1970-01-01 00:00:00.000000 \n",
"1 4260f129-7451-44cd-baa8-89373a5c8521 1970-01-01 00:16:33.819868 \n",
"0 6c3e62da-1229-42c1-b1da-035a7743920a 1970-01-01 00:00:00.000000 \n",
"1 6c3e62da-1229-42c1-b1da-035a7743920a 1970-01-01 00:16:33.819868 \n",
"\n",
" ActivityState \n",
"0 START \n",
Expand All @@ -390,6 +385,38 @@
"source": [
"display(openclsim.plot.get_log_dataframe(activity))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "833ba8d1-06d1-492c-ae10-9ee0401502a5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"datetime.datetime(1970, 1, 1, 0, 16, 33, 819868)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# compute by hand to confirm the duration\n",
"\n",
"distance = WGS84.geometry_length(edge_geometry)\n",
"datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=distance / vessel.v)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "805fd1c5-8e96-411b-949d-36e7e3d475a8",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -408,7 +435,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
"version": "3.11.6"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit e8ee298

Please sign in to comment.