{ "cells": [ { "cell_type": "markdown", "id": "c200034d-b5f7-4af8-89fe-589d508d65d5", "metadata": { "tags": [] }, "source": [ "# Example 1" ] }, { "cell_type": "code", "execution_count": 1, "id": "e4582473-0c39-466a-9312-3f8851d561f2", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/opt/conda/lib/python3.10/site-packages/pytct/name_converter.py:70: UserWarning: controllable and uncontrollable status of event is not specified. All events are set to be controllable.\n", " warnings.warn(\"controllable and uncontrollable status of event is not specified. All events are set to be controllable.\")\n" ] } ], "source": [ "import pytct\n", "\n", "pytct.init(\"Chp1\", overwrite=True)\n", "\n", "statenum=3 #number of states\n", "#states are sequentially labeled 0,1,...,statenum-1\n", "#initial state is labeled 0\n", "\n", "trans=[(0,'start',1),\n", " (1,'auto_finish',0),\n", " (1,'manual_stop',0),\n", " (1,'breakdown',2),\n", " (2,'fix',0)] # set of transitions\n", "#each triple is (exit state, event label, entering state)\n", "\n", "marker = [0,1] #set of marker states\n", "\n", "pytct.create('PRINTER', statenum, trans, marker)\n", "#create automaton A" ] }, { "cell_type": "code", "execution_count": 2, "id": "27b27515-c590-45b8-a880-4f84bd66987e", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "finite_state_machine\n", "\n", "\n", "\n", "_a\n", "\n", "\n", "\n", "\n", "0\n", "\n", "\n", "0\n", "\n", "\n", "\n", "_a->0\n", "\n", "\n", "\n", "\n", "\n", "1\n", "\n", "\n", "1\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "start\n", "\n", "\n", "\n", "1->0\n", "\n", "\n", "auto_finish\n", "\n", "\n", "\n", "1->0\n", "\n", "\n", "manual_stop\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "1->2\n", "\n", "\n", "breakdown\n", "\n", "\n", "\n", "2->0\n", "\n", "\n", "fix\n", "\n", "\n", "\n" ], "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pytct.display_automaton('PRINTER')\n", "#plot PRINTER automaton\n", "#pytct.AutomatonDisplay('PRINTER',convert=True, color=True)" ] }, { "cell_type": "code", "execution_count": 3, "id": "9c39e080-d563-421e-acaf-8c897c279266", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pytct.statenum('PRINTER')" ] }, { "cell_type": "code", "execution_count": 4, "id": "ee5cb9c5-80da-4ec7-9e8e-63c1cdeb9c69", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['manual_stop', 'fix', 'start', 'breakdown', 'auto_finish']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pytct.events('PRINTER')" ] }, { "cell_type": "code", "execution_count": 5, "id": "a76e7070-3304-4232-903f-cefaf65a2366", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(0, 'start', 1),\n", " (1, 'auto_finish', 0),\n", " (1, 'manual_stop', 0),\n", " (1, 'breakdown', 2),\n", " (2, 'fix', 0)]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pytct.trans('PRINTER')" ] }, { "cell_type": "code", "execution_count": 6, "id": "2b598a88-aef2-427f-a6cf-536e8ebd7c21", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pytct.marker('PRINTER')" ] }, { "cell_type": "code", "execution_count": 7, "id": "bbcf2673-49b0-494e-8a0c-f5d7405f0653", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 0]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pytct.simulate_automaton('PRINTER',['start','breakdown','fix'])" ] }, { "cell_type": "code", "execution_count": 8, "id": "d375bac4-f18a-4dce-b76e-822fce1e37b8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 0, 1]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pytct.sample_automaton('PRINTER',3)\n", "# generate a state trajectory of sampling length k " ] }, { "cell_type": "code", "execution_count": 9, "id": "bad75eca-d971-4157-af13-9bed3aaf0b93", "metadata": {}, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'matplotlib'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn [9], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# plot sample (please run datascience environment)\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;66;03m# If minimal environment: File -> HubControlPanel -> Stop My Server -> wait... -> Start My Server\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpyplot\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mplt\u001b[39;00m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnumpy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;66;03m# create x, y data\u001b[39;00m\n", "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'matplotlib'" ] } ], "source": [ "# plot sample (please run datascience environment)\n", "# If minimal environment: File -> HubControlPanel -> Stop My Server -> wait... -> Start My Server\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "# create x, y data\n", "y = [0] + pytct.sample_automaton('PRINTER', 10, strict=False)\n", "# strict = False: ignore error if length of sampled string is shorter than specified due to blocking\n", "# print(y)\n", "x = np.arange(len(y))\n", "# print(x)\n", "\n", "# calculate max\n", "# max_x_y = max(max(y), max(x))\n", "# print(max_x_y)\n", "\n", "# plot\n", "fig, ax = plt.subplots()\n", "ax.step(x, y, linewidth=2.5)\n", "ax.set(xlim=(0, max(x)), xticks=np.arange(0, max(x),10),\n", " ylim=(0, max(y)), yticks=np.arange(0, max(y)+1))\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "4dbab645-5803-4c86-809f-dc8b93f065e1", "metadata": {}, "source": [ "Robot Navigation" ] }, { "cell_type": "code", "execution_count": 10, "id": "24b2273b-3a07-46fb-ab50-eb5aa6e07def", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/opt/conda/lib/python3.10/site-packages/pytct/name_converter.py:70: UserWarning: controllable and uncontrollable status of event is not specified. All events are set to be controllable.\n", " warnings.warn(\"controllable and uncontrollable status of event is not specified. All events are set to be controllable.\")\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "finite_state_machine\n", "\n", "\n", "\n", "_a\n", "\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "_a->0\n", "\n", "\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "up\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "right\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "1->3\n", "\n", "\n", "right\n", "\n", "\n", "\n", "2->3\n", "\n", "\n", "up\n", "\n", "\n", "\n", "4\n", "\n", "\n", "4\n", "\n", "\n", "\n", "2->4\n", "\n", "\n", "right\n", "\n", "\n", "\n", "3->2\n", "\n", "\n", "down\n", "\n", "\n", "\n", "5\n", "\n", "5\n", "\n", "\n", "\n", "3->5\n", "\n", "\n", "right\n", "\n", "\n", "\n", "5->4\n", "\n", "\n", "down\n", "\n", "\n", "\n" ], "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "statenum=6 #number of states\n", "#states are sequentially labeled 0,1,...,statenum\n", "#initial state is labeled 0\n", "\n", "trans=[(0,'up',1),\n", " (0,'right',2),\n", " (1,'right',3),\n", " (2,'up',3),\n", " (3,'down',2),\n", " (2,'right',4),\n", " (3,'right',5),\n", " (5,'down',4)] # set of transitions\n", "#each triple is (exit state, event label, entering state)\n", "\n", "marker = [4] #set of marker states\n", "\n", "pytct.create('ROBOT', statenum, trans, marker)\n", "\n", "pytct.display_automaton('ROBOT')" ] }, { "cell_type": "code", "execution_count": 11, "id": "e144936c-b5bf-4976-a3c9-8f17885ca310", "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAhYAAAGiCAYAAAC79I8tAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/bCgiHAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAPlUlEQVR4nO3dX4hVhb7A8d845rZjs+emocxcZ0wI0hSDVML+iP0ThhB96yHE230yzJJ5iUkO0j8meonA46DRrfsSSpDVQ0m+mHVCUEsStSCI43AbkxJnqxd2Oe77cGjI45/rGn97xq2fD6yHtVzL9aPtn29rre1qqtVqtQAASDBurAcAAK4fwgIASCMsAIA0wgIASCMsAIA0wgIASCMsAIA0wgIASCMsAIA0wgIASHNVYdHb2xtNTU2xbt26pHEAgEY24rDYu3dvbNmyJebNm5c5DwDQwEYUFqdPn44nn3wy3nrrrbj11luzZwIAGtT4kRy0Zs2aePzxx+PRRx+NV1555bL7VqvVqFarw+vnzp2LEydOxJQpU6KpqWkkpwcARlmtVotTp05Fe3t7jBt36esShcNi69at8fXXX8fevXuvaP/e3t548cUXi54GALgG9ff3x/Tp0y/544XCor+/P5577rn47LPPYuLEiVd0TE9PT3R3dw+vDw4ORmdnZ/T390e5XC5yegBuEPv/cSJW/deV/Q8so+Nc9X/jf/r+I1paWi67X6Gw2L9/fxw/fjzmz58/vG1oaCh2794dGzdujGq1Gs3NzecdUyqVolQqXfBzlctlYQHARd3ScjbGlf4yvP7y8jkxq83fGWPp9KlKPNwX/+9jDIXC4pFHHomDBw+et+2pp56KWbNmxfPPP39BVABAhllt5Vh4++SxHuOGVqlcWTIUCouWlpaYO3fuedsmTZoUU6ZMuWA7AHDj8S9vAgBpRvR10z/btWtXwhgAwPXAFQsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAII2wAADSCAsAIE2hsOjr64t58+ZFuVyOcrkcixYtik8//bReswEADaZQWEyfPj1ee+212LdvX+zbty8efvjhWL58eRw6dKhe8wEADWR8kZ2XLVt23vqrr74afX19sWfPnpgzZ07qYHAj+X3oXBwZqET17LmxHoWIKI0fF7PbynFTs7vFUFShsPizoaGheP/99+PMmTOxaNGiS+5XrVajWq0Or1cqlZGeEq5Lvw+dixV/+3sc+snvjWvJnPZyfLjmfnEBBRX+HXPw4MG45ZZbolQqxerVq2P79u1x1113XXL/3t7eaG1tHV46OjquamC43hwZqIiKa9ChnypxZMDnAkUVDos777wzDhw4EHv27Imnn346Vq1aFYcPH77k/j09PTE4ODi89Pf3X9XAcL1x++Pa5bOB4grfCpkwYULccccdERGxYMGC2Lt3b7z55puxefP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Kma3leOmZlcsGsWIwmLt2rXx8ccfx+7du2P69OnZMwGMmZuax8W86f821mNAwyoUFrVaLdauXRvbt2+PXbt2xcyZM+s1FwDQgAqFxZo1a+K9996Ljz76KFpaWuLYsWMREdHa2ho333xzXQYEABpHoZtWfX19MTg4GEuWLIm2trbhZdu2bfWaDwBoIIVvhQAAXIrHbAGANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANMICAEgjLACANIXDYvfu3bFs2bJob2+Ppqam+PDDD+swFgDQiAqHxZkzZ+Luu++OjRs31mMeAKCBjS96QFdXV3R1ddVjFkbZ70Pn4shAJapnz431KDe07wYqYz0CQJrCYVFUtVqNarU6vF6p+EP0WvD70LlY8be/x6GffB4A5Kn7w5u9vb3R2to6vHR0dNT7lFyBIwMVUXGNKo33TDXQuOr+J1hPT08MDg4OL/39/fU+JVfA7Y9r05z2csxuK4/1GAAjVvdbIaVSKUqlUr1Pw1V6efmcmOUvtDFVGj8uZreV46ZmVyyAxlX3sKAxzGorx8LbJ4/1GAA0uMJhcfr06fjhhx+G13/88cc4cOBATJ48OTo7O1OHAwAaS+Gw2LdvXzz00EPD693d3RERsWrVqnj33XfTBgMAGk/hsFiyZEnUarV6zAIANDhPiQEAaYQFAJBGWAAAaYQFAJBGWAAAaYQFAJBGWAAAaYQFAJBGWAAAaYQFAJBGWAAAaYQFAJBGWAAAaYQFA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PR1dUVR48eHa0RAIA6G7WvmxZ93ToA0HhG5YrFb7/9Fvv374+lS5eet33p0qXx1VdfjcYIAMAoGJWwGMnr1gGAxjOqD28Wed06ANB4RiUsvG4dAG4MoxIWXrcOADeGwq9NH6mrfd06AHDtG7WweOKJJ+LXX3+Nl156afh165988knMmDFjtEYAAOrMa9MBgDTeFQIApBEWAEAaYQEApBEWAEAaYQEApBEWAEAaYQEApBEWAEAaYQEApBEWAEAaYQEApPk/Pg8Ms9SLHnsAAAAASUVORK5CYII=", "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# plot sample (please run datascience environment)\n", "# If minimal environment: File -> HubControlPanel -> Stop My Server -> wait... -> Start My Server\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "# create x, y data\n", "y = [0] + pytct.sample_automaton('ROBOT', 10, strict=False)\n", "# strict = False: ignore error if length of sampled string is shorter than specified due to blocking\n", "# print(y)\n", "x = np.arange(len(y))\n", "# print(x)\n", "\n", "# calculate max\n", "# max_x_y = max(max(y), max(x))\n", "# print(max_x_y)\n", "\n", "# plot\n", "fig, ax = plt.subplots()\n", "ax.step(x, y, linewidth=2.5)\n", "ax.set(xlim=(0, max(x)), xticks=np.arange(0, max(x),10),\n", " ylim=(0, max(y)), yticks=np.arange(0, max(y)+1))\n", "\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "d8685bad-0f2d-4931-b30b-754a20afb5de", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "bc2230f8-f836-440c-944e-747a5d43c6e6", "metadata": {}, "outputs": [], "source": [] } ], "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.11.6" } }, "nbformat": 4, "nbformat_minor": 5 }