Пример #1
0
def test_pqtree_after_reduce_chars_rand_examples():
    ITERATIONS = 1000

    merged = 0
    rts = []

    for i in range(ITERATIONS):
        iter_stats = {}

        # ------------------------- Generation of permutations -------------------------
        id_perm = list(range(1, 10))
        duplication_mutations(id_perm, 2)

        other_perms = [list(id_perm), list(id_perm)]
        for p in other_perms:
            mutate_collection(p, 2)

        ps = tmap(tuple, (id_perm, *other_perms))

        # ------------------------- Try to merge same adjacent chars -------------------------
        start_time = time.time()
        pq = PQTreeDup.from_perms_wth_multi(ps)
        iter_stats["merged"] = time.time() - start_time

        if not pq:
            continue
        else:
            merged += 1

        # ------------------------- find all the trees with minimal size -------------------------
        start_time = time.time()
        all_possibilities = list(PQTreeDup.from_perms(ps))
        iter_stats["no_merge"] = time.time() - start_time
        iter_stats["perms"] = ps

        best_size = all_possibilities[0].approx_frontier_size()
        only_best_sized = lfilter(lambda t: t.approx_frontier_size() == best_size, all_possibilities)

        # verify tree with multi chars contains in its frontier one of the best trees
        try:
            front = set(pq.frontier())
            assert any(front.issuperset(t.frontier()) for t in only_best_sized)
        except:
            print(ps)
            # PQTreeVisualizer.show_all(pq, *only_best_sized)
            raise
        else:
            rts.append(iter_stats)

    print(f"multi merged: {merged} / {ITERATIONS}")
    lmap(print, rts)
Пример #2
0
def test_pqtree_with_merges_rand():
    ITERATIONS = 100

    merged = 0

    for i in range(ITERATIONS):
        id_perm = list(range(1, 100))
        duplication_mutations(id_perm, 5)

        other_perms = [list(id_perm), list(id_perm)]
        for p in other_perms:
            mutate_collection(p, 2)

        ps = tmap(tuple, (id_perm, *other_perms))


        pqs = list(PQTreeDup.from_perms_with_merge(ps))

        if not pqs:
            continue
        else:
            merged += 1

        best_size_found = min(pq.approx_frontier_size() for pq in pqs)
        only_best_sized_found = tfilter_fx_eq(PQTree.approx_frontier_size, best_size_found, pqs)

        all_possibilities = list(PQTreeDup.from_perms(ps))
        best_size = min(t.approx_frontier_size() for t in all_possibilities)
        only_best_sized = lfilter(lambda t: t.approx_frontier_size() == best_size, all_possibilities)
        only_best_sized_parens = smap(PQTree.to_parens, only_best_sized)

        try:
            assert best_size_found == best_size
            assert all(len(list(pq.frontier())) == best_size for pq in only_best_sized_found)
            assert any(pq.to_parens() in only_best_sized_parens for pq in only_best_sized_found)
        except:
            print("same cahrs together:", any(any(o[1] != 1 for o in iter_char_occurrence(p)) for p in ps))

            print(f"best no opt: {best_size}, best with merge: {best_size_found}")
            print(f"Merged best: {[pq.to_parens() for pq in only_best_sized_found]}")
            print(f"Merged all: {[pq.to_parens() for pq in pqs]}")
            print(ps)
            print(f"actual front sizes = {[len(list(t.frontier())) for t in only_best_sized]}")
            print([t.to_parens() for t in all_possibilities])
            print([t.to_parens() for t in only_best_sized])
            # PQTreeVisualizer.show_all(pq, *only_best_sized)
            continue

    print(f"merged {merged}")
Пример #3
0
    def translate(cls, cperms: CPermutations, translation: Translation):
        dict_trans = merge(*map(dict, translation))

        def translate_perm(cperm):
            it2 = window(chain(cperm, [None]))
            perm = []

            for w in it2:
                if w in dict_trans:
                    perm.append(dict_trans[w])
                    next(it2)
                else:
                    perm.append(w[0].char)
            return tuple(perm)

        return tmap(translate_perm, cperms)
Пример #4
0
def rand_size_tests():
    ITERATIONS = 1000

    for i in range(ITERATIONS):
        id_perm = list(range(1, 10))

        other_perms = [list(id_perm), list(id_perm)]
        for p in other_perms:
            mutate_collection(p, 2)

        ps = tmap(tuple, (id_perm, *other_perms))

        pq = PQTreeBuilder.from_perms(ps)
        assert pq.approx_frontier_size() == len(list(pq.frontier())), [
            pq.to_parens(),
            set(pq.frontier()),
            pq.approx_frontier_size(),
            len(list(pq.frontier())),
            len(set(pq.frontier()))
        ]
Пример #5
0
def test_pqtree_with_duplications_rand():
    ITERATIONS = 10

    for i in range(ITERATIONS):
        x = 0
        id_perm = list(range(1, 10))
        duplication_mutations(id_perm, 2)

        other_perms = [list(id_perm) for _ in range(6)]
        for p in other_perms:
            mutate_collection(p, 2)

        ps = tmap(tuple, (id_perm, *other_perms))

        # all_possibilities = list(PQTreeDup.from_perms(ps))
        # best_size = min(t.approx_frontier_size() for t in all_possibilities)
        for p in PQTreeDup.from_perms(ps):
            x += 1
            # print(2)
        print(x)
Пример #6
0
def char_neighbour_tuples(cchars_collections: Collection[ContextChar], char) -> Tuple[Tuple[object, object], ...]:
    return tmap(
        lambda cc: (cc.left_char, cc.char) if cc.left_char == char else (cc.char, cc.right_char),
        cchars_collections
    )