genetics.py

from numpy import sin as fonction import matplotlib.pyplot as plt import random as rd import numpy as np ordre = 5 nb_parents = 8 nb_enfants = 50 nb_random = 20 gen_size = 2 * nb_parents + nb_enfants + nb_random # 25 alea_min = -1 alea_max = 1 #individu = [a0, a1, a2...] # ////////////////// # # GENERATION # # ////////////////// # def individu_alea(): individu = [] for i in range(ordre + 1): individu += [rd.uniform(alea_min, alea_max)] return individu def first_gen(): pop = [] for i in range(gen_size): pop += [individu_alea()] return pop # ////////////////// # # EVALUATION # # ////////////////// # def horner(individu, val): if (individu != []): return individu[0] + val * (horner(individu[1:], val)) else: return 0 # le plus petit est le mieux def fitness(individu): valeurs = np.arange(-3, 3, 0.01) return np.sum((horner(individu, valeurs) - fonction(valeurs)) ** 2) # ecart type entre fonction(x) et horner(individu, x) # ////////////////// # # SELECTION # # ////////////////// # def pos_min(liste): min_courant = liste[0] imin = 0 for j in range(gen_size): if (liste[j] < min_courant): imin = j min_courant = liste[j] return imin def fittest(Generation): select = [] fit_list = [fitness(individu) for individu in Generation] maxi = max(fit_list) for i in range(nb_parents): imin = pos_min(fit_list) select += [Generation[imin]] fit_list[imin] = maxi return select # ////////////////// # # EVOLUTION # # ////////////////// # def crossover2(parent1, parent2): return [parent1[i] if rd.randint(0,1) else parent2[i] for i in range(ordre + 1)] def crossover(parent1, parent2): return [(parent1[i] + parent2[i]) / 2 for i in range(ordre + 1)] mutate_prob = 0.2 mutate_effect = 0.5 def mutate(individu): mutated = list(individu) for i in range(len(individu)): if(rd.random() <= mutate_prob): mutated[i] *= (1 + (2*rd.random() - 1) * mutate_effect) return mutated def next_gen(Generation): parents = fittest(Generation) NextGen = parents NextGen += [mutate(parent) for parent in parents] for i in range(nb_enfants): p1 = rd.randint(0, nb_parents - 1) p2 = rd.randint(0, nb_parents - 1) while (p1 == p2): p2 = rd.randint(0, nb_parents - 1) NextGen += [mutate(crossover(parents[p1], parents[p2]))] for i in range(nb_random): NextGen += [individu_alea()] return NextGen def EVOLUTION(nb_gen = 1000): Generation = first_gen() for i in range(nb_gen): Generation = next_gen(Generation) if (i % 50 == 0): print(fitness(Generation[1])) best = fittest(Generation)[1] print([round(i, 5) for i in best]) trace_graphique(best) # ////////////////// # # AFFICHAGE # # ////////////////// # def trace_graphique(individu): plt.xkcd() plt.clf() valeurs = np.arange(-3, 3, 0.01) f1 = fonction(valeurs) f2 = horner(individu, valeurs) f3 = horner([0,1,0,-1/6.0, 0, 1/120.0], valeurs) plt.subplot(111) plt.plot(f3, label="dl", color="pink") plt.plot(f1, label="sin", color="red") plt.plot(f2, label="poly", color="blue") plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=3, mode="expand", borderaxespad=0.) plt.ylabel("f(t)") plt.xlabel("t") plt.show('hold')

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.