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)
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_negative_values(self): prism_program = stormpy.parse_prism_program( get_example_path("dtmc", "negativevals.pm")) prism_program = stormpy.preprocess_symbolic_input( prism_program, [], "")[0].as_prism_program() simulator = stormpy.simulator.create_simulator(prism_program, seed=42) simulator.set_action_mode( stormpy.simulator.SimulatorActionMode.GLOBAL_NAMES) state, rew, labels = simulator.restart() assert state["s"] == -1 assert int(state["s"]) == -1
def test_build_parametric_dtmc_preprocess(self): program = stormpy.parse_prism_program( get_example_path("pdtmc", "herman5.pm")) formulas = stormpy.parse_properties_for_prism_program( "R=? [ F \"stable\" ]", program) trans_program, trans_formulas = stormpy.preprocess_symbolic_input( program, formulas, "") trans_prism = trans_program.as_prism_program() model = stormpy.build_parametric_model(trans_prism, trans_formulas) assert model.nr_states == 33 assert model.nr_transitions == 266 assert model.model_type == stormpy.ModelType.DTMC assert model.supports_parameters assert model.has_parameters assert type(model) is stormpy.SparseParametricDtmc
def example_simulator_03(): 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() simulator = stormpy.simulator.create_simulator(prism_program, seed=42) simulator.set_action_mode( stormpy.simulator.SimulatorActionMode.GLOBAL_NAMES) final_outcomes = dict() for n in range(5): while not simulator.is_done(): actions = simulator.available_actions() observation, reward, labels = simulator.step(actions[0]) if observation not in final_outcomes: final_outcomes[observation] = 1 else: final_outcomes[observation] += 1 simulator.restart()
def demo(model_name, constants): logging.basicConfig(filename='demo.log', level=logging.DEBUG) model = experiment_to_grid_model_names[model_name] constants = dict(item.split('=') for item in constants.split(",")) input = model(**constants) prism_program = sp.parse_prism_program(input.path) prop = sp.parse_properties_for_prism_program(input.properties[0], prism_program)[0] prism_program, props = sp.preprocess_symbolic_input(prism_program, [prop], input.constants) prop = props[0] prism_program = prism_program.as_prism_program() raw_formula = prop.raw_formula logger.info("Construct POMDP representation...") model = build_pomdp(prism_program, raw_formula) model = sp.pomdp.make_canonic(model) renderer = plotter.Plotter(prism_program, input.annotations, model) renderer.set_title("Demo") recorder = gridstorm.recorder.VideoRecorder(renderer, False) executor = SimulationExecutor(model, seed=42) executor.simulate(recorder) recorder.save(".", "demo")
def monitor(path, risk_property, constants, trace_length, options, verbose=False, simulator_seed=0, promptness_deadline=10000, model_id="no_id_given"): """ :param path: The path the the model file :param risk_property: The property that describes the risk :param constants: Values for constants that appear in the model. :param trace_length: How long should the traces be :param options: Options to configure the monitoring. :param verbose: Should we print :param simulator_seed: A range of seeds we use for the simulator. :param promptness_deadline: :param model_id: A name for creating good stats files. :return: """ start_time = time.monotonic() use_forward_filtering = isinstance(options, ForwardFilteringOptions) use_unfolding = isinstance(options, UnfoldingOptions) if not use_forward_filtering and not use_unfolding: raise RuntimeError("Unknown type of options, method cannot be deduced") assert not (use_forward_filtering and use_unfolding) logger.info("Parse MDP representation...") prism_program = sp.parse_prism_program(path) prop = sp.parse_properties_for_prism_program(risk_property, prism_program)[0] prism_program, props = sp.preprocess_symbolic_input( prism_program, [prop], constants) prop = props[0] prism_program = prism_program.as_prism_program() raw_formula = prop.raw_formula logger.info("Construct MDP representation...") model = build_model(prism_program, raw_formula, exact_arithmetic=options.exact_arithmetic) if (verbose): print(model) assert model.has_observation_valuations() logger.info("Compute risk per state") risk_assessment = analyse_model(model, prop).get_values() # The seed can be given as a single value or as a if isinstance(simulator_seed, Iterable): simulator_seed_range = simulator_seed else: simulator_seed_range = range(simulator_seed, simulator_seed + 1) logger.info("Initialize simulator...") simulator = sp.simulator.create_simulator(model) if use_forward_filtering: logger.info("Initialize tracker...") tracker = stormpy.pomdp.create_nondeterminstic_belief_tracker( model, promptness_deadline, promptness_deadline) tracker.set_risk(risk_assessment) if use_unfolding: expr_manager = stormpy.ExpressionManager() unfolder = stormpy.pomdp.create_observation_trace_unfolder( model, risk_assessment, expr_manager) initialize_time = time.monotonic() - start_time #stormpy.export_to_drn(model, "model.drn") stats_folder = f"stats/{model_id}-{options.method_id}/" if not os.path.isdir(stats_folder): os.makedirs(stats_folder) else: raise RuntimeWarning( f"We are writing to an existing folder '{stats_folder}'.") with open(os.path.join(stats_folder, "stats.out"), 'w') as file: file.write(f"states={model.nr_states}\n") file.write(f"transitions={model.nr_transitions}\n") file.write(f"init_time={initialize_time}\n") file.write(f"promptness_deadline={promptness_deadline}") for seed in tqdm(simulator_seed_range): simulator.set_seed(seed) logger.info("Restart simulator...") observation, _ = simulator.restart() stats_file = f"{stats_folder}/stats-{model_id}-{options.method_id}-{seed}.csv" if use_forward_filtering: filtering(simulator, tracker, trace_length, options.convex_hull_reduction, stats_file, deadline=promptness_deadline, verbose=verbose, observation_valuations=model.observation_valuations) if use_unfolding: unfolding(simulator, unfolder, trace_length, stats_file, use_ovi=not options.exact_arithmetic, deadline=promptness_deadline)