예제 #1
0
def get_model(drn_file, solver_type=None):
    logging.debug("Get DRN model for pid {}".format(os.getpid()))
    global MODEL, VARS, INITIAL_STATE, PROPERTY, ENV, LOAD_TIME
    time_start = time.time()
    MODEL = stormpy.build_parametric_model_from_drn(drn_file)
    INITIAL_STATE = MODEL.initial_states[0]
    VARS = build.get_parameters(MODEL)
    properties = stormpy.parse_properties("R=? [F \"stable\"]")
    assert (len(properties) == 1)
    PROPERTY = properties[0]
    ENV = stormpy.Environment()
    if solver_type is not None:
        ENV.solver_environment.set_linear_equation_solver_type(solver_type)
    logging.info(
        "Model loaded for pid {}: {} states and {} transitions.".format(
            os.getpid(), MODEL.nr_states, MODEL.nr_transitions))
    LOAD_TIME = time.time() - time_start
예제 #2
0
def example_getting_started_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)
    model = stormpy.build_model(prism_program, properties)

    print(model.model_type)

    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 {}, with probability {}, go to state {}".
                      format(state, transition.value(), transition.column))
예제 #3
0
def init_solver(threshold, model, env, program=None):
    """
    Initialize PLA solver with new threshold
    :param threshold: Threshold. Can be None.
    :param model: Model.
    :param env: Environment.
    :param program: Prism program (optional).
    :return: Tuple (solver, environment)
    """
    # Set formula with current threshold
    if threshold is None:
        prop = "R=? [F \"stable\"]"
    else:
        prop = "R<={} [F \"stable\"]".format(threshold)
    formulas = stormpy.parse_properties(prop, program)
    assert len(formulas) == 1
    formula = formulas[0]
    return stormpy.pars.create_region_checker(env, model, formula.raw_formula)
예제 #4
0
    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)
예제 #5
0
def expCTMC(infile, outfile):
    prop = "Pmin=? [F \"Fail\"]"

    model = stormpy.build_model_from_drn(infile)
    proper = stormpy.parse_properties(prop)
    initial_state = model.initial_states[0]
    model, proper = stormpy.eliminate_non_markovian_chains(
        model, proper, stormpy.EliminationLabelBehavior.DELETE_LABELS)
    result = stormpy.model_checking(model, proper[0], extract_scheduler=True)
    intermediate = model.apply_scheduler(result.scheduler)
    model, proper = stormpy.eliminate_non_markovian_chains(
        intermediate, proper, stormpy.EliminationLabelBehavior.DELETE_LABELS)
    stormpy.export_to_drn(model, outfile)
    exit(1)
    fin = open(infile, 'r')
    lst = []
    for line in fin:
        if "action 0" in line:
            continue
        else:
            lst.append(line)
    fin.close()
    fin = open(outfile, 'w+')

    for line in lst:
        fin.write(line)
    fin.close()

    if result.has_scheduler:

        scheduler = result.scheduler
        assert scheduler.memoryless
        assert scheduler.memory_size == 1
        assert scheduler.deterministic
        intermediate = model.apply_scheduler(scheduler)
        #assert intermediate.model_type == stormpy.ModelType.MA
        for state in intermediate.states:
            assert len(state.actions) == 1
        for state in intermediate.states:
            choice = scheduler.get_choice(state)
            action = choice.get_deterministic_choice()
            print("In state {} choose action {}".format(state, action))
    else:
        print("I am here")
예제 #6
0
def example_dfts_01():
    # Load from JSON
    path_json = stormpy.examples.files.dft_json_and
    dft_small = stormpy.dft.load_dft_json_file(path_json)
    print(dft_small)

    # Load from Galileo
    path = stormpy.examples.files.dft_galileo_hecs
    dft = stormpy.dft.load_dft_galileo_file(path)
    print("DFT with {} elements.".format(dft.nr_elements()))
    print("DFT has {} BEs and {} dynamic elements.".format(
        dft.nr_be(), dft.nr_dynamic()))

    # Analyze
    formula_str = "T=? [ F \"failed\" ]"
    formulas = stormpy.parse_properties(formula_str)
    results = stormpy.dft.analyze_dft(dft, [formulas[0].raw_formula])
    result = results[0]
    print(result)
예제 #7
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)))
예제 #8
0
def example_analysis_03():
    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_model(prism_program, properties)
    env = stormpy.Environment()
    env.solver_environment.set_linear_equation_solver_type(stormpy.EquationSolverType.native)
    env.solver_environment.native_solver_environment.method = stormpy.NativeLinearEquationSolverMethod.power_iteration
    env.solver_environment.native_solver_environment.maximum_iterations = 2
    result = stormpy.model_checking(model, properties[0], environment=env)
    print(result.at(model.initial_states[0]))

    dd_model = stormpy.build_symbolic_model(prism_program, properties)
    result = stormpy.model_checking(dd_model, properties[0], environment=env)
    filter = stormpy.create_filter_initial_states_symbolic(dd_model)
    result.filter(filter)
    assert result.min == result.max
    print(result.min)
예제 #9
0
    def test_transform_continuous_to_discrete_time_model_ctmc(self):
        ctmc = stormpy.build_model_from_drn(get_example_path(
            "ctmc", "dft.drn"))
        formulas = stormpy.parse_properties("T=? [ F \"failed\" ]")
        assert ctmc.nr_states == 16
        assert ctmc.nr_transitions == 33
        assert len(ctmc.initial_states) == 1
        initial_state = ctmc.initial_states[0]
        assert initial_state == 1
        result = stormpy.model_checking(ctmc, formulas[0])
        assert math.isclose(result.at(initial_state), 4.166666667)

        dtmc, dtmc_formulas = stormpy.transform_to_discrete_time_model(
            ctmc, formulas)
        assert dtmc.nr_states == 16
        assert dtmc.nr_transitions == 33
        assert len(dtmc.initial_states) == 1
        initial_state = dtmc.initial_states[0]
        assert initial_state == 1
        result = stormpy.model_checking(dtmc, dtmc_formulas[0])
        assert math.isclose(result.at(initial_state), 4.166666667)
예제 #10
0
def example_exploration_01():
    """
    Example to exploration of MDPs.
    :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)

    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))
예제 #11
0
def example_parametric_models_04():
    # 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=2]"
    properties = stormpy.parse_properties(formula_str, prism_program)
    model = stormpy.build_parametric_model(prism_program, properties)

    # Modify
    i = 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:
                if len(transition.value().gather_variables()) > 0:

                    new_var = pycarl.Variable("p{}".format(i))
                    i += 1
                    new_pol = stormpy.Polynomial(new_var)
                    pol_in_right_format = stormpy.FactorizedPolynomial(new_pol, transition.value().numerator.cache())

                    new_factorized_ratfunc = stormpy.FactorizedRationalFunction(pol_in_right_format)
                    transition.set_value(new_factorized_ratfunc)

    # Display
    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 {}, with probability {}, go to state {}".format(state, transition.value(), transition.column))
예제 #12
0
import time

import aev_examples.read_cmdp_data

from MRRP.MRRP_calculate import MRRP_by_pruned, MRRP_by_quotient

cmdp_info = aev_examples.read_cmdp_data.cmdp_info("AEV_601_cap80", 80)
mdp = cmdp_info.cmdp
cap = cmdp_info.cap
targets = cmdp_info.targets
SF = cmdp_info.SF
SFR = cmdp_info.SFR
SFRR = cmdp_info.SFRR

formula = 'Pmax=? [F "target" ]'
prop = stormpy.parse_properties(formula)


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:
예제 #13
0
 def __init__(self, dtmc_generator, _lambda=0.005):
     self.dtmc_generator = dtmc_generator
     self.formula = stormpy.parse_properties("P=? [ F (\"far\"  | \"collided\" | \"fallen\")]")
     self._lambda = _lambda
예제 #14
0
def test2():
    t0 = time.time()
    #path = "collision_partial_obs_2d_upd_hobs_20_small.prism"

    path, interval_path, formula_str, threshold = input_files()

    prism_program = stormpy.parse_prism_program(path)

    #formula_str = "P=? [!\"bad\" U \"goal\"]"
    #formula_str = "P=? [F \"goal\"]"
    #interval_path="collision_partial_obs_2d_upd_hobs_20_small.intervals"
    opts = stormpy.DirectEncodingParserOptions()
    opts.build_choice_labels = True
    properties = stormpy.parse_properties_for_prism_program(formula_str, prism_program)
    # construct the pPOMDP
    import inspect
    print(inspect.getfullargspec(stormpy.build_parametric_model))
    pomdp = stormpy.build_parametric_model(prism_program, properties)

    pomdp_parameters = pomdp.collect_probability_parameters()
    stormpy.export_parametric_to_drn(pomdp, "pomdp_ex")

    # make its representation canonic.
    pomdp = stormpy.pomdp.make_canonic(pomdp)
    #stormpy.export_parametric_to_drn(pomdp, "export_pomdp.drn")

    # construct the memory for the FSC
    # in this case, a selective counter with two states
    memory_builder = stormpy.pomdp.PomdpMemoryBuilder()
    memval=1
    memory = memory_builder.build(stormpy.pomdp.PomdpMemoryPattern.selective_counter, memval)
    # apply the memory onto the POMDP to get the cartesian product
    pomdp = stormpy.pomdp.unfold_memory(pomdp, memory)
    print("Number of pomdp states before simple:",pomdp.nr_states)
    print("Number of transitions: {}".format(pomdp.nr_transitions))

    # make the POMDP simple. This step is optional but often beneficial
    pomdp = stormpy.pomdp.make_simple(pomdp)
    print("Number of pomdp states after simple:",pomdp.nr_states)
    print("Number of transitions: {}".format(pomdp.nr_transitions))

    # apply the unknown FSC to obtain a pmc from the POMDP
    pmc = stormpy.pomdp.apply_unknown_fsc(pomdp, stormpy.pomdp.PomdpFscApplicationMode.simple_linear)
    print("Number of pomdp states after simple",pmc.nr_states)
    print("Number of transitions: {}".format(pmc.nr_transitions))
    print(pmc)
    print("applied pmc")
    path_pmc = "export_" + str(memval) + "_mem_" + path
    print(path_pmc)
    stormpy.export_parametric_to_drn(pmc, path_pmc)
    print("built model")
    #print(model.initial_states)
    fsc_parameters = pmc.collect_probability_parameters() - pomdp.collect_probability_parameters()
    print("number of pomdp parameters:",len(fsc_parameters))
    #print(fsc_parameters)
    #print(pomdp_parameters)
    #intervals2, polyhedrons = interval_parser.parse_input(interval_path)
    intervals, polyhedrons,items = interval_parser.parse_model_interval(pmc,pomdp_parameters,interval_path)
    #for item in intervals:
        #print(item,"printing in the main")
    #for item in items:
    #    print(item,"printing in the main")
    for p in polyhedrons:
        #print(p)
        p.compute_vertices()


    properties = stormpy.parse_properties(formula_str)
    print("Building model from {}".format(path))
    #parameters = model.collect_probability_parameters()
    print(pmc.nr_states)

    prob0E, prob1A = stormpy.prob01max_states(pmc, properties[0].raw_formula.subformula)
    #print(prob0E)
    #threshold = 0.90
    direction = "below"  # can be "below" or "above"
    options = QcqpOptions(mu=1e4, maxiter=10000, graph_epsilon=1e-2, silent=False)

    # result = solver.run(reward_model_name ,model_rew, parameters_rew, rew0, rew_threshold, direction, options)
    solver = QcqpSolver_affine_simple_fun()
    result = solver.run(pmc,  fsc_parameters, pomdp_parameters,properties, prob0E, prob1A, threshold, direction, options,intervals,items,True)
    print("number of iterations={}".format(solver.iterations))
    print("solver time={}".format(solver.solver_timer))

    #compute the policy against the robust interval
    #interval_path="collision_partial_obs_2d_upd_hobs_20_small.intervals"
    intervals, polyhedrons,items = interval_parser.parse_model_interval(pmc,pomdp_parameters,interval_path)
    regiondict = dict()
    for x in pomdp_parameters:
        for item in items:
            if item.name == x.name:
                regiondict[x] = (stormpy.RationalRF(item.lowerbound), stormpy.RationalRF(item.upperbound))
    region = stormpy.pars.ParameterRegion(regiondict)
    instantiator = stormpy.pars.PartialPDtmcInstantiator(pmc)
    instantiated_model = instantiator.instantiate(solver.solver_params)

    env = stormpy.Environment()
    env.solver_environment.set_linear_equation_solver_type(stormpy.EquationSolverType.eigen)
    env.solver_environment.native_solver_environment.method = stormpy.NativeLinearEquationSolverMethod.optimistic_value_iteration
    env.solver_environment.native_solver_environment.precision = stormpy.Rational('0.01')
    start_check = time.time()

    region_checker = stormpy.pars.create_region_checker(env, instantiated_model, properties[0].raw_formula,
                                                                        allow_model_simplification=False)

    print("region check")
    result = region_checker.get_bound_all_states(env, region, maximise=False)
    end_check = time.time()
    print("model checking ans:")
    ansval=result.at(pmc.initial_states[0])
    print(ansval)
    tend = time.time()
    print("Total time: ", str(tend - t0))
예제 #15
0
def test():

    #model path
    path = "pomdp_attempt_prob_36_sat_065_dist_5_obs_diff_orb_len_1.drn"
    #the specification string
    formula_str = "P=? [F \"goal\"]"
    # building options for stormpy
    opts = stormpy.DirectEncodingParserOptions()
    opts.build_choice_labels = True
    # build the model
    pomdp = stormpy.build_parametric_model_from_drn(path, opts)
    # path for the interval values
    interval_path="satellite_robust.intervals"
    # gather the interval parameters
    pomdp_parameters = pomdp.collect_probability_parameters()
    # make the pomdp canonic, which is required for simple transformation.
    pomdp = stormpy.pomdp.make_canonic(pomdp)
    # construct the memory for the FSC
    memory_builder = stormpy.pomdp.PomdpMemoryBuilder()
    #number of memory states in the FSC
    memval=1
    # build the memory
    memory = memory_builder.build(stormpy.pomdp.PomdpMemoryPattern.selective_counter, memval)
    # apply the memory onto the POMDP to get the cartesian product
    pomdp = stormpy.pomdp.unfold_memory(pomdp, memory)
    print("Number of pomdp states before simple:",pomdp.nr_states)
    print("Number of transitions: {}".format(pomdp.nr_transitions))

    # make the POMDP simple.
    pomdp = stormpy.pomdp.make_simple(pomdp)
    print("Number of pomdp states after simple:",pomdp.nr_states)
    print("Number of transitions: {}".format(pomdp.nr_transitions))

    # apply the unknown FSC to obtain a pmc from the POMDP
    pmc = stormpy.pomdp.apply_unknown_fsc(pomdp, stormpy.pomdp.PomdpFscApplicationMode.simple_linear)
    print("Number of pomdp states after simple",pmc.nr_states)
    print("Number of transitions: {}".format(pmc.nr_transitions))
    print("applied pmc")
    path_pmc = "export_" + str(memval) + "_mem_" + path
    #export the resulting model file
    stormpy.export_parametric_to_drn(pmc, path_pmc)
    print("built model")
    fsc_parameters = pmc.collect_probability_parameters() - pomdp.collect_probability_parameters()
    print("number of pomdp parameters:",len(fsc_parameters))

    # gather intervals and items from the model
    intervals, polyhedrons,items = interval_parser.parse_model_interval(pmc,pomdp_parameters,interval_path)

    for item in items:
        print(item,"printing in the main")

    properties = stormpy.parse_properties(formula_str)
    print("Building model from {}".format(path))
    print(pmc.nr_states)
    #compute prob01max states for any policy
    prob0E, prob1A = stormpy.prob01max_states(pmc, properties[0].raw_formula.subformula)
    #compute threshold
    threshold = 0.9999
    direction = "below"  # can be "below" or "above"
    # solver parameters
    options = QcqpOptions(mu=1e4, maxiter=1000, graph_epsilon=1e-2, silent=False)

    # run solver
    solver = QcqpSolver_affine_simple_fun()
    result = solver.run(pmc,  fsc_parameters, pomdp_parameters,properties, prob0E, prob1A, threshold, direction, options,intervals,items,True)

    print("number of iterations={}".format(solver.iterations))
    print("solver time={}".format(solver.solver_timer))

    print("solver time={}".format(solver.solver_timer))
예제 #16
0
 def test_parse_formula(self):
     formula = "P=? [F \"one\"]"
     properties = stormpy.parse_properties(formula)
     assert len(properties) == 1
     assert str(properties[0].raw_formula) == formula
def check(path_to_model, property_str):
    program = sp.parse_prism_program(path_to_model)
    props = sp.parse_properties(property_str, program)
    model = sp.build_model(program, props)
    result = sp.model_checking(model, props[0])
    return result.at(model.initial_states[0]) > 0.5
예제 #18
0
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)))

# print()

i=0
re = []
while(i < number):
	initial_state = model.states[i]
	# print("Expected number of steps to reach state 'found' from state: ", initial_state, " -> {}" .format(result.at(initial_state)))
	re.append(result.at(initial_state))
	i += 1
    def find_optimum(self, model_file, verbose=False):
        logging.info("Running PLA on single process")
        self.verbose = verbose
        result = Result(model_file, self.config)

        # Get initial regions by computing the roots
        time_roots_start = time.time()
        roots = analyse.gather_roots(self.model, self.vars)
        result.time_roots = time.time() - time_roots_start
        logging.info("Computing roots took {}s".format(result.time_roots))

        # Create initial intervals per parameter by splitting at roots
        initial_intervals = dict()
        for p in self.vars:
            initial_interval = []
            current = 0 + self.config.eps  # 0 and 1 change graph structure
            for root in roots[p]:
                initial_interval.append(Interval(current, root))
                current = root
            initial_interval.append(Interval(current, 1 - self.config.eps))
            initial_intervals[p] = initial_interval
        # Create initial regions
        initial_regions = []
        for product in itertools.product(*initial_intervals.values()):
            region = {
                p.name: interval
                for p, interval in zip(self.vars, product)
            }
            initial_regions.append(Region(region))

        if verbose:
            logging.debug("------------")
            for region in initial_regions:
                logging.debug("Initial region {}".format(region))
            logging.debug("------------")

        properties = stormpy.parse_properties("R=? [F \"stable\"]")
        assert len(properties) == 1
        property = properties[0]
        self.inst_checker = pla_helper.init_instantiation_checker(
            self.model, property, self.config.exact)

        # Find upper bound
        start_pla = time.time()
        logging.info("No. initial regions: {}".format(len(initial_regions)))
        upper_bound, best_sample = self.sample_points(self.vars,
                                                      self.config.no_samples)
        logging.info("Found upper bound {} for sample {}".format(
            upper_bound, best_sample))
        logging.debug("Time: {:.3f}s".format(time.time() - start_pla))

        if not self.config.exact:
            # Slightly increase upper bound to avoid precision issues
            upper_bound += 1e-4
        lower_bound = 0

        self.solver = pla_helper.init_solver(None, self.model, self.env)

        # Find optimum by iterating the following:
        # - use PLA (minimize) to obtain lower bounds
        # - discard all regions whose minimal result is greater than the current upper bound
        # - sample remaining regions to improve upper bound
        # - split remaining regions in half
        start_time_pla = time.time()
        regions = initial_regions
        iteration = 0
        if self.config.exact:
            precision = stormpy.Rational(self.config.precision)
        else:
            precision = self.config.precision
        while upper_bound - lower_bound > precision:
            iteration += 1
            if iteration == 1:
                # Use initial regions
                new_regions = regions
            else:
                # Split regions
                new_regions = []
                for region in regions:
                    # Split region into two
                    self.no_splits += 1
                    new_regions.extend(region.split(self.vars))

            regions, sample, lower_bound, upper_bound = self.compute_satisfying_regions(
                upper_bound, new_regions)
            iteration_time = time.time() - start_time_pla
            start_time_pla = time.time()
            logging.info(
                "Iteration {}: bounds: [{}, {}], best sample: {}, {} regions remaining, {} calls, {} splits, time: {:.3f}s"
                .format(iteration, lower_bound, upper_bound, best_sample,
                        len(regions), self.no_calls, self.no_splits,
                        iteration_time))
            if sample is not None:
                best_sample = sample

            if verbose:
                logging.debug("------------")
                if self.config.exact:
                    logging.debug(
                        "Current bounds: [{}, {}], precision: {}".format(
                            lower_bound, upper_bound,
                            upper_bound - lower_bound))
                else:
                    logging.debug(
                        "Current bounds: [{}, {}], precision: {:.1e}".format(
                            lower_bound, upper_bound,
                            upper_bound - lower_bound))
                logging.debug("Best sample: {}".format(best_sample))
                tmp = sort_regions(list(regions), self.vars)
                for region in tmp:
                    logging.debug("Region {}".format(region))
                logging.debug("Time: {:.3f}s".format(time.time() -
                                                     start_time_pla))
                logging.debug("------------")

        logging.info(
            "Remaining regions: {}, best sample: {}, {} calls, {} splits".
            format(len(regions), best_sample, self.no_calls, self.no_splits))

        end_pla = time.time()

        result.time_analysis = end_pla - start_pla
        result.result_ert = Interval(lower_bound, upper_bound)
        result.best_sample = best_sample
        result.result_region = sort_regions(regions, self.vars)
        return result
예제 #20
0
def unfolding(simulator,
              unfolder,
              trace_length,
              stats_file,
              deadline=None,
              terminate_on_deadline=True,
              use_ovi=False):
    """

    :param simulator: The simulator that spits out the observations
    :param tracker: The tracker that keeps track of the state estimation
    :param trace_length: How many steps to take.
    :param stats_file: The file to output all the statistics to.
    :param deadline: The deadline for every computation step
    :param terminate_on_deadline: Should we abort the trace if we exceeded computation time
    :param use_ovi: Whether to use OVI or not.
    :return:
    """
    #TODO make the stats_file optional
    observation, _ = simulator.restart()
    unfolder.reset(observation)
    env = stormpy.Environment()
    if use_ovi:
        env.solver_environment.minmax_solver_environment.method = stormpy.MinMaxMethod.optimistic_value_iteration
        env.solver_environment.minmax_solver_environment.precision = stormpy.Rational(
            "0.01")

    with open(stats_file, 'w') as file:
        writer = csv.writer(file)
        writer.writerow([
            "Index", "Observation", "Risk", "UnfTime", "McTime", "TotalTime",
            "MdpStates", "MdpTransitions", "TimedOut"
        ])
        for i in tqdm(range(trace_length)):
            observation, _ = simulator.random_step()
            start_time = time.monotonic()
            mdp = unfolder.extend(observation)
            end_time = time.monotonic()
            unfold_time = end_time - start_time
            #stormpy.export_to_drn(mdp,"unrolling.out")
            prop = sp.parse_properties("Pmax=? [F \"_goal\"]")[0]
            #
            #mdpl = stormpy.build_model_from_drn("unrolling.out")
            timeout = False
            start_time = time.monotonic()
            stormpy.reset_timeout()
            stormpy.set_timeout(int(deadline / 1000))
            stormpy.install_signal_handlers()
            try:
                result = stormpy.model_checking(mdp,
                                                prop,
                                                environment=env,
                                                only_initial_states=True)
                risk = result.at(0)
            except RuntimeError:
                timeout = True
            stormpy.reset_timeout()
            end_time = time.monotonic()
            mc_time = end_time - start_time
            total_time = unfold_time + mc_time
            if deadline and total_time * 1000 > deadline:
                timeout = True
            writer.writerow([
                i, observation, risk, unfold_time, mc_time, total_time,
                mdp.nr_states, mdp.nr_transitions, timeout
            ])
            file.flush()
            if terminate_on_deadline and timeout:
                return False
    return True