From 62f4d4225550522479b82dc570737ec3f789df67 Mon Sep 17 00:00:00 2001 From: PimLeerkes Date: Sat, 26 Oct 2024 13:10:09 +0200 Subject: [PATCH] added notebook for the simulator --- docs/getting_started/die.ipynb | 2 +- docs/getting_started/mdp.ipynb | 10 +-- docs/getting_started/simulator.ipynb | 99 ++++++++++++++++++++++++++++ docs/getting_started/study.ipynb | 2 +- pyproject.toml | 5 ++ stormvogel/simulator.py | 20 +++++- 6 files changed, 128 insertions(+), 10 deletions(-) create mode 100644 docs/getting_started/simulator.ipynb diff --git a/docs/getting_started/die.ipynb b/docs/getting_started/die.ipynb index 7ba0593..0844265 100644 --- a/docs/getting_started/die.ipynb +++ b/docs/getting_started/die.ipynb @@ -137,7 +137,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.2" + "version": "3.12.7" } }, "nbformat": 4, diff --git a/docs/getting_started/mdp.ipynb b/docs/getting_started/mdp.ipynb index 1532656..a999f02 100644 --- a/docs/getting_started/mdp.ipynb +++ b/docs/getting_started/mdp.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 14, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -12,16 +12,16 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 15, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -364,7 +364,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.4" + "version": "3.12.7" } }, "nbformat": 4, diff --git a/docs/getting_started/simulator.ipynb b/docs/getting_started/simulator.ipynb new file mode 100644 index 0000000..0a4df45 --- /dev/null +++ b/docs/getting_started/simulator.ipynb @@ -0,0 +1,99 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "a8ddc37c-66d2-43e4-8162-6be19a1d70a1", + "metadata": {}, + "outputs": [], + "source": [ + "import stormvogel.simulator\n", + "import stormvogel.model\n", + "import examples.monty_hall" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "eb0fadc0-7bb6-4c1d-ae3e-9e16527726ab", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ModelType.MDP with name None\n", + "\n", + "States:\n", + "State 0 with labels ['init'] and features {}\n", + "State 1 with labels ['carchosen'] and features {}\n", + "State 2 with labels ['open'] and features {}\n", + "State 3 with labels ['goatrevealed'] and features {}\n", + "State 4 with labels ['done'] and features {}\n", + "\n", + "Transitions:\n" + ] + } + ], + "source": [ + "#we take for example the monty hall mdp model:\n", + "mdp = examples.monty_hall.create_monty_hall_mdp()\n", + "\n", + "#we cam then simulate the model with the simulator to create a partial model:\n", + "partial_model = stormvogel.simulator.simulate(mdp, runs=1, steps=4, seed=123456)\n", + "\n", + "print(partial_model)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "34d0c293-d090-4e3d-9e80-4351f5fcba62", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "initial state --(action: empty)--> state: 2 --(action: open2)--> state: 9 --(action: empty)--> state: 20 --(action: stay)--> state: 39\n" + ] + } + ], + "source": [ + "#we can also simulate a path:\n", + "path = stormvogel.simulator.simulate_path(mdp, steps=4, seed=123456)\n", + "\n", + "print(path)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6b03bb18-4c65-4544-9f2b-fd58682a829d", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python (stormvogel)", + "language": "python", + "name": "stormvogel-env" + }, + "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.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/getting_started/study.ipynb b/docs/getting_started/study.ipynb index 80e4a55..32100ee 100644 --- a/docs/getting_started/study.ipynb +++ b/docs/getting_started/study.ipynb @@ -216,7 +216,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.2" + "version": "3.12.7" } }, "nbformat": 4, diff --git a/pyproject.toml b/pyproject.toml index a84074c..a3241c1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,6 +10,11 @@ authors = ["The stormvogel team"] license = "GPLv3" readme = "README.md" +packages = [ + { include = "stormvogel" }, + { include = "examples" } +] + [tool.poetry.dependencies] python = "^3.11" ipywidgets = "^8.1.3" diff --git a/stormvogel/simulator.py b/stormvogel/simulator.py index b366a81..2dab3f6 100644 --- a/stormvogel/simulator.py +++ b/stormvogel/simulator.py @@ -108,7 +108,14 @@ def simulate_path( seed: int | None = None, ) -> Path: """ - Simulates the model a given number of steps and returns the path created by the process. + Simulates the model and returns the path created by the process. + Args: + model: The stormvogel model that the simulator should run on. + steps: The number of steps the simulator walks through the model. + scheduler: A stormvogel scheduler to determine what actions should be taken. Random if not provided. + seed: The seed for the function that determines for each state what the next state will be. Random seed if not provided. + + Returns a path object. """ def get_range_index(stateid: int): @@ -172,8 +179,15 @@ def simulate( seed: int | None = None, ) -> stormvogel.model.Model | None: """ - Simulates the model a given number of steps for a given number of runs. - Returns the partial model discovered by the simulator + Simulates the model. + Args: + model: The stormvogel model that the simulator should run on + steps: The number of steps the simulator walks through the model + runs: The number of times the model gets simulated. + scheduler: A stormvogel scheduler to determine what actions should be taken. Random if not provided. + seed: The seed for the function that determines for each state what the next state will be. Random seed if not provided. + + Returns the partial model discovered by all the runs of the simulator together """ def get_range_index(stateid: int):