{ "cells": [ { "cell_type": "markdown", "id": "38fc4bf8", "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": "1e6e0ca5", "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": "dc437a89", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:41:37.884754Z", "iopub.status.busy": "2026-03-26T10:41:37.884593Z", "iopub.status.idle": "2026-03-26T10:41:38.314396Z", "shell.execute_reply": "2026-03-26T10:41:38.313827Z" } }, "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 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", "extensions.embed_gif(filename)" ] }, { "cell_type": "markdown", "id": "aa8fd49a", "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": "9b59e030", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:41:38.316395Z", "iopub.status.busy": "2026-03-26T10:41:38.316101Z", "iopub.status.idle": "2026-03-26T10:41:38.507292Z", "shell.execute_reply": "2026-03-26T10:41:38.506698Z" } }, "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" }, { "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": "03abc240", "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": "3ac98db9", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:41:38.514620Z", "iopub.status.busy": "2026-03-26T10:41:38.514252Z", "iopub.status.idle": "2026-03-26T10:41:38.551434Z", "shell.execute_reply": "2026-03-26T10:41:38.550754Z" } }, "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": [ "res = model_checking(sv_model, 'Pmax=? [F \"target\"]')\n", "vis2 = show(sv_model, result=res)" ] }, { "cell_type": "markdown", "id": "10f9aa01", "metadata": {}, "source": [ "Let's highlight the path to see what the scheduler is doing." ] }, { "cell_type": "code", "execution_count": 4, "id": "9915b0e3", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:41:38.559054Z", "iopub.status.busy": "2026-03-26T10:41:38.558853Z", "iopub.status.idle": "2026-03-26T10:41:53.619440Z", "shell.execute_reply": "2026-03-26T10:41:53.618806Z" } }, "outputs": [], "source": [ "path = simulate_path(sv_model, scheduler=res.scheduler, steps=20)\n", "vis2.highlight_path(path, color=\"orange\")" ] }, { "cell_type": "markdown", "id": "b76bff91", "metadata": {}, "source": [ "Alternatively, we can show what our scheduler does in the frozen lake environment itself." ] }, { "cell_type": "code", "execution_count": 5, "id": "26b029dd", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:41:53.621313Z", "iopub.status.busy": "2026-03-26T10:41:53.621139Z", "iopub.status.idle": "2026-03-26T10:41:53.668084Z", "shell.execute_reply": "2026-03-26T10:41:53.667559Z" } }, "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", "extensions.embed_gif(filename)" ] }, { "cell_type": "markdown", "id": "0c4e9089", "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": "85d24210", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:41:53.669943Z", "iopub.status.busy": "2026-03-26T10:41:53.669772Z", "iopub.status.idle": "2026-03-26T10:41:53.974595Z", "shell.execute_reply": "2026-03-26T10:41:53.974083Z" } }, "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", "extensions.embed_gif(filename)" ] }, { "cell_type": "markdown", "id": "3289da87", "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": "85caac0f", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:41:53.976571Z", "iopub.status.busy": "2026-03-26T10:41:53.976348Z", "iopub.status.idle": "2026-03-26T10:41:53.982607Z", "shell.execute_reply": "2026-03-26T10:41:53.982133Z" } }, "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": "9cbae131", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:41:53.984344Z", "iopub.status.busy": "2026-03-26T10:41:53.984170Z", "iopub.status.idle": "2026-03-26T10:41:54.023239Z", "shell.execute_reply": "2026-03-26T10:41:54.022746Z" } }, "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" }, { "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": "b45aa1a5", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:41:54.030759Z", "iopub.status.busy": "2026-03-26T10:41:54.030536Z", "iopub.status.idle": "2026-03-26T10:41:54.224084Z", "shell.execute_reply": "2026-03-26T10:41:54.223514Z" } }, "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", "extensions.embed_gif(filename)" ] }, { "cell_type": "markdown", "id": "b7d9f139", "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": "ec41d165", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:41:54.226005Z", "iopub.status.busy": "2026-03-26T10:41:54.225814Z", "iopub.status.idle": "2026-03-26T10:41:54.427107Z", "shell.execute_reply": "2026-03-26T10:41:54.426458Z" } }, "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-v3\", 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": "b2c99d68", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:41:54.428957Z", "iopub.status.busy": "2026-03-26T10:41:54.428771Z", "iopub.status.idle": "2026-03-26T10:41:55.377419Z", "shell.execute_reply": "2026-03-26T10:41:55.376798Z" } }, "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", "extensions.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.12.13" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": { "509c8532dac74650a4782aeb64178a0b": { "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_845b332b64fa498ca788fa8b870d3091", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "516a5186d7b44781abcb6a520c8aaaaf": { "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_d74381ad29964019b0a92bb26fdfbbc4", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "55356123c13847b98e0c1f9f473cd6b7": { "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 } }, "59d239dfe3ca4bf4979cc2419003a7ca": { "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 } }, "726e5bf9558640cb940148c5dec6ed39": { "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_d1033ccf70594e1aacd20e0f5e3f297d", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "845b332b64fa498ca788fa8b870d3091": { "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 } }, "9275ba784cd5456188721a9dce3d834e": { "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_59d239dfe3ca4bf4979cc2419003a7ca", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "9ea0817a30d74959809a57a00985ed16": { "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 } }, "a59b03a7ae974ca8ba303959b2f962ae": { "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_9ea0817a30d74959809a57a00985ed16", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "d1033ccf70594e1aacd20e0f5e3f297d": { "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 } }, "d74381ad29964019b0a92bb26fdfbbc4": { "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 } }, "daf3421e6a3d4866aa26135c00f29050": { "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_55356123c13847b98e0c1f9f473cd6b7", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } } }, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }