def run_active_Alergia(data, sul, sampler, n_iter): """ Active version of IOAlergia algorithm. Based on intermediate hypothesis sampling on the system is performed. Sampled data is added to the learning data and more accurate model is learned. Proposed in "Aichernig and Tappler, Probabilistic Black-Box Reachability Checking" Args: data: initial learning data, in form [[O, (I,O), (I,O)...] ,...] where O is outputs and I input. sul: system under learning which is basis for sampling sampler: instance of Sampler class n_iter: number of iterations of active learning Returns: learned MDP """ model = None for i in range(n_iter): print(f'Active Alergia Iteration: {i}') model = run_Alergia(data, automaton_type='mdp') new_samples = sampler.sample(sul, model) data.extend(new_samples) return model
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
def run_active_Alergia(data, sul, sampler, n_iter, eps=0.005, compatibility_checker=None, automaton_type='mdp', print_info=True): """ Active version of IOAlergia algorithm. Based on intermediate hypothesis sampling on the system is performed. Sampled data is added to the learning data and more accurate model is learned. Proposed in "Aichernig and Tappler, Probabilistic Black-Box Reachability Checking" Args: data: initial learning data, in form [[O, (I,O), (I,O)...] ,...] where O is outputs and I input. sul: system under learning which is basis for sampling sampler: instance of Sampler class n_iter: number of iterations of active learning eps: epsilon value if the default checker is used. Look in run_Alergia for description compatibility_checker: passed to run_Alergia, check there for description automaton_type: either 'mdp' or 'smm' (Markov decision process or Stochastic Mealy Machine) print_info: print current learning iteration Returns: learned MDP """ model = None for i in range(n_iter): if print_info: print(f'Active Alergia Iteration: {i}') model = run_Alergia(data, automaton_type='mdp', eps=eps, compatibility_checker=compatibility_checker) new_samples = sampler.sample(sul, model) data.extend(new_samples) return model
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
mdp_sul = MdpSUL(original_mdp) for _ in range(1): data = [] for _ in range(num_traces): sample = [mdp_sul.pre()] for _ in range(random.randint(10, 50)): i = random.choice(input_alphabet) o = mdp_sul.step(i) sample.append((i, o)) data.append(sample) mdp_sul.post() learned_mdp = run_Alergia(data, automaton_type='mdp') for s in data: s.pop(0) learned_smm = run_Alergia(data, automaton_type='smm') smm_2_mdp = smm_to_mdp_conversion(learned_smm) mdp_results, mdp_err = model_check_experiment(get_properties_file(exp_name), get_correct_prop_values(exp_name), learned_mdp) smm_results, smm_err = model_check_experiment(get_properties_file(exp_name), get_correct_prop_values(exp_name), smm_2_mdp) print(learned_mdp.size, learned_smm.size, smm_2_mdp.size) print(f'-------{exp_name}---------')