コード例 #1
0
ファイル: Examples.py プロジェクト: DES-Lab/AALpy
def learn_python_class():
    """
    Learn a Mealy machine where inputs are methods and arguments of the class that serves as SUL.
    :return: Mealy machine
    """

    # class
    from aalpy.SULs import PyClassSUL, FunctionDecorator
    from aalpy.oracles import StatePrefixEqOracle
    from aalpy.learning_algs import run_Lstar
    from aalpy.utils import MockMqttExample, visualize_automaton

    mqtt = MockMqttExample

    input_al = [FunctionDecorator(mqtt.connect), FunctionDecorator(mqtt.disconnect),
                FunctionDecorator(mqtt.subscribe, 'topic'), FunctionDecorator(mqtt.unsubscribe, 'topic'),
                FunctionDecorator(mqtt.publish, 'topic')]

    sul = PyClassSUL(mqtt)

    eq_oracle = StatePrefixEqOracle(input_al, sul, walks_per_state=20, walk_len=20)

    mealy = run_Lstar(input_al, sul, eq_oracle=eq_oracle, automaton_type='mealy', cache_and_non_det_check=True)

    visualize_automaton(mealy)
コード例 #2
0
def train_and_extract_tomita(tomita_grammar,
                             acc_stop=1.,
                             loss_stop=0.005,
                             load=False):
    tomita_alphabet = ["0", "1"]

    if not load:
        rnn = train_RNN_on_tomita_grammar(tomita_grammar,
                                          acc_stop=acc_stop,
                                          loss_stop=loss_stop)
    else:
        rnn = train_RNN_on_tomita_grammar(tomita_grammar, train=False)
        rnn.load(f"RNN_Models/tomita_{tomita_grammar}.model")

    sul = RnnBinarySUL(rnn)
    alphabet = tomita_alphabet

    state_eq_oracle = StatePrefixEqOracle(alphabet,
                                          sul,
                                          walks_per_state=1000,
                                          walk_len=5)

    dfa = run_Lstar(alphabet=alphabet,
                    sul=sul,
                    eq_oracle=state_eq_oracle,
                    automaton_type='dfa',
                    cache_and_non_det_check=True)

    save_automaton_to_file(dfa,
                           f'LearnedAutomata/learned_tomita{tomita_grammar}')
    visualize_automaton(dfa)
コード例 #3
0
def learn_diff_drive_robot(visualize=False):
    all_faults = [
        'left_faster', 'left_slower', 'left_stuck', 'right_faster',
        'right_slower', 'right_stuck'
    ]

    wheel_inputs = [(0, 0), (0, 2), (2, 0), (2, 2), (0, -2), (2, -2), (-2, 0),
                    (-2, 2), (-2, -2)]

    alphabet = list(wheel_inputs)
    alphabet.extend(all_faults)

    sul = StrongFaultRobot(upper_speed_limit=10)

    eq_oracle = RandomWMethodEqOracle(alphabet,
                                      sul,
                                      walks_per_state=20,
                                      walk_len=15)

    learned_model = run_Lstar(alphabet, sul, eq_oracle, automaton_type='mealy')

    if visualize:
        visualize_automaton(learned_model, display_same_state_trans=False)

    return learned_model
コード例 #4
0
def learn_vending_machine(visualize=False):
    sul = VendingMachineSUL()
    alphabet = sul.alphabet

    eq_oracle = RandomWMethodEqOracle(alphabet,
                                      sul,
                                      walks_per_state=50,
                                      walk_len=20)

    learned_model = run_Lstar(alphabet, sul, eq_oracle, automaton_type='mealy')

    # Example of a error
    sul = MealySUL(learned_model)
    print(
        sul.query((
            'add_coin_0.2',
            'add_coin_0.5',
            'add_coin_0.2',
            'add_coin_0.2',
            'add_coin_0.2',
            'add_coin_0.2',
        )))

    if visualize:
        visualize_automaton(learned_model, display_same_state_trans=False)

    return learned_model
コード例 #5
0
 def visualize(self,
               path='LearnedModel',
               file_type='pdf',
               display_same_state_transitions=True):
     from aalpy.utils import visualize_automaton
     visualize_automaton(self, path, file_type,
                         display_same_state_transitions)
コード例 #6
0
    def find_cex(self, hypothesis):

        self.curr_hypothesis += 1
        inputs = []
        visualize_automaton(hypothesis,
                            path=f'Hypothesis_{self.curr_hypothesis}')
        while True:
            inp = input('Please provide an input: ')
            if inp == 'print alphabet':
                print(self.alphabet)
                continue
            if inp == 'current inputs':
                print(inputs)
                continue
            if inp == 'cex':
                if inputs:
                    return inputs
            if inp == 'end':
                return None
            if inp == 'reset':
                inputs.clear()
                hypothesis.reset_to_initial()
                print(
                    'You are back in the initial state. Please provide an input: '
                )
                continue
            if inp not in self.alphabet:
                print("Provided input is not in the input alphabet.")
                continue
            inputs.append(inp)
            out = hypothesis.step(inp)
            print('Output:', out)
コード例 #7
0
ファイル: Examples.py プロジェクト: DES-Lab/AALpy
def alergia_smm_example():
    from aalpy.SULs import StochasticMealySUL
    from random import randint, choice
    from aalpy.learning_algs import run_Alergia
    from aalpy.utils import visualize_automaton, generate_random_smm

    smm = generate_random_smm(5, 2, 5)
    visualize_automaton(smm, path='Original')
    sul = StochasticMealySUL(smm)
    inputs = smm.get_input_alphabet()

    data = []
    for _ in range(100000):
        str_len = randint(5, 15)
        sul.pre()
        seq = []
        for _ in range(str_len):
            i = choice(inputs)
            o = sul.step(i)
            seq.append((i, o))
        sul.post()
        data.append(seq)

    # run alergia with the data and automaton_type set to 'mdp' to True to learn a MDP
    model = run_Alergia(data, automaton_type='smm', eps=0.005, print_info=True)

    visualize_automaton(model)
    return model
コード例 #8
0
ファイル: Examples.py プロジェクト: DES-Lab/AALpy
def learn_date_validator():
    from aalpy.base import SUL
    from aalpy.utils import visualize_automaton, DateValidator
    from aalpy.oracles import StatePrefixEqOracle
    from aalpy.learning_algs import run_Lstar

    class DateSUL(SUL):
        """
        An example implementation of a system under learning that
        can be used to learn the behavior of the date validator.
        """

        def __init__(self):
            super().__init__()
            # DateValidator is a black-box class used for date string verification
            # The format of the dates is %d/%m/%Y'
            # Its method is_date_accepted returns True if date is accepted, False otherwise
            self.dv = DateValidator()
            self.string = ""

        def pre(self):
            # reset the string used for testing
            self.string = ""
            pass

        def post(self):
            pass

        def step(self, letter):
            # add the input to the current string
            if letter is not None:
                self.string += str(letter)

            # test if the current sting is accepted
            return self.dv.is_date_accepted(self.string)

    # instantiate the SUL
    sul = DateSUL()

    # define the input alphabet
    alphabet = list(range(0, 9)) + ['/']

    # define a equivalence oracle

    eq_oracle = StatePrefixEqOracle(alphabet, sul, walks_per_state=500, walk_len=15)

    # run the learning algorithm

    learned_model = run_Lstar(alphabet, sul, eq_oracle, automaton_type='dfa')
    # visualize the automaton
    visualize_automaton(learned_model)
コード例 #9
0
ファイル: Examples.py プロジェクト: DES-Lab/AALpy
def jAlergiaExample():
    from aalpy.learning_algs import run_JAlergia
    from aalpy.utils import visualize_automaton

    # if you need more heap space check
    model = run_JAlergia(path_to_data_file='jAlergia/exampleMdpData.txt', automaton_type='mdp', eps=0.005,
                         path_to_jAlergia_jar='jAlergia/alergia.jar', optimize_for='memory')

    # # alternatively pass the data in following form
    # mc_data = [[1,2,3,4,5], [1,2,3,4,2,1], [1,3,5,2,3]]
    # mdp_data = [[1,2,3,1,2], [1,3,6,4,2]]
    # model = run_JAlergia(path_to_data_file=mc_data, automaton_type='mdp', eps=0.005,
    #                      path_to_jAlergia_jar='jAlergia/alergia.jar', optimize_for='memory')

    visualize_automaton(model)
    return model
コード例 #10
0
def learn_light_switch(visualize=False):
    alphabet = ['press', 'increase_delay', 'fix_delay']  # 'fix_delay'

    sul = LightSwitchSUL()

    eq_oracle = RandomWMethodEqOracle(alphabet,
                                      sul,
                                      walks_per_state=20,
                                      walk_len=15)

    learned_model = run_Lstar(alphabet, sul, eq_oracle, automaton_type='moore')

    if visualize:
        visualize_automaton(learned_model, display_same_state_trans=False)

    return learned_model
コード例 #11
0
ファイル: UserInputEqOracle.py プロジェクト: haubitzer/AALpy
    def find_cex(self, hypothesis):

        self.reset_hyp_and_sul(hypothesis)

        self.curr_hypothesis += 1
        inputs = []
        visualize_automaton(hypothesis,
                            path=f'Hypothesis_{self.curr_hypothesis}')
        while True:
            inp = input('Please provide an input: ')
            if inp == 'help':
                print(
                    'Use one of following commands [print alphabet, current inputs, cex, end, reset] '
                    'or provide an input')
                continue
            if inp == 'print alphabet':
                print(self.alphabet)
                continue
            if inp == 'current inputs':
                print(inputs)
                continue
            if inp == 'cex':
                if inputs:
                    self.sul.post()
                    return inputs
            if inp == 'end':
                return None
            if inp == 'reset':
                inputs.clear()
                self.reset_hyp_and_sul(hypothesis)
                print(
                    'You are back in the initial state. Please provide an input: '
                )
                continue
            if inp not in self.alphabet:
                print("Provided input is not in the input alphabet.")
                continue
            inputs.append(inp)
            self.num_steps += 1
            out_hyp = hypothesis.step(inp)
            out_sul = self.sul.step(inp)
            print('Hypothesis Output :', out_hyp)
            print('SUL Output        :', out_sul)
            if out_hyp != out_sul:
                print(
                    'Counterexample found.\nIf you want to return it, type \'cex\'.'
                )
コード例 #12
0
def learn_gearbox(visualize=False):
    alphabet = [
        'press_clutch', 'release_clutch', 'put_in_reverse', 'increase_gear',
        'decrease_gear'
    ]

    sul = GearBoxSUL()

    eq_oracle = RandomWMethodEqOracle(alphabet,
                                      sul,
                                      walks_per_state=2000,
                                      walk_len=15)

    learned_model = run_Lstar(alphabet, sul, eq_oracle, automaton_type='mealy')

    if visualize:
        visualize_automaton(learned_model, display_same_state_trans=False)

    return learned_model
コード例 #13
0
def learn_wind_turbine(visualize=False):
    alphabet = [
        'increase_speed', 'stop_turbine', 'unexpected_speed_increase',
        'unexpected_slow_down'
    ]

    sul = TurbineSUL()

    eq_oracle = RandomWMethodEqOracle(alphabet,
                                      sul,
                                      walks_per_state=20,
                                      walk_len=15)

    learned_model = run_Lstar(alphabet, sul, eq_oracle, automaton_type='mealy')

    if visualize:
        visualize_automaton(learned_model, display_same_state_trans=False)

    return learned_model
コード例 #14
0
def learn_coffee_machine_mbd(visualize=False):
    sul = FaultInjectedCoffeeMachineSUL()
    alphabet = ['coin', 'button', 'coin_double_value', 'button_no_effect']

    eq_oracle = RandomWMethodEqOracle(alphabet,
                                      sul,
                                      walks_per_state=5000,
                                      walk_len=20)

    learned_model = run_Lstar(alphabet,
                              sul,
                              eq_oracle,
                              automaton_type='mealy',
                              cache_and_non_det_check=False)

    if visualize:
        visualize_automaton(learned_model, display_same_state_trans=True)

    return learned_model
コード例 #15
0
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
コード例 #16
0
def learn_language_of_coffee_machine_error(visualize=False):
    sul = FaultyCoffeeMachineSULDFA()
    alphabet = ['coin', 'button']

    eq_oracle = RandomWMethodEqOracle(alphabet,
                                      sul,
                                      walks_per_state=5000,
                                      walk_len=20)

    learned_model = run_Lstar(alphabet,
                              sul,
                              eq_oracle,
                              automaton_type='dfa',
                              cache_and_non_det_check=True)

    if visualize:
        visualize_automaton(learned_model, display_same_state_trans=True)

    return learned_model
コード例 #17
0
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
コード例 #18
0
ファイル: Examples.py プロジェクト: DES-Lab/AALpy
def alergia_mc_example():
    from os import remove
    from aalpy.SULs import McSUL
    from random import randint
    from aalpy.learning_algs import run_Alergia
    from aalpy.utils import visualize_automaton, generate_random_markov_chain
    from aalpy.utils import CharacterTokenizer

    mc = generate_random_markov_chain(10)
    visualize_automaton(mc, path='Original')

    sul = McSUL(mc)

    # note that this example shows writing to file just to show how tokenizer is used...
    # this step can ofc be skipped and lists passed to alergia
    data = []
    for _ in range(20000):
        str_len = randint(4, 12)
        seq = [f'{sul.pre()}']
        for _ in range(str_len):
            o = sul.step()
            seq.append(f'{o}')
        sul.post()
        data.append(''.join(seq))

    with open('mcData.txt', 'w') as file:
        for seq in data:
            file.write(f'{seq}\n')

    file.close()

    # create tokenizer
    tokenizer = CharacterTokenizer()
    # parse data
    data = tokenizer.tokenize_data('mcData.txt')
    # run alergia with the data and automaton_type set to 'mc' to learn a Markov Chain
    model = run_Alergia(data, automaton_type='mc', eps=0.005, print_info=True)
    # print(model)

    visualize_automaton(model)
    remove('mcData.txt')
    return model
コード例 #19
0
def learn_crossroad(visualize=False):
    sul = CrossroadSUL()
    alphabet = sul.full_alphabet

    eq_oracle = RandomWMethodEqOracle(alphabet,
                                      sul,
                                      walks_per_state=10,
                                      walk_len=30)

    learned_model = run_Lstar(alphabet,
                              sul,
                              eq_oracle,
                              automaton_type='mealy',
                              cache_and_non_det_check=False,
                              max_learning_rounds=10)

    if visualize:
        visualize_automaton(learned_model,
                            display_same_state_trans=False,
                            file_type="dot")

    return learned_model
コード例 #20
0
ファイル: Examples.py プロジェクト: DES-Lab/AALpy
def mqtt_example():
    from aalpy.base import SUL
    from aalpy.oracles import RandomWalkEqOracle
    from aalpy.learning_algs import run_Lstar
    from aalpy.utils import visualize_automaton, MockMqttExample

    class MQTT_SUL(SUL):
        def __init__(self):
            super().__init__()
            self.mqtt = MockMqttExample()

        def pre(self):
            self.mqtt.state = 'CONCLOSED'

        def post(self):
            self.mqtt.topics.clear()

        def step(self, letter):
            if letter == 'connect':
                return self.mqtt.connect()
            elif letter == 'disconnect':
                return self.mqtt.disconnect()
            elif letter == 'publish':
                return self.mqtt.publish(topic='test')
            elif letter == 'subscribe':
                return self.mqtt.subscribe(topic='test')
            else:
                return self.mqtt.unsubscribe(topic='test')

    sul = MQTT_SUL()
    input_al = ['connect', 'disconnect', 'publish', 'subscribe', 'unsubscribe']

    eq_oracle = RandomWalkEqOracle(input_al, sul, num_steps=2000, reset_after_cex=True, reset_prob=0.15)

    mealy = run_Lstar(input_al, sul, eq_oracle=eq_oracle, automaton_type='mealy', cache_and_non_det_check=True,
                      print_level=3)

    visualize_automaton(mealy)
コード例 #21
0
def learn_with_mapper():
    train_seq, train_labels, input_al, output_al = generate_concrete_data_MQTT(
        num_examples=300000,
        num_rand_topics=2,
        lens=(1, 2, 3, 5, 8, 10, 12),
        uniform_concretion=True)

    x_train, y_train, x_test, y_test = split_train_validation(train_seq,
                                                              train_labels,
                                                              0.8,
                                                              uniform=True)

    # train_seq, train_labels = generate_data_based_on_characterization_set(mealy_machine)
    # x_train, y_train, x_test, y_test = split_train_validation(train_seq, train_labels, 0.8, uniform=True)

    rnn = RNNClassifier(input_al,
                        output_dim=len(output_al),
                        num_layers=5,
                        hidden_dim=40,
                        x_train=x_train,
                        y_train=y_train,
                        x_test=x_test,
                        y_test=y_test,
                        batch_size=32,
                        nn_type='GRU')

    load = True
    ex_name = 'abstracted_mqtt'

    if not load:
        rnn.train(epochs=200, stop_acc=1.0, stop_epochs=3)
        rnn.save(f'RNN_Models/{ex_name}.rnn')
        with open(f'RNN_Models/{ex_name}.pickle', 'wb') as handle:
            pickle.dump((input_al, output_al),
                        handle,
                        protocol=pickle.HIGHEST_PROTOCOL)
    else:
        rnn.load(f'RNN_Models/{ex_name}.rnn')
        with open(f'RNN_Models/{ex_name}.pickle', 'rb') as handle:
            inp_out_tuple = pickle.load(handle)
        input_al, output_al = inp_out_tuple[0], inp_out_tuple[1]
        rnn.token_dict = dict((c, i) for i, c in enumerate(input_al))

    sul = Abstract_Mapper_MQTT_RNN_SUL(rnn, input_al, output_al)

    abstract_inputs = sul.abstract_inputs

    eq_oracle = StatePrefixEqOracle(abstract_inputs,
                                    sul,
                                    walks_per_state=100,
                                    walk_len=20)

    model = run_Lstar(abstract_inputs,
                      sul,
                      eq_oracle,
                      automaton_type='mealy',
                      cache_and_non_det_check=True,
                      suffix_closedness=False)

    visualize_automaton(model)
    return model
コード例 #22
0
sul = RnnBinarySUL(rnn)
alphabet = tomita_alphabet

state_eq_oracle = StatePrefixEqOracle(alphabet,
                                      sul,
                                      walks_per_state=200,
                                      walk_len=6)

dfa = run_Lstar(alphabet=alphabet,
                sul=sul,
                eq_oracle=state_eq_oracle,
                automaton_type='dfa',
                cache_and_non_det_check=True)

save_automaton_to_file(dfa, f'RNN_Models/tomita{3}')
visualize_automaton(dfa)

# train and extract balanced parentheses
bp_model = train_and_extract_bp(
    path='TrainingDataAndAutomata/balanced()_2.txt', load=False)
print("Print extracted model")
print(bp_model)

# train and learn mealy machine example
coffee_machine_automaton = train_RNN_and_extract_FSM('coffee')
save_automaton_to_file(coffee_machine_automaton, 'CoffeeMachineModel')

mqtt_automaton = train_RNN_and_extract_FSM('mqtt')
save_automaton_to_file(coffee_machine_automaton, 'MqttModel')