Thomas Knoll
1 year ago
10 changed files with 201 additions and 72 deletions
-
46examples/shields/01_pre_shield_extraction.py
-
49examples/shields/02_post_shield_extraction.py
-
33examples/shields/03_optimal_shield_extraction.py
-
21examples/shields/04_pre_shield_export.py
-
22examples/shields/05_post_shield_export.py
-
34examples/shields/06_optimal_shield_export.py
-
59examples/shields/07_pre_shield_simulator.py
-
0examples/shields/08_post_shield_simulator.py
-
0examples/shields/09_optimal_shield_simulator.py
-
9examples/shields/10_optimal_controller.py
@ -0,0 +1,46 @@ |
|||||
|
import stormpy |
||||
|
import stormpy.core |
||||
|
import stormpy.simulator |
||||
|
|
||||
|
|
||||
|
import stormpy.shields |
||||
|
|
||||
|
import stormpy.examples |
||||
|
import stormpy.examples.files |
||||
|
|
||||
|
""" |
||||
|
|
||||
|
Example for the extraction of a Pre Safety Shield |
||||
|
from a model checking result and querying the shield |
||||
|
for allowed choices in a state. |
||||
|
|
||||
|
""" |
||||
|
|
||||
|
def pre_shield_extraction(): |
||||
|
path = stormpy.examples.files.prism_mdp_lava_simple |
||||
|
formula_str = "<ShieldFileName, PreSafety, gamma=0.9> Pmax=? [G !\"AgentIsInLavaAndNotDone\"]" |
||||
|
|
||||
|
program = stormpy.parse_prism_program(path) |
||||
|
formulas = stormpy.parse_properties_for_prism_program(formula_str, program) |
||||
|
|
||||
|
options = stormpy.BuilderOptions([p.raw_formula for p in formulas]) |
||||
|
options.set_build_state_valuations(True) |
||||
|
options.set_build_choice_labels(True) |
||||
|
options.set_build_all_labels() |
||||
|
model = stormpy.build_sparse_model_with_options(program, options) |
||||
|
|
||||
|
initial_state = model.initial_states[0] |
||||
|
assert initial_state == 0 |
||||
|
result = stormpy.model_checking(model, formulas[0], extract_scheduler=True) |
||||
|
assert result.has_scheduler |
||||
|
assert result.has_shield |
||||
|
|
||||
|
shield = result.shield |
||||
|
|
||||
|
for state_id in model.states: |
||||
|
choices = shield.construct().get_choice(state_id) |
||||
|
print(F"Allowed choices in state {state_id}, are {choices.choice_map} ") |
||||
|
|
||||
|
|
||||
|
if __name__ == '__main__': |
||||
|
pre_shield_extraction() |
@ -0,0 +1,49 @@ |
|||||
|
import stormpy |
||||
|
import stormpy.core |
||||
|
import stormpy.simulator |
||||
|
|
||||
|
|
||||
|
import stormpy.shields |
||||
|
|
||||
|
import stormpy.examples |
||||
|
import stormpy.examples.files |
||||
|
|
||||
|
|
||||
|
""" |
||||
|
|
||||
|
Example for the extraction of a Post Safety Shield |
||||
|
from a model checking result and querying the shield |
||||
|
for allowed actions. |
||||
|
|
||||
|
""" |
||||
|
|
||||
|
|
||||
|
def post_shield_extraction(): |
||||
|
path = stormpy.examples.files.prism_mdp_lava_simple |
||||
|
formula_str = "<ShieldFileName, PostSafety, gamma=0.9> Pmax=? [G !\"AgentIsInLavaAndNotDone\"]" |
||||
|
|
||||
|
program = stormpy.parse_prism_program(path) |
||||
|
formulas = stormpy.parse_properties_for_prism_program(formula_str, program) |
||||
|
|
||||
|
options = stormpy.BuilderOptions([p.raw_formula for p in formulas]) |
||||
|
options.set_build_state_valuations(True) |
||||
|
options.set_build_choice_labels(True) |
||||
|
options.set_build_all_labels() |
||||
|
model = stormpy.build_sparse_model_with_options(program, options) |
||||
|
|
||||
|
initial_state = model.initial_states[0] |
||||
|
assert initial_state == 0 |
||||
|
result = stormpy.model_checking(model, formulas[0], extract_scheduler=True) |
||||
|
assert result.has_scheduler |
||||
|
assert result.has_shield |
||||
|
|
||||
|
shield = result.shield |
||||
|
|
||||
|
for state_id in model.states: |
||||
|
choices = shield.construct().get_choice(state_id) |
||||
|
print(F"Allowed choices in state {state_id}, are {choices.choice_map} ") |
||||
|
|
||||
|
|
||||
|
|
||||
|
if __name__ == '__main__': |
||||
|
post_shield_extraction() |
@ -0,0 +1,59 @@ |
|||||
|
import stormpy |
||||
|
import stormpy.core |
||||
|
import stormpy.simulator |
||||
|
|
||||
|
import stormpy.shields |
||||
|
|
||||
|
import stormpy.examples |
||||
|
import stormpy.examples.files |
||||
|
|
||||
|
import random |
||||
|
|
||||
|
""" |
||||
|
Simulating a model with the usage of a pre shield |
||||
|
""" |
||||
|
|
||||
|
def example_pre_shield_simulator(): |
||||
|
path = stormpy.examples.files.prism_mdp_lava_simple |
||||
|
formula_str = "<ShieldFileName, PreSafety, gamma=0.9> Pmax=? [G !\"AgentIsInLavaAndNotDone\"]" |
||||
|
|
||||
|
program = stormpy.parse_prism_program(path) |
||||
|
formulas = stormpy.parse_properties_for_prism_program(formula_str, program) |
||||
|
|
||||
|
options = stormpy.BuilderOptions([p.raw_formula for p in formulas]) |
||||
|
options.set_build_state_valuations(True) |
||||
|
options.set_build_choice_labels(True) |
||||
|
options.set_build_all_labels() |
||||
|
model = stormpy.build_sparse_model_with_options(program, options) |
||||
|
|
||||
|
initial_state = model.initial_states[0] |
||||
|
assert initial_state == 0 |
||||
|
result = stormpy.model_checking(model, formulas[0], extract_scheduler=True) |
||||
|
assert result.has_scheduler |
||||
|
assert result.has_shield |
||||
|
|
||||
|
shield = result.shield |
||||
|
|
||||
|
pre_scheduler = shield.construct() |
||||
|
|
||||
|
simulator = stormpy.simulator.create_simulator(model, seed=42) |
||||
|
final_outcomes = dict() |
||||
|
for n in range(1000): |
||||
|
while not simulator.is_done(): |
||||
|
current_state = simulator.get_current_state() |
||||
|
choices = pre_scheduler.get_choice(current_state).choice_map |
||||
|
index = random.randint(0, len(choices) - 1) |
||||
|
selected_action = choices[index] |
||||
|
state_string = model.state_valuations.get_string(current_state) |
||||
|
print(F"Simulator is in state {state_string}. Allowed Choices are {choices}. Selected Action: {selected_action}") |
||||
|
observation, reward = simulator.step(selected_action[1]) |
||||
|
if observation not in final_outcomes: |
||||
|
final_outcomes[observation] = 1 |
||||
|
else: |
||||
|
final_outcomes[observation] += 1 |
||||
|
simulator.restart() |
||||
|
|
||||
|
|
||||
|
|
||||
|
if __name__ == '__main__': |
||||
|
example_pre_shield_simulator() |
@ -0,0 +1,9 @@ |
|||||
|
import stormpy |
||||
|
import stormpy.core |
||||
|
import stormpy.simulator |
||||
|
|
||||
|
|
||||
|
import stormpy.shields |
||||
|
|
||||
|
import stormpy.examples |
||||
|
import stormpy.examples.files |
Write
Preview
Loading…
Cancel
Save
Reference in new issue