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_actions_mdp(self): model = stormpy.build_sparse_model_from_explicit( get_example_path("mdp", "two_dice.tra"), get_example_path("mdp", "two_dice.lab")) for state in model.states: assert len(state.actions) == 1 or len(state.actions) == 2 for action in state.actions: assert action.id == 0 or action.id == 1
def test_actions_dtmc(self): model = stormpy.build_sparse_model_from_explicit( get_example_path("dtmc", "die.tra"), get_example_path("dtmc", "die.lab")) for state in model.states: assert len(state.actions) == 1 for action in state.actions: assert action.id == 0
def test_parse_explicit_mdp(self): model = stormpy.build_sparse_model_from_explicit(get_example_path("mdp", "two_dice.tra"), get_example_path("mdp", "two_dice.lab")) assert model.nr_states == 169 assert model.nr_transitions == 436 assert model.model_type == stormpy.ModelType.MDP assert not model.supports_parameters assert type(model) is stormpy.SparseMdp
def test_parse_explicit_dtmc(self): model = stormpy.build_sparse_model_from_explicit(get_example_path("dtmc", "die.tra"), get_example_path("dtmc", "die.lab")) 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_transitions_mdp(self): model = stormpy.build_sparse_model_from_explicit( get_example_path("mdp", "two_dice.tra"), get_example_path("mdp", "two_dice.lab")) assert model.states[1].id == 1 for state in model.states: i = 0 for action in state.actions: i += 1 for transition in action.transitions: assert transition.value() == 0.5 or transition.value() == 1 assert i == 1 or i == 2
def test_labels_dtmc(self): model = stormpy.build_sparse_model_from_explicit( get_example_path("dtmc", "die.tra"), get_example_path("dtmc", "die.lab")) labelsOrig = [["init"], [], [], [], [], [], [], ["one", "done"], ["two", "done"], ["three", "done"], ["four", "done"], ["five", "done"], ["six", "done"]] i = 0 for state in model.states: labels = state.labels for label in labelsOrig[i]: assert label in labels i += 1
def test_matrix(self): model = stormpy.build_sparse_model_from_explicit( get_example_path("dtmc", "die.tra"), get_example_path("dtmc", "die.lab")) matrix = model.transition_matrix assert type(matrix) is stormpy.storage.SparseMatrix assert matrix.nr_rows == model.nr_states assert matrix.nr_columns == model.nr_states assert matrix.nr_entries == 20 assert matrix.nr_entries == model.nr_transitions for e in matrix: assert e.value() == 0.5 or e.value() == 0 or (e.value() == 1 and e.column > 6)
def test_states_dtmc(self): model = stormpy.build_sparse_model_from_explicit( get_example_path("dtmc", "die.tra"), get_example_path("dtmc", "die.lab")) i = 0 states = model.states assert len(states) == 13 for state in states: assert state.id == i i += 1 assert i == model.nr_states state = model.states[2] assert state.id == 2 state = states[5] assert state.id == 5
def test_states_mdp(self): model = stormpy.build_sparse_model_from_explicit( get_example_path("mdp", "two_dice.tra"), get_example_path("mdp", "two_dice.lab")) i = 0 assert len(model.states) == 169 for state in model.states: assert state.id == i i += 1 assert i == model.nr_states states = model.states state = model.states[6] assert state.id == 6 state = states[1] assert state.id == 1
def test_change_matrix(self): model = stormpy.build_sparse_model_from_explicit( get_example_path("dtmc", "die.tra"), get_example_path("dtmc", "die.lab")) matrix = model.transition_matrix for e in matrix: assert e.value() == 0.5 or e.value() == 0 or e.value() == 1 i = 0 for e in matrix: e.set_value(i) i += 0.1 i = 0 for e in matrix: assert e.value() == i i += 0.1
def test_change_matrix_modelchecking(self): import stormpy.logic model = stormpy.build_sparse_model_from_explicit( get_example_path("dtmc", "die.tra"), get_example_path("dtmc", "die.lab")) matrix = model.transition_matrix # Check matrix for e in matrix: assert e.value() == 0.5 or e.value() == 0 or e.value() == 1 # First model checking formulas = stormpy.parse_properties("P=? [ F \"one\" ]") result = stormpy.model_checking(model, formulas[0]) resValue = result.at(model.initial_states[0]) assert math.isclose(resValue, 0.16666666666666663) # Change probabilities i = 0 for e in matrix: if e.value() == 0.5: if i % 2 == 0: e.set_value(0.3) else: e.set_value(0.7) i += 1 for e in matrix: assert e.value() == 0.3 or e.value() == 0.7 or e.value( ) == 1 or e.value() == 0 # Second model checking result = stormpy.model_checking(model, formulas[0]) resValue = result.at(model.initial_states[0]) assert math.isclose(resValue, 0.06923076923076932) # Change probabilities again for state in model.states: for action in state.actions: for transition in action.transitions: if transition.value() == 0.3: transition.set_value(0.8) elif transition.value() == 0.7: transition.set_value(0.2) # Third model checking result = stormpy.model_checking(model, formulas[0]) resValue = result.at(model.initial_states[0]) assert math.isclose(resValue, 0.3555555555555556)
def compute_probabilities(self, base_dir='data/repair'): import stormpy import stormpy.logic goal_formula_str = "P=? [ F \"goal\" ]" goal_formula = stormpy.parse_properties(goal_formula_str) fallen_formula_str = "P=? [ F \"fallen\" ]" fallen_formula = stormpy.parse_properties(fallen_formula_str) far_formula_str = "P=? [ F \"far\" ]" far_formula = stormpy.parse_properties(far_formula_str) collided_formula_str = "P=? [ F \"collided\" ]" collided_formula = stormpy.parse_properties(collided_formula_str) total_formula_str = "P=? [ F (\"far\" | \"collided\" | \"fallen\")]" total_formula = stormpy.parse_properties(total_formula_str) self.save('temp', base_dir) #model = stormpy.parse_explicit_model(os.path.join(base_dir, 'temp.tra'), # os.path.join(base_dir, 'temp.lab')) model = stormpy.build_sparse_model_from_explicit( os.path.join(base_dir, 'temp.tra'), os.path.join(base_dir, 'temp.lab')) goal_prob = stormpy.model_checking(model, goal_formula[0]) goal_prob_Init = stormpy.ExplicitQuantitativeCheckResult.at( goal_prob, state_mapper.INITIAL_STATE) fallen_prob = stormpy.model_checking(model, fallen_formula[0]) fallen_prob_Init = stormpy.ExplicitQuantitativeCheckResult.at( fallen_prob, state_mapper.INITIAL_STATE) far_prob = stormpy.model_checking(model, far_formula[0]) far_prob_Init = stormpy.ExplicitQuantitativeCheckResult.at( far_prob, state_mapper.INITIAL_STATE) collided_prob = stormpy.model_checking(model, collided_formula[0]) collided_prob_Init = stormpy.ExplicitQuantitativeCheckResult.at( collided_prob, state_mapper.INITIAL_STATE) total_prob = stormpy.model_checking(model, total_formula[0]) total_prob_Init = stormpy.ExplicitQuantitativeCheckResult.at( total_prob, state_mapper.INITIAL_STATE) #print('At Init Probs: goal: {} , fallen: {} far: {} collided : {} total : {}'.format(goal_prob_Init,fallen_prob_Init,far_prob_Init,collided_prob_Init,total_prob_Init)) return { 'goal': goal_prob_Init, 'fallen': fallen_prob_Init, 'far': far_prob_Init, 'collided': collided_prob_Init, 'total': total_prob_Init }
def test_row_iterator(self): transitions_orig = [(0, 1, 0.5), (0, 2, 0.5), (1, 3, 0.5), (1, 4, 0.5), (2, 5, 0.5), (2, 6, 0.5), (3, 1, 0.5), (3, 7, 0.5), (4, 8, 0.5), (4, 9, 0.5), (5, 10, 0.5), (5, 11, 0.5), (6, 2, 0.5), (6, 12, 0.5), (7, 7, 1), (8, 8, 1), (9, 9, 1), (10, 10, 1), (11, 11, 1), (12, 12, 1)] model = stormpy.build_sparse_model_from_explicit( get_example_path("dtmc", "die.tra"), get_example_path("dtmc", "die.lab")) i = 0 for state in model.states: for transition in model.transition_matrix.row_iter( state.id, state.id): transition_orig = transitions_orig[i] i += 1 assert state.id == transition_orig[0] assert transition.column == transition_orig[1] assert transition.value() == transition_orig[2]
def test_transitions_dtmc(self): transitions_orig = [(0, 1, 0.5), (0, 2, 0.5), (1, 3, 0.5), (1, 4, 0.5), (2, 5, 0.5), (2, 6, 0.5), (3, 1, 0.5), (3, 7, 0.5), (4, 8, 0.5), (4, 9, 0.5), (5, 10, 0.5), (5, 11, 0.5), (6, 2, 0.5), (6, 12, 0.5), (7, 7, 1), (8, 8, 1), (9, 9, 1), (10, 10, 1), (11, 11, 1), (12, 12, 1)] model = stormpy.build_sparse_model_from_explicit( get_example_path("dtmc", "die.tra"), get_example_path("dtmc", "die.lab")) i = 0 for state in model.states: for action in state.actions: assert (state.id < 7 and len(action.transitions) == 2) or ( state.id >= 7 and len(action.transitions) == 1) for transition in action.transitions: transition_orig = transitions_orig[i] i += 1 assert state.id == transition_orig[0] assert transition.column == transition_orig[1] assert transition.value() == transition_orig[2]
def test_submatrix(self): model = stormpy.build_sparse_model_from_explicit( get_example_path("dtmc", "die.tra"), get_example_path("dtmc", "die.lab")) matrix = model.transition_matrix assert matrix.nr_rows == 13 assert matrix.nr_columns == 13 assert matrix.nr_entries == 20 assert matrix.nr_entries == model.nr_transitions for e in matrix: assert e.value() == 0.5 or e.value() == 0 or (e.value() == 1 and e.column > 6) row_constraint = stormpy.BitVector(13, [0, 1, 3, 4, 7, 8, 9]) submatrix = matrix.submatrix(row_constraint, row_constraint) assert submatrix.nr_rows == 7 assert submatrix.nr_columns == 7 assert submatrix.nr_entries == 10 for e in submatrix: assert e.value() == 0.5 or e.value() == 0 or (e.value() == 1 and e.column > 3)