def example_getting_started_02(): path = stormpy.examples.files.prism_dtmc_die prism_program = stormpy.parse_prism_program(path) model = stormpy.build_model(prism_program) print("Number of states: {}".format(model.nr_states)) print("Number of transitions: {}".format(model.nr_transitions)) print("Labels in the model: {}".format(model.labeling.get_labels())) formula_str = "P=? [F s=2]" properties = stormpy.parse_properties_for_prism_program( formula_str, prism_program) model_for_formula = stormpy.build_model(prism_program, properties) print("Number of states: {}".format(model_for_formula.nr_states)) print("Number of transitions: {}".format(model_for_formula.nr_transitions)) print("Labels in the model: {}".format( model_for_formula.labeling.get_labels())) formula_str_2 = "P=? [F s=7 & d=2]" properties_2 = stormpy.parse_properties_for_prism_program( formula_str_2, prism_program) model_for_formula_2 = stormpy.build_model(prism_program, properties_2) print("Number of states: {}".format(model_for_formula_2.nr_states)) print("Number of transitions: {}".format( model_for_formula_2.nr_transitions)) print("Labels in the model: {}".format( model_for_formula_2.labeling.get_labels()))
def test_build_ctmc(self): program = stormpy.parse_prism_program(get_example_path("ctmc", "polling2.sm"), True) formulas = stormpy.parse_properties_for_prism_program("P=? [ F<=3 \"target\" ]", program) model = stormpy.build_model(program) assert model.nr_states == 12 assert model.nr_transitions == 22 assert model.model_type == stormpy.ModelType.CTMC assert not model.supports_parameters assert type(model) is stormpy.SparseCtmc model_for_formula = stormpy.build_model(program, formulas) assert model_for_formula.nr_states == 12 assert model_for_formula.nr_transitions == 21 assert model_for_formula.model_type == stormpy.ModelType.CTMC assert not model_for_formula.supports_parameters assert type(model_for_formula) is stormpy.SparseCtmc
def example_simulator_01(): path = stormpy.examples.files.prism_mdp_maze prism_program = stormpy.parse_prism_program(path) model = stormpy.build_model(prism_program) simulator = stormpy.simulator.create_simulator(model, seed=42) # 5 paths of at most 20 steps. paths = [] for m in range(5): path = [] state = simulator.restart() path = [f"{state}"] for n in range(20): actions = simulator.available_actions() select_action = random.randint(0,len(actions)-1) #print(f"Randomly select action nr: {select_action} from actions {actions}") path.append(f"--act={actions[select_action]}-->") state = simulator.step(actions[select_action]) #print(state) path.append(f"{state}") if simulator.is_done(): #print("Trapped!") break paths.append(path) for path in paths: print(" ".join(path))
def example_building_models_01(): path = stormpy.examples.files.drn_ctmc_dft model = stormpy.build_model_from_drn(path) print(model.model_type) print("Number of states: {}".format(model.nr_states)) # And the parametric path = stormpy.examples.files.drn_pdtmc_die model = stormpy.build_parametric_model_from_drn(path) print(model.model_type) print("Number of states: {}".format(model.nr_states)) path = stormpy.examples.files.jani_dtmc_die jani_program, properties = stormpy.parse_jani_model(path) model = stormpy.build_model(jani_program) print(model.model_type) print("Number of states: {}".format(model.nr_states)) # POMDPs should be constructed with choice labels path = stormpy.examples.files.drn_pomdp_maze opts = stormpy.DirectEncodingParserOptions() opts.build_choice_labels = True pomdp = stormpy.build_model_from_drn(stormpy.examples.files.drn_pomdp_maze, opts) # POMDPs need to be in a canonic representation pomdp = stormpy.pomdp.make_canonic(pomdp)
def test_scheduler_ma_via_mdp(self): program = stormpy.parse_prism_program(get_example_path("ma", "simple.ma"), False, True) formulas = stormpy.parse_properties_for_prism_program("Tmin=? [ F s=4 ]", program) ma = stormpy.build_model(program, formulas) assert ma.nr_states == 5 assert ma.nr_transitions == 8 assert ma.model_type == stormpy.ModelType.MA # Convert MA to MDP mdp, mdp_formulas = stormpy.transform_to_discrete_time_model(ma, formulas) assert mdp.nr_states == 5 assert mdp.nr_transitions == 8 assert mdp.model_type == stormpy.ModelType.MDP assert len(mdp.initial_states) == 1 initial_state = mdp.initial_states[0] assert initial_state == 0 result = stormpy.model_checking(mdp, mdp_formulas[0], extract_scheduler=True) assert math.isclose(result.at(initial_state), 0.08333333333) assert result.has_scheduler scheduler = result.scheduler assert scheduler.memoryless assert scheduler.memory_size == 1 assert scheduler.deterministic for state in mdp.states: choice = scheduler.get_choice(state) assert choice.defined assert choice.deterministic action = choice.get_deterministic_choice() if state.id == 0: assert action == 1 else: assert action == 0
def test_standard_properties(self): gspn_parser = stormpy.gspn.GSPNParser() gspn = gspn_parser.parse( get_example_path("gspn", "philosophers_4.pnpro")) assert gspn.get_name() == "Philosophers4" assert gspn.get_number_of_timed_transitions() == 12 assert gspn.get_number_of_immediate_transitions() == 0 assert gspn.get_number_of_places() == 16 # Build jani program jani_builder = stormpy.gspn.GSPNToJaniBuilder(gspn) jani_program = jani_builder.build() # Use standard properties properties = jani_builder.create_deadlock_properties(jani_program) # Instantiate constants jani_program, properties = stormpy.preprocess_symbolic_input( jani_program, properties, "TIME_BOUND=1") jani_program = jani_program.as_jani_model() # Build model # Leads to incorrect result #model = stormpy.build_model(jani_program, properties) model = stormpy.build_model(jani_program) # Model checking initial_state = model.initial_states[0] assert initial_state == 0 result = stormpy.model_checking(model, properties[0]) assert math.isclose(result.at(initial_state), 1.0) result = stormpy.model_checking(model, properties[1]) assert math.isclose(result.at(initial_state), 0.09123940783) result = stormpy.model_checking(model, properties[2]) assert math.isclose(result.at(initial_state), 5.445544554455446)
def model_checking(tulip_transys, formula, prism_file_path, extract_policy=False): """Model check tulip_transys against formula @type tulip_transys: either a MarkovChain or MarkovDecisionProcess object. @type formula: a string describing PCTL formula according to Prism format. @type prism_file_path: a string indicating the path to export the intermediate prism file (mostly for debugging purpose) @type extract_policy: boolean that indicates whether to extract policy @return result * If extract_policy = False, then for each state in model.states, result[state] is the probability of satisfying the formula starting at state. * If extract_policy = True, then result = (prob,policy) where for each state in model.states, prob[state] is the probability of satisfying the formula starting at state and policy[state] is the action to be applied at state. """ assert type(tulip_transys) == MDP or type(tulip_transys) == MC to_prism_file(tulip_transys, prism_file_path) prism_program = stormpy.parse_prism_program(prism_file_path) stormpy_model = stormpy.build_model(prism_program) properties = stormpy.parse_properties(formula, prism_program) result = stormpy.model_checking( stormpy_model, properties[0], extract_scheduler=extract_policy ) prob = _extract_probability(result, stormpy_model, tulip_transys) if not extract_policy: return prob policy = _extract_policy(result, stormpy_model, tulip_transys) return (prob, policy)
def example_simulator_01(): path = stormpy.examples.files.prism_dtmc_die prism_program = stormpy.parse_prism_program(path) model = stormpy.build_model(prism_program) simulator = stormpy.simulator.create_simulator(model, seed=42) final_outcomes = dict() for n in range(1000): while not simulator.is_done(): observation = simulator.step() if observation not in final_outcomes: final_outcomes[observation] = 1 else: final_outcomes[observation] += 1 simulator.restart() print(final_outcomes) options = stormpy.BuilderOptions([]) options.set_build_state_valuations() model = stormpy.build_sparse_model_with_options(prism_program, options) simulator = stormpy.simulator.create_simulator(model, seed=42) simulator.set_observation_mode(stormpy.simulator.SimulatorObservationMode.PROGRAM_LEVEL) final_outcomes = dict() for n in range(1000): while not simulator.is_done(): observation = simulator.step() if observation not in final_outcomes: final_outcomes[observation] = 1 else: final_outcomes[observation] += 1 simulator.restart() print(", ".join([f"{str(k)}: {v}" for k,v in final_outcomes.items()]))
def example_exploration_02(): """ Example to exploration of POMDPs. :return: """ program = stormpy.parse_prism_program( stormpy.examples.files.prism_pomdp_maze) prop = "R=? [F \"goal\"]" properties = stormpy.parse_properties(prop, program) model = stormpy.build_model(program, properties) print(model.model_type) # Internally, POMDPs are just MDPs with additional observation information. # Thus, data structure exploration for MDPs can be applied as before. initial_state = model.initial_states[0] for state in model.states: if state.id in model.initial_states: print(state) for action in state.actions: for transition in action.transitions: print( "From state {} by action {}, with probability {}, go to state {}" .format(state, action, transition.value(), transition.column)) print(model.nr_observations) for state in model.states: print("State {} has observation id {}".format( state.id, model.observations[state.id]))
def test_transform_continuous_to_discrete_time_model_ma(self): program = stormpy.parse_prism_program( get_example_path("ma", "simple.ma"), False, True) formulas = stormpy.parse_properties_for_prism_program( "Tmin=? [ F s=4 ]", program) ma = stormpy.build_model(program, formulas) assert ma.nr_states == 5 assert ma.nr_transitions == 8 assert ma.model_type == stormpy.ModelType.MA assert len(ma.initial_states) == 1 initial_state = ma.initial_states[0] assert initial_state == 0 result = stormpy.model_checking(ma, formulas[0]) assert math.isclose(result.at(initial_state), 0.08333333333) mdp, mdp_formulas = stormpy.transform_to_discrete_time_model( ma, formulas) assert mdp.nr_states == 5 assert mdp.nr_transitions == 8 assert mdp.model_type == stormpy.ModelType.MDP assert len(mdp.initial_states) == 1 initial_state = mdp.initial_states[0] assert initial_state == 0 result = stormpy.model_checking(mdp, mdp_formulas[0]) assert math.isclose(result.at(initial_state), 0.08333333333)
def _build_model(self, instance, with_origins=True, register_stats=True): """ Build a (sparse) model from the given prism program instance :param instance: A highlevel description of the model :param with_origins: If the model is to be analysed with highlevel counterex, then this flag should be true. :param register_stats: Should the stats for this model be registered. Put to False, when the model is just build for debugging purposes. :return: The Markov chain. """ start_mb = time.time() assert len(self.properties) + len(self.qualitative_properties) > 0 or self._optimality if with_origins: raw_formulae = [p.property.raw_formula for p in self.properties] if self._optimality: raw_formulae.append(self._optimality.criterion.raw_formula) options = stormpy.BuilderOptions(raw_formulae) options.set_build_with_choice_origins(True) options.set_add_overlapping_guards_label() model = stormpy.build_sparse_model_with_options(instance, options) else: model = stormpy.build_model(instance, [p.property for p in self.properties]) self._print_overlapping_guards(model) model.reduce_to_state_based_rewards() building_time = time.time() - start_mb if register_stats: self.stats.report_model_building(building_time, model.nr_states) logger.debug("Build model with {} states in {} seconds".format(model.nr_states, building_time)) assert len(model.initial_states) == 1 #logger.debug(instance) return model
def example_schedulers_02(): path = stormpy.examples.files.prism_ma_simple formula_str = "Tmin=? [ F s=4 ]" program = stormpy.parse_prism_program(path, False, True) formulas = stormpy.parse_properties_for_prism_program(formula_str, program) ma = stormpy.build_model(program, formulas) assert ma.model_type == stormpy.ModelType.MA # Convert MA to MDP mdp, mdp_formulas = stormpy.transform_to_discrete_time_model(ma, formulas) assert mdp.model_type == stormpy.ModelType.MDP initial_state = mdp.initial_states[0] assert initial_state == 0 result = stormpy.model_checking(mdp, mdp_formulas[0], extract_scheduler=True) assert result.has_scheduler scheduler = result.scheduler print(scheduler) assert scheduler.memoryless assert scheduler.deterministic for state in mdp.states: choice = scheduler.get_choice(state) action = choice.get_deterministic_choice() print("In state {} choose action {}".format(state, action))
def test_initial_states(self): program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm")) formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"one\" ]", program) model = stormpy.build_model(program, formulas) initial_states = model.initial_states assert len(initial_states) == 1 assert 0 in initial_states
def test_custom_property(self): gspn_parser = stormpy.gspn.GSPNParser() gspn = gspn_parser.parse( get_example_path("gspn", "philosophers_4.pnpro")) assert gspn.get_name() == "Philosophers4" assert gspn.get_number_of_timed_transitions() == 12 assert gspn.get_number_of_immediate_transitions() == 0 assert gspn.get_number_of_places() == 16 # Build jani program jani_builder = stormpy.gspn.GSPNToJaniBuilder(gspn) jani_program = jani_builder.build() # Set properties properties = stormpy.parse_properties_for_jani_model( 'P=? [F<=10 eating1=1]', jani_program) # Build model model = stormpy.build_model(jani_program, properties) # Model checking initial_state = model.initial_states[0] assert initial_state == 0 result = stormpy.model_checking(model, properties[0]) assert math.isclose(result.at(initial_state), 0.4372171069840004)
def _indiv_info(self, individual): # Create an assignment for the holes hole_assignments = self._individual_to_constants_assignment(individual) # And construct instance = self.build_instance(hole_assignments) model = stormpy.build_model(instance, [p.property for p in self.properties]) model.reduce_to_state_based_rewards() for p_qual, p_quan in zip(self.properties, self.quantitative_properties): mc_result = stormpy.model_checking(model, p_quan).at( model.initial_states[0]) threshold = float(p_qual.property.raw_formula.threshold) if p_qual.property.raw_formula.is_probability_operator: logger.debug("Probability operator: {}".format( p_qual.property.raw_formula.comparison_type)) elif p_qual.property.raw_formula.is_reward_operator: logger.debug("Reward operator: {}".format( p_qual.property.raw_formula.comparison_type)) else: assert False logger.debug("Operator's threshold: {}".format(threshold)) logger.debug( "Computed value for the operator: {}".format(mc_result))
def test_apply_scheduler_ma(self): program = stormpy.parse_prism_program( get_example_path("ma", "simple.ma"), False, True) formulas = stormpy.parse_properties_for_prism_program( "Tmin=? [ F s=4 ]", program) ma = stormpy.build_model(program, formulas) assert ma.nr_states == 5 assert ma.nr_transitions == 8 assert ma.model_type == stormpy.ModelType.MA initial_state = ma.initial_states[0] assert initial_state == 0 result = stormpy.model_checking(ma, formulas[0], extract_scheduler=True) assert math.isclose(result.at(initial_state), 0.08333333333) assert result.has_scheduler scheduler = result.scheduler assert scheduler.memoryless assert scheduler.memory_size == 1 assert scheduler.deterministic intermediate = ma.apply_scheduler(scheduler) assert intermediate.model_type == stormpy.ModelType.MA assert intermediate.nr_states == 3 assert intermediate.nr_transitions == 4 for state in intermediate.states: assert len(state.actions) == 1
def test_bisimulation(self): program = stormpy.parse_prism_program( get_example_path("dtmc", "crowds5_5.pm")) assert program.nr_modules == 1 assert program.model_type == stormpy.PrismModelType.DTMC prop = "P=? [F \"observe0Greater1\"]" properties = stormpy.parse_properties_for_prism_program(prop, program) model = stormpy.build_model(program, properties) assert model.nr_states == 7403 assert model.nr_transitions == 13041 assert model.model_type == stormpy.ModelType.DTMC assert not model.supports_parameters initial_state = model.initial_states[0] assert initial_state == 0 result = stormpy.model_checking(model, properties[0]) model_bisim = stormpy.perform_bisimulation( model, properties, stormpy.BisimulationType.STRONG) assert model_bisim.nr_states == 64 assert model_bisim.nr_transitions == 104 assert model_bisim.model_type == stormpy.ModelType.DTMC assert not model_bisim.supports_parameters result_bisim = stormpy.model_checking(model_bisim, properties[0]) initial_state_bisim = model_bisim.initial_states[0] assert initial_state_bisim == 34 assert math.isclose(result.at(initial_state), result_bisim.at(initial_state_bisim), rel_tol=1e-4)
def test_apply_scheduler_mdp(self): program = stormpy.parse_prism_program( get_example_path("mdp", "coin2-2.nm")) formulas = stormpy.parse_properties_for_prism_program( "Pmin=? [ F \"finished\" & \"all_coins_equal_1\"]", program) model = stormpy.build_model(program, formulas) assert model.nr_states == 272 assert model.nr_transitions == 492 assert len(model.initial_states) == 1 initial_state = model.initial_states[0] assert initial_state == 0 result = stormpy.model_checking(model, formulas[0], extract_scheduler=True) assert result.has_scheduler scheduler = result.scheduler assert scheduler.memoryless assert scheduler.memory_size == 1 assert scheduler.deterministic assert not scheduler.partial intermediate = model.apply_scheduler(scheduler, True) assert intermediate.model_type == stormpy.ModelType.DTMC assert intermediate.nr_states == 126 assert intermediate.nr_transitions == 156 for state in intermediate.states: assert len(state.actions) == 1
def test_model_checking_prob01(self): program = stormpy.parse_prism_program( get_example_path("dtmc", "die.pm")) formulaPhi = stormpy.parse_properties("true")[0] formulaPsi = stormpy.parse_properties("\"six\"")[0] model = stormpy.build_model(program, [formulaPsi]) phiResult = stormpy.model_checking(model, formulaPhi) phiStates = phiResult.get_truth_values() assert phiStates.number_of_set_bits() == model.nr_states psiResult = stormpy.model_checking(model, formulaPsi) psiStates = psiResult.get_truth_values() assert psiStates.number_of_set_bits() == 1 (prob0, prob1) = stormpy.compute_prob01_states(model, phiStates, psiStates) assert prob0.number_of_set_bits() == 9 assert prob1.number_of_set_bits() == 1 (prob0, prob1) = stormpy.compute_prob01min_states(model, phiStates, psiStates) assert prob0.number_of_set_bits() == 9 assert prob1.number_of_set_bits() == 1 (prob0, prob1) = stormpy.compute_prob01max_states(model, phiStates, psiStates) assert prob0.number_of_set_bits() == 9 assert prob1.number_of_set_bits() == 1 labelprop = stormpy.core.Property("cora", formulaPsi.raw_formula) result = stormpy.model_checking(model, labelprop) assert result.get_truth_values().number_of_set_bits() == 1
def test_scheduler_mdp(self): program = stormpy.parse_prism_program( get_example_path("mdp", "coin2-2.nm")) formulas = stormpy.parse_properties_for_prism_program( "Pmin=? [ F \"finished\" & \"all_coins_equal_1\"]", program) model = stormpy.build_model(program, formulas) assert model.nr_states == 272 assert model.nr_transitions == 492 assert len(model.initial_states) == 1 initial_state = model.initial_states[0] assert initial_state == 0 result = stormpy.model_checking(model, formulas[0], extract_scheduler=True) assert result.has_scheduler scheduler = result.scheduler assert scheduler.memoryless assert scheduler.memory_size == 1 assert scheduler.deterministic for state in model.states: choice = scheduler.get_choice(state) assert choice.defined assert choice.deterministic action = choice.get_deterministic_choice() assert 0 <= action assert action < len(state.actions) distribution = choice.get_choice() assert str(distribution).startswith("{[1:")
def test_build_dtmc_from_prism_program(self): program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm")) model = stormpy.build_model(program) assert model.nr_states == 13 assert model.nr_transitions == 20 assert model.model_type == stormpy.ModelType.DTMC assert not model.supports_parameters assert type(model) is stormpy.SparseDtmc
def build_stormpy_model(path): """Return the stormpy model created from the prism file @type path: a string indicating the path to the prism file """ prism_program = stormpy.parse_prism_program(path) return stormpy.build_model(prism_program)
def example_getting_started_01(): path = stormpy.examples.files.prism_dtmc_die prism_program = stormpy.parse_prism_program(path) model = stormpy.build_model(prism_program) print("Number of states: {}".format(model.nr_states)) print("Number of transitions: {}".format(model.nr_transitions)) print("Labels: {}".format(model.labeling.get_labels()))
def test_build_dtmc_from_jani_model(self): jani_model, properties = stormpy.parse_jani_model(get_example_path("dtmc", "die.jani")) model = stormpy.build_model(jani_model) assert model.nr_states == 13 assert model.nr_transitions == 20 assert model.model_type == stormpy.ModelType.DTMC assert not model.supports_parameters assert type(model) is stormpy.SparseDtmc
def test_build_mdp(self): program = stormpy.parse_prism_program(get_example_path("mdp", "two_dice.nm")) formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"two\" ]", program) model = stormpy.build_model(program, formulas) assert model.nr_states == 169 assert model.nr_transitions == 435 assert model.model_type == stormpy.ModelType.MDP assert not model.supports_parameters assert type(model) is stormpy.SparseMdp
def test_build_ma(self): program = stormpy.parse_prism_program(get_example_path("ma", "simple.ma"), False, True) formulas = stormpy.parse_properties_for_prism_program("Pmax=? [ F<=2 s=2 ]", program) model = stormpy.build_model(program, formulas) assert model.nr_states == 4 assert model.nr_transitions == 7 assert model.model_type == stormpy.ModelType.MA assert not model.supports_parameters assert type(model) is stormpy.SparseMA
def test_model_checking_all_dtmc(self): program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm")) formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"one\" ]", program) model = stormpy.build_model(program, formulas) assert model.nr_states == 13 assert model.nr_transitions == 20 result = stormpy.model_checking(model, formulas[0]) assert result.result_for_all_states reference = [1 / 6, 1 / 3, 0, 2 / 3, 0, 0, 0, 1, 0, 0, 0, 0, 0] assert all(map(math.isclose, result.get_values(), reference))
def __init__(self): self.target_label = "one" program_path = get_example_path("dtmc", "die.pm") raw_formula = "P=? [ F \"" + self.target_label + "\" ]" program = stormpy.parse_prism_program(program_path) formulas = stormpy.parse_properties_for_prism_program( raw_formula, program) self.model = stormpy.build_model(program, formulas)
def storm_check(program, formula): """ Model Checks formula(string) over model """ properties = stormpy.parse_properties_for_jani_model(formula, program) model = stormpy.build_model(program, properties) results_for_all_states = stormpy.check_model_sparse(model, properties[0]) initial_state = model.initial_states[0] result = results_for_all_states.at(initial_state) return result
def test_model_checking_only_initial(self): program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm")) formulas = stormpy.parse_properties_for_prism_program("Pmax=? [F{\"coin_flips\"}<=3 \"one\"]", program) model = stormpy.build_model(program, formulas) assert len(model.initial_states) == 1 initial_state = model.initial_states[0] assert initial_state == 0 result = stormpy.model_checking(model, formulas[0], only_initial_states=True) assert not result.result_for_all_states assert math.isclose(result.at(initial_state), 1 / 8)