Skip to content

Commit

Permalink
the simulator test now works again
Browse files Browse the repository at this point in the history
  • Loading branch information
PimLeerkes committed Feb 4, 2025
1 parent cfc2e6c commit 8574594
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 68 deletions.
8 changes: 7 additions & 1 deletion stormvogel/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ class Action:
Two actions with the same labels are considered equal.
Args:
name: A name for this action.
labels: The labels of this action. Corresponds to Storm labels.
"""

Expand Down Expand Up @@ -730,6 +729,13 @@ def get_branch(self, state_or_id: State | int) -> Branch:
raise RuntimeError("Called get_branch on a non-empty transition.")
return transition[EmptyAction]

def get_action_with_labels(self, labels: frozenset[str]) -> Action | None:
"""Get the action with provided list of labels"""
assert self.actions is not None
for action in self.actions:
if action.labels == labels:
return action

def new_action(self, labels: frozenset[str] | str | None = None) -> Action:
"""Creates a new action and returns it."""
if not self.supports_actions():
Expand Down
2 changes: 1 addition & 1 deletion stormvogel/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __str__(self) -> str:
and isinstance(t[0], stormvogel.model.Action)
and isinstance(t[1], stormvogel.model.State)
)
path += f" --(action: {t[0].name})--> state: {t[1].id}"
path += f" --(action: {t[0].labels})--> state: {t[1].id}"
else:
for state in self.path.values():
path += f" --> state: {state.id}"
Expand Down
156 changes: 90 additions & 66 deletions tests/test_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,70 +104,94 @@ def scheduler(state: stormvogel.model.State) -> stormvogel.model.Action:

assert partial_model == other_mdp


# TODO Pim could you also finish this test? :)
# def test_simulate_path():
# # we make the nuclear fusion ctmc and run simulate path with it
# ctmc = examples.nuclear_fusion_ctmc.create_nuclear_fusion_ctmc()
# path = stormvogel.simulator.simulate_path(ctmc, steps=5, seed=1)

# # we make the path that the simulate path function should create
# other_path = stormvogel.simulator.Path(
# {
# 1: ctmc.get_state_by_id(1),
# 2: ctmc.get_state_by_id(2),
# 3: ctmc.get_state_by_id(3),
# 4: ctmc.get_state_by_id(4),
# },
# ctmc,
# )

# assert path == other_path
# ##############################################################################################
# # we make the monty hall pomdp and run simulate path with it
# pomdp = examples.monty_hall_pomdp.create_monty_hall_pomdp()
# taken_actions = {}
# for id, state in pomdp.states.items():
# taken_actions[id] = state.available_actions()[
# len(state.available_actions()) - 1
# ]
# scheduler = stormvogel.result.Scheduler(pomdp, taken_actions)
# path = stormvogel.simulator.simulate_path(
# pomdp, steps=4, seed=1, scheduler=scheduler
# )

# # we make the path that the simulate path function should create
# other_path = stormvogel.simulator.Path(
# {
# 1: (stormvogel.model.EmptyAction, pomdp.get_state_by_id(3)),
# 2: (pomdp.actions["open0"], pomdp.get_state_by_id(10)),
# 3: (stormvogel.model.EmptyAction, pomdp.get_state_by_id(21)),
# 4: (pomdp.actions["stay"], pomdp.get_state_by_id(41)),
# },
# pomdp,
# )

# assert path == other_path

# ##############################################################################################
# # we test the monty hall pomdp with a lambda as scheduler
# def scheduler(state: stormvogel.model.State) -> stormvogel.model.Action:
# actions = state.available_actions()
# return actions[0]

# pomdp = examples.monty_hall_pomdp.create_monty_hall_pomdp()
# path = stormvogel.simulator.simulate_path(
# pomdp, steps=4, seed=1, scheduler=scheduler
# )

# # we make the path that the simulate path function should create
# other_path = stormvogel.simulator.Path(
# {
# 1: (stormvogel.model.EmptyAction, pomdp.get_state_by_id(3)),
# 2: (pomdp.actions["open0"], pomdp.get_state_by_id(10)),
# 3: (stormvogel.model.EmptyAction, pomdp.get_state_by_id(21)),
# 4: (pomdp.actions["stay"], pomdp.get_state_by_id(41)),
# },
# pomdp,
# )

# assert path == other_path
def test_simulate_path():
# we make the nuclear fusion ctmc and run simulate path with it
ctmc = examples.nuclear_fusion_ctmc.create_nuclear_fusion_ctmc()
path = stormvogel.simulator.simulate_path(ctmc, steps=5, seed=1)

# we make the path that the simulate path function should create
other_path = stormvogel.simulator.Path(
{
1: ctmc.get_state_by_id(1),
2: ctmc.get_state_by_id(2),
3: ctmc.get_state_by_id(3),
4: ctmc.get_state_by_id(4),
},
ctmc,
)

assert path == other_path
##############################################################################################
# we make the monty hall pomdp and run simulate path with it
pomdp = examples.monty_hall_pomdp.create_monty_hall_pomdp()
taken_actions = {}
for id, state in pomdp.states.items():
taken_actions[id] = state.available_actions()[
len(state.available_actions()) - 1
]
scheduler = stormvogel.result.Scheduler(pomdp, taken_actions)
path = stormvogel.simulator.simulate_path(
pomdp, steps=4, seed=1, scheduler=scheduler
)

# we make the path that the simulate path function should create

action0 = pomdp.get_action_with_labels(frozenset({"open2"}))
assert action0 is not None
action1 = pomdp.get_action_with_labels(frozenset({"switch"}))
assert action1 is not None

other_path = stormvogel.simulator.Path(
{
1: (stormvogel.model.EmptyAction, pomdp.get_state_by_id(3)),
2: (
action0,
pomdp.get_state_by_id(12),
),
3: (stormvogel.model.EmptyAction, pomdp.get_state_by_id(23)),
4: (
action1,
pomdp.get_state_by_id(46),
),
},
pomdp,
)

assert path == other_path

##############################################################################################
# we test the monty hall pomdp with a lambda as scheduler
def scheduler(state: stormvogel.model.State) -> stormvogel.model.Action:
actions = state.available_actions()
return actions[0]

pomdp = examples.monty_hall_pomdp.create_monty_hall_pomdp()
path = stormvogel.simulator.simulate_path(
pomdp, steps=4, seed=1, scheduler=scheduler
)

action0 = pomdp.get_action_with_labels(frozenset({"open0"}))
assert action0 is not None
action1 = pomdp.get_action_with_labels(frozenset({"stay"}))
assert action1 is not None

# we make the path that the simulate path function should create
other_path = stormvogel.simulator.Path(
{
1: (stormvogel.model.EmptyAction, pomdp.get_state_by_id(3)),
2: (
action0,
pomdp.get_state_by_id(10),
),
3: (stormvogel.model.EmptyAction, pomdp.get_state_by_id(21)),
4: (
action1,
pomdp.get_state_by_id(41),
),
},
pomdp,
)

assert path == other_path

0 comments on commit 8574594

Please sign in to comment.