Skip to content

Commit

Permalink
Get API working
Browse files Browse the repository at this point in the history
  • Loading branch information
glatteis committed Jun 25, 2024
1 parent f081fa2 commit 3902ad8
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 74 deletions.
36 changes: 0 additions & 36 deletions examples/create_model.py

This file was deleted.

12 changes: 12 additions & 0 deletions examples/die.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import stormvogel.model

dtmc = stormvogel.model.new_dtmc("Die")

init = dtmc.get_initial_state()

# roll die
init.set_transitions(
[(1 / 6, dtmc.new_state(f"rolled{i}", {"rolled": i})) for i in range(6)]
)

print(dtmc.to_dot())
64 changes: 64 additions & 0 deletions examples/monty_hall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import stormvogel.model

mdp = stormvogel.model.new_mdp("Monty Hall")

init = mdp.get_initial_state()

# first choose car position
init.set_transitions(
[(1 / 3, mdp.new_state("carchosen", {"car_pos": i})) for i in range(3)]
)

# we choose a door in each case
for s in mdp.get_states_with("carchosen"):
s.set_transitions(
[
(
mdp.action(f"open{i}"),
mdp.new_state("open", s.features | {"chosen_pos": i}),
)
for i in range(3)
]
)

# the other goat is revealed
for s in mdp.get_states_with("open"):
car_pos = s.features["car_pos"]
chosen_pos = s.features["chosen_pos"]
other_pos = {0, 1, 2} - {car_pos, chosen_pos}
s.set_transitions(
[
(
1 / len(other_pos),
mdp.new_state("goatrevealed", s.features | {"reveal_pos": i}),
)
for i in other_pos
]
)

# we must choose whether we want to switch
for s in mdp.get_states_with("goatrevealed"):
car_pos = s.features["car_pos"]
chosen_pos = s.features["chosen_pos"]
reveal_pos = s.features["reveal_pos"]
other_pos = list({0, 1, 2} - {reveal_pos, chosen_pos})[0]
s.set_transitions(
[
(
mdp.action("stay"),
mdp.new_state(
["done"] + (["target"] if chosen_pos == car_pos else []),
s.features | {"chosen_pos": chosen_pos},
),
),
(
mdp.action("switch"),
mdp.new_state(
["done"] + (["target"] if other_pos == car_pos else []),
s.features | {"chosen_pos": other_pos},
),
),
]
)

print(mdp.to_dot())
1 change: 1 addition & 0 deletions stormvogel/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""The stormvogel package"""
Loading

0 comments on commit 3902ad8

Please sign in to comment.