9 changed files with 191 additions and 7 deletions
-
45examples/shields/01_pre_shield.py
-
43examples/shields/02_post_shield.py
-
5lib/stormpy/examples/files.py
-
48lib/stormpy/examples/files/mdp/simple.prism
-
30lib/stormpy/shields/__init__.py
-
4src/mod_shields.cpp
-
1src/shields/optimal_shield.cpp
-
1src/shields/post_shield.cpp
-
17src/storage/scheduler.cpp
@ -0,0 +1,45 @@ |
|||||
|
import stormpy |
||||
|
import stormpy.core |
||||
|
import stormpy.simulator |
||||
|
|
||||
|
|
||||
|
import stormpy.shields |
||||
|
|
||||
|
import stormpy.examples |
||||
|
import stormpy.examples.files |
||||
|
import random |
||||
|
|
||||
|
|
||||
|
def pre_schield_01(): |
||||
|
path = stormpy.examples.files.prism_mdp_lava_simple |
||||
|
formula_str = "<ShieldFileName, PreSafety, gamma=0.9> Pmax=? [G !\"AgentIsInLavaAndNotDone\"]" |
||||
|
|
||||
|
|
||||
|
#--buildstateval --buildchoicelab |
||||
|
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_schield |
||||
|
|
||||
|
shield = result.shield |
||||
|
|
||||
|
lookup = stormpy.shields.create_shield_action_lookup(model, shield) |
||||
|
query = list(lookup.keys())[0] |
||||
|
|
||||
|
print(query) |
||||
|
print(lookup[query]) |
||||
|
print(lookup) |
||||
|
|
||||
|
|
||||
|
if __name__ == '__main__': |
||||
|
pre_schield_01() |
@ -0,0 +1,43 @@ |
|||||
|
import stormpy |
||||
|
import stormpy.core |
||||
|
import stormpy.simulator |
||||
|
|
||||
|
|
||||
|
import stormpy.shields |
||||
|
|
||||
|
import stormpy.examples |
||||
|
import stormpy.examples.files |
||||
|
import random |
||||
|
|
||||
|
|
||||
|
def post_shield_02(): |
||||
|
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_schield |
||||
|
|
||||
|
shield = result.shield |
||||
|
|
||||
|
lookup = stormpy.shields.create_shield_action_lookup(model, shield) |
||||
|
query = list(lookup.keys())[0] |
||||
|
|
||||
|
print(query) |
||||
|
print(lookup[query]) |
||||
|
|
||||
|
|
||||
|
|
||||
|
if __name__ == '__main__': |
||||
|
post_shield_02() |
@ -0,0 +1,48 @@ |
|||||
|
mdp |
||||
|
|
||||
|
formula AgentCannotMoveNorth = (xAgent=2&yAgent=1) | (xAgent=3&yAgent=1) | (xAgent=4&yAgent=1) | (xAgent=5&yAgent=1) | (xAgent=6&yAgent=1) | (xAgent=1&yAgent=1); |
||||
|
formula AgentCannotMoveEast = (xAgent=6&yAgent=1) | (xAgent=6&yAgent=2) | (xAgent=6&yAgent=3) | (xAgent=6&yAgent=4) | (xAgent=6&yAgent=5); |
||||
|
formula AgentCannotMoveSouth = (xAgent=1&yAgent=5) | (xAgent=2&yAgent=5) | (xAgent=3&yAgent=5) | (xAgent=4&yAgent=5) | (xAgent=5&yAgent=5) | (xAgent=6&yAgent=5); |
||||
|
formula AgentCannotMoveWest = (xAgent=1&yAgent=2) | (xAgent=1&yAgent=3) | (xAgent=1&yAgent=4) | (xAgent=1&yAgent=5) | (xAgent=1&yAgent=1); |
||||
|
formula AgentIsOnSlippery = false; |
||||
|
formula AgentIsInLava = (xAgent=2&yAgent=2) | (xAgent=3&yAgent=2) | (xAgent=2&yAgent=3) | (xAgent=3&yAgent=3) | (xAgent=2&yAgent=4) | (xAgent=3&yAgent=4); |
||||
|
formula AgentIsInLavaAndNotDone = AgentIsInLava & !AgentDone; |
||||
|
label "AgentIsInLavaAndNotDone" = AgentIsInLava & !AgentDone; |
||||
|
|
||||
|
formula AgentIsInGoal = (xAgent=6&yAgent=5); |
||||
|
formula AgentIsInGoalAndNotDone = AgentIsInGoal & !AgentDone; |
||||
|
label "AgentIsInGoalAndNotDone" = AgentIsInGoal & !AgentDone; |
||||
|
module Agent |
||||
|
xAgent : [1..6] init 1; |
||||
|
yAgent : [1..7] init 1; |
||||
|
|
||||
|
AgentDone : bool init false; |
||||
|
viewAgent : [0..3] init 0; |
||||
|
|
||||
|
[Agent_turn_right] !AgentIsInGoal & !AgentIsInLava & !AgentIsOnSlippery -> (viewAgent'=mod(viewAgent + 1, 4)) ; |
||||
|
[Agent_turn_left] !AgentIsInGoal & !AgentIsInLava & !AgentIsOnSlippery & viewAgent>0 -> (viewAgent'=viewAgent - 1) ; |
||||
|
[Agent_turn_left] !AgentIsInGoal & !AgentIsInLava & !AgentIsOnSlippery & viewAgent=0 -> (viewAgent'=3) ; |
||||
|
|
||||
|
|
||||
|
[Agent_move_north] viewAgent=3 & !AgentIsOnSlippery & !AgentIsInLava &!AgentIsInGoal & !AgentCannotMoveNorth -> (yAgent'=yAgent-1); |
||||
|
[Agent_move_east] viewAgent=0 & !AgentIsOnSlippery & !AgentIsInLava &!AgentIsInGoal & !AgentCannotMoveEast -> (xAgent'=xAgent+1); |
||||
|
[Agent_move_south] viewAgent=1 & !AgentIsOnSlippery & !AgentIsInLava &!AgentIsInGoal & !AgentCannotMoveSouth -> (yAgent'=yAgent+1); |
||||
|
[Agent_move_west] viewAgent=2 & !AgentIsOnSlippery & !AgentIsInLava &!AgentIsInGoal & !AgentCannotMoveWest -> (xAgent'=xAgent-1); |
||||
|
[Agent_done] AgentIsInGoal | AgentIsInLava -> (AgentDone'=true); |
||||
|
|
||||
|
endmodule |
||||
|
|
||||
|
// <resultingFile, PreSafety, gamma=0.9> Pmax=? [G !'AgentIsInLavaAndNotDone']; |
||||
|
// Model handling soll auch das Shield zurückgeben |
||||
|
// Im SparseMdpPrctlModelChecker wird das shield mit createShield erstellt |
||||
|
// es soll irgendwie im CheckResult landen welches im model-handling.h verifyWithSparseEngine (Methode) gehandelt wird. |
||||
|
|
||||
|
// Result für den MDP Typen ist im SparseMdpPrctlHelper.computeUntilProbabilities |
||||
|
// MDPSparseModelCheckingHelperReturnType ist der Typ von dem |
||||
|
|
||||
|
// Das ergebnis vom SparseMdpPrctlModelChecker ist ein ExplicitQuantitativeCheckResult |
||||
|
|
||||
|
// Prinzipieller ablauf |
||||
|
// PRISM -> Parser -> ModelEngine | |
||||
|
// model_handlling.h -> AbstractMC -> SparseMDPML -> SparseMdpPrctlHelper |
||||
|
// Property -> Formula Parser -> | |
@ -1,3 +1,33 @@ |
|||||
import stormpy.shields |
import stormpy.shields |
||||
from . import shields |
from . import shields |
||||
from .shields import * |
from .shields import * |
||||
|
|
||||
|
def create_action_lookup(model, scheduler): |
||||
|
ret = {} |
||||
|
|
||||
|
for state_id in model.states: |
||||
|
choice = scheduler.get_choice(state_id) |
||||
|
action = choice.get_deterministic_choice() |
||||
|
state_valuation = model.state_valuations.get_string(state_id) |
||||
|
|
||||
|
action_to_be_executed = model.choice_labeling.get_labels_of_choice(model.get_choice_index(state_id, action)) |
||||
|
ret[state_valuation] = action_to_be_executed |
||||
|
|
||||
|
return ret |
||||
|
|
||||
|
def create_shield_action_lookup(model, shield): |
||||
|
ret = {} |
||||
|
|
||||
|
for state_id in model.states: |
||||
|
choices = shield.construct().get_choice(state_id) |
||||
|
state_valuation = model.state_valuations.get_string(state_id) |
||||
|
|
||||
|
l = [] |
||||
|
for choice in choices.choice_map: |
||||
|
action = choice[1] |
||||
|
action_to_be_executed = model.choice_labeling.get_labels_of_choice(model.get_choice_index(state_id, action)) |
||||
|
l.append(action_to_be_executed) |
||||
|
|
||||
|
ret[state_valuation] = l |
||||
|
|
||||
|
return ret |
Write
Preview
Loading…
Cancel
Save
Reference in new issue