Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. from numpy import sin as fonction
  2. import matplotlib.pyplot as plt
  3. import random as rd
  4. import numpy as np
  5.  
  6. ordre = 5
  7.  
  8. nb_parents = 8
  9. nb_enfants = 50
  10. nb_random = 20
  11. gen_size = 2 * nb_parents + nb_enfants + nb_random # 25
  12.  
  13. alea_min = -1
  14. alea_max = 1
  15.  
  16. #individu = [a0, a1, a2...]
  17.  
  18.  
  19. # ////////////////// #
  20. # GENERATION #
  21. # ////////////////// #
  22.  
  23.  
  24. def individu_alea():
  25. individu = []
  26. for i in range(ordre + 1):
  27. individu += [rd.uniform(alea_min, alea_max)]
  28. return individu
  29.  
  30.  
  31. def first_gen():
  32. pop = []
  33. for i in range(gen_size):
  34. pop += [individu_alea()]
  35. return pop
  36.  
  37.  
  38. # ////////////////// #
  39. # EVALUATION #
  40. # ////////////////// #
  41.  
  42.  
  43. def horner(individu, val):
  44. if (individu != []):
  45. return individu[0] + val * (horner(individu[1:], val))
  46. else:
  47. return 0
  48.  
  49.  
  50. # le plus petit est le mieux
  51. def fitness(individu):
  52. valeurs = np.arange(-3, 3, 0.01)
  53. return np.sum((horner(individu, valeurs) - fonction(valeurs)) ** 2)
  54.  
  55. # ecart type entre fonction(x) et horner(individu, x)
  56.  
  57.  
  58. # ////////////////// #
  59. # SELECTION #
  60. # ////////////////// #
  61.  
  62.  
  63. def pos_min(liste):
  64. min_courant = liste[0]
  65. imin = 0
  66. for j in range(gen_size):
  67. if (liste[j] < min_courant):
  68. imin = j
  69. min_courant = liste[j]
  70. return imin
  71.  
  72.  
  73. def fittest(Generation):
  74. select = []
  75. fit_list = [fitness(individu) for individu in Generation]
  76. maxi = max(fit_list)
  77. for i in range(nb_parents):
  78. imin = pos_min(fit_list)
  79. select += [Generation[imin]]
  80. fit_list[imin] = maxi
  81. return select
  82.  
  83.  
  84. # ////////////////// #
  85. # EVOLUTION #
  86. # ////////////////// #
  87.  
  88.  
  89. def crossover2(parent1, parent2):
  90. return [parent1[i] if rd.randint(0,1) else parent2[i] for i in range(ordre + 1)]
  91.  
  92. def crossover(parent1, parent2):
  93. return [(parent1[i] + parent2[i]) / 2 for i in range(ordre + 1)]
  94.  
  95.  
  96. mutate_prob = 0.2
  97. mutate_effect = 0.5
  98.  
  99.  
  100. def mutate(individu):
  101. mutated = list(individu)
  102. for i in range(len(individu)):
  103. if(rd.random() <= mutate_prob):
  104. mutated[i] *= (1 + (2*rd.random() - 1) * mutate_effect)
  105. return mutated
  106.  
  107.  
  108. def next_gen(Generation):
  109. parents = fittest(Generation)
  110. NextGen = parents
  111. NextGen += [mutate(parent) for parent in parents]
  112. for i in range(nb_enfants):
  113. p1 = rd.randint(0, nb_parents - 1)
  114. p2 = rd.randint(0, nb_parents - 1)
  115. while (p1 == p2):
  116. p2 = rd.randint(0, nb_parents - 1)
  117. NextGen += [mutate(crossover(parents[p1], parents[p2]))]
  118. for i in range(nb_random):
  119. NextGen += [individu_alea()]
  120. return NextGen
  121.  
  122.  
  123. def EVOLUTION(nb_gen = 1000):
  124. Generation = first_gen()
  125. for i in range(nb_gen):
  126. Generation = next_gen(Generation)
  127. if (i % 50 == 0):
  128. print(fitness(Generation[1]))
  129. best = fittest(Generation)[1]
  130. print([round(i, 5) for i in best])
  131. trace_graphique(best)
  132.  
  133.  
  134. # ////////////////// #
  135. # AFFICHAGE #
  136. # ////////////////// #
  137.  
  138.  
  139.  
  140. def trace_graphique(individu):
  141. plt.xkcd()
  142. plt.clf()
  143. valeurs = np.arange(-3, 3, 0.01)
  144. f1 = fonction(valeurs)
  145. f2 = horner(individu, valeurs)
  146. f3 = horner([0,1,0,-1/6.0, 0, 1/120.0], valeurs)
  147. plt.subplot(111)
  148. plt.plot(f3, label="dl", color="pink")
  149. plt.plot(f1, label="sin", color="red")
  150. plt.plot(f2, label="poly", color="blue")
  151. plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=3, mode="expand", borderaxespad=0.)
  152. plt.ylabel("f(t)")
  153. plt.xlabel("t")
  154. plt.show('hold')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement