Exemple #1
0
def build_pomdp(program, formula):
    options = sp.BuilderOptions([formula])
    options.set_build_state_valuations()
    options.set_build_choice_labels()
    options.set_build_all_labels()
    logger.debug("Start building the POMDP")
    return sp.build_sparse_model_with_options(program, options)
Exemple #2
0
    def test_explicit_builder(self):
        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_for_prism_program(formula_str, prism_program)

        # Fix variables in the program.
        module = prism_program.modules[0]
        s_var = module.get_integer_variable("s").expression_variable
        d_var = module.get_integer_variable("d").expression_variable

        # Construct the model
        options = stormpy.BuilderOptions([p.raw_formula for p in properties])
        options.set_build_state_valuations()
        model_builder = stormpy.make_sparse_model_builder(prism_program, options)
        model = model_builder.build()
        # and export the model from building
        state_mapping = model_builder.export_lookup()

        #lookup 1
        state = { s_var : prism_program.expression_manager.create_integer(3), d_var : prism_program.expression_manager.create_integer(0)}
        id = state_mapping.lookup(state)
        assert model.state_valuations.get_integer_value(id, s_var) == 3
        assert model.state_valuations.get_integer_value(id, d_var) == 0

        #lookup 2
        state = { s_var : prism_program.expression_manager.create_integer(7), d_var : prism_program.expression_manager.create_integer(3)}
        id = state_mapping.lookup(state)
        assert model.state_valuations.get_integer_value(id, s_var) == 7
        assert model.state_valuations.get_integer_value(id, d_var) == 3
def example_building_models_04():
    path = stormpy.examples.files.prism_mdp_firewire
    prism_program = stormpy.parse_prism_program(path)
    prism_program = stormpy.preprocess_symbolic_input(
        prism_program, [], "delay=10,fast=0.8")[0].as_prism_program()
    options = stormpy.BuilderOptions()
    options.set_build_state_valuations()
    options.set_build_all_reward_models(True)
    options.set_build_choice_labels(True)

    def permissive_policy(state_valuation, action_index):
        """
        Whether for the given state and action, the action should be allowed in the model.

        :param state_valuation:
        :param action_index:
        :return: True or False
        """
        action_name = prism_program.get_action_name(action_index)
        print(f"{state_valuation.to_json()}, {action_name}")
        # conditions on the action
        cond1 = action_name.startswith("snd")
        # conditions on the state (efficient)
        cond2 = state_valuation.get_integer_value(
            prism_program.get_module("node2").get_integer_variable(
                "x2").expression_variable) < 20
        # conditions on the json repr of the state (inefficient, string handling, etc)
        cond3 = json.loads(str(state_valuation.to_json()))["x1"] < 40
        return (cond1 or (cond2 and cond3))

    constructor = stormpy.make_sparse_model_builder(
        prism_program, options,
        stormpy.StateValuationFunctionActionMaskDouble(permissive_policy))
    model = constructor.build()
    print(model)
Exemple #4
0
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()]))
Exemple #5
0
    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_building_models_03():
    path = stormpy.examples.files.prism_pdtmc_brp
    prism_program = stormpy.parse_prism_program(path)
    formula_str = "P=? [F s=5]"
    properties = stormpy.parse_properties_for_prism_program(
        formula_str, prism_program)

    options = stormpy.BuilderOptions([p.raw_formula for p in properties])
    options.set_build_state_valuations()
    model = stormpy.build_sparse_parametric_model_with_options(
        prism_program, options)

    valuations = model.state_valuations
    values2 = json.loads(str(valuations.get_json(2)))
    print(values2)

    integer_variables = []
    for module in prism_program.modules:
        print("module {}".format(module.name))
        integer_variables += module.integer_variables

    print(", ".join([
        "{}: {}".format(
            str(iv.name),
            valuations.get_integer_value(2, iv.expression_variable))
        for iv in integer_variables
    ]))
Exemple #7
0
    def test_choice_origins(self):
        program, _ = stormpy.parse_jani_model(get_example_path("dtmc", "die.jani"))
        a = stormpy.FlatSet()

        options = stormpy.BuilderOptions()
        options.set_build_with_choice_origins()
        model = stormpy.build_sparse_model_with_options(program, options)
        a = model.choice_origins.get_edge_index_set(3)
Exemple #8
0
def build_pomdp(program):
    options = sp.BuilderOptions([])
    options.set_build_state_valuations()
    options.set_build_choice_labels()
    options.set_build_all_labels()
    options.set_build_all_reward_models()

    model = sp.build_sparse_model_with_options(program, options)
    model = sp.pomdp.make_canonic(model)
    return model
Exemple #9
0
def example_simulator_02():
    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, reward, labels = 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, reward, labels = 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))

    options = stormpy.BuilderOptions()
    options.set_build_state_valuations()
    options.set_build_choice_labels(True)
    model = stormpy.build_sparse_model_with_options(prism_program, options)
    print(model)
    simulator = stormpy.simulator.create_simulator(model, seed=42)
    simulator.set_observation_mode(stormpy.simulator.SimulatorObservationMode.PROGRAM_LEVEL)
    simulator.set_action_mode(stormpy.simulator.SimulatorActionMode.GLOBAL_NAMES)
    # 5 paths of at most 20 steps.
    paths = []
    for m in range(5):
        path = []
        state, reward, labels = 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, reward, labels = 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))
Exemple #10
0
 def test_label(self):
     program = stormpy.parse_prism_program(
         get_example_path("dtmc", "brp-16-2.pm"))
     properties = stormpy.parse_properties_for_prism_program(
         "P=? [ F s=5 ]", program)
     options = stormpy.BuilderOptions([p.raw_formula for p in properties])
     options.set_build_choice_labels(True)
     model = stormpy.build_sparse_model_with_options(program, options)
     clabeling = model.choice_labeling
     clabels = clabeling.get_labels()
     assert len(clabels) == 7
     assert "aB" in clabels
     assert "aA" in clabels
     assert "NewFile" in clabeling.get_labels_of_choice(0)
     assert "aG" in clabeling.get_labels_of_choice(7)
Exemple #11
0
    def initialize(cls, formulae):
        # builder options
        cls.builder_options = stormpy.BuilderOptions(formulae)
        cls.builder_options.set_build_with_choice_origins(True)
        cls.builder_options.set_build_state_valuations(True)
        cls.builder_options.set_add_overlapping_guards_label()

        # model checking environment
        cls.environment = stormpy.Environment()

        se = cls.environment.solver_environment

        se.set_linear_equation_solver_type(stormpy.EquationSolverType.gmmxx)
        # se.minmax_solver_environment.precision = stormpy.Rational(Property.mc_precision)
        # se.minmax_solver_environment.method = stormpy.MinMaxMethod.policy_iteration
        se.minmax_solver_environment.method = stormpy.MinMaxMethod.value_iteration
 def build_model(self):
     ## Different build functions depending on the model format.
     if self.file_type == ".prism" or self.file_type == ".jani":
         ## Defining build options to ensure that choice labels remain.
         options = stormpy.BuilderOptions()
         options.set_build_state_valuations()
         options.set_build_choice_labels()
         self.model = stormpy.build_sparse_model_with_options(
             self.program, options)
     elif self.file_type == ".drn":
         ## Defining build options to ensure that choice labels remain.
         parser_options = stormpy.DirectEncodingParserOptions()
         parser_options.build_choice_labels = True
         self.model = stormpy.build_model_from_drn(self.file_path,
                                                   parser_options)
     else:
         raise Exception("Model file type not supported to build models")
    def createHypothesis(self):
        print()
        print('Building hypothesis MRM...')
        RM = self.OT.build_hypothesis()
        print('Hypothesis MRM built !')
        self.buildProductAutomaton(
            RM)  # Write the prism file with the hypothesis

        program = stormpy.parse_prism_program(TMP_MODEL_PATH)
        properties = stormpy.parse_properties_for_prism_program(
            "Rmax=? [ LRA ]", program)
        options = stormpy.BuilderOptions(True,
                                         True)  #To keep rewards and labels
        self.h = stormpy.build_sparse_model_with_options(program, options)
        self.result_h = stormpy.model_checking(self.h,
                                               properties[0],
                                               extract_scheduler=True).at(0)
        self.scheduler = stormpy.model_checking(
            self.h, properties[0], extract_scheduler=True).scheduler
Exemple #14
0
def example_analysis_04():
    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)

    options = stormpy.BuilderOptions([p.raw_formula for p in properties])
    options.set_build_state_valuations()
    model = stormpy.build_sparse_model_with_options(prism_program, options)

    result = stormpy.model_checking(model, properties[0])

    # Print the model checking result for all states

    print("Model checking results:")
    for i in range(len(model.states)):
        print("\tstate #{}\t {}:\t {}".format(
            i, model.state_valuations.get_string(i), result.at(i)))
Exemple #15
0
    def build_model(self, jani_program, formulae, alt_formulae):
        if self._model:
            logger.warning("Rebuilding a model...")
        logger.info("Build model...")
        options = stormpy.BuilderOptions(formulae)
        options.set_build_with_choice_origins(True)

        options.set_add_overlapping_guards_label()
        self._formulae = formulae
        self._alt_formulae = alt_formulae
        self._expression_manager = jani_program.expression_manager
        result = stormpy.build_sparse_model_with_options(jani_program, options)
        logger.info(
            "done. Model has {} states, {} actions and {} transitions".format(
                result.nr_states, result.nr_choices, result.nr_transitions))
        self._model = result
        self._submodel = self._model
        self._print_overlapping_guards(self._model)
        return self._model
Exemple #16
0
def build_model(program, formula, exact_arithmetic):
    """
    Takes a model and a formula that describes the bad event.

    :param program: PRISM program
    :param formula: a PCTL specification for a bad event
    :param exact_arithmetic: Flag for using exact arithmetic.
    :return: A sparse MDP
    """
    options = sp.BuilderOptions([formula])
    options.set_build_state_valuations()
    options.set_build_observation_valuations()
    options.set_build_choice_labels()
    options.set_build_all_labels()
    logger.debug("Start building the MDP")
    if exact_arithmetic:
        return sp.build_sparse_exact_model_with_options(program, options)
    else:
        return sp.build_sparse_model_with_options(program, options)
Exemple #17
0
def main():
    path = stormpy.examples.files.prism_pomdp_maze
    prism_program = stormpy.parse_prism_program(path)

    options = stormpy.BuilderOptions()
    options.set_build_state_valuations()
    options.set_build_choice_labels()
    pomdp = stormpy.build_sparse_model_with_options(prism_program, options)

    pomdp = stormpy.pomdp.make_canonic(pomdp)
    memory_builder = stormpy.pomdp.PomdpMemoryBuilder()
    memory = memory_builder.build(
        stormpy.pomdp.PomdpMemoryPattern.selective_counter, 2)

    pomdp = stormpy.pomdp.unfold_memory(pomdp, memory,\
         add_memory_labels=True, keep_state_valuations=True)
    pomdp = stormpy.pomdp.make_simple(pomdp, keep_state_valuations=True)
    pmc = stormpy.pomdp.apply_unknown_fsc(
        pomdp, stormpy.pomdp.PomdpFscApplicationMode.simple_linear)

    stormpy.export_to_drn(pmc, "test.drn")
    test = stormpy.build_parametric_model_from_drn("test.drn")
Exemple #18
0
    def execute(self):
        to_add = [self.states[-1]]
        while len(to_add) > 0:
            next_add = to_add.pop(0)
            to_add += self.createNewMdp(next_add[0], next_add[1])
        self.createPrismModel()

        program = stormpy.parse_prism_program(TMP_MODEL_PATH)
        if self.mode == MODE_MAX:
            prop = 'Pmax=? [F "target"]'
        if self.mode == MODE_MIN:
            prop = 'Rmin=? [F "target"]'
        properties = stormpy.parse_properties_for_prism_program(prop, program)
        options = stormpy.BuilderOptions(True,
                                         True)  #To keep rewards and labels
        built_model = stormpy.build_sparse_model_with_options(program, options)

        #compute prop---------------------------------------------------------------
        result = stormpy.model_checking(built_model,
                                        properties[0],
                                        extract_scheduler=True)

        #extract scheduler----------------------------------------------------------
        scheduler = result.scheduler
        #print(scheduler)
        self.scheduler_nrmdp = []
        for i in range(len(self.model.map.states)):
            self.scheduler_nrmdp.append([])
            for j in range(len(self.observations) + 1):
                self.scheduler_nrmdp[-1].append(-1)
        for state in built_model.states:
            if not "sink" in state.labels:
                state_lbl = self.getStateInNRMDP(state.labels)
                self.scheduler_nrmdp[state_lbl[0]][
                    state_lbl[1]] = scheduler.get_choice(
                        state).get_deterministic_choice()
Exemple #19
0
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
    from pycarl.formula import FormulaType, Relation
    if stormpy.info.storm_ratfunc_use_cln():
        import pycarl.cln.formula
    else:
        import pycarl.gmp.formula

    # Prevent curious side effects from earlier runs (for tests only)
    pycarl.clear_pools()
    # ###
    # # How to apply an unknown FSC to obtain a pMC from a POMDP
    # path = stormpy.examples.files.prism_pomdp_maze
    # prism_program = stormpy.parse_prism_program(path)
    #
    # formula_str = "P=? [!\"bad\" U \"goal\"]"
    # properties = stormpy.parse_properties_for_prism_program(formula_str, prism_program)
    # # construct the POMDP
    # pomdp = stormpy.build_model(prism_program, properties)
    # # make its representation canonic.
    # pomdp = stormpy.pomdp.make_canonic(pomdp)
    # # make the POMDP simple. This step is optional but often beneficial
    # pomdp = stormpy.pomdp.make_simple(pomdp)
    # # construct the memory for the FSC
    # # in this case, a selective counter with two states
    # memory_builder = stormpy.pomdp.PomdpMemoryBuilder()
    # memory = memory_builder.build(stormpy.pomdp.PomdpMemoryPattern.selective_counter, 2)
    # # apply the memory onto the POMDP to get the cartesian product
    # pomdp = stormpy.pomdp.unfold_memory(pomdp, memory)
    # # apply the memory onto the POMDP to get the cartesian product
    # pmc = stormpy.pomdp.apply_unknown_fsc(pomdp, stormpy.pomdp.PomdpFscApplicationMode.simple_linear)

    ####
    # How to apply an unknown FSC to obtain a pMC from a pPOMDP
    path = stormpy.examples.files.prism_par_pomdp_maze
    prism_program = stormpy.parse_prism_program(path)

    formula_str = "P=? [!\"bad\" U \"goal\"]"
    properties = stormpy.parse_properties_for_prism_program(formula_str, prism_program)
    # construct the pPOMDP
    options = stormpy.BuilderOptions([p.raw_formula for p in properties])
    options.set_build_state_valuations()
    options.set_build_choice_labels()
    pomdp = stormpy.build_sparse_parametric_model_with_options(prism_program, options)
    # make its representation canonic.
    pomdp = stormpy.pomdp.make_canonic(pomdp)

    # construct the memory for the FSC
    # in this case, a selective counter with two states
    memory_builder = stormpy.pomdp.PomdpMemoryBuilder()
    memory = memory_builder.build(stormpy.pomdp.PomdpMemoryPattern.selective_counter, 3)
    # apply the memory onto the POMDP to get the cartesian product
    pomdp = stormpy.pomdp.unfold_memory(pomdp, memory, add_memory_labels=True, keep_state_valuations=True)
    # make the POMDP simple. This step is optional but often beneficial
    pomdp = stormpy.pomdp.make_simple(pomdp, keep_state_valuations=True)
    # apply the unknown FSC to obtain a pmc from the POMDP
    pmc = stormpy.pomdp.apply_unknown_fsc(pomdp, stormpy.pomdp.PomdpFscApplicationMode.simple_linear)

    export_pmc = False # Set to True to export the pMC as drn.
    if export_pmc:
        export_options = stormpy.core.DirectEncodingOptions()
        export_options.allow_placeholders = False
        stormpy.export_to_drn(pmc, "test.out", export_options)
Exemple #20
0
def prism_to_consmdp(filename,
                     constants=None,
                     state_valuations=True,
                     action_labels=True,
                     return_targets=False):
    """
    Build a ConsMDP from a PRISM symbolic description using Stormpy.

    The model must specify `consumption` reward on each action (choice) and
    it needs to contain `reload` label.

    The following code sets the consumption of each action to `1` and marks
    each state where the variable `rel` is equal to `1` as a reloading state.

    >>> rewards "consumption"
    >>>   [] true: 1;
    >>>	endrewards
    >>> label "reload" = (rel=1);

    The dict `constants` must be given if a parametric prism model is to be
    read. It must defined all unused constants of the parametric model that
    affect the model's state space. On the other hand, it must not be defined
    if the model is not parametric. The format of the dictionary is
    `{ "constant_name" : constant_value }` where constant value is either an
    integer or a string that contains a name of other constant.

    :param filename: Path to the PRISM model. Must be an mdp.

    :param constants: Dictionary for uninitialized constant initialization.
    :type: constants: dict[str(constant_names) -> int/str(constant_names)]

    :param state_valuations: If True (default), set the valuation of states as
    names in the resulting ConsMDP.
    :param action_labels: If True (default), copies the choice labels in the
    PRISM model into the ConsMDP as action labels.

    :param return_targets: If True (default False), return also the list of
    states labeled by the label `target`.

    :return: ConsMDP object for the given model, or
             `ConsMDP, targets` if `return_targets`
    """
    prism_prog = stormpy.parse_prism_program(filename)

    if constants is None:
        constants = {}
    elif not prism_prog.has_undefined_constants and len(constants) > 0:
        raise ValueError("There are no constants to be defined")

    prism_constants = {}
    man = prism_prog.expression_manager
    for const, value in constants.items():
        var = man.get_variable(const)
        if type(value) == int:
            expression = man.create_integer(value)
        elif type(value) == str:
            expression = man.get_variable(value).get_expression()
        # elif type(value) == stormpy.storage.Expression:
        #     expression = value
        else:
            raise ValueError("Constants values must be either int, "
                             "str (a name of another constant), or "
                             "a stormpy Expression.")
        prism_constants[var] = expression

    prism_prog = prism_prog.define_constants(prism_constants)

    options = stormpy.BuilderOptions()
    if state_valuations:
        options.set_build_state_valuations()
    if action_labels:
        options.set_build_choice_labels()

    model = stormpy.build_sparse_model_with_options(prism_prog, options)

    res = storm_sparsemdp_to_consmdp(model,
                                     state_valuations=state_valuations,
                                     action_labels=action_labels,
                                     return_targets=return_targets)

    return res
Exemple #21
0
import stormpy
import stormpy.info
import matplotlib.pyplot as plt
import numpy as np

prism_program = stormpy.parse_prism_program("simp.nm")
print("\n")
print(prism_program.model_type)

options = stormpy.BuilderOptions(True, True)
options.set_build_state_valuations()
options.set_build_choice_labels()
model = stormpy.stormpy.build_sparse_model_with_options(prism_program, options)

choice_labels = model.choice_labeling

number = model.nr_states
print("Number of states: {}" .format(model.nr_states))
print("Number of transitions: {}" .format(model.nr_transitions))
print("Labels: {}" .format(model.labeling.get_labels()))
print()

formula_str = 'R{\"states\"}min=? [F \"found\"]'
#formula_str = 'Pmax=? [F \"found\"]'
properties = stormpy.parse_properties(formula_str, prism_program)
#model checking
result = stormpy.model_checking(model, properties[0])

initial_state = model.states[0]
# print("Expected number of steps to reach state from initial state: ", initial_state, " -> {}" .format(result.at(initial_state)))