Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
def onfsm_mealy_paper_example():
    """
    Learning a ONFSM presented in 'Learning Finite State Models of Observable Nondeterministic Systems in a Testing
    Context'.
    :return: learned ONFSM
    """
    from random import seed
    seed(3)
    onfsm = get_benchmark_ONFSM()

    alph = onfsm.get_input_alphabet()

    sul = OnfsmSUL(onfsm)
    eq_oracle = UnseenOutputRandomWalkEqOracle(alph,
                                               sul,
                                               num_steps=5000,
                                               reset_prob=0.09,
                                               reset_after_cex=True)
    eq_oracle = UnseenOutputRandomWordEqOracle(alph,
                                               sul,
                                               num_walks=500,
                                               min_walk_len=4,
                                               max_walk_len=10)

    learned_onfsm = run_Lstar_ONFSM(alph,
                                    sul,
                                    eq_oracle,
                                    n_sampling=15,
                                    print_level=3)

    return learned_onfsm
Ejemplo n.º 4
0
def random_onfsm_example(num_states, input_size, output_size, n_sampling):
    """
    Generate and learn random ONFSM.
    :param num_states: number of states of the randomly generated automaton
    :param input_size: size of the input alphabet
    :param output_size: size of the output alphabet
    :param n_sampling: number of times each query will be repeated to ensure that all non-determinist outputs are
    observed
    :return: learned ONFSM
    """
    onfsm = generate_random_ONFSM(num_states=num_states,
                                  num_inputs=input_size,
                                  num_outputs=output_size)
    alphabet = onfsm.get_input_alphabet()

    sul = OnfsmSUL(onfsm)
    eq_oracle = UnseenOutputRandomWordEqOracle(alphabet,
                                               sul,
                                               num_walks=500,
                                               min_walk_len=10,
                                               max_walk_len=50)
    eq_oracle = UnseenOutputRandomWalkEqOracle(alphabet,
                                               sul,
                                               num_steps=5000,
                                               reset_prob=0.15,
                                               reset_after_cex=True)

    learned_model = run_Lstar_ONFSM(alphabet,
                                    sul,
                                    eq_oracle=eq_oracle,
                                    n_sampling=n_sampling)
    return learned_model
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
    def test_non_det(self):

        from aalpy.SULs import OnfsmSUL
        from aalpy.oracles import UnseenOutputRandomWordEqOracle, UnseenOutputRandomWalkEqOracle
        from aalpy.learning_algs import run_non_det_Lstar
        from aalpy.utils import get_benchmark_ONFSM

        onfsm = get_benchmark_ONFSM()
        alphabet = onfsm.get_input_alphabet()

        for _ in range(100):
            sul = OnfsmSUL(onfsm)

            oracle = UnseenOutputRandomWordEqOracle(alphabet,
                                                    sul,
                                                    num_walks=500,
                                                    min_walk_len=2,
                                                    max_walk_len=5)

            learned_onfsm = run_non_det_Lstar(alphabet,
                                              sul,
                                              oracle,
                                              n_sampling=50,
                                              print_level=0)

            eq_oracle = UnseenOutputRandomWalkEqOracle(alphabet,
                                                       sul,
                                                       num_steps=10000,
                                                       reset_prob=0.09,
                                                       reset_after_cex=True)

            cex = eq_oracle.find_cex(learned_onfsm)

            if cex or len(learned_onfsm.states) != len(onfsm.states):
                assert False
        assert True
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
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: nubmer 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
    """
    mdp, input_alphabet = generate_random_mdp(num_states, input_len,
                                              num_outputs)
    sul = MdpSUL(mdp)
    eq_oracle = UnseenOutputRandomWalkEqOracle(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