Guest User

Untitled

a guest
Mar 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {
  7. "collapsed": true
  8. },
  9. "outputs": [],
  10. "source": [
  11. "def get_population():\n",
  12. " population = []\n",
  13. " for i in range(N_POP):\n",
  14. " arr = [random.randint(0,9) for j in range(10)]\n",
  15. " population.append(arr)\n",
  16. "\n",
  17. " return population\n",
  18. "\n",
  19. "def clac_score(x):\n",
  20. " return sum(x)\n",
  21. "\n",
  22. "def evaluate(pop):\n",
  23. " fitness = []\n",
  24. " for p in pop:\n",
  25. " if p[0] == -1:\n",
  26. " fitness.append((clac_score(p[1]), p[1]))\n",
  27. " else:\n",
  28. " fitness.append(p)\n",
  29. " fitness.sort()\n",
  30. " fitness.reverse()\n",
  31. " \n",
  32. " return fitness\n",
  33. "\n",
  34. "def mutate(parent):\n",
  35. " r = int(math.floor(random.random()*len(parent)))\n",
  36. " child = copy.deepcopy(parent)\n",
  37. " child[r] = (parent[r] + 1) % 2\n",
  38. " \n",
  39. " return child\n",
  40. "\n",
  41. "def crossover(parent1, parent2):\n",
  42. " length = len(parent1)\n",
  43. " r1 = int(math.floor(random.random()*length))\n",
  44. " r2 = r1 + int(math.floor(random.random()*(length-r1)))\n",
  45. " \n",
  46. " child = copy.deepcopy(parent1)\n",
  47. " child[r1:r2] = parent2[r1:r2]\n",
  48. "\n",
  49. " return child"
  50. ]
  51. },
  52. {
  53. "cell_type": "code",
  54. "execution_count": 6,
  55. "metadata": {
  56. "collapsed": true
  57. },
  58. "outputs": [],
  59. "source": [
  60. "import random\n",
  61. "import math\n",
  62. "import copy\n",
  63. "\n",
  64. "N_PARAM = 16\n",
  65. "N_POP = 500\n",
  66. "N_GEN = 20\n",
  67. "MUTATE_PROB = 0.2\n",
  68. "ELITE_RATE = 0.3\n",
  69. "\n",
  70. "\n",
  71. "\n",
  72. "\n",
  73. "def main():\n",
  74. " pop = [(-1, p) for p in get_population()]\n",
  75. " \n",
  76. " for g in range(N_GEN):\n",
  77. " print(\"Generation: \" + str(g))\n",
  78. " \n",
  79. " # Get elites\n",
  80. " fitness = evaluate(pop)\n",
  81. " elites = fitness[:int(len(pop)*ELITE_RATE)]\n",
  82. " \n",
  83. " # Cross and mutate\n",
  84. " pop = elites[:]\n",
  85. " while len(pop) < N_POP:\n",
  86. " if random.random() < MUTATE_PROB:\n",
  87. " m = random.randint(0, len(elites)-1)\n",
  88. " child = mutate(elites[m][1])\n",
  89. " else:\n",
  90. " c1 = random.randint(0, len(elites)-1)\n",
  91. " c2 = random.randint(0, len(elites)-1)\n",
  92. " child = crossover(elites[c1][1], elites[c2][1])\n",
  93. " pop.append((-1, child))\n",
  94. " \n",
  95. " # Evaluate individual \n",
  96. " fitness = evaluate(pop)\n",
  97. " elites = fitness[:int(len(pop)*ELITE_RATE)]\n",
  98. " \n",
  99. " print(pop[0])\n",
  100. " print()"
  101. ]
  102. },
  103. {
  104. "cell_type": "code",
  105. "execution_count": 7,
  106. "metadata": {},
  107. "outputs": [
  108. {
  109. "name": "stdout",
  110. "output_type": "stream",
  111. "text": [
  112. "Generation: 0\n",
  113. "(75, [9, 8, 9, 5, 7, 4, 8, 9, 8, 8])\n",
  114. "\n",
  115. "Generation: 1\n",
  116. "(76, [6, 9, 6, 7, 9, 6, 6, 9, 9, 9])\n",
  117. "\n",
  118. "Generation: 2\n",
  119. "(77, [9, 9, 9, 7, 8, 8, 9, 5, 8, 5])\n",
  120. "\n",
  121. "Generation: 3\n",
  122. "(80, [9, 8, 4, 7, 9, 9, 9, 9, 8, 8])\n",
  123. "\n",
  124. "Generation: 4\n",
  125. "(83, [9, 8, 7, 8, 8, 8, 8, 9, 9, 9])\n",
  126. "\n",
  127. "Generation: 5\n",
  128. "(87, [9, 8, 9, 9, 9, 9, 9, 9, 8, 8])\n",
  129. "\n",
  130. "Generation: 6\n",
  131. "(87, [9, 8, 9, 9, 9, 9, 9, 9, 8, 8])\n",
  132. "\n",
  133. "Generation: 7\n",
  134. "(88, [8, 9, 9, 9, 9, 9, 9, 9, 8, 9])\n",
  135. "\n",
  136. "Generation: 8\n",
  137. "(88, [9, 9, 9, 9, 9, 9, 8, 9, 8, 9])\n",
  138. "\n",
  139. "Generation: 9\n",
  140. "(88, [9, 9, 9, 9, 9, 9, 9, 9, 7, 9])\n",
  141. "\n",
  142. "Generation: 10\n",
  143. "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
  144. "\n",
  145. "Generation: 11\n",
  146. "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
  147. "\n",
  148. "Generation: 12\n",
  149. "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
  150. "\n",
  151. "Generation: 13\n",
  152. "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
  153. "\n",
  154. "Generation: 14\n",
  155. "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
  156. "\n",
  157. "Generation: 15\n",
  158. "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
  159. "\n",
  160. "Generation: 16\n",
  161. "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
  162. "\n",
  163. "Generation: 17\n",
  164. "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
  165. "\n",
  166. "Generation: 18\n",
  167. "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
  168. "\n",
  169. "Generation: 19\n",
  170. "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
  171. "\n"
  172. ]
  173. }
  174. ],
  175. "source": [
  176. "if __name__ == \"__main__\":\n",
  177. " main()"
  178. ]
  179. },
  180. {
  181. "cell_type": "code",
  182. "execution_count": null,
  183. "metadata": {
  184. "collapsed": true
  185. },
  186. "outputs": [],
  187. "source": []
  188. },
  189. {
  190. "cell_type": "code",
  191. "execution_count": null,
  192. "metadata": {
  193. "collapsed": true
  194. },
  195. "outputs": [],
  196. "source": []
  197. }
  198. ],
  199. "metadata": {
  200. "kernelspec": {
  201. "display_name": "Python 3",
  202. "language": "python",
  203. "name": "python3"
  204. },
  205. "language_info": {
  206. "codemirror_mode": {
  207. "name": "ipython",
  208. "version": 3
  209. },
  210. "file_extension": ".py",
  211. "mimetype": "text/x-python",
  212. "name": "python",
  213. "nbconvert_exporter": "python",
  214. "pygments_lexer": "ipython3",
  215. "version": "3.6.2"
  216. }
  217. },
  218. "nbformat": 4,
  219. "nbformat_minor": 2
  220. }
Add Comment
Please, Sign In to add comment