{ "cells": [ { "cell_type": "markdown", "id": "f11e951b", "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": "becaf7e5", "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": "4d6fadae", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:48:30.820388Z", "iopub.status.busy": "2026-03-26T10:48:30.820125Z", "iopub.status.idle": "2026-03-26T10:48:31.214780Z", "shell.execute_reply": "2026-03-26T10:48:31.214215Z" }, "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" } ], "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", "vis_m1 = show(m1)" ] }, { "cell_type": "markdown", "id": "33bc8518", "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": "2f191806", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:48:31.222228Z", "iopub.status.busy": "2026-03-26T10:48:31.221919Z", "iopub.status.idle": "2026-03-26T10:48:31.251764Z", "shell.execute_reply": "2026-03-26T10:48:31.251181Z" } }, "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": [ "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", "vis_m2 = show(m2)" ] }, { "cell_type": "markdown", "id": "9ebfe2fd", "metadata": {}, "source": [ "## Quotient model\n", "Now we construct the quotient model." ] }, { "cell_type": "code", "execution_count": 3, "id": "e4d594a0", "metadata": { "execution": { "iopub.execute_input": "2026-03-26T10:48:31.258914Z", "iopub.status.busy": "2026-03-26T10:48:31.258732Z", "iopub.status.idle": "2026-03-26T10:48:31.291503Z", "shell.execute_reply": "2026-03-26T10:48:31.290906Z" } }, "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": [ "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", "vis_q = 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": { "0218259bd18e48e9a1b5c08fa93b54b2": { "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_85b518a6b7bc4e759838a402659defb4", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "0bd219b68ac44c88bb020f72bfdad70a": { "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_1dcc3a2b3dd04cac8eb095c9ef5ac57a", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "1dcc3a2b3dd04cac8eb095c9ef5ac57a": { "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 } }, "3232893a97074eea9d6945bfff066fca": { "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 } }, "530049f1339049dfb27fa28cae57cda0": { "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 } }, "5cfbf7ce11e9451fab734b439cc54ff6": { "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 } }, "81d46efc1859481baa314ae14dbae388": { "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 } }, "85b518a6b7bc4e759838a402659defb4": { "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 } }, "b44ba8bacd1f475286485b03bec49864": { "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_3232893a97074eea9d6945bfff066fca", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "bd1e768f56c94c94942cf16014840835": { "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_81d46efc1859481baa314ae14dbae388", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "c523ce739be94b4baed2c0cbc6a88b4f": { "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_530049f1339049dfb27fa28cae57cda0", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } }, "f0968c36752a463ca408bb5369ad59ea": { "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_5cfbf7ce11e9451fab734b439cc54ff6", "msg_id": "", "outputs": [], "tabbable": null, "tooltip": null } } }, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }