Example #1
0
def generation(code_type, input_type, index):
    utility = Utility()
    population = create_population(250)
    parents = []
    children = []
    i = 0
    same_count = 0
    control_group = []
    Pms = 0.8
    Pmc = 0.4
    Pbs = 0.1
    prob_list = []
    quality = []
    diff = []
    option = ''
    while i < 5000:
        if i == 0:
            for ind in population:
                parents.append(ind)
            pareto = utility.non_dominated(parents)
            control_group = pareto[:]
        elif i > 0:
            new_pareto = utility.non_dominated(children)
            pareto.extend(new_pareto)
            pareto = utility.non_dominated(pareto)
            if len(pareto) > len(children):
                reduced = reduceParetoWithSort(pareto, utility)
                del pareto[:]
                pareto = reduced[:]
            parents = clear_parents(parents)
            parents = [par for par in children]
            children = clear_children(children)
            if control_group != pareto:
                del control_group[:]
                control_group = pareto[:]
                same_count = 0
            else:
                same_count += 1
            if same_count >= 40:
#                f = open("/Users/sirinsaygili/workspace/seggen/results/"+code_type+"_"+input_type+"_"+str(index)+".txt","w")
#                print "%s type,input %s, result at %d. generation" %(code_type, input_type, (i+1))
#                reference = "0000000001000000000100001000010000001000000000000"
#                for key in pick_better_results(pareto,utility):
#                    s = ''.join(str(x) for x in key)
#                    r = [key,windowdiff(reference,s,9)]
#                    f.write(str(r)+"\n")
#                f.write(str("program finished at %d. generation" %(i+1)))
#                f.close()
                for t in agg_val.items():
                    print t
                break

        pareto_quota = random.randint(1, len(pareto))
        pop_quota = (len(parents)-pareto_quota)
        create_pareto_fitness_dict(population, pareto, utility)
        create_population_fitness_dict(population, pareto, pareto_hardness_dict.values(), utility)
        pop_probabilities = get_probability_list()
        pareto_probabilities = get_pareto_probability_list()

        #ga operator: selection (roulette wheel)
        mating_pool = roulette_wheel_pop(population, pop_probabilities, pop_quota)
        selected_from_pareto = roulette_wheel_pareto(pareto, pareto_probabilities, pareto_quota)
        mating_pool.extend(selected_from_pareto)

        #ga operator: crossover
        for j in range(0,60):
            if len(mating_pool) >= 2:
                indexes = random.sample(range(len(mating_pool)), 4)
                if option == 'C' or 'M1C' or 'M2C':
                    created = crossover_merge(mating_pool[indexes[2]], mating_pool[indexes[3]])
                    children.append(created)
                    crossed = crossover_same_boundary_count(mating_pool[indexes[0]], mating_pool[indexes[1]])
                    if crossed[0] is True:
                        children.extend(crossed[1:])
                    else:
                        children.extend(crossover_keep_boundary(mating_pool[indexes[0]], mating_pool[indexes[1]]))
                else:
                    children.extend(crossover(mating_pool[indexes[0]], mating_pool[indexes[1]]))
            else:
                print "sampler larger than %d" % len(mating_pool)
                for k in agg_val.items():
                    print k
                break

        #ga operator: mutation
        rand_indexes = random.sample(set(range(len(children))), 2)
        if option == 'M1' or 'M2' or 'M1C' or 'M2C':
            mutated_boundary_shift = mutation_boundary_shift(children[rand_indexes[0]], Pbs)
            children.append(mutated_boundary_shift)
            mutated_boundary_add = mutation_add_boundary(children[rand_indexes[0]], Pbs)
            children.append(mutated_boundary_add)
        else:
            children[rand_indexes[0]] = mutation_Pms(children[rand_indexes[0]], children[rand_indexes[1]], Pms)
            mutated_pmc = mutation_Pmc(children[rand_indexes[0]], Pmc)
            children.append(mutated_pmc)

        agg_val = aggregation(pareto, utility)
        if option == 'M2' or 'M2C':
            if i == 9:
                values = [x for x in agg_val.values()]
                mean_val = sum(values) / float(len(values))
                prob_list.append(mean_val)
            if i>0 and i%10 == 0:
                values = [x for x in agg_val.values()]
                mean = sum(values) / float(len(values))
                if mean < prob_list[-1] and Pbs<=0.8:
                    Pbs+=0.05
                prob_list.append(mean)

        if i == 9:
            quality.append(calculate_global_quality(parents,utility))
        if i>0 and i%10 == 0:
            instant = calculate_global_quality(parents,utility)
            if instant < quality[-1]:
                option = choice(options)
            quality.append(instant)
            print quality[-1]
            print option

        if i == 4999:
            for t in agg_val.items():
                print t
        i += 1
Example #2
0
def generation(code_type, input_type, index):
    utility = Utility()
    population = create_population(250)
    parents = []
    children = []
    i = 0
    same_count = 0
    control = {}
    Pms = 0.8
    Pmc = 0.4
    Pbs = 0.1
    prob_list = []
    while i < 5000:
        if i == 0:
            for ind in population:
                parents.append(ind)
            pareto = utility.weighted_non_dominated(parents)
        elif i > 0:
            new_pareto = utility.weighted_non_dominated(children)
            pareto.extend(new_pareto)
            pareto = utility.weighted_non_dominated(pareto)
            if len(pareto) > len(children):
                reduced = reduceParetoWithSort(pareto, utility)
                del pareto[:]
                pareto = reduced[:]
            parents = clear_parents(parents)
            parents = [par for par in children]
            children = clear_children(children)
            if set(control.items()) != set(get_diff(agg_val,w_agg_val).items()):
                control.clear()
                control = get_diff(agg_val,w_agg_val)
                same_count = 0
            else:
                same_count += 1
            if same_count >= 30:
                print "%s type,input %s, result at %d. generation" %(code_type, input_type, (i+1))
                f = open("/Users/sirinsaygili/workspace/seggen/results/"+code_type+"_"+input_type+"_"+str(index)+".txt","w")
                reference = "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]"
                #reference = "[0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]"
                for item,key in zip(control.items(),control.keys()):
                    s = ''.join(str(x) for x in key)
                    r = [item,windowdiff(reference,s,9)]
                    f.write(str(r)+"\n")
                f.close()
                break

        pareto_quota = random.randint(1, len(pareto))
        pop_quota = (len(parents)-pareto_quota)
        create_pareto_fitness_dict(population, pareto, utility)
        create_population_fitness_dict(population, pareto, pareto_hardness_dict.values(), utility)
        pop_probabilities = get_probability_list()
        pareto_probabilities = get_pareto_probability_list()

        #ga operator: selection (roulette wheel)
        mating_pool = roulette_wheel_pop(population, pop_probabilities, pop_quota)
        selected_from_pareto = roulette_wheel_pareto(pareto, pareto_probabilities, pareto_quota)
        mating_pool.extend(selected_from_pareto)

        #ga operator: crossover
        for j in range(0,60):
            if len(mating_pool) >= 2:
                indexes = random.sample(range(len(mating_pool)), 2)
                #children.extend(crossover(mating_pool[indexes[0]], mating_pool[indexes[1]]))
                crossed = crossover_same_boundary_count(mating_pool[indexes[0]], mating_pool[indexes[1]])
                if crossed[0] is True:
                    children.extend(crossed[1:])
                else:
                    children.extend(crossover_keep_boundary(mating_pool[indexes[0]], mating_pool[indexes[1]]))
            else:
                print "sampler larger than %d" % len(mating_pool)
                for k in agg_val.items():
                    print k
                break

        #ga operator: mutation
        rand_indexes = random.sample(set(range(len(children))), 2)
#        children[rand_indexes[0]] = mutation_Pms(children[rand_indexes[0]], children[rand_indexes[1]], Pms)
#        mutated_pmc = mutation_Pmc(children[rand_indexes[0]], Pmc)
#        children.append(mutated_pmc)
        mutated_boundary_shift = mutation_boundary_shift(children[rand_indexes[0]], Pbs)
        children.append(mutated_boundary_shift)
        mutated_boundary_add = mutation_add_boundary(children[rand_indexes[0]], Pbs)
        children.append(mutated_boundary_add)
        agg_val = aggregation(pareto, utility)
        w_agg_val = weighted_aggregation(pareto, utility)
        #print "%d. generation pareto archive size %d"  % ((i+1), len(pareto))
        if i == 9:
            values = [x for x in agg_val.values()]
            mean_val = sum(values) / float(len(values))
            prob_list.append(mean_val)
        if i>0 and i%10 == 0:
            values = [x for x in agg_val.values()]
            mean = sum(values) / float(len(values))
            if mean < prob_list[-1] and Pbs<=0.8:
                Pbs+=0.05
#            print "average of population %f at %d . generation" % (mean, (i+1))
#            print "new Pbs value %f" % Pbs
            prob_list.append(mean)

        if i == 4999:
            for t in agg_val.items():
                print t
        i += 1