{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## model.RepeatActivity\n", "\n", "This notebook provides an example of OpenCLSim's __model.RepeatActivity__. The repeat activity repeats a set of __sub_processes__ a given number of times." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 0. Import libraries" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import datetime, time\n", "import simpy\n", "\n", "import shapely.geometry\n", "import pandas as pd\n", "\n", "import openclsim.core as core\n", "import openclsim.model as model\n", "import openclsim.plot as plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1. Initialise simpy environment" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# setup environment\n", "simulation_start = 0\n", "my_env = simpy.Environment(initial_time=simulation_start)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2. Define object classes\n", "In this example we won't use object classes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 3. Create objects\n", "##### 3.1. Create site object(s)\n", "No site objects are created" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 3.2. Create vessel object(s)\n", "No vessel objects are created" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 3.3 Create activity/activities" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# initialise registry\n", "registry = {}" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# create a list of the sub processes\n", "sub_processes = [\n", " model.BasicActivity(\n", " env=my_env,\n", " name=\"Basic activity\",\n", " registry=registry,\n", " duration=20,\n", " )\n", "]\n", "\n", "# create a 'repeat activity' that is made up of the 'sub_processes'\n", "repeat_activity = model.RepeatActivity(\n", " env=my_env,\n", " name=\"Repeat activity\",\n", " registry=registry,\n", " sub_processes=sub_processes,\n", " repetitions=3,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 4. Register processes and run simpy" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# initate the simpy processes defined in the 'repeat activity' and run simpy\n", "model.register_processes([repeat_activity])\n", "my_env.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 5. Inspect results\n", "##### 5.1 Inspect logs" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ActivityTimestampActivityStatetyperef
0Repeat activity1970-01-01 00:00:00STARTNaNNaN
1Repeat activity1970-01-01 00:00:00STARTsubprocessf38fdf97-d4c5-4e1a-ba02-a86c89faa934
2Repeat activity1970-01-01 00:00:20STOPsubprocessf38fdf97-d4c5-4e1a-ba02-a86c89faa934
3Repeat activity1970-01-01 00:00:20STARTsubprocessf38fdf97-d4c5-4e1a-ba02-a86c89faa934
4Repeat activity1970-01-01 00:00:40STOPsubprocessf38fdf97-d4c5-4e1a-ba02-a86c89faa934
5Repeat activity1970-01-01 00:00:40STARTsubprocessf38fdf97-d4c5-4e1a-ba02-a86c89faa934
6Repeat activity1970-01-01 00:01:00STOPsubprocessf38fdf97-d4c5-4e1a-ba02-a86c89faa934
7Repeat activity1970-01-01 00:01:00STOPNaNNaN
\n", "
" ], "text/plain": [ " Activity Timestamp ActivityState type \\\n", "0 Repeat activity 1970-01-01 00:00:00 START NaN \n", "1 Repeat activity 1970-01-01 00:00:00 START subprocess \n", "2 Repeat activity 1970-01-01 00:00:20 STOP subprocess \n", "3 Repeat activity 1970-01-01 00:00:20 START subprocess \n", "4 Repeat activity 1970-01-01 00:00:40 STOP subprocess \n", "5 Repeat activity 1970-01-01 00:00:40 START subprocess \n", "6 Repeat activity 1970-01-01 00:01:00 STOP subprocess \n", "7 Repeat activity 1970-01-01 00:01:00 STOP NaN \n", "\n", " ref \n", "0 NaN \n", "1 f38fdf97-d4c5-4e1a-ba02-a86c89faa934 \n", "2 f38fdf97-d4c5-4e1a-ba02-a86c89faa934 \n", "3 f38fdf97-d4c5-4e1a-ba02-a86c89faa934 \n", "4 f38fdf97-d4c5-4e1a-ba02-a86c89faa934 \n", "5 f38fdf97-d4c5-4e1a-ba02-a86c89faa934 \n", "6 f38fdf97-d4c5-4e1a-ba02-a86c89faa934 \n", "7 NaN " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display(plot.get_log_dataframe(repeat_activity, [repeat_activity]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 5.2 Visualise gantt charts" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.plotly.v1+json": { "config": { "linkText": "Export to plot.ly", "plotlyServerURL": "https://plot.ly", "showLink": false }, "data": [ { "connectgaps": false, "hoverinfo": "y+name", "line": { "color": "rgb(205,6,81)", "width": 10 }, "mode": "lines", "name": "Repeat activity", "type": "scatter", "x": [ "1970-01-01T00:00:00", "1970-01-01T00:00:00", "1970-01-01T00:00:20", "1970-01-01T00:00:20", "1970-01-01T00:00:20", "1970-01-01T00:00:20", "1970-01-01T00:00:20", "1970-01-01T00:00:40", "1970-01-01T00:00:40", "1970-01-01T00:00:40", "1970-01-01T00:00:40", "1970-01-01T00:00:40", "1970-01-01T00:01:00", "1970-01-01T00:01:00", "1970-01-01T00:01:00", "1970-01-01T00:00:40", "1970-01-01T00:00:40", "1970-01-01T00:01:00", "1970-01-01T00:01:00", "1970-01-01T00:01:00" ], "y": [ "Repeat activity", "Repeat activity", "Repeat activity", "Repeat activity", null, "Repeat activity", "Repeat activity", "Repeat activity", "Repeat activity", null, "Repeat activity", "Repeat activity", "Repeat activity", "Repeat activity", null, "Repeat activity", "Repeat activity", "Repeat activity", "Repeat activity", null ] }, { "connectgaps": false, "hoverinfo": "y+name", "line": { "color": "rgb(77,134,209)", "width": 10 }, "mode": "lines", "name": "Basic activity", "type": "scatter", "x": [ "1970-01-01T00:00:00", "1970-01-01T00:00:00", "1970-01-01T00:00:20", "1970-01-01T00:00:20", "1970-01-01T00:00:20", "1970-01-01T00:00:20", "1970-01-01T00:00:20", "1970-01-01T00:00:40", "1970-01-01T00:00:40", "1970-01-01T00:00:40", "1970-01-01T00:00:40", "1970-01-01T00:00:40", "1970-01-01T00:01:00", "1970-01-01T00:01:00", "1970-01-01T00:01:00" ], "y": [ "Basic activity", "Basic activity", "Basic activity", "Basic activity", null, "Basic activity", "Basic activity", "Basic activity", "Basic activity", null, "Basic activity", "Basic activity", "Basic activity", "Basic activity", null ] } ], "layout": { "hovermode": "closest", "legend": { "orientation": "h", "x": 0, "y": -0.2 }, "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "sequentialminus": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } }, "title": { "text": "GANTT Chart" }, "xaxis": { "range": [ "1970-01-01T00:00:00", "1970-01-01T00:01:00" ], "title": { "font": { "color": "#7f7f7f", "family": "Courier New, monospace", "size": 18 }, "text": "Time" } }, "yaxis": { "title": { "font": { "color": "#7f7f7f", "family": "Courier New, monospace", "size": 18 }, "text": "Activities" } } } }, "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "plot.get_gantt_chart([repeat_activity, *sub_processes])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Observe that the basic activity has a duration of 20 time units. The repeat activity has specified 3 repetitions. \n", "\n", "The logs show that indeed three Basic activities have been completed. The simulation stops at t = 60.\n", "\n", "Also the gantt charts shows this. You can find the edges of the basic activities by hovering with your mouse over the Basic activity bar. The horizontal time axis of the gantt chart ends at t = 60." ] } ], "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.12" } }, "nbformat": 4, "nbformat_minor": 2 }