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_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 test_parametric_bisimulation(self): program = stormpy.parse_prism_program( get_example_path("pdtmc", "brp16_2.pm")) assert program.nr_modules == 5 assert program.model_type == stormpy.PrismModelType.DTMC assert program.has_undefined_constants assert program.undefined_constants_are_graph_preserving prop = "P=? [F s=5]" properties = stormpy.parse_properties_for_prism_program(prop, program) model = stormpy.build_parametric_model(program, properties) assert model.nr_states == 613 assert model.nr_transitions == 803 assert model.model_type == stormpy.ModelType.DTMC assert model.has_parameters initial_state = model.initial_states[0] assert initial_state == 0 result = stormpy.model_checking(model, properties[0]) ratFunc = result.at(initial_state) model_bisim = stormpy.perform_bisimulation( model, properties, stormpy.BisimulationType.STRONG) assert model_bisim.nr_states == 324 assert model_bisim.nr_transitions == 452 assert model_bisim.model_type == stormpy.ModelType.DTMC assert model_bisim.has_parameters result_bisim = stormpy.model_checking(model_bisim, properties[0]) initial_state_bisim = model_bisim.initial_states[0] assert initial_state_bisim == 316 ratFunc_bisim = result_bisim.at(initial_state_bisim) assert ratFunc == ratFunc_bisim
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_change_parametric_matrix_modelchecking(self): import stormpy.logic program = stormpy.parse_prism_program( get_example_path("pdtmc", "brp16_2.pm")) formulas = stormpy.parse_properties_for_prism_program( "P=? [ F s=5 ]", program) model = stormpy.build_parametric_model(program, formulas) initial_state = model.initial_states[0] assert initial_state == 0 matrix = model.transition_matrix # Check matrix one_pol = stormpy.RationalRF(1) one_pol = stormpy.FactorizedPolynomial(one_pol) one = stormpy.FactorizedRationalFunction(one_pol, one_pol) for e in matrix: assert e.value() == one or len(e.value().gather_variables()) > 0 # First model checking result = stormpy.model_checking(model, formulas[0]) ratFunc = result.at(initial_state) assert len(ratFunc.gather_variables()) > 0 # Change probabilities two_pol = stormpy.RationalRF(2) two_pol = stormpy.FactorizedPolynomial(two_pol) new_val = stormpy.FactorizedRationalFunction(one_pol, two_pol) for e in matrix: if len(e.value().gather_variables()) > 0: e.set_value(new_val) for e in matrix: assert e.value() == new_val or e.value() == one # Second model checking result = stormpy.model_checking(model, formulas[0]) ratFunc = result.at(initial_state) assert len(ratFunc.gather_variables()) == 0
def example_analysis_02(): path = stormpy.examples.files.prism_dtmc_die prism_program = stormpy.parse_prism_program(path) formula_str = "P=? [F s=7 & d=2]" properties = stormpy.parse_properties(formula_str, prism_program) model = stormpy.build_symbolic_model(prism_program, properties) result = stormpy.model_checking(model, properties[0]) filter = stormpy.create_filter_initial_states_symbolic(model) result.filter(filter) assert result.min == result.max print(result.min) # Create an auxiliary mapping to build expressions that matches some state. variables = dict() for m in prism_program.modules: for v in m.integer_variables: variables[v.name] = v.expression_variable.get_expression() expr_manager = prism_program.expression_manager expr_for_state_1 = Expression.Conjunction([ Expression.Eq(variables["s"], expr_manager.create_integer(1)), Expression.Eq(variables["d"], expr_manager.create_integer(0)) ]) expr_for_state_2 = Expression.And( Expression.Eq(variables["s"], expr_manager.create_integer(4)), Expression.Eq(variables["d"], expr_manager.create_integer(0))) result = stormpy.model_checking(model, properties[0]) cached_res = result.clone() cached_res.filter(stormpy.create_filter_symbolic(model, expr_for_state_1)) print(cached_res.min) result.filter(stormpy.create_filter_symbolic(model, expr_for_state_2)) assert result.min == result.max print(result.min)
def test_explicit_model_builder_approximation_complete(self): dft = stormpy.dft.load_dft_galileo_file( get_example_path("dft", "rc.dft")) properties = stormpy.parse_properties("T=? [ F \"failed\" ]") prop = properties[0] symmetries = stormpy.dft.DFTSymmetries() builder = stormpy.dft.ExplicitDFTModelBuilder_double(dft, symmetries) # Iteration 0 builder.build(0, 1.0) model_low = builder.get_partial_model(True, True) assert model_low.model_type == stormpy.ModelType.CTMC assert type(model_low) is stormpy.SparseCtmc assert model_low.nr_states == 25 assert model_low.nr_transitions == 46 assert not model_low.supports_parameters initial_state = model_low.initial_states[0] assert initial_state == 1 result_low = stormpy.model_checking(model_low, prop) assert math.isclose(result_low.at(initial_state), 0.5735024009) # Final iteration builder.build(1) model = builder.get_model() assert model.model_type == stormpy.ModelType.CTMC assert type(model) is stormpy.SparseCtmc assert model.nr_states == 145 assert model.nr_transitions == 625 assert not model.supports_parameters result = stormpy.model_checking(model, prop) assert math.isclose(result.at(initial_state), 0.6471760795)
def repair(self, local_repairer, dtmc_name, policy_name, base_dir): dtmc = self.dtmc_generator.compute_dtmc() dtmc.save(dtmc_name, base_dir) dtmc_tra = os.path.join(base_dir, dtmc_name + '.tra') dtmc_lab = os.path.join(base_dir, dtmc_name + '.lab') model = stormpy.build_sparse_model_from_explicit(dtmc_tra, dtmc_lab) #unsafe_reachability_prob = stormpy.model_checking_all(model, self.formula[0]) unsafe_reachability_prob = stormpy.model_checking(model, self.formula[0]) ##unsafe=stormpy.ExplicitQuantitativeCheckResult.get_values(unsafe_reachability_prob) ##print(' Unsafe Prob {}'.format(unsafe)) #init_unsafe_prob = unsafe_reachability_prob[state_mapper.INITIAL_STATE] init_unsafe_prob = stormpy.ExplicitQuantitativeCheckResult.at(unsafe_reachability_prob,state_mapper.INITIAL_STATE) print('Init Unsafe Prob {}'.format(init_unsafe_prob)) while init_unsafe_prob > self._lambda and local_repairer.repair(self.dtmc_generator, unsafe_reachability_prob): dtmc = self.dtmc_generator.compute_dtmc() dtmc.save(dtmc_name, base_dir) self.dtmc_generator.save_policy(policy_name, base_dir) model = stormpy.build_sparse_model_from_explicit(dtmc_tra, dtmc_lab) #unsafe_reachability_prob = stormpy.model_checking_all(model, self.formula[0]) unsafe_reachability_prob = stormpy.model_checking(model, self.formula[0]) init_unsafe_prob = stormpy.ExplicitQuantitativeCheckResult.at(unsafe_reachability_prob,state_mapper.INITIAL_STATE) print('(New Init Unsafe)Prob {}'.format(init_unsafe_prob)) print('Final Unsafe Prob {}'.format(init_unsafe_prob)) return dtmc
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 test_eliminate_non_markovian_chains(self): program = stormpy.parse_prism_program( get_example_path("ma", "stream2.ma"), False, True) formulas = stormpy.parse_properties_for_prism_program( "Pmin=? [ F \"done\"];Tmin=? [ F \"done\" ]", program) ma = stormpy.build_model(program, formulas) assert ma.nr_states == 12 assert ma.nr_transitions == 14 assert ma.model_type == stormpy.ModelType.MA assert len(ma.initial_states) == 1 initial_state = ma.initial_states[0] assert initial_state == 0 assert len(formulas) == 2 result = stormpy.model_checking(ma, formulas[0]) assert math.isclose(result.at(initial_state), 1) # Keep labels ma_elim, ma_formulas_elim = stormpy.eliminate_non_markovian_chains( ma, formulas, stormpy.EliminationLabelBehavior.KEEP_LABELS) assert ma_elim.nr_states == 9 assert ma_elim.nr_transitions == 11 assert ma_elim.model_type == stormpy.ModelType.MA assert len(ma_elim.initial_states) == 1 initial_state = ma_elim.initial_states[0] assert initial_state == 0 assert len(ma_formulas_elim) == 1 result = stormpy.model_checking(ma_elim, ma_formulas_elim[0]) assert math.isclose(result.at(initial_state), 1) # Merge labels ma_elim, ma_formulas_elim = stormpy.eliminate_non_markovian_chains( ma, formulas, stormpy.EliminationLabelBehavior.MERGE_LABELS) assert ma_elim.nr_states == 8 assert ma_elim.nr_transitions == 10 assert ma_elim.model_type == stormpy.ModelType.MA assert len(ma_elim.initial_states) == 1 initial_state = ma_elim.initial_states[0] assert initial_state == 0 assert len(ma_formulas_elim) == 1 result = stormpy.model_checking(ma_elim, ma_formulas_elim[0]) assert math.isclose(result.at(initial_state), 1) # Delete labels ma_elim, ma_formulas_elim = stormpy.eliminate_non_markovian_chains( ma, formulas, stormpy.EliminationLabelBehavior.DELETE_LABELS) assert ma_elim.nr_states == 8 assert ma_elim.nr_transitions == 10 assert ma_elim.model_type == stormpy.ModelType.MA assert len(ma_elim.initial_states) == 1 initial_state = ma_elim.initial_states[0] assert initial_state == 0 assert len(ma_formulas_elim) == 1 result = stormpy.model_checking(ma_elim, ma_formulas_elim[0]) assert math.isclose(result.at(initial_state), 1)
def test_model_checking_dtmc_all_labels(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) assert model.nr_states == 13 assert model.nr_transitions == 20 assert len(model.initial_states) == 1 initial_state = model.initial_states[0] assert initial_state == 0 result = stormpy.model_checking(model, formulas[0]) assert math.isclose(result.at(initial_state), 1 / 6) formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"two\" ]", program) result = stormpy.model_checking(model, formulas[0]) assert math.isclose(result.at(initial_state), 1 / 6)
def test_model_checking_jani_dtmc(self): jani_model, formulas = stormpy.parse_jani_model( get_example_path("dtmc", "die.jani")) formulas = stormpy.eliminate_reward_accumulations(jani_model, formulas) assert len(formulas) == 2 model = stormpy.build_model(jani_model, formulas) assert model.nr_states == 13 assert model.nr_transitions == 20 assert len(model.initial_states) == 1 initial_state = model.initial_states[0] assert initial_state == 0 result = stormpy.model_checking(model, formulas[0]) assert math.isclose(result.at(initial_state), 1 / 6) result = stormpy.model_checking(model, formulas[1]) assert math.isclose(result.at(initial_state), 11 / 3)
def test_model_checking_jani_dtmc(self): jani_model, properties = stormpy.parse_jani_model(get_example_path("dtmc", "die.jani")) formulas = [properties["Probability to throw a six"], properties["Expected number of coin flips"]] formulas = stormpy.eliminate_reward_accumulations(jani_model, formulas) assert len(formulas) == 2 model = stormpy.build_model(jani_model, formulas) assert model.nr_states == 13 assert model.nr_transitions == 20 assert len(model.initial_states) == 1 initial_state = model.initial_states[0] assert initial_state == 0 result = stormpy.model_checking(model, formulas[0]) assert math.isclose(result.at(initial_state), 1 / 6) result = stormpy.model_checking(model, formulas[1]) assert math.isclose(result.at(initial_state), 11 / 3)
def MRP_by_pruned(mdp, targets, cap, SF, SFR, SFRR): t1 = time.time() pruned_mdp = cmdp2p_mdp.pruned_mdp(mdp, targets, cap, SF, SFR, SFRR) # pruned_mdp = cmdp2smdp_RR.pruned_mdp(mdp, targets, cap, SF, SFR, SFRR) print("mdp convert to pruned one successfully") pruned_storm_mdp = consmdp_to_storm_consmdp(pruned_mdp, pruned_mdp.new_targets) print( f"pruned------The number of states in the explicit MDP in Storm: {pruned_storm_mdp.nr_states}" ) print(f"transitions number:{pruned_storm_mdp.nr_transitions}") storm_result = stormpy.model_checking(pruned_storm_mdp, prop[0]) strategy_extract = strategy_extraction.StrategyExtract( pruned_mdp, storm_result) strategy_result = strategy_extract.extractOptAction() t2 = time.time() print( "runtime(s) of computing MRP by prunning flattened mdp and extracting optimal policy:", t2 - t1) return pruned_mdp, storm_result, strategy_result
def example_parametric_models_02(): # Check support for parameters if not config.storm_with_pars: print("Support parameters is missing. Try building storm-pars.") return import stormpy.pars from pycarl.formula import FormulaType, Relation if stormpy.info.storm_ratfunc_use_cln(): import pycarl.cln.formula else: import pycarl.gmp.formula path = stormpy.examples.files.prism_pdtmc_die prism_program = stormpy.parse_prism_program(path) formula_str = "P=? [F s=7 & d=2]" properties = stormpy.parse_properties_for_prism_program(formula_str, prism_program) model = stormpy.build_parametric_model(prism_program, properties) initial_state = model.initial_states[0] result = stormpy.model_checking(model, properties[0]) print("Result: {}".format(result.at(initial_state))) collector = stormpy.ConstraintCollector(model) print("Well formed constraints:") for formula in collector.wellformed_constraints: print(formula.get_constraint()) print("Graph preserving constraints:") for formula in collector.graph_preserving_constraints: print(formula.get_constraint())
def check_property(self, prop): # check primary direction primary = self.model_check_property(prop, alt=False) # no need to check secondary direction if primary direction yields UNSAT if not primary.sat: return MdpPropertyResult(prop, primary, None, False, None, None, None, None) # primary direction is SAT # check if the primary scheduler is consistent selection, choice_values, expected_visits, scores, consistent = self.quotient_container.scheduler_consistent( self, prop, primary.result) # regardless of whether it is consistent or not, we compute secondary direction to show that all SAT # compute secondary direction secondary = self.model_check_property(prop, alt=True) if self.is_dtmc and primary.value != secondary.value: dtmc = self.quotient_container.mdp_to_dtmc(self.model) result = stormpy.model_checking( dtmc, prop.formula, only_initial_states=False, extract_scheduler=(not self.is_dtmc), # extract_scheduler=True, environment=self.environment) feasibility = True if secondary.sat else None return MdpPropertyResult(prop, primary, secondary, feasibility, selection, choice_values, expected_visits, scores)
def _check_optimal_property(self, family_ref, assignment, cex_generator=None, optimal_value=None): if optimal_value is None: assert family_ref.dtmc is not None # Model checking of the optimality property result = stormpy.model_checking(family_ref.dtmc, self._optimality_setting.criterion) optimal_value = result.at(family_ref.dtmc.initial_states[0]) # Check whether the improvement was achieved if self._optimality_setting.is_improvement(optimal_value, self._optimal_value): # Set the new values of the optimal attributes self._optimal_value = optimal_value self._optimal_assignment = assignment # Construct the violation property according newly found optimal value self._construct_violation_property(family_ref, cex_generator) logger.debug(f"Optimal value improved to: {self._optimal_value}") return True
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_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 MRRP_by_pruned(mdp, targets, cap, SF): pruned_mdp = cmdp2n_mdp.naive_flattened_mdp(mdp, targets, cap, SF) print("mdp convert to pruned one successfully") pruned_storm_mdp = consmdp_to_storm_consmdp(pruned_mdp, pruned_mdp.new_targets) print( f"pruned------The number of states in the explicit MDP in Storm: {pruned_storm_mdp.nr_states}" ) print(f"transitions number:{pruned_storm_mdp.nr_transitions}") storm_result = stormpy.model_checking(pruned_storm_mdp, prop[0]) for i in pruned_mdp.states: ini_state = i[0] resource_level = i[1] print(f"-----MRRP for state: ({ini_state},{resource_level})-----") ind = pruned_mdp.states.index((ini_state, resource_level)) print("probability by pruned construction:", storm_result.at(ind)) # t5 = time.time() strategy_extract = strategy_extraction.StrategyExtract( pruned_mdp, storm_result) strategy_result = strategy_extract.prob_vector # t6 = time.time() # print("runtime(s) of extracting optimal policy:", t6 - t5) return pruned_mdp, storm_result, strategy_result
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 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 _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_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 MRP_by_quotient(pruned_mdp, SFR, SFRR): t1 = time.time() quotient_mdp = p_mdp2q_mdp.quotient_mdp(pruned_mdp, SFR, SFRR) print("pruned convert to quotient successfully") quotient_storm_mdp = consmdp_to_storm_consmdp(quotient_mdp, quotient_mdp.new_targets) print( f"quotient-----The number of states in the explicit MDP in Storm: {quotient_storm_mdp.nr_states}" ) print(f"transitions number:{quotient_storm_mdp.nr_transitions}") quotient_storm_result = stormpy.model_checking(quotient_storm_mdp, prop[0]) strategy_extract = strategy_extraction.StrategyExtract( quotient_mdp, quotient_storm_result) strategy_result = strategy_extract.extractOptAction() t2 = time.time() print( "runtime(s) of computing MRP by quotient flattened mdp and extracting optimal policy:", t2 - t1) return quotient_mdp, quotient_storm_result, strategy_result
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 MRP_by_naive(mdp, targets, cap, SF): t1 = time.time() naive_flattened_mdp = cmdp2n_mdp.naive_flattened_mdp(mdp, targets, cap, SF) print("cmdp convert to mdp successfully") storm_mdp = consmdp_to_storm_consmdp(naive_flattened_mdp, naive_flattened_mdp.new_targets) print( f"naive-------The number of states in the explicit MDP in Storm: {storm_mdp.nr_states}" ) print(f"transitions number:{storm_mdp.nr_transitions}") nstorm_result = stormpy.model_checking(storm_mdp, prop[0]) strategy_extract = strategy_extraction.StrategyExtract( naive_flattened_mdp, nstorm_result) strategy_result = strategy_extract.extractOptAction() t2 = time.time() print( "runtime(s) of computing MRP by naive flattened mdp and extracting optimal policy:", t2 - t1) return naive_flattened_mdp, nstorm_result, strategy_result
def example_parametric_models_01(): # Check support for parameters if not config.storm_with_pars: print("Support parameters is missing. Try building storm-pars.") return import stormpy.pars path = stormpy.examples.files.prism_pdtmc_die prism_program = stormpy.parse_prism_program(path) formula_str = "P=? [F s=7 & d=3]" properties = stormpy.parse_properties_for_prism_program(formula_str, prism_program) model = stormpy.build_parametric_model(prism_program, properties) print("Model supports parameters: {}".format(model.supports_parameters)) parameters = model.collect_probability_parameters() assert len(parameters) == 2 instantiator = stormpy.pars.PDtmcInstantiator(model) point = dict() for x in parameters: print(x.name) point[x] = stormpy.RationalRF(0.0) instantiated_model = instantiator.instantiate(point) result = stormpy.model_checking(instantiated_model, properties[0]) print(result)
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 compute_no_further_reward(self, index=0): assert self._expression_manager is not None assert self._formulae[index].is_reward_operator rew0_formula = self._formulae[index].clone() rew0_formula.set_optimality_type(stormpy.OptimizationDirection.Maximize) rew0_formula.set_bound(stormpy.ComparisonType.LEQ, self._expression_manager.create_integer(0)) result = stormpy.model_checking(self._model, rew0_formula, only_initial_states=False, extract_scheduler=True) return result