Ejemplo n.º 1
0
def DPFAgenerate(n):
    idx = 1
    for _ in range(n):
        fname = 'inputs/input{}.txt'.format(idx)
        PFA_utils.DPFAgenerator(fname)
        print("Created an input file")
        if PFA_utils.verifier(fname):
            print("Verified #{} input.".format(idx))
            idx += 1
        else:
            print("Failed to verify")
Ejemplo n.º 2
0
def main(args):
    algorithm = args.algorithm.lower()
    k_range = args.k_range
    n_range = args.n_range
    nbS_range = args.nbS_range
    nbL_range = args.nbL_range
    iters = args.iters
    result_path = args.result_path
    if os.path.exists(result_path):
        print(result_path,'exists')
        sys.exit()
    with open(result_path, 'w+') as f:
        f.write('algorithm,k,n,nbS,nbL,RT\n')
    for i in range(iters):
        k = random.randrange(*k_range)
        n = random.randrange(*n_range)
        nbS = random.randrange(*nbS_range)
        nbL = random.randrange(*nbL_range)
        # 1. generate a DPFA and string w \in \Sigma s.t. |w| = n
        dpfa = PFA_utils.DPFA_generator(nbS, nbL)
        sigma = [str(chr(ord('a')+i)) for i in range(nbL)]
        w = ''.join(np.random.choice(sigma, n))
        print("[{}] k: {}, n: {}, nbS: {}, nbL: {}".format(algorithm, k, n, nbS, nbL))
        print('given string',w)
        # 2. run MPS
        start_time = time.time()
        if 'intersect' in algorithm.lower():
            print('hamming automaton...')
            dfa = PFA_utils.DFA_constructor(w, k, sigma)
            print('nbS of the hamming automaton: {}'.format(dfa.nbS))
            print('intersecting...')
            sub_dpfa = dpfa.intersect_with_DFA(dfa)
            sub_dpfa = PFA.PFA(sub_dpfa.nbL, sub_dpfa.nbS, sub_dpfa.initial, sub_dpfa.final, sub_dpfa.transitions)
            print('nbS of intersected DPFA: {}'.format(sub_dpfa.nbS))
            print('normalizing...')
            dpfa = PFA_utils.normalizer(sub_dpfa)
            print('MPS...')
            w_star = dpfa.MPS_sampling()
        elif 'dp' in algorithm.lower():
            w_star = dpfa.k_MPS(w, k)
        elif 'bf' in algorithm.lower():
            w_star = dpfa.k_MPS_bf(w, k)
        else:
            raise NotImplementedError
        print('done!')
        end_time = time.time()
        RT = end_time - start_time
        print('time elapsed: {:.4f}s'.format(RT))
        # 3. record RT
        with open(result_path, 'a') as f:
            f.write('{},{},{},{},{},{:.5f}\n'.format(
                    algorithm, k, n, nbS, nbL, RT))
Ejemplo n.º 3
0
def DPFAgenerate(n, nb_state_min, nb_state_max):
    idx = 0
    while (True):
        fname = 'inputs/pfa/input{}.txt'.format(idx)
        PFA_utils.DPFAgenerator(fname, nb_state_min, nb_state_max)
        print("Created an input file")
        if PFA_utils.verifier(fname):
            print("Verified #{} input.".format(idx))
            idx += 1
            if idx == n:
                return
        else:
            print("Failed to verify")
Ejemplo n.º 4
0
def string_for_PFA():
    folder = "./inputs/pfa"
    for file in os.listdir(folder):
        filepath = os.path.join(folder, file)
        pfa = PFA_utils.parser(filepath)  # PFA
        pfa.make_string_file("inputs/string/string{}.txt".format(file[5]),
                             num_of_strings=10)
Ejemplo n.º 5
0
            """
            alphabet = transition[1]
            from_state = transition[0]
            to_state = transition[2]
            """
            if alphabet is '*':
                for key in self.transitions:
                    self.transitions[key][current_state,next_state] = 1
            else:
                self.transitions[alphabet][current_state,next_state] = 1
        if np.count_nonzero(self.transitions['']) == 0:
            self.transitions.pop('',None)

if __name__ == "__main__":
    """ Levinshtein Automata class unit-test code """
    at = PFA_utils.parser("./inputs/pfa/input0.txt")
    print('parsing done')
    start_time = time.time()
    lev = LevenshteinAutomaton("abaaab", 3)
    print('levenshtein done')
    r = at.intersect_with_DFA(lev)
    end_time = time.time()
    print('intersect done')
    """
    state0 = lev.start()
    print(state0)
    state1 = lev.step(state0, 'w')
    print(state1)
    state2 = lev.step(state1, 'o')
    print(state2)
    """
from common_header import *

import PFA
import PFA_utils
import RA

total_start = time.time()

# Generate a DFA that accepts strings only within k-hamming distance from given input string
start = time.time()
dfa = PFA_utils.DFA_constructor('abab', 2, ['a', 'b'])  # w, k, sigma
end = time.time()
print('Finished dfa construction...', end - start)

# Genearte a random DPFA
start = time.time()
dpfa = PFA_utils.DPFA_generator(20, 7)  # nbS, nbL
end = time.time()
print('Finished dpfa genearation...', end - start)

# Intersect above two automata, the resulting automata is a sub-DPFA
start = time.time()
ra = dpfa.intersect_with_DFA(dfa)  # Currently, RA
sub_dpfa = PFA.PFA(ra.nbL, ra.nbS, ra.initial, ra.final, ra.transitions)
end = time.time()
print('Finished dpfa & dfa intersection...', end - start)
print(PFA_utils.verifier(at=dpfa, isFile=False))

# Normalize the sub_dpfa to dpfa
start = time.time()
dpfa = PFA_utils.normalizer(sub_dpfa)
Ejemplo n.º 7
0
            print("Failed to verify")


# Main

if __name__ == "__main__":
    """
    automaton = PFA_utils.parser(sys.argv[1])  # sys.argv[1] for input file name

    test(automaton, sys.argv[2], int(sys.argv[3]))  # sys.argv[2] for string, 3 for k
    """
    #generate(int(sys.argv[1]))
    #DPFAgenerate(int(sys.argv[1]))
    #PFA_utils.pfa2input(PFA_utils.normalizer(PFA_utils.parser('inputs/input1.txt')), 'new.txt')

    # DFA, DPFA -> sub-DPFA -> DPFA
    for i in range(4, 10):
        dpfa = PFA_utils.parser('./inputs/pfa/input{}.txt'.format(i))
        #sub_dpfa.print()
        #dpfa = PFA_utils.normalizer(sub_dpfa)
        dpfa.print()
        #print(PFA_utils.verifier(at=dpfa, isFile=False))
        #print('results of MPS() and MPS_sampling() for input{}.txt'.format(i))
        w1 = dpfa.MPS()
        w2 = dpfa.MPS_sampling()
        if w1 != w2:
            print(PFA_utils.verifier('./inputs/pfa/input{}.txt'.format(i)))
            print("MPS {} MPS_samping {} when input {}".format(w1, w2, i))
            print("probs: {}, {}".format(dpfa.parse(w1), dpfa.parse(w2)))
            exit()