def prob_TL(self, phi): model_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "models") prism_file_path = os.path.join(model_path, "pedestrian.nm") path_MC = os.path.join(model_path, "model_MC.nm") env_MC = os.path.join(model_path, "env_MC.nm") # Print self markov chain: # print(self.MC) # Writing prism files: stormpy_int.to_prism_file(self.MC, path_MC) stormpy_int.to_prism_file(self.true_env_MC, env_MC) composed = synchronous_parallel([self.MC, self.true_env_MC]) # print(composed.transitions) result = stormpy_int.model_checking(composed, phi, prism_file_path) # Returns a tulip transys: # MC_ts = stormpy_int.to_tulip_transys(path_MC) result = stormpy_int.model_checking( self.MC, phi, prism_file_path ) # Since there is no moving obstacle, try checking only the pedestrian obstacle # for state in self.MC.states: # print(self.MC.states[state]["ap"]) # self.MC.states[state]["ap"] |= {"good"} # Debugging: print states of result: # print(self.MC) # print(result) # print(" ") # for state in self.MC.states: # print(" State {}, with labels {}, Pr = {}".format(state, self.MC.states[state], result[(state, None)])) # for state in self.MC.states: # print(" State {}, with labels {}, Pr = {}".format(state, self.MC.states[state]["ap"], result[state])) return result # Implement this soon: Storm py
def light_analysis_test(): # Build models from prism files light = stormpy_int.to_tulip_transys(light_path) # Check properties formula = 'P=? [ F ("green") ]' # Model checking result = stormpy_int.model_checking(light, formula, out_model_path) os.remove(out_model_path) assert len(light.states) == 2 expected_result = [ { "labels": {"green"}, "result": 1.0 }, { "labels": {"red"}, "result": 1.0 }, ] _check_result(light, result, expected_result)
def compose_test(): # Build models from prism files mh = stormpy_int.to_tulip_transys(mh_path) light = stormpy_int.to_tulip_transys(light_path) # Compose models composed = synchronous_parallel([mh, light]) # Check properties formula = 'P=? [ "green" U "h6" ]' # Model checking result = stormpy_int.model_checking(composed, formula, out_model_path) os.remove(out_model_path) assert len(composed.states) == 14 expected_result = [ { "labels": {"h6", "green"}, "result": 1.0 }, { "labels": {"red", "h6"}, "result": 1.0 }, { "labels": {"h2", "green"}, "result": 0.42122366709344317 }, { "labels": {"red", "h2"}, "result": 0.0 }, { "labels": {"h4", "green"}, "result": 0.7256235827664395 }, { "labels": {"red", "h4"}, "result": 0.0 }, { "labels": {"h5", "green"}, "result": 0.9523809523809521 }, { "labels": {"red", "h5"}, "result": 0.0 }, { "labels": {"green", "h1"}, "result": 0.3209323177854804 }, { "labels": {"red", "h1"}, "result": 0.0 }, { "labels": {"h0", "green"}, "result": 0.2445198611698898 }, { "labels": {"red", "h0"}, "result": 0.0 }, { "labels": {"green", "h3"}, "result": 0.5528560630601442 }, { "labels": {"red", "h3"}, "result": 0.0 }, ] _check_result(composed, result, expected_result)
def synthesis_test(): # Build models from prism files ma = stormpy_int.to_tulip_transys(ma_path) mh = stormpy_int.to_tulip_transys(mh_path) light = stormpy_int.to_tulip_transys(light_path) # Compose models composed = synchronous_parallel([ma, mh, light]) # Check properties safety = '!("h4" & "a4") & !("red" & ("a8" | "a4"))' reach = '"a9"' formula = "Pmax=? [ ({}) U ({}) ]".format(safety, reach) # Construct policy (result, policy) = stormpy_int.model_checking(composed, formula, out_model_path, True) os.remove(out_model_path) assert abs(result[list(composed.states.initial)[0]] - 0.7429340826573935) < 1e-6 # Get the MC induced by applying policy_opt on model induced_mc = apply_policy(composed, policy) # Model checking result = stormpy_int.model_checking(induced_mc, formula, out_model_path) os.remove(out_model_path) expected_result = [ { "labels": {"green", "h5", "a8"}, "result": 0.745341614906832 }, { "labels": {"h5", "red", "a8"}, "result": 0.0 }, { "labels": {"green", "h0", "a8"}, "result": 0.7429340826573935 }, { "labels": {"h0", "red", "a8"}, "result": 0.0 }, { "labels": {"green", "h1", "a8"}, "result": 0.7287988161717747 }, { "labels": {"h1", "a8", "red"}, "result": 0.0 }, { "labels": {"green", "a8", "h4"}, "result": 0.6059687926071806 }, { "labels": {"red", "a8", "h4"}, "result": 0.0 }, { "labels": {"green", "a8", "h6"}, "result": 0.745341614906832 }, { "labels": {"red", "a8", "h6"}, "result": 0.0 }, { "labels": {"green", "a8", "h3"}, "result": 0.3861264940442798 }, { "labels": {"red", "a8", "h3"}, "result": 0.0 }, { "labels": {"green", "h2", "a8"}, "result": 0.6458232194557872 }, { "labels": {"h2", "a8", "red"}, "result": 0.0 }, { "labels": {"green", "h5", "a4"}, "result": 0.9523809523809523 }, { "labels": {"h5", "red", "a4"}, "result": 0.0 }, { "labels": {"green", "h0", "a4"}, "result": 0.9520897806888625 }, { "labels": {"h0", "red", "a4"}, "result": 0.0 }, { "labels": {"green", "h1", "a4"}, "result": 0.9501789664595234 }, { "labels": {"h1", "a4", "red"}, "result": 0.0 }, { "labels": {"green", "a4", "h4"}, "result": 0.0 }, { "labels": {"red", "a4", "h4"}, "result": 0.0 }, { "labels": {"green", "a4", "h6"}, "result": 0.9523809523809521 }, { "labels": {"red", "a4", "h6"}, "result": 0.0 }, { "labels": {"green", "a4", "h3"}, "result": 0.8264462809917354 }, { "labels": {"red", "a4", "h3"}, "result": 0.0 }, { "labels": {"green", "h2", "a4"}, "result": 0.9357284338501467 }, { "labels": {"h2", "a4", "red"}, "result": 0.0 }, { "labels": {"green", "h5", "a9"}, "result": 1.0 }, { "labels": {"h5", "a9", "red"}, "result": 1.0 }, { "labels": {"green", "h0", "a9"}, "result": 1.0 }, { "labels": {"h0", "a9", "red"}, "result": 1.0 }, { "labels": {"green", "a9", "h1"}, "result": 1.0 }, { "labels": {"a9", "red", "h1"}, "result": 1.0 }, { "labels": {"green", "a9", "h4"}, "result": 1.0 }, { "labels": {"a9", "red", "h4"}, "result": 1.0 }, { "labels": {"green", "a9", "h6"}, "result": 1.0 }, { "labels": {"a9", "h6", "red"}, "result": 1.0 }, { "labels": {"green", "a9", "h3"}, "result": 1.0 }, { "labels": {"a9", "h3", "red"}, "result": 1.0 }, { "labels": {"green", "a9", "h2"}, "result": 1.0 }, { "labels": {"a9", "h2", "red"}, "result": 1.0 }, ] _check_result(composed, result, expected_result)
def compose_test(): # Build models from prism files mh = stormpy_int.to_tulip_transys(mh_path) light = stormpy_int.to_tulip_transys(light_path) # Compose models composed = synchronous_parallel([mh, light]) # Check properties formula = 'P=? [ "green" U "h6" ]' # Model checking result = stormpy_int.model_checking(composed, formula, out_model_path) os.remove(out_model_path) assert len(composed.states) == 14 expected_result = [ { "labels": {"h6", "green"}, "result": 1.0 }, { "labels": {"red", "h6"}, "result": 1.0 }, { "labels": {"h2", "green"}, "result": 0.421_223_667_093_443_17 }, { "labels": {"red", "h2"}, "result": 0.0 }, { "labels": {"h4", "green"}, "result": 0.725_623_582_766_439_5 }, { "labels": {"red", "h4"}, "result": 0.0 }, { "labels": {"h5", "green"}, "result": 0.952_380_952_380_952_1 }, { "labels": {"red", "h5"}, "result": 0.0 }, { "labels": {"green", "h1"}, "result": 0.320_932_317_785_480_4 }, { "labels": {"red", "h1"}, "result": 0.0 }, { "labels": {"h0", "green"}, "result": 0.244_519_861_169_889_8 }, { "labels": {"red", "h0"}, "result": 0.0 }, { "labels": {"green", "h3"}, "result": 0.552_856_063_060_144_2 }, { "labels": {"red", "h3"}, "result": 0.0 }, ] _check_result(composed, result, expected_result)
def synthesis_test(): # Build models from prism files ma = stormpy_int.to_tulip_transys(ma_path) mh = stormpy_int.to_tulip_transys(mh_path) light = stormpy_int.to_tulip_transys(light_path) # Compose models composed = synchronous_parallel([ma, mh, light]) # Check properties safety = '!("h4" & "a4") & !("red" & ("a8" | "a4"))' reach = '"a9"' formula = "Pmax=? [ ({}) U ({}) ]".format(safety, reach) # Construct policy (result, policy) = stormpy_int.model_checking(composed, formula, out_model_path, True) os.remove(out_model_path) assert abs(result[list(composed.states.initial)[0]] - 0.742_934_082_657_393_5) < 1e-6 # Get the MC induced by applying policy_opt on model induced_mc = apply_policy(composed, policy) # Model checking result = stormpy_int.model_checking(induced_mc, formula, out_model_path) os.remove(out_model_path) expected_result = [ { "labels": {"green", "h5", "a8"}, "result": 0.745_341_614_906_832 }, { "labels": {"h5", "red", "a8"}, "result": 0.0 }, { "labels": {"green", "h0", "a8"}, "result": 0.742_934_082_657_393_5 }, { "labels": {"h0", "red", "a8"}, "result": 0.0 }, { "labels": {"green", "h1", "a8"}, "result": 0.728_798_816_171_774_7 }, { "labels": {"h1", "a8", "red"}, "result": 0.0 }, { "labels": {"green", "a8", "h4"}, "result": 0.605_968_792_607_180_6 }, { "labels": {"red", "a8", "h4"}, "result": 0.0 }, { "labels": {"green", "a8", "h6"}, "result": 0.745_341_614_906_832 }, { "labels": {"red", "a8", "h6"}, "result": 0.0 }, { "labels": {"green", "a8", "h3"}, "result": 0.386_126_494_044_279_8 }, { "labels": {"red", "a8", "h3"}, "result": 0.0 }, { "labels": {"green", "h2", "a8"}, "result": 0.645_823_219_455_787_2 }, { "labels": {"h2", "a8", "red"}, "result": 0.0 }, { "labels": {"green", "h5", "a4"}, "result": 0.952_380_952_380_952_3 }, { "labels": {"h5", "red", "a4"}, "result": 0.0 }, { "labels": {"green", "h0", "a4"}, "result": 0.952_089_780_688_862_5 }, { "labels": {"h0", "red", "a4"}, "result": 0.0 }, { "labels": {"green", "h1", "a4"}, "result": 0.950_178_966_459_523_4 }, { "labels": {"h1", "a4", "red"}, "result": 0.0 }, { "labels": {"green", "a4", "h4"}, "result": 0.0 }, { "labels": {"red", "a4", "h4"}, "result": 0.0 }, { "labels": {"green", "a4", "h6"}, "result": 0.952_380_952_380_952_1 }, { "labels": {"red", "a4", "h6"}, "result": 0.0 }, { "labels": {"green", "a4", "h3"}, "result": 0.826_446_280_991_735_4 }, { "labels": {"red", "a4", "h3"}, "result": 0.0 }, { "labels": {"green", "h2", "a4"}, "result": 0.935_728_433_850_146_7 }, { "labels": {"h2", "a4", "red"}, "result": 0.0 }, { "labels": {"green", "h5", "a9"}, "result": 1.0 }, { "labels": {"h5", "a9", "red"}, "result": 1.0 }, { "labels": {"green", "h0", "a9"}, "result": 1.0 }, { "labels": {"h0", "a9", "red"}, "result": 1.0 }, { "labels": {"green", "a9", "h1"}, "result": 1.0 }, { "labels": {"a9", "red", "h1"}, "result": 1.0 }, { "labels": {"green", "a9", "h4"}, "result": 1.0 }, { "labels": {"a9", "red", "h4"}, "result": 1.0 }, { "labels": {"green", "a9", "h6"}, "result": 1.0 }, { "labels": {"a9", "h6", "red"}, "result": 1.0 }, { "labels": {"green", "a9", "h3"}, "result": 1.0 }, { "labels": {"a9", "h3", "red"}, "result": 1.0 }, { "labels": {"green", "a9", "h2"}, "result": 1.0 }, { "labels": {"a9", "h2", "red"}, "result": 1.0 }, ] _check_result(composed, result, expected_result)
import os from tulip.interfaces import stormpy as stormpy_int from tulip.transys.compositions import synchronous_parallel model_path = os.path.join("/home/ubuntu/eeci/c1-probabilistic", "models") ma_path = os.path.join(model_path, "ma.nm") mh_path = os.path.join(model_path, "mh.pm") light_path = os.path.join(model_path, "light.pm") ma = stormpy_int.to_tulip_transys(ma_path) mh = stormpy_int.to_tulip_transys(mh_path) light = stormpy_int.to_tulip_transys(light_path) composed = synchronous_parallel([mh, light]) formula = 'P=? [ "green" U "h6" ]' out_model_path = os.path.join(model_path, "out_composed_model.nm") result = stormpy_int.model_checking(composed, formula, out_model_path) for state in composed.states: print(" State {}, with labels {}, Pr = {}".format(state, composed.states[state]["ap"], result[state]))