def custom_smm_example(smm, n_c=20, n_resample=100, min_rounds=10, max_rounds=500): """ Learning custom SMM. :param smm: stochastic Mealy machine to learn :param n_c: cutoff for a state to be considered complete :param n_resample: resampling size :param min_rounds: minimum number of learning rounds :param max_rounds: maximum number of learning rounds :return: learned SMM """ input_al = smm.get_input_alphabet() sul = StochasticMealySUL(smm) eq_oracle = UnseenOutputRandomWalkEqOracle(alphabet=input_al, sul=sul, num_steps=5000, reset_prob=0.2, reset_after_cex=True) learned_model = run_stochastic_Lstar(input_al, sul, eq_oracle, n_c=n_c, n_resample=n_resample, automaton_type='smm', min_rounds=min_rounds, max_rounds=max_rounds, print_level=3) return learned_model
def weird_coffee_machine_mdp_example(): """ Learning faulty coffee machine that can be found in Chapter 5 and Chapter 7 of Martin's Tappler PhD thesis. :return learned MDP """ mdp = get_weird_coffee_machine_MDP() input_alphabet = mdp.get_input_alphabet() sul = MdpSUL(mdp) eq_oracle = UnseenOutputRandomWalkEqOracle(input_alphabet, sul=sul, num_steps=4000, reset_prob=0.11, reset_after_cex=True) learned_mdp = run_stochastic_Lstar(input_alphabet, sul, eq_oracle, n_c=20, n_resample=1000, min_rounds=10, max_rounds=500, strategy='normal', cex_processing=None) return learned_mdp
def custom_stochastic_example(stochastic_machine, learning_type='smm', min_rounds=10, max_rounds=500): """ Learning custom SMM. :param stochastic_machine: stochastic Mealy machine or MDP to learn :param learning_type: 'smm' or 'mdp' :param min_rounds: minimum number of learning rounds :param max_rounds: maximum number of learning rounds :return: learned model """ from aalpy.SULs import MdpSUL, StochasticMealySUL from aalpy.automata import Mdp from aalpy.oracles import RandomWordEqOracle from aalpy.learning_algs import run_stochastic_Lstar input_al = stochastic_machine.get_input_alphabet() if isinstance(stochastic_machine, Mdp): sul = MdpSUL(stochastic_machine) else: sul = StochasticMealySUL(stochastic_machine) eq_oracle = RandomWordEqOracle(alphabet=input_al, sul=sul, num_walks=1000, min_walk_len=10, max_walk_len=30, reset_after_cex=True) learned_model = run_stochastic_Lstar(input_al, sul, eq_oracle, automaton_type=learning_type, min_rounds=min_rounds, max_rounds=max_rounds, print_level=2) return learned_model
def random_mdp_example(num_states, input_len, num_outputs, n_c=20, n_resample=1000, min_rounds=10, max_rounds=1000): """ Generate and learn random MDP. :param num_states: number of states in generated MDP :param input_len: size of input alphabet :param n_c: cutoff for a state to be considered complete :param n_resample: resampling size :param num_outputs: size of output alphabet :param min_rounds: minimum number of learning rounds :param max_rounds: maximum number of learning rounds :return: learned MDP """ from aalpy.SULs import MdpSUL from aalpy.oracles import RandomWalkEqOracle from aalpy.learning_algs import run_stochastic_Lstar from aalpy.utils import generate_random_mdp mdp, input_alphabet = generate_random_mdp(num_states, input_len, num_outputs) sul = MdpSUL(mdp) eq_oracle = RandomWalkEqOracle(input_alphabet, sul=sul, num_steps=5000, reset_prob=0.11, reset_after_cex=True) learned_mdp = run_stochastic_Lstar(input_alphabet, sul, eq_oracle, n_c=n_c, n_resample=n_resample, min_rounds=min_rounds, max_rounds=max_rounds) return learned_mdp
def test_learning_based_on_accuracy_based_stopping(self): example = 'first_grid' mdp = load_automaton_from_file(f'../DotModels/MDPs/{example}.dot', automaton_type='mdp') min_rounds = 10 max_rounds = 500 from aalpy.automata import StochasticMealyMachine from aalpy.utils import model_check_experiment, get_properties_file, \ get_correct_prop_values from aalpy.automata.StochasticMealyMachine import smm_to_mdp_conversion aalpy.paths.path_to_prism = "C:/Program Files/prism-4.6/bin/prism.bat" aalpy.paths.path_to_properties = "../Benchmarking/prism_eval_props/" stopping_based_on_prop = (get_properties_file(example), get_correct_prop_values(example), 0.02) input_alphabet = mdp.get_input_alphabet() automaton_type = ['mdp', 'smm'] similarity_strategy = ['classic', 'normal', 'chi2'] cex_processing = [None, 'longest_prefix'] samples_cex_strategy = [None, 'bfs', 'random:200:0.3'] for aut_type in automaton_type: for strategy in similarity_strategy: for cex in cex_processing: for sample_cex in samples_cex_strategy: sul = StochasticMealySUL(mdp) if aut_type == 'smm' else MdpSUL(mdp) eq_oracle = UnseenOutputRandomWalkEqOracle(input_alphabet, sul=sul, num_steps=200, reset_prob=0.25, reset_after_cex=True) learned_model = run_stochastic_Lstar(input_alphabet=input_alphabet, eq_oracle=eq_oracle, sul=sul, n_c=20, n_resample=1000, min_rounds=min_rounds, max_rounds=max_rounds, automaton_type=aut_type, strategy=strategy, cex_processing=cex, samples_cex_strategy=sample_cex, target_unambiguity=0.99, property_based_stopping=stopping_based_on_prop, print_level=0) if isinstance(learned_model, StochasticMealyMachine): mdp = smm_to_mdp_conversion(learned_model) else: mdp = learned_model results, diff = model_check_experiment(get_properties_file(example), get_correct_prop_values(example), mdp) for d in diff.values(): if d > stopping_based_on_prop[2]: assert False assert True
def benchmark_mdp_2_smm_example(example='first_grid', n_c=20, n_resample=1000, min_rounds=10, max_rounds=500, strategy='normal', cex_processing=None, reset_prob=0.125, samples_cex_strategy=None): """ Learning the stochastic Mealy Machine(SMM) various benchmarking examples found in Chapter 7 of Martin's Tappler PhD thesis. :param n_c: cutoff for a state to be considered complete :param n_resample: resampling size :param example: One of ['first_grid', 'second_grid', 'shared_coin', 'slot_machine'] :param min_rounds: minimum number of learning rounds :param max_rounds: maximum number of learning rounds :param strategy: normal or no_cq :param cex_processing: counterexample processing stategy :param reset_prob: reset probability for random walk eq oracle :param samples_cex_strategy: strategy to sample cex in the trace tree :return: learned SMM """ mdp = load_automaton_from_file(f'./DotModels/MDPs/{example}.dot', automaton_type='mdp') input_alphabet = mdp.get_input_alphabet() sul = StochasticMealySUL(mdp) eq_oracle = UnseenOutputRandomWalkEqOracle(input_alphabet, sul=sul, num_steps=5000, reset_prob=0.09, reset_after_cex=True) eq_oracle = UnseenOutputRandomWordEqOracle(input_alphabet, sul, num_walks=150, min_walk_len=5, max_walk_len=12, reset_after_cex=True) learned_mdp = run_stochastic_Lstar( input_alphabet=input_alphabet, eq_oracle=eq_oracle, sul=sul, n_c=n_c, n_resample=n_resample, min_rounds=min_rounds, max_rounds=max_rounds, automaton_type='smm', strategy=strategy, cex_processing=cex_processing, samples_cex_strategy=samples_cex_strategy) return learned_mdp
def benchmark_mdp_example(example='first_grid', n_c=20, n_resample=1000, min_rounds=10, max_rounds=500, strategy='normal'): """ Learning the MDP various benchmarking examples found in Chapter 7 of Martin's Tappler PhD thesis. :param example: One of ['first_grid', 'second_grid', 'shared_coin', 'slot_machine'] :param n_c: cutoff for a state to be considered complete :param n_resample: resampling size :param min_rounds: minimum number of learning rounds :param max_rounds: maximum number of learning rounds :param strategy: normal or no_cq :return: learned MDP """ mdp = load_automaton_from_file(f'./DotModels/MDPs/{example}.dot', automaton_type='mdp') input_alphabet = mdp.get_input_alphabet() output_alphabet = list({state.output for state in mdp.states}) sul = MdpSUL(mdp) eq_oracle = UnseenOutputRandomWalkEqOracle(input_alphabet, sul=sul, num_steps=5000, reset_prob=0.09, reset_after_cex=True) eq_oracle = UnseenOutputRandomWordEqOracle(input_alphabet, sul, num_walks=500, min_walk_len=10, max_walk_len=100, reset_after_cex=True) learned_mdp = run_stochastic_Lstar(input_alphabet=input_alphabet, sul=sul, eq_oracle=eq_oracle, n_c=n_c, n_resample=n_resample, min_rounds=min_rounds, max_rounds=max_rounds, strategy=strategy) return learned_mdp
def learn_stochastic_coffee_machine(visualize=False): sul = StochasticCoffeeMachineSUL() alphabet = ['coin', 'button'] eq_oracle = UnseenOutputRandomWordEqOracle(alphabet, sul, num_walks=100, min_walk_len=5, max_walk_len=10) learned_model = run_stochastic_Lstar(alphabet, sul, eq_oracle, automaton_type='smm') if visualize: visualize_automaton(learned_model, display_same_state_trans=True) return learned_model
def learn_stochastic_light_switch(visualize=False): sul = StochasticLightSUL() alphabet = ['press', 'release'] eq_oracle = UnseenOutputRandomWordEqOracle(alphabet, sul, num_walks=100, min_walk_len=3, max_walk_len=7) learned_model = run_stochastic_Lstar(alphabet, sul, eq_oracle, automaton_type='smm') if visualize: visualize_automaton(learned_model, display_same_state_trans=True) return learned_model
def benchmark_stochastic_example(example, automaton_type='smm', n_c=20, n_resample=1000, min_rounds=10, max_rounds=500, strategy='normal', cex_processing='longest_prefix', stopping_based_on_prop=None, samples_cex_strategy=None): """ Learning the stochastic Mealy Machine(SMM) various benchmarking examples found in Chapter 7 of Martin's Tappler PhD thesis. :param n_c: cutoff for a state to be considered complete :param automaton_type: either smm (stochastic mealy machine) or mdp (Markov decision process) :param n_resample: resampling size :param example: One of ['first_grid', 'second_grid', 'shared_coin', 'slot_machine'] :param min_rounds: minimum number of learning rounds :param max_rounds: maximum number of learning rounds :param strategy: normal, classic or chi2 :param cex_processing: counterexample processing strategy :stopping_based_on_prop: a tuple (path to properties, correct values, error bound) :param samples_cex_strategy: strategy to sample cex in the trace tree :return: learned SMM """ from aalpy.SULs import MdpSUL from aalpy.oracles import RandomWalkEqOracle, RandomWordEqOracle from aalpy.learning_algs import run_stochastic_Lstar from aalpy.utils import load_automaton_from_file # Specify the path to the dot file containing a MDP mdp = load_automaton_from_file(f'./DotModels/MDPs/{example}.dot', automaton_type='mdp') input_alphabet = mdp.get_input_alphabet() sul = MdpSUL(mdp) eq_oracle = RandomWordEqOracle(input_alphabet, sul, num_walks=100, min_walk_len=5, max_walk_len=15, reset_after_cex=True) eq_oracle = RandomWalkEqOracle(input_alphabet, sul=sul, num_steps=2000, reset_prob=0.25, reset_after_cex=True) learned_mdp = run_stochastic_Lstar(input_alphabet=input_alphabet, eq_oracle=eq_oracle, sul=sul, n_c=n_c, n_resample=n_resample, min_rounds=min_rounds, max_rounds=max_rounds, automaton_type=automaton_type, strategy=strategy, cex_processing=cex_processing, samples_cex_strategy=samples_cex_strategy, target_unambiguity=0.99, property_based_stopping=stopping_based_on_prop) return learned_mdp
def weird_coffee_machine_mdp_example(): """ Learning faulty coffee machine that can be found in Chapter 5 and Chapter 7 of Martin's Tappler PhD thesis. :return learned MDP """ from aalpy.SULs import MdpSUL from aalpy.oracles import RandomWordEqOracle from aalpy.learning_algs import run_stochastic_Lstar from aalpy.utils import get_weird_coffee_machine_MDP mdp = get_weird_coffee_machine_MDP() input_alphabet = mdp.get_input_alphabet() sul = MdpSUL(mdp) eq_oracle = RandomWordEqOracle(input_alphabet, sul=sul, num_walks=2000, min_walk_len=4, max_walk_len=10, reset_after_cex=True) learned_mdp = run_stochastic_Lstar(input_alphabet, sul, eq_oracle, n_c=20, n_resample=1000, min_rounds=10, max_rounds=500, strategy='normal', cex_processing=None, samples_cex_strategy=None, automaton_type='smm') return learned_mdp
def faulty_coffee_machine_mdp_example(automaton_type='mdp'): """ Learning faulty coffee machine that can be found in Chapter 5 and Chapter 7 of Martin's Tappler PhD thesis. :automaton_type either mdp or smm :return learned MDP """ from aalpy.SULs import MdpSUL from aalpy.oracles import RandomWalkEqOracle from aalpy.learning_algs import run_stochastic_Lstar from aalpy.utils import get_faulty_coffee_machine_MDP mdp = get_faulty_coffee_machine_MDP() input_alphabet = mdp.get_input_alphabet() sul = MdpSUL(mdp) eq_oracle = RandomWalkEqOracle(input_alphabet, sul=sul, num_steps=500, reset_prob=0.11, reset_after_cex=False) learned_mdp = run_stochastic_Lstar(input_alphabet, sul, automaton_type=automaton_type, eq_oracle=eq_oracle, n_c=20, n_resample=100, min_rounds=3, max_rounds=50, print_level=3, cex_processing='longest_prefix', samples_cex_strategy='bfs') return learned_mdp
mdp_sul = MdpSUL(original_mdp) eq_oracle = UnseenOutputRandomWalkEqOracle(input_alphabet, mdp_sul, num_steps=n_resample * (1 / 0.25), reset_after_cex=True, reset_prob=0.25) learned_mdp, data_mdp = run_stochastic_Lstar( input_alphabet, mdp_sul, eq_oracle, automaton_type='mdp', n_c=n_c, n_resample=n_resample, min_rounds=min_rounds, strategy=strategy, max_rounds=max_rounds, return_data=True, samples_cex_strategy="bfs") mdp_2_prism_format( learned_mdp, f'learned_mdp_{exp_name}', output_path=f'{benchmark_dir}/learned_mdp_{exp_name}.prism') mdp_sul.num_steps = 0 mdp_sul.num_queries = 0 learned_smm, data_smm = run_stochastic_Lstar(
eq_oracle = UnseenOutputRandomWordEqOracle( input_alphabet, mdp_sul, num_walks=150, min_walk_len=5, max_walk_len=15, reset_after_cex=True) learned_mdp, data_mdp = run_stochastic_Lstar( input_alphabet, mdp_sul, eq_oracle, automaton_type='mdp', n_c=n_c, n_resample=n_resample, min_rounds=min_rounds, strategy=strat, max_rounds=max_rounds, return_data=True, samples_cex_strategy=cex_stat, print_level=1, cex_processing=cex_proc, property_based_stopping=stopping_data) del mdp_sul del eq_oracle random.seed(seeds[seed]) mdp_sul = MdpSUL(original_mdp) eq_oracle = UnseenOutputRandomWordEqOracle( input_alphabet,
eq_oracle = UnseenOutputRandomWordEqOracle( input_alphabet, mdp_sul, num_walks=150, min_walk_len=5, max_walk_len=16, reset_after_cex=True) learned_mdp, data_mdp = run_stochastic_Lstar( input_alphabet, mdp_sul, eq_oracle, automaton_type='mdp', min_rounds=min_rounds, strategy=strat, max_rounds=max_rounds, return_data=True, samples_cex_strategy=cex_stat, print_level=1, cex_processing=cex_proc, target_unambiguity=0.99) del mdp_sul del eq_oracle random.seed(seeds[seed]) mdp_sul = MdpSUL(original_mdp) eq_oracle = UnseenOutputRandomWordEqOracle( input_alphabet, mdp_sul,
else: mdp_state.transitions[i].append( (moore_mdp_state_map[reached_state], correct_output, 1.)) mdp = StochasticMealyMachine(initial_mdp_state, list(moore_mdp_state_map.values())) return mdp mdp = to_smm() # mdp.visualize() # exit() # mdp.save(file_path='CC2640R2-no-feature-req-stochastic') # exit() # mdp.make_input_complete('self_loop') # mdp_sul = StochasticMealySUL(mdp) mdp_sul = MdpSUL(mdp.to_mdp()) eq_oracle = RandomWordEqOracle(alphabet, model_sul, num_walks=10000, min_walk_len=10, max_walk_len=100) stochastic_model = run_stochastic_Lstar(alphabet, mdp_sul, eq_oracle, automaton_type='mdp') # mdp = mdp.to_mdp() # mdp.save('CYW43455_stochastic')