Ejemplo n.º 1
0
def oracle_gen():
    """
    calculate max probability for a color from colors other than it
    """
    list_oracle_crossing = []

    for f in flower_names:
        t, cross_fn, prob_fn = get_flower_data(f)
        non_seed_color = get_rare_colors(t)
        d = {}
        list_oracle_crossing.append((f, d))
        for rare in non_seed_color:
            max_p = 0.0
            for i, ci in enumerate(t):
                if ci == '-' or ci == rare:
                    continue
                for j, cj in enumerate(t):
                    if cj == '-' or cj == rare:
                        continue
                    for g, p in cross_fn(i, j):
                        if t[g] == rare:
                            max_p = max(max_p, prob_fn(p))

            d[rare] = max_p
    return list_oracle_crossing
Ejemplo n.º 2
0
def get_seed_gene():
    for f in flower_names:
        print(f)
        t, _, _ = get_flower_data(f)
        for i, c in enumerate(t):
            if 'seed' in c:
                print(c, format(i, "#08b"))
Ejemplo n.º 3
0
def Count_by_Colors():
    for f in flower_names:
        t, _, _ = get_flower_data(f)
        cc = Counter()
        for c in t:
            cc[c.split()[0]] += 1
        cc.pop('-')
        print(f, "color count=", len(cc), ":", cc)
Ejemplo n.º 4
0
def crossing(flower, iter_num, filename):
    """
    main function.
    simulating iter_num generations of crossing.
    the searching space is rather large, so some optimization is used.
    """
    color_gene, gene_crossing_fun, prob_fun = get_flower_data(flower)

    parents_result = []
    color_geneX = []

    rare_colors = set(color_gene)
    rare_colors.discard('-')
    for gene, color in enumerate(color_gene):
        if 'seed' in color:
            parents_result.append(Crossing_Result({gene: 1.0}, 1.0, color, []))
            rare_colors.discard(color)
            rare_colors.discard(color.split()[0])
        # remove seed
        color_geneX.append(color.split()[0])

    pruning_gene_dict = {
        g: [0.0, []]
        for g, c in enumerate(color_geneX) if c != '-'
    }
    pruning_color_dict = {c: [0.0, []] for c in color_gene}

    skip_i = -1
    for _ in range(iter_num):
        print('new iter, parents', len(parents_result))
        for i in pruning_gene_dict:
            pruning_gene_dict[i][0] += 0.00001
            pruning_gene_dict[i][1].clear()
        for i in pruning_color_dict:
            pruning_color_dict[i][0] += 0.00001
            pruning_color_dict[i][1].clear()
        for i, a in enumerate(parents_result):
            if (i + 1) % 50 == 0:
                print("running", i)
            if i < skip_i: continue
            skip_i = i

            # normal crossing
            for j, b in enumerate(parents_result):
                # crossing is symmetric, skip half crossing results
                if i < j: break
                l_crossing = normal_crossing(a, b, color_geneX,
                                             gene_crossing_fun, prob_fun)
                filtering_split(l_crossing, rare_colors, pruning_gene_dict,
                                pruning_color_dict)
                # from pprint import pprint
                # print(i,j,"crossing\t")
                # pprint(l_crossing)

            # self duplication
            # if len(a.gene)==1:
            #     continue
            l_dup = dup_crossing(a, color_geneX, gene_crossing_fun, prob_fun)
            filtering_split(l_dup, rare_colors, pruning_gene_dict,
                            pruning_color_dict)
            # print(i,'-',"dup\t")
            # from pprint import pprint
            # pprint(l_dup)

        print(
            "updated methods count",
            sum((len(pruning_gene_dict[g][1])
                 for g in pruning_gene_dict)) + sum(
                     (len(pruning_color_dict[c][1])
                      for c in pruning_color_dict)))
        print("reducing!!!")
        old_parent_len = len(parents_result)
        unique_accumulating(parents_result, pruning_gene_dict,
                            pruning_color_dict, color_geneX)
        print("reduced!!!", len(parents_result) - old_parent_len)
    print(flower)
    with open(filename, 'w') as f:
        pick_methods(parents_result, rare_colors, f)
Ejemplo n.º 5
0
def get_Color_by_gene(flower, gene):
    print(flower, gene, get_flower_data(flower)[0][gene])