{ "cells": [ { "cell_type": "markdown", "id": "1f7ff43e", "metadata": {}, "source": [ "# Parallel composition\n", "Using the Bird API, you can define your own parallel composition logic. This works similar to PRISM models. We give an example here. We create two MDP models `m1` and `m2`, and then we create the quotient model `q`. They synchronize on the action `r`." ] }, { "cell_type": "markdown", "id": "8c7d76f2", "metadata": {}, "source": [ "## m1\n", "`m1` is a simple 2x2 grid model where taking `l` `r` `u` and `d` move to the next tile." ] }, { "cell_type": "code", "execution_count": 1, "id": "13d6187e", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:42:14.882037Z", "iopub.status.busy": "2026-03-26T10:42:14.881862Z", "iopub.status.idle": "2026-03-26T10:42:15.337773Z", "shell.execute_reply": "2026-03-26T10:42:15.337177Z" }, "lines_to_next_cell": 2 }, "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": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from stormvogel import *\n", "\n", "N = 2\n", "\n", "ACTION_SEMANTICS = {\"l\": (-1, 0), \"r\": (1, 0), \"u\": (0, -1), \"d\": (0, 1)}\n", "\n", "\n", "def available_actions_m1(s):\n", " res = []\n", " if s[0] > 0:\n", " res.append(\"l\")\n", " if s[0] < N - 1:\n", " res.append(\"r\")\n", " if s[1] > 0:\n", " res.append(\"u\")\n", " if s[1] < N - 1:\n", " res.append(\"d\")\n", " return res\n", "\n", "\n", "def pairwise_plus(t1, t2):\n", " return (t1[0] + t2[0], t1[1] + t2[1])\n", "\n", "\n", "def delta_m1(s, a):\n", " return [(1, pairwise_plus(s, ACTION_SEMANTICS[a]))]\n", "\n", "\n", "def labels_m1(s):\n", " return [str(s)]\n", "\n", "\n", "m1 = bird.build_bird(\n", " init=(0, 0),\n", " available_actions=available_actions_m1,\n", " labels=labels_m1,\n", " delta=delta_m1,\n", ")\n", "show(m1)" ] }, { "cell_type": "markdown", "id": "3bb4556d", "metadata": { "lines_to_next_cell": 2 }, "source": [ "## m2\n", "`m2` is a model that counts the number of `r` up until two, and has a faulty reset button `c` to reset the counter. It only works in 80% of the cases. It only allows `r` if the count is not already 2." ] }, { "cell_type": "code", "execution_count": 2, "id": "5f730a1a", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:42:15.344795Z", "iopub.status.busy": "2026-03-26T10:42:15.344471Z", "iopub.status.idle": "2026-03-26T10:42:15.376309Z", "shell.execute_reply": "2026-03-26T10:42:15.375729Z" } }, "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": [ "def available_actions_m2(s):\n", " if s <= 1:\n", " return [\"r\", \"c\"]\n", " if s == 2:\n", " return [\"c\"]\n", "\n", "\n", "def delta_m2(s, a):\n", " if s <= 1:\n", " if \"r\" in a:\n", " return [(1, s + 1)]\n", " elif s == 0:\n", " return [(1, 0)]\n", " else:\n", " return [(0.8, 0), (0.2, s)]\n", " else:\n", " return [(0.8, 0), (0.2, s)]\n", "\n", "\n", "def labels_m2(s):\n", " return [str(s)]\n", "\n", "\n", "m2 = bird.build_bird(\n", " init=0, available_actions=available_actions_m2, labels=labels_m2, delta=delta_m2\n", ")\n", "show(m2)" ] }, { "cell_type": "markdown", "id": "a7d40be7", "metadata": {}, "source": [ "## Quotient model\n", "Now we construct the quotient model." ] }, { "cell_type": "code", "execution_count": 3, "id": "6d1a351e", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:42:15.383355Z", "iopub.status.busy": "2026-03-26T10:42:15.383152Z", "iopub.status.idle": "2026-03-26T10:42:15.419034Z", "shell.execute_reply": "2026-03-26T10:42:15.418447Z" } }, "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": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "SYNC = [\"r\"]\n", "\n", "\n", "def prob_product(branch1, branch2):\n", " return [(p1 * p2, (r1, r2)) for p1, r1 in branch1 for p2, r2 in branch2]\n", "\n", "\n", "def available_actions(s):\n", " a1 = available_actions_m1(s[0])\n", " a2 = available_actions_m2(s[1])\n", " union = set(a1 + a2)\n", " intersection = set(a1) & set(a2)\n", " return [x for x in union if x in intersection or x not in SYNC]\n", "\n", "\n", "def delta(s, a):\n", " a1 = available_actions_m1(s[0])\n", " a2 = available_actions_m2(s[1])\n", "\n", " if a in a1 and a in a2:\n", " return prob_product(delta_m1(s[0], a), delta_m2(s[1], a))\n", " elif a in a1:\n", " return [(p, (s_, s[1])) for p, s_ in delta_m1(s[0], a)]\n", " elif a in a2:\n", " return [(p, (s[0], s_)) for p, s_ in delta_m2(s[1], a)]\n", " else:\n", " return [(1, s)]\n", "\n", "\n", "def labels(s):\n", " return labels_m1(s[0]) + labels_m2(s[1])\n", "\n", "\n", "q = bird.build_bird(\n", " init=((0, 0), 0), available_actions=available_actions, labels=labels, delta=delta\n", ")\n", "show(q)" ] } ], "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": { "1c032797455c49c39a2a2cfe6eff8885": { "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 } }, "433c1e43a1a8487caf0ab5ac12235353": { "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_f04db17b5aeb4c00b9a4d9a45a91101a", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "51817bb1eb5e4b8cb64163ef6703b5d8": { "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 } }, "53dc656920034764bf4be2e8feb24d7d": { "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 } }, "5711217f5183496fabc78209983d821c": { "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_f4c69477337a407381f0390a4f2bc6d5", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "879cbd56efd347eaa6d96dc184e35b52": { "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_1c032797455c49c39a2a2cfe6eff8885", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "8ecad002053444eeaf67eb3632d76cdd": { "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_51817bb1eb5e4b8cb64163ef6703b5d8", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "9d9263f847804f59ba6d327aa193872e": { "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_9f61831fec3e4ef9bc142d9329ade6bf", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "9f61831fec3e4ef9bc142d9329ade6bf": { "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 } }, "d03af6337ff74bf5b2d18c03388340cc": { "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_53dc656920034764bf4be2e8feb24d7d", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "f04db17b5aeb4c00b9a4d9a45a91101a": { "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 } }, "f4c69477337a407381f0390a4f2bc6d5": { "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 }