Guest User

Untitled

a guest
Mar 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.84 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 34,
  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,99) for j in range(16)]\n",
  15. " population.append(arr)\n",
  16. "\n",
  17. " return population\n",
  18. "\n",
  19. "def clac_score(x):\n",
  20. " return np.mean(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": 49,
  55. "metadata": {
  56. "collapsed": true
  57. },
  58. "outputs": [],
  59. "source": [
  60. "import random\n",
  61. "import math\n",
  62. "import copy\n",
  63. "import numpy as np\n",
  64. "\n",
  65. "N_PARAM = 16\n",
  66. "N_POP = 500\n",
  67. "N_GEN = 40\n",
  68. "MUTATE_PROB = 0.35\n",
  69. "ELITE_RATE = 0.4\n",
  70. "\n",
  71. "\n",
  72. "def main():\n",
  73. " pop = [(-1, p) for p in get_population()]\n",
  74. " \n",
  75. " for g in range(N_GEN):\n",
  76. " print(\"Generation: \" + str(g))\n",
  77. " \n",
  78. " # Get elites\n",
  79. " fitness = evaluate(pop)\n",
  80. " elites = fitness[:int(len(pop)*ELITE_RATE)]\n",
  81. " \n",
  82. " # Cross and mutate\n",
  83. " pop = elites[:]\n",
  84. " while len(pop) < N_POP:\n",
  85. " if random.random() < MUTATE_PROB:\n",
  86. " m = random.randint(0, len(elites)-1)\n",
  87. " child = mutate(elites[m][1])\n",
  88. " else:\n",
  89. " c1 = random.randint(0, len(elites)-1)\n",
  90. " c2 = random.randint(0, len(elites)-1)\n",
  91. " child = crossover(elites[c1][1], elites[c2][1])\n",
  92. " pop.append((-1, child))\n",
  93. " \n",
  94. " # Evaluate individual \n",
  95. " fitness = evaluate(pop)\n",
  96. " elites = fitness[:int(len(pop)*ELITE_RATE)]\n",
  97. " \n",
  98. " print(pop[0])\n",
  99. " print()"
  100. ]
  101. },
  102. {
  103. "cell_type": "code",
  104. "execution_count": 50,
  105. "metadata": {},
  106. "outputs": [
  107. {
  108. "name": "stdout",
  109. "output_type": "stream",
  110. "text": [
  111. "Generation: 0\n",
  112. "(69.875, [74, 32, 50, 82, 59, 93, 61, 52, 71, 79, 65, 53, 91, 69, 96, 91])\n",
  113. "\n",
  114. "Generation: 1\n",
  115. "(69.875, [74, 32, 50, 82, 59, 93, 61, 52, 71, 79, 65, 53, 91, 69, 96, 91])\n",
  116. "\n",
  117. "Generation: 2\n",
  118. "(73.875, [86, 75, 94, 82, 59, 93, 61, 52, 71, 79, 65, 53, 91, 69, 96, 56])\n",
  119. "\n",
  120. "Generation: 3\n",
  121. "(75.125, [88, 47, 85, 92, 63, 85, 59, 97, 98, 90, 41, 82, 89, 79, 11, 96])\n",
  122. "\n",
  123. "Generation: 4\n",
  124. "(77.9375, [99, 75, 94, 87, 34, 86, 37, 69, 91, 95, 90, 88, 85, 71, 90, 56])\n",
  125. "\n",
  126. "Generation: 5\n",
  127. "(80.75, [97, 90, 37, 75, 65, 77, 92, 91, 98, 92, 9, 98, 93, 96, 87, 95])\n",
  128. "\n",
  129. "Generation: 6\n",
  130. "(80.75, [97, 90, 37, 75, 65, 77, 92, 91, 98, 92, 9, 98, 93, 96, 87, 95])\n",
  131. "\n",
  132. "Generation: 7\n",
  133. "(84.0625, [97, 92, 82, 76, 99, 96, 26, 71, 69, 96, 67, 96, 93, 89, 99, 97])\n",
  134. "\n",
  135. "Generation: 8\n",
  136. "(85.375, [99, 75, 94, 87, 93, 61, 76, 85, 99, 92, 72, 98, 93, 96, 90, 56])\n",
  137. "\n",
  138. "Generation: 9\n",
  139. "(89.25, [99, 58, 94, 87, 99, 98, 78, 85, 99, 92, 78, 88, 85, 96, 95, 97])\n",
  140. "\n",
  141. "Generation: 10\n",
  142. "(89.25, [99, 58, 94, 87, 99, 98, 78, 85, 99, 92, 78, 88, 85, 96, 95, 97])\n",
  143. "\n",
  144. "Generation: 11\n",
  145. "(90.3125, [88, 75, 94, 87, 93, 86, 99, 97, 91, 79, 78, 98, 93, 96, 95, 96])\n",
  146. "\n",
  147. "Generation: 12\n",
  148. "(91.625, [97, 90, 94, 87, 93, 73, 96, 97, 99, 92, 72, 98, 93, 96, 94, 95])\n",
  149. "\n",
  150. "Generation: 13\n",
  151. "(91.6875, [89, 92, 82, 76, 99, 73, 96, 97, 98, 95, 92, 98, 93, 96, 95, 96])\n",
  152. "\n",
  153. "Generation: 14\n",
  154. "(92.125, [99, 75, 94, 87, 93, 86, 99, 97, 98, 90, 97, 98, 93, 96, 98, 74])\n",
  155. "\n",
  156. "Generation: 15\n",
  157. "(92.625, [99, 75, 94, 87, 93, 73, 96, 97, 98, 95, 92, 98, 93, 96, 99, 97])\n",
  158. "\n",
  159. "Generation: 16\n",
  160. "(93.375, [99, 75, 94, 87, 93, 86, 99, 97, 98, 90, 97, 98, 93, 96, 95, 97])\n",
  161. "\n",
  162. "Generation: 17\n",
  163. "(94.5625, [99, 75, 94, 87, 99, 98, 96, 97, 99, 98, 92, 98, 93, 96, 95, 97])\n",
  164. "\n",
  165. "Generation: 18\n",
  166. "(94.5625, [99, 75, 94, 87, 99, 98, 96, 97, 99, 98, 92, 98, 93, 96, 95, 97])\n",
  167. "\n",
  168. "Generation: 19\n",
  169. "(94.625, [97, 90, 94, 87, 99, 86, 99, 97, 98, 90, 97, 98, 93, 96, 98, 95])\n",
  170. "\n",
  171. "Generation: 20\n",
  172. "(94.875, [99, 92, 94, 87, 93, 86, 99, 97, 98, 98, 92, 98, 93, 96, 99, 97])\n",
  173. "\n",
  174. "Generation: 21\n",
  175. "(95.875, [99, 92, 94, 87, 99, 98, 96, 97, 99, 98, 92, 98, 93, 96, 99, 97])\n",
  176. "\n",
  177. "Generation: 22\n",
  178. "(95.875, [99, 92, 94, 87, 99, 98, 96, 97, 99, 98, 92, 98, 93, 96, 99, 97])\n",
  179. "\n",
  180. "Generation: 23\n",
  181. "(95.875, [99, 92, 94, 87, 99, 98, 96, 97, 99, 98, 92, 98, 93, 96, 99, 97])\n",
  182. "\n",
  183. "Generation: 24\n",
  184. "(96.0625, [99, 92, 94, 87, 99, 98, 99, 97, 98, 98, 97, 98, 93, 96, 95, 97])\n",
  185. "\n",
  186. "Generation: 25\n",
  187. "(96.0625, [99, 92, 94, 87, 99, 98, 99, 97, 98, 98, 97, 98, 93, 96, 95, 97])\n",
  188. "\n",
  189. "Generation: 26\n",
  190. "(96.0625, [99, 92, 94, 87, 99, 98, 99, 97, 98, 98, 97, 98, 93, 96, 95, 97])\n",
  191. "\n",
  192. "Generation: 27\n",
  193. "(96.0625, [99, 92, 94, 87, 99, 98, 99, 97, 98, 98, 97, 98, 93, 96, 95, 97])\n",
  194. "\n",
  195. "Generation: 28\n",
  196. "(96.375, [97, 91, 94, 98, 99, 98, 96, 97, 99, 98, 92, 98, 93, 96, 99, 97])\n",
  197. "\n",
  198. "Generation: 29\n",
  199. "(96.5, [97, 91, 94, 98, 99, 98, 99, 97, 98, 98, 92, 98, 93, 96, 99, 97])\n",
  200. "\n",
  201. "Generation: 30\n",
  202. "(96.75, [99, 92, 94, 98, 99, 98, 99, 97, 98, 98, 97, 98, 93, 96, 95, 97])\n",
  203. "\n",
  204. "Generation: 31\n",
  205. "(96.75, [99, 92, 94, 98, 99, 98, 99, 97, 98, 98, 97, 98, 93, 96, 95, 97])\n",
  206. "\n",
  207. "Generation: 32\n",
  208. "(96.8125, [97, 91, 94, 98, 99, 98, 99, 97, 98, 98, 97, 98, 93, 96, 99, 97])\n",
  209. "\n",
  210. "Generation: 33\n",
  211. "(97.0, [99, 92, 94, 98, 99, 98, 99, 97, 98, 98, 97, 98, 93, 96, 99, 97])\n",
  212. "\n",
  213. "Generation: 34\n",
  214. "(97.0, [99, 92, 94, 98, 99, 98, 99, 97, 98, 98, 97, 98, 93, 96, 99, 97])\n",
  215. "\n",
  216. "Generation: 35\n",
  217. "(97.0625, [99, 92, 94, 98, 99, 98, 99, 97, 99, 98, 97, 98, 93, 96, 99, 97])\n",
  218. "\n",
  219. "Generation: 36\n",
  220. "(97.0625, [99, 92, 94, 98, 99, 98, 99, 97, 99, 98, 97, 98, 93, 96, 99, 97])\n",
  221. "\n",
  222. "Generation: 37\n",
  223. "(97.0625, [99, 92, 94, 98, 99, 98, 99, 97, 99, 98, 97, 98, 93, 96, 99, 97])\n",
  224. "\n",
  225. "Generation: 38\n",
  226. "(97.0625, [99, 92, 94, 98, 99, 98, 99, 97, 99, 98, 97, 98, 93, 96, 99, 97])\n",
  227. "\n",
  228. "Generation: 39\n",
  229. "(97.0625, [99, 92, 94, 98, 99, 98, 99, 97, 99, 98, 97, 98, 93, 96, 99, 97])\n",
  230. "\n"
  231. ]
  232. }
  233. ],
  234. "source": [
  235. "if __name__ == \"__main__\":\n",
  236. " main()"
  237. ]
  238. },
  239. {
  240. "cell_type": "code",
  241. "execution_count": null,
  242. "metadata": {
  243. "collapsed": true
  244. },
  245. "outputs": [],
  246. "source": []
  247. },
  248. {
  249. "cell_type": "code",
  250. "execution_count": null,
  251. "metadata": {},
  252. "outputs": [],
  253. "source": []
  254. },
  255. {
  256. "cell_type": "code",
  257. "execution_count": null,
  258. "metadata": {},
  259. "outputs": [],
  260. "source": []
  261. },
  262. {
  263. "cell_type": "code",
  264. "execution_count": null,
  265. "metadata": {
  266. "collapsed": true
  267. },
  268. "outputs": [],
  269. "source": []
  270. }
  271. ],
  272. "metadata": {
  273. "kernelspec": {
  274. "display_name": "Python 3",
  275. "language": "python",
  276. "name": "python3"
  277. },
  278. "language_info": {
  279. "codemirror_mode": {
  280. "name": "ipython",
  281. "version": 3
  282. },
  283. "file_extension": ".py",
  284. "mimetype": "text/x-python",
  285. "name": "python",
  286. "nbconvert_exporter": "python",
  287. "pygments_lexer": "ipython3",
  288. "version": "3.6.2"
  289. }
  290. },
  291. "nbformat": 4,
  292. "nbformat_minor": 2
  293. }
Add Comment
Please, Sign In to add comment