Ejemplo n.º 1
0
def natselect(popn, iteration):
    tpopn = list()
    while len(tpopn) < POPULATION_SIZE * 8:
        if random.random() < .5:
            child = indermediary_recomb(parentselect(popn))
            mutate(child, iteration)
            tpopn.append(individual(child, fitness(child)))
        else:
            child = discrete_recomb(parentselect(popn))
            mutate(child, iteration)
            tpopn.append(individual(child, fitness(child)))
    return survivorselect(tpopn)
Ejemplo n.º 2
0
def main(A, B, C, eps, sim_steps = 100):
    """
        Genetic Algorithm simulation with a given number of steps and initial values: A, B, C, eps,
        required for the goal function calculation.
        :return: Last population after GA simulation is finished.
        """
    steps = int(sim_steps)  # simulation steps
    p_c = 0.15  # cross over probability # 0.15
    p_m = 0.0005  # mutation probability # 0.0005
    p_i = 0.0001  # inversion probability # 0.0001

    pop_size = 200  # 200
    ### INITIALIZATION ###
    pop = func.init(pop_size, 'multinomial')
    best_fs = [] # goal function of the best one in populationin each step
    fs = [] # summary goal function of the population in each step
    for ii in range(0, steps):
        ##### 1 SELECTION
        p_sel = func.selection(pop, A, B, C, eps)
        fs.append(np.sum(p_sel))
        best_fs.append(np.max(p_sel))
        new_pop = func.roulette_select(pop, p_sel, pop_size)

        ##### 2 CROSSOVER
        pop = func.crossover(new_pop, pop_size, p_c)

        ###### 3 MUTATION
        for cc in range(0, len(pop)):
            pop[cc] = func.mutate(pop[cc], p_m)

        ###### 4 INVERSION
        probs_inv = np.random.uniform(size=len(pop))
        for nn in range(0, len(probs_inv)):
            if probs_inv[nn] < p_i:
                pop[nn] = func.inverse(pop[nn])

    # after the GA steps >= simulation steps - i.e. the result of GA
    # the code below calculates results averaged over the whole population
    vals = np.zeros(16)
    get_av = False
    if get_av:
        for ii in range(0, 16):
            for em in pop:
                vals[ii] += em[ii]
        vals /= pop_size
        terminal_view(vals)

    return pop, vals, np.array(best_fs), fs
Ejemplo n.º 3
0
                elit = population[np.where(fit == fittest)]
                fitness_history[iter, gen] = fittest
                best_results_gen.append(fittest)
                if gen > 0:
                    if best_results_gen[j] == best_results_gen[j-1] or best_results_gen[j] < best_results_gen[j-1] - 0.01 * best_results_gen[j-1]:
                        count += 1

                # επιλογή γονέων
                parents = np.zeros(population.shape)
                for i in range(0, num_indiv):
                    parents[i, :] = functions.select_parents(population, fit, 0.75)

                # crossover
                children = functions.mate(parents.astype(int), crossrate)
                # mutate
                mutated = functions.mutate(children, mutrate)

                population = mutated
                population[0, :] = elit[0, :]
                j += 1
                if count > 5:
                    print(f'Μηδενική ή πολύ μικρή βελτίωση ατόμου, επόμενο τρέξιμο αλγορίθμου:')
                    gens = gen
                    break
                gens = gen

            # Μετά την ολοκλήρωση του for loop, θεωρητικά έχουμε το καλύτερο αποτέλεσμα πληθυσμού
            fit = functions.fitness(population, x_test, y_test)
            fittest = np.min(fit)
            fitness_history[iter, gen+1] = fittest
            gens += 1