{ "cells": [ { "cell_type": "markdown", "id": "eafd58d7", "metadata": {}, "source": [ "# Simulator\n", "If we have a transition system, it might be nice to run a simulation. In this case, we have an MDP that models a hungry lion. Depending on the state it is in, it needs to decide whether it wants to 'rawr' or 'hunt' in order to prevent reaching the state 'dead'." ] }, { "cell_type": "code", "execution_count": 1, "id": "76b86858", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:47:43.876896Z", "iopub.status.busy": "2026-03-26T10:47:43.876664Z", "iopub.status.idle": "2026-03-26T10:47:44.271421Z", "shell.execute_reply": "2026-03-26T10:47:44.270835Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", " Network\n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from stormvogel import *\n", "\n", "lion = examples.create_lion_mdp()\n", "vis = show(lion, layout=Layout(\"layouts/lion.json\"))" ] }, { "cell_type": "markdown", "id": "1b3e5f86", "metadata": {}, "source": [ "Now, let's run a simulation of the lion! If we do not provide a scheduling function, then the simulator just does a random walk, taking a random choice each time." ] }, { "cell_type": "code", "execution_count": 2, "id": "db7a5713", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:47:44.278653Z", "iopub.status.busy": "2026-03-26T10:47:44.278339Z", "iopub.status.idle": "2026-03-26T10:47:44.281180Z", "shell.execute_reply": "2026-03-26T10:47:44.280739Z" } }, "outputs": [], "source": [ "path = simulate_path(lion, steps=5, seed=1234)" ] }, { "cell_type": "markdown", "id": "48c383bc", "metadata": {}, "source": [ "Let's visualize this path in the visualization. Take a look at the model above after executing the next cell." ] }, { "cell_type": "code", "execution_count": 3, "id": "d095c7db", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:47:44.282872Z", "iopub.status.busy": "2026-03-26T10:47:44.282707Z", "iopub.status.idle": "2026-03-26T10:47:55.322548Z", "shell.execute_reply": "2026-03-26T10:47:55.321970Z" }, "lines_to_next_cell": 2 }, "outputs": [], "source": [ "vis.highlight_path(path, \"red\", 1, clear=True)" ] }, { "cell_type": "markdown", "id": "1b4b53e8", "metadata": { "lines_to_next_cell": 2 }, "source": [ "We could also provide a scheduling function to choose the actions ourselves. This is somewhat similar to the `bird` API." ] }, { "cell_type": "code", "execution_count": 4, "id": "0fcd066b", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:47:55.324260Z", "iopub.status.busy": "2026-03-26T10:47:55.324094Z", "iopub.status.idle": "2026-03-26T10:47:55.326898Z", "shell.execute_reply": "2026-03-26T10:47:55.326475Z" } }, "outputs": [], "source": [ "def scheduler(s: State) -> Action:\n", " return Action(\"rawr\")\n", "\n", "\n", "path2 = stormvogel.simulator.simulate_path(\n", " lion, steps=5, seed=1234, scheduler=scheduler\n", ")" ] }, { "cell_type": "code", "execution_count": 5, "id": "fec81952", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:47:55.328561Z", "iopub.status.busy": "2026-03-26T10:47:55.328397Z", "iopub.status.idle": "2026-03-26T10:48:06.368128Z", "shell.execute_reply": "2026-03-26T10:48:06.367559Z" } }, "outputs": [], "source": [ "vis.highlight_path(path2, \"red\", 1, clear=True)" ] }, { "cell_type": "markdown", "id": "d40a797b", "metadata": {}, "source": [ "We can also use the scheduler to create a partial model. This model contains all the states that have been discovered by the the simulation." ] }, { "cell_type": "code", "execution_count": 6, "id": "fc530026", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:48:06.370099Z", "iopub.status.busy": "2026-03-26T10:48:06.369931Z", "iopub.status.idle": "2026-03-26T10:48:06.398812Z", "shell.execute_reply": "2026-03-26T10:48:06.398260Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", " Network\n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "partial_model = stormvogel.simulator.simulate(\n", " lion, steps=5, scheduler=scheduler, seed=1234\n", ")\n", "vis2 = show(partial_model)" ] } ], "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.12.13" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": { "2b54dcc60fba410e98807a71720e9f47": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "2.0.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "2.0.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border_bottom": null, "border_left": null, "border_right": null, "border_top": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "359b48683dbd46a3b657b8a9d4a463d7": { "model_module": "@jupyter-widgets/output", "model_module_version": "1.0.0", "model_name": "OutputModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/output", "_model_module_version": "1.0.0", "_model_name": "OutputModel", "_view_count": null, "_view_module": "@jupyter-widgets/output", "_view_module_version": "1.0.0", "_view_name": "OutputView", "layout": "IPY_MODEL_54953f54b48942e7ba697e1eac5351b1", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "41db7d68220240519d76ed0bf821b8c8": { "model_module": "@jupyter-widgets/output", "model_module_version": "1.0.0", "model_name": "OutputModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/output", "_model_module_version": "1.0.0", "_model_name": "OutputModel", "_view_count": null, "_view_module": "@jupyter-widgets/output", "_view_module_version": "1.0.0", "_view_name": "OutputView", "layout": "IPY_MODEL_61cb54a624894a649ff042184555c7c2", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "54953f54b48942e7ba697e1eac5351b1": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "2.0.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "2.0.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border_bottom": null, "border_left": null, "border_right": null, "border_top": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "570b3be5537d41c6b1d019cad59e1707": { "model_module": "@jupyter-widgets/output", "model_module_version": "1.0.0", "model_name": "OutputModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/output", "_model_module_version": "1.0.0", "_model_name": "OutputModel", "_view_count": null, "_view_module": "@jupyter-widgets/output", "_view_module_version": "1.0.0", "_view_name": "OutputView", "layout": "IPY_MODEL_a96a4b166c85428d9de94c5a948bef49", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "61cb54a624894a649ff042184555c7c2": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "2.0.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "2.0.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border_bottom": null, "border_left": null, "border_right": null, "border_top": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "7efa78ef193f44cbaa903c547d09dc59": { "model_module": "@jupyter-widgets/output", "model_module_version": "1.0.0", "model_name": "OutputModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/output", "_model_module_version": "1.0.0", "_model_name": "OutputModel", "_view_count": null, "_view_module": "@jupyter-widgets/output", "_view_module_version": "1.0.0", "_view_name": "OutputView", "layout": "IPY_MODEL_2b54dcc60fba410e98807a71720e9f47", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "a96a4b166c85428d9de94c5a948bef49": { "model_module": "@jupyter-widgets/base", "model_module_version": "2.0.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "2.0.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "2.0.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border_bottom": null, "border_left": null, "border_right": null, "border_top": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } } }, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }