Thomas Knoll
1 year ago
10 changed files with 269 additions and 10 deletions
-
3examples/shields/01_pre_shield.py
-
58examples/shields/03_optimal_shield.py
-
58examples/shields/04_optimal_shield.py
-
7lib/stormpy/examples/files.py
-
73lib/stormpy/examples/files/smg/lights.prism
-
54lib/stormpy/examples/files/smg/robotics_planning.prism
-
3src/mod_shields.cpp
-
9src/shields/shield_handling.cpp
-
2src/shields/shield_handling.h
-
12src/storage/model.cpp
@ -0,0 +1,58 @@ |
|||
import stormpy |
|||
import stormpy.core |
|||
import stormpy.simulator |
|||
|
|||
|
|||
import stormpy.shields |
|||
|
|||
import stormpy.examples |
|||
import stormpy.examples.files |
|||
import random |
|||
|
|||
|
|||
def optimal_shield_03(): |
|||
path = stormpy.examples.files.prism_smg_lights |
|||
formula_str = "<tlsShield, Optimal> <<shield>> R{\"differenceWithInterferenceCost\"}min=? [ LRA ]" |
|||
|
|||
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) |
|||
|
|||
result = stormpy.model_checking(model, formulas[0], extract_scheduler=True) |
|||
assert result.has_scheduler |
|||
print(F"Check Scheduler: {result.has_scheduler}") |
|||
print(F"Check Shield: {result.has_schield}") |
|||
|
|||
print(type(result)) |
|||
|
|||
shield = result.shield |
|||
scheduler = result.scheduler |
|||
|
|||
print(type(shield)) |
|||
|
|||
assert scheduler.memoryless |
|||
assert scheduler.deterministic |
|||
|
|||
constructed_shield = shield.construct() |
|||
|
|||
print(type(constructed_shield)) |
|||
|
|||
stormpy.shields.export_shieldDouble(model, shield) |
|||
|
|||
# for state in model.states: |
|||
# choice = scheduler.get_choice(state) |
|||
# action = choice.get_deterministic_choice() |
|||
# print("In state {} choose action {}".format(state, action)) |
|||
|
|||
# dtmc = model.apply_scheduler(scheduler) |
|||
# print(dtmc) |
|||
|
|||
|
|||
|
|||
if __name__ == '__main__': |
|||
optimal_shield_03() |
@ -0,0 +1,58 @@ |
|||
import stormpy |
|||
import stormpy.core |
|||
import stormpy.simulator |
|||
|
|||
|
|||
import stormpy.shields |
|||
|
|||
import stormpy.examples |
|||
import stormpy.examples.files |
|||
import random |
|||
|
|||
|
|||
def optimal_shield_03(): |
|||
path = stormpy.examples.files.prism_smg_robot |
|||
formula_str = "<path_correction, Optimal> <<sh>> R{\"travel_costs\"}min=? [ LRA ]" |
|||
|
|||
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) |
|||
|
|||
result = stormpy.model_checking(model, formulas[0], extract_scheduler=True) |
|||
assert result.has_scheduler |
|||
print(F"Check Scheduler: {result.has_scheduler}") |
|||
print(F"Check Shield: {result.has_schield}") |
|||
|
|||
print(type(result)) |
|||
|
|||
shield = result.shield |
|||
scheduler = result.scheduler |
|||
|
|||
print(type(shield)) |
|||
|
|||
assert scheduler.memoryless |
|||
assert scheduler.deterministic |
|||
|
|||
constructed_shield = shield.construct() |
|||
|
|||
print(type(constructed_shield)) |
|||
|
|||
stormpy.shields.export_shieldDouble(model, shield) |
|||
|
|||
# for state in model.states: |
|||
# choice = scheduler.get_choice(state) |
|||
# action = choice.get_deterministic_choice() |
|||
# print("In state {} choose action {}".format(state, action)) |
|||
|
|||
# dtmc = model.apply_scheduler(scheduler) |
|||
# print(dtmc) |
|||
|
|||
|
|||
|
|||
if __name__ == '__main__': |
|||
optimal_shield_03() |
@ -0,0 +1,73 @@ |
|||
smg |
|||
|
|||
player controller |
|||
cont |
|||
endplayer |
|||
|
|||
player shield |
|||
[reduceNS_Shield], [reduceEW_Shield], [reduceNS_ShieldDev], [reduceEW_ShieldDev] |
|||
endplayer |
|||
|
|||
player environment |
|||
env |
|||
endplayer |
|||
|
|||
global move : [0..2]; |
|||
|
|||
const int lmax = 1; |
|||
|
|||
global N : [0..lmax]; |
|||
global E : [0..lmax]; |
|||
global S : [0..lmax]; |
|||
global W : [0..lmax]; |
|||
|
|||
module env |
|||
[] move=0 -> 1/8 : (N'=min(lmax,N+1)) & (move'=1) + |
|||
1/8 : (S'=min(lmax,S+1)) & (move'=1) + |
|||
1/8 : (E'=min(lmax,E+1)) & (move'=1) + |
|||
1/8 : (W'=min(lmax,W+1)) & (move'=1) + |
|||
|
|||
7/120 : (N'=min(lmax,N+1)) & (S'=min(lmax,S+1)) & (move'=1) + |
|||
7/120 : (S'=min(lmax,S+1)) & (W'=min(lmax,W+1)) & (move'=1) + |
|||
7/120 : (E'=min(lmax,E+1)) & (N'=min(lmax,N+1)) & (move'=1) + |
|||
7/120 : (W'=min(lmax,W+1)) & (N'=min(lmax,N+1)) & (move'=1) + |
|||
7/120 : (E'=min(lmax,E+1)) & (S'=min(lmax,S+1)) & (move'=1) + |
|||
7/120 : (W'=min(lmax,W+1)) & (S'=min(lmax,S+1)) & (move'=1) + |
|||
|
|||
3/120 : (E'=min(lmax,E+1)) & (N'=min(lmax,N+1)) & (W'=min(lmax,W+1)) & (move'=1) + |
|||
3/120 : (W'=min(lmax,W+1)) & (N'=min(lmax,N+1)) & (E'=min(lmax,E+1)) & (move'=1) + |
|||
3/120 : (E'=min(lmax,E+1)) & (S'=min(lmax,S+1)) & (W'=min(lmax,W+1)) & (move'=1) + |
|||
3/120 : (W'=min(lmax,W+1)) & (S'=min(lmax,S+1)) & (N'=min(lmax,N+1)) & (move'=1) + |
|||
|
|||
6/120 : (W'=min(lmax,W+1)) & (S'=min(lmax,S+1)) & (E'=min(lmax,E+1)) & (N'=min(lmax,N+1)) & (move'=1); |
|||
endmodule |
|||
|
|||
module cont |
|||
action : [0..1]; |
|||
|
|||
[] move=1 -> (action'=0) & (move'=2); |
|||
[] move=1 -> (action'=1) & (move'=2); |
|||
endmodule |
|||
|
|||
module sh |
|||
[reduceNS_Shield] move=2 & action=0 -> (N'=max(0,N-1)) & (S'=max(0,S-1)) & (move'=0); |
|||
[reduceEW_Shield] move=2 & action=1 -> (W'=max(0,W-1)) & (E'=max(0,E-1)) & (move'=0); |
|||
|
|||
[reduceNS_ShieldDev] move=2 & action=1 -> (N'=max(0,N-1)) & (S'=max(0,S-1)) & (move'=0); |
|||
[reduceEW_ShieldDev] move=2 & action=0 -> (W'=max(0,W-1)) & (E'=max(0,E-1)) & (move'=0); |
|||
endmodule |
|||
|
|||
formula diff = pow(pow((N+S)-(E+W),2),0.5); |
|||
rewards "difference" |
|||
true : diff; |
|||
endrewards |
|||
|
|||
const double lambda = 0.8; |
|||
const double interference = 2 * lmax; |
|||
|
|||
rewards "differenceWithInterferenceCost" |
|||
[reduceNS_Shield] true : lambda * diff; |
|||
[reduceEW_Shield] true : lambda * diff; |
|||
[reduceNS_ShieldDev] true : lambda * diff + (1 - lambda) * interference; |
|||
[reduceEW_ShieldDev] true : lambda * diff + (1 - lambda) * interference; |
|||
endrewards |
@ -0,0 +1,54 @@ |
|||
smg |
|||
|
|||
player sh |
|||
[planA], [planA_Dev], [planB], [planB_Dev] |
|||
endplayer |
|||
|
|||
player learned_controller |
|||
controller, env |
|||
endplayer |
|||
|
|||
global move : [0..2] init 0; |
|||
|
|||
const int lengthA = 10; |
|||
const int lengthB = lengthA + 5; |
|||
|
|||
global stepA : [0..lengthA] init 0; |
|||
global stepB : [0..lengthB] init 0; |
|||
|
|||
global planA : bool init true; |
|||
global shieldPlanA : bool init true; |
|||
|
|||
global stepTaken : bool init false; |
|||
global waiting : bool init false; |
|||
|
|||
const int waiting_cost = 2; |
|||
|
|||
const double blockedProb = 0.5; |
|||
module controller |
|||
[] move=0 & shieldPlanA & stepA<lengthA -> (planA'=true) & (stepTaken'=false) & (waiting'=false) & (move'=1); |
|||
[] move=0 & shieldPlanA & stepA<lengthA -> (planA'=false) & (stepTaken'=false) & (waiting'=false) & (move'=1); |
|||
|
|||
[] move=0 & !shieldPlanA & stepB<lengthB -> (planA'=false) & (stepTaken'=false) & (waiting'=false) & (move'=1); |
|||
endmodule |
|||
|
|||
module shield |
|||
[planA] move=1 & planA -> (shieldPlanA'=true) & (move'=2); |
|||
[planB_Dev] move=1 & planA -> (shieldPlanA'=false) & (move'=2); |
|||
|
|||
[planB] move=1 & !planA -> (shieldPlanA'=false) & (move'=2); |
|||
[planA_Dev] move=1 & !planA -> (shieldPlanA'=true) & (move'=2); |
|||
endmodule |
|||
|
|||
module env |
|||
[] move=2 & shieldPlanA& stepA<lengthA -> 1 - blockedProb : (stepA'=min(stepA+1, lengthA)) & (stepTaken'=true) & (move'=0) + blockedProb : (stepA'=stepA) & (waiting'=true) & (move'=0); |
|||
|
|||
[] move=2 & !shieldPlanA & stepB<lengthB -> (stepB'=min(stepB+1, lengthB)) & (stepTaken'=true) & (move'=0); |
|||
|
|||
[] move=2 & (lengthB=stepB | lengthA=stepA) -> (stepTaken'=false) & (waiting'=false); |
|||
endmodule |
|||
|
|||
rewards "travel_costs" |
|||
stepTaken : 1; |
|||
waiting : waiting_cost; |
|||
endrewards |
Write
Preview
Loading…
Cancel
Save
Reference in new issue