{ "cells": [ { "cell_type": "markdown", "id": "aaa8f205", "metadata": {}, "source": [ "# Gymnasium integration\n", "[Gymnasium](https://gymnasium.farama.org/) is an API standard for reinforcement learning with a diverse collection of reference environments. Stormvogel supports some integration with gymnasium. In particular, you can construct explicit models from the gymnasium environmnents under Gymnasium's [ToyText](https://gymnasium.farama.org/environments/toy_text/) (except Blackjack)." ] }, { "cell_type": "markdown", "id": "4c1befb9", "metadata": {}, "source": [ "## FrozenLake\n", "Let us create one of these environments, called FrozenLake.\n", "Our agent wants to get to the present. Currently, it just chooses a random action." ] }, { "cell_type": "code", "execution_count": 1, "id": "2f07c621", "metadata": { "execution": { "iopub.execute_input": "2026-07-01T08:29:56.272712Z", "iopub.status.busy": "2026-07-01T08:29:56.272532Z", "iopub.status.idle": "2026-07-01T08:29:57.051235Z", "shell.execute_reply": "2026-07-01T08:29:57.050630Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import gymnasium as gym\n", "from stormvogel.extensions.gym_grid import *\n", "from stormvogel.extensions.gifs import embed_gif\n", "from stormvogel import *\n", "\n", "env = gym.make(\n", " \"FrozenLake-v1\", render_mode=\"rgb_array\", is_slippery=False\n", ") # Set `is_slippery=True` for stochastic behavior\n", "filename = gymnasium_render_model_gif(env, filename=\"ice1\")\n", "embed_gif(filename)" ] }, { "cell_type": "markdown", "id": "ae807723", "metadata": {}, "source": [ "We can convert it into an explicit MDP as follows. Each state has a label that relates to the coordinates of the tile." ] }, { "cell_type": "code", "execution_count": 2, "id": "24b5c248", "metadata": { "execution": { "iopub.execute_input": "2026-07-01T08:29:57.053382Z", "iopub.status.busy": "2026-07-01T08:29:57.053096Z", "iopub.status.idle": "2026-07-01T08:29:57.241515Z", "shell.execute_reply": "2026-07-01T08:29:57.240993Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", " Network\n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", " \n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sv_model = gymnasium_grid_to_stormvogel(env, GRID_ACTION_LABEL_MAP)\n", "# GRID_ACTION_LABEL_MAP = {0: \"←\", 1: \"↓\", 2: \"→\", 3: \"↑\", 4: \"pickup\", 5: \"dropoff\"} (map between gymnasium action ids and labels)\n", "show(sv_model)" ] }, { "cell_type": "markdown", "id": "e3b004da", "metadata": {}, "source": [ "Now, let's do some model checking to calculate a strategy to solve the puzzle. We will tell the model checker to maximize the probability of getting to the target state (the present)." ] }, { "cell_type": "code", "execution_count": 3, "id": "0d05ddab", "metadata": { "execution": { "iopub.execute_input": "2026-07-01T08:29:57.250441Z", "iopub.status.busy": "2026-07-01T08:29:57.250030Z", "iopub.status.idle": "2026-07-01T08:29:57.284425Z", "shell.execute_reply": "2026-07-01T08:29:57.283812Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", " Network\n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", " \n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "res = model_checking(sv_model, 'Pmax=? [F \"target\"]')\n", "vis2 = show(sv_model, result=res)" ] }, { "cell_type": "markdown", "id": "ff0fc47e", "metadata": {}, "source": [ "Let's highlight the path to see what the scheduler is doing." ] }, { "cell_type": "code", "execution_count": 4, "id": "c5297f21", "metadata": { "execution": { "iopub.execute_input": "2026-07-01T08:29:57.294130Z", "iopub.status.busy": "2026-07-01T08:29:57.293866Z", "iopub.status.idle": "2026-07-01T08:30:12.351680Z", "shell.execute_reply": "2026-07-01T08:30:12.351094Z" } }, "outputs": [], "source": [ "path = simulate_path(sv_model, scheduler=res.scheduler, steps=20)\n", "vis2.highlight_path(path, color=\"orange\")" ] }, { "cell_type": "markdown", "id": "0ecd63f1", "metadata": {}, "source": [ "Alternatively, we can show what our scheduler does in the frozen lake environment itself." ] }, { "cell_type": "code", "execution_count": 5, "id": "1a9a54ed", "metadata": { "execution": { "iopub.execute_input": "2026-07-01T08:30:12.353494Z", "iopub.status.busy": "2026-07-01T08:30:12.353304Z", "iopub.status.idle": "2026-07-01T08:30:12.403802Z", "shell.execute_reply": "2026-07-01T08:30:12.403221Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from stormvogel.extensions.gym_grid import *\n", "\n", "gs = to_gymnasium_scheduler(sv_model, res.scheduler, GRID_ACTION_LABEL_MAP)\n", "filename = gymnasium_render_model_gif(env, gs, filename=\"ice2\")\n", "embed_gif(filename)" ] }, { "cell_type": "markdown", "id": "1f91a99d", "metadata": {}, "source": [ "We can also define a function to act as the scheduler on the model and convert it to a gymnasium scheduler. This one just keeps going in a loop..." ] }, { "cell_type": "code", "execution_count": 6, "id": "e099cc62", "metadata": { "execution": { "iopub.execute_input": "2026-07-01T08:30:12.405742Z", "iopub.status.busy": "2026-07-01T08:30:12.405553Z", "iopub.status.idle": "2026-07-01T08:30:12.736704Z", "shell.execute_reply": "2026-07-01T08:30:12.736151Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from stormvogel.model import Action\n", "\n", "\n", "def my_scheduler(s: stormvogel.model.State):\n", " # \"←\" \"↓\" \"→\" \"↑\"\n", " if s.is_initial():\n", " return Action(\"→\")\n", " env_id = s.get_valuation(stormvogel.model.Variable(\"env_id\"))\n", " x, y = to_coordinate(env_id, env)\n", " if x < 2 and y == 0:\n", " return Action(\"→\")\n", " elif x == 2 and y < 2:\n", " return Action(\"↓\")\n", " elif x > 0 and y == 2:\n", " return Action(\"←\")\n", " else:\n", " return Action(\"↑\")\n", "\n", "\n", "gs = to_gymnasium_scheduler(sv_model, my_scheduler, GRID_ACTION_LABEL_MAP)\n", "filename = gymnasium_render_model_gif(env, gs, filename=\"ice3\")\n", "embed_gif(filename)" ] }, { "cell_type": "markdown", "id": "326abe41", "metadata": {}, "source": [ "## CliffWalking\n", "CliffWalking is a slightly more boring version of FrozenLake. You can apply the same principles that we just applied to FrozenLake." ] }, { "cell_type": "code", "execution_count": 7, "id": "c233d369", "metadata": { "execution": { "iopub.execute_input": "2026-07-01T08:30:12.738496Z", "iopub.status.busy": "2026-07-01T08:30:12.738305Z", "iopub.status.idle": "2026-07-01T08:30:12.743869Z", "shell.execute_reply": "2026-07-01T08:30:12.743319Z" } }, "outputs": [], "source": [ "import gymnasium as gym\n", "from stormvogel.extensions.gym_grid import *\n", "\n", "env = gym.make(\"CliffWalking-v1\", render_mode=\"rgb_array\")" ] }, { "cell_type": "code", "execution_count": 8, "id": "2035e2e3", "metadata": { "execution": { "iopub.execute_input": "2026-07-01T08:30:12.745700Z", "iopub.status.busy": "2026-07-01T08:30:12.745525Z", "iopub.status.idle": "2026-07-01T08:30:12.781668Z", "shell.execute_reply": "2026-07-01T08:30:12.781155Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", " Network\n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", " \n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sv_model = gymnasium_grid_to_stormvogel(env, GRID_ACTION_LABEL_MAP)\n", "show(sv_model)" ] }, { "cell_type": "code", "execution_count": 9, "id": "825fc39e", "metadata": { "execution": { "iopub.execute_input": "2026-07-01T08:30:12.791535Z", "iopub.status.busy": "2026-07-01T08:30:12.791287Z", "iopub.status.idle": "2026-07-01T08:30:12.997082Z", "shell.execute_reply": "2026-07-01T08:30:12.996490Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from stormvogel.stormpy_utils.model_checking import model_checking\n", "\n", "res = model_checking(sv_model, 'Pmax=? [F \"target\"]')\n", "gs = to_gymnasium_scheduler(sv_model, res.scheduler, GRID_ACTION_LABEL_MAP)\n", "filename = gymnasium_render_model_gif(env, gs, filename=\"cliff\")\n", "embed_gif(filename)" ] }, { "cell_type": "markdown", "id": "61428987", "metadata": {}, "source": [ "## Taxi\n", "In the Taxi scenario, a taxi has to pick up passengers and transport them to the hotel. The position of the target, passenger and taxi are chosen at random." ] }, { "cell_type": "code", "execution_count": 10, "id": "5bb2e6cb", "metadata": { "execution": { "iopub.execute_input": "2026-07-01T08:30:12.998981Z", "iopub.status.busy": "2026-07-01T08:30:12.998789Z", "iopub.status.idle": "2026-07-01T08:30:13.149617Z", "shell.execute_reply": "2026-07-01T08:30:13.149104Z" } }, "outputs": [ { "data": { "text/plain": [ "'ModelType.MDP model with 3529 states, 6049 choices, and 1005 distinct labels.'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import gymnasium as gym\n", "from stormvogel.extensions.gym_grid import *\n", "\n", "env = gym.make(\n", " \"Taxi-v4\", render_mode=\"rgb_array\"\n", ") # Set `is_slippery=True` for stochastic behavior\n", "sv_model = gymnasium_grid_to_stormvogel(env)\n", "# This model is so big that it is better not to display it.\n", "sv_model.summary()" ] }, { "cell_type": "code", "execution_count": 11, "id": "3be22ee7", "metadata": { "execution": { "iopub.execute_input": "2026-07-01T08:30:13.151457Z", "iopub.status.busy": "2026-07-01T08:30:13.151278Z", "iopub.status.idle": "2026-07-01T08:30:14.127288Z", "shell.execute_reply": "2026-07-01T08:30:14.126660Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "target = get_target_state(env)\n", "res = model_checking(sv_model, \"Rmax=? [S]\")\n", "gs = to_gymnasium_scheduler(sv_model, res.scheduler, GRID_ACTION_LABEL_MAP)\n", "filename = gymnasium_render_model_gif(env, gs, filename=\"taxi\")\n", "embed_gif(filename)" ] } ], "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.13.14" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": { "2a10e877b1cd429a9b9416d39cf52273": { "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_2e6062ab7ec64445b7f93fc7213c86d8", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "2e6062ab7ec64445b7f93fc7213c86d8": { "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 } }, "339f5f2dd2964d61be1695471b137326": { "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_369aa70d03ab43b5bd61d9a822feae64", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "369aa70d03ab43b5bd61d9a822feae64": { "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 } }, "372f36f6930549759b594ffd355721ed": { "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 } }, "8b9ffb82f310433c8b8c72f24405c035": { "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_faa4a45f9c284908a4c4b61d7045583d", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "afb60f9ffb0e4a01aabd9db19dcaa157": { "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_de27591772b447929a7ac5de0842217b", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "b32733c8248a4ff28b1e6c9abef7ff4c": { "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 } }, "c61bafe0846047e0a486ac6b7fe04797": { "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_372f36f6930549759b594ffd355721ed", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "de27591772b447929a7ac5de0842217b": { "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 } }, "ea7d418f600e46d497cc407b2c268e3e": { "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_b32733c8248a4ff28b1e6c9abef7ff4c", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "faa4a45f9c284908a4c4b61d7045583d": { "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 }