예제 #1
0
def ga(pixel_arr, constraints, pop_size=100, g_max=40, p_c=0.6, p_m=0.4, verbose=True):

    start_time = time.time()
    num_rows, num_cols = pixel_arr.shape[0], pixel_arr.shape[1]
    population = [Chromosome(pixel_arr) for i in range(pop_size)]
    avg_fitness = sum(c.fitness for c in population)
    best_ind = max(population, key=lambda c: c.fitness)
    
    if verbose:
        print("Generation 0:")
        print("Best fitness:", best_ind.fitness, "Avg fitness:", avg_fitness)
        print("Number of segmentations:", max(best_ind.segments))
        print("Edge value:", best_ind.edge_value)
        print("Connectivity:", best_ind.connectivity)
        print("Deviation:", best_ind.deviation)
        print(f"Initial generation time: {time.time() - start_time}\n", flush=True)
    
    for g in range(1, g_max+1):
        start_time = time.time()
        offspring = []
        for i in range(pop_size//2):
            p1 = parent_selection(population)
            p2 = parent_selection(population)
            if random() < p_c:
                cutoff = randint(0, len(p1.genotype)-1)
                c1_geno = crossover(p1.genotype, p2.genotype, cutoff)
                c2_geno = crossover(p2.genotype, p1.genotype, cutoff)
            else:
                c1_geno = p1.genotype
                c2_geno = p2.genotype

            if random() < p_m:
                c1_geno = mutate(c1_geno, num_rows, num_cols, arr, constraints)
            if random() < p_m:
                c2_geno = mutate(c2_geno, num_rows, num_cols, arr, constraints)

            c1 = Chromosome(pixel_arr, c1_geno)
            c2 = Chromosome(pixel_arr, c2_geno)
        
            offspring.append(c1)
            offspring.append(c2)   

        population = elitist_replacement(population, offspring)

        avg_fitness = sum(c.fitness for c in population)
        best_ind = max(population, key=lambda c: c.fitness)
        
        if verbose:
            print(f"Generation {g}:")
            print("Best fitness:", best_ind.fitness, "Avg fitness:", avg_fitness)
            print("Number of segmentations:", max(best_ind.segments))
            print("Edge value:", best_ind.edge_value)
            print("Connectivity:", best_ind.connectivity)
            print("Deviation:", best_ind.deviation)
            print(f"Time elapsed: {time.time() - start_time}\n", flush=True)
    
    return best_ind
예제 #2
0
파일: gautil.py 프로젝트: shamisp/aiproj
def gsacrossover_population(M, chrs, temp):
    c_ppl = select_for_crossover(chrs)
    for _ in c_ppl:
        minch = maxch = None
        a = c_ppl.pop()
        b = c_ppl.pop()
        c = crossover(M, a, b)
        if DEBUG: print "A: ", a
        if DEBUG: print "B: ", b
        if DEBUG: print "C: ", c
        mutate_chromosome(c)
        a_val=a.value()
        b_val=b.value()
        c_val=c.value()
        
        if (a_val > b_val):
            maxch = a
            minch = b
        else:
            maxch = b
            minch = a
        
        if c_val < (maxch.value() + temp) :
            # accept mutant to new population
            chrs.append(c)
            chrs.remove(maxch)
        
        # otherwise we don't accept the mutant to
        # the new population
    assert len(chrs) == P_SIZE-1
예제 #3
0
파일: gautil.py 프로젝트: shamisp/aiproj
def crossover_population(M, chrs):
    c_ppl = select_for_crossover(chrs)
    for _ in c_ppl:
        a = c_ppl.pop()
        b = c_ppl.pop()
        c = crossover(M, a, b)
        if DEBUG: print "A: ", a
        if DEBUG: print "B: ", b
        if DEBUG: print "C: ", c
        mutate_chromosome(c)
        chrs.append(c)
예제 #4
0
def createNew(withFitness):
    global glassc
    ranked = map(lambda t: t[1], sorted(withFitness, key=lambda t: t[0]))
    best = ranked[:perGeneration / 4]
    mutated = []

    glassc += 1

    for i in range(perGeneration - perGeneration / 4):
        p1 = best[random.randint(0, len(best) - 1)]
        p2 = best[random.randint(0, len(best) - 1)]
        child1, child2 = chromosome.crossover(p1, p2)
        child1 = child1.mutated()
        child2 = child2.mutated()
        mutated.append(child1)

    return best + mutated
예제 #5
0
def createNew(withFitness):
    global glassc
    ranked = map(lambda t: t[1], sorted(withFitness, key=lambda t: t[0]))
    best = ranked[: perGeneration / 4]
    mutated = []

    glassc += 1

    for i in range(perGeneration - perGeneration / 4):
        p1 = best[random.randint(0, len(best) - 1)]
        p2 = best[random.randint(0, len(best) - 1)]
        child1, child2 = chromosome.crossover(p1, p2)
        child1 = child1.mutated()
        child2 = child2.mutated()
        mutated.append(child1)

    return best + mutated
def mycross(ind1, ind2, gen_no):
    child1 = crossover(ind1, ind2, gen_no, inputdim=indim, outputdim=outdim)
    child2 = crossover(ind1, ind2, gen_no, inputdim=indim, outputdim=outdim)

    return child1, child2
예제 #7
0
def mycross(ind1, ind2, gen_no):
    child1 = crossover(ind1, ind2, gen_no, inputdim=8, outputdim=2)
    child2 = crossover(ind1, ind2, gen_no, inputdim=8, outputdim=2)

    return child1, child2