Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "def get_population():\n",
- " population = []\n",
- " for i in range(N_POP):\n",
- " arr = [random.randint(0,9) for j in range(10)]\n",
- " population.append(arr)\n",
- "\n",
- " return population\n",
- "\n",
- "def clac_score(x):\n",
- " return sum(x)\n",
- "\n",
- "def evaluate(pop):\n",
- " fitness = []\n",
- " for p in pop:\n",
- " if p[0] == -1:\n",
- " fitness.append((clac_score(p[1]), p[1]))\n",
- " else:\n",
- " fitness.append(p)\n",
- " fitness.sort()\n",
- " fitness.reverse()\n",
- " \n",
- " return fitness\n",
- "\n",
- "def mutate(parent):\n",
- " r = int(math.floor(random.random()*len(parent)))\n",
- " child = copy.deepcopy(parent)\n",
- " child[r] = (parent[r] + 1) % 2\n",
- " \n",
- " return child\n",
- "\n",
- "def crossover(parent1, parent2):\n",
- " length = len(parent1)\n",
- " r1 = int(math.floor(random.random()*length))\n",
- " r2 = r1 + int(math.floor(random.random()*(length-r1)))\n",
- " \n",
- " child = copy.deepcopy(parent1)\n",
- " child[r1:r2] = parent2[r1:r2]\n",
- "\n",
- " return child"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "import random\n",
- "import math\n",
- "import copy\n",
- "\n",
- "N_PARAM = 16\n",
- "N_POP = 500\n",
- "N_GEN = 20\n",
- "MUTATE_PROB = 0.2\n",
- "ELITE_RATE = 0.3\n",
- "\n",
- "\n",
- "\n",
- "\n",
- "def main():\n",
- " pop = [(-1, p) for p in get_population()]\n",
- " \n",
- " for g in range(N_GEN):\n",
- " print(\"Generation: \" + str(g))\n",
- " \n",
- " # Get elites\n",
- " fitness = evaluate(pop)\n",
- " elites = fitness[:int(len(pop)*ELITE_RATE)]\n",
- " \n",
- " # Cross and mutate\n",
- " pop = elites[:]\n",
- " while len(pop) < N_POP:\n",
- " if random.random() < MUTATE_PROB:\n",
- " m = random.randint(0, len(elites)-1)\n",
- " child = mutate(elites[m][1])\n",
- " else:\n",
- " c1 = random.randint(0, len(elites)-1)\n",
- " c2 = random.randint(0, len(elites)-1)\n",
- " child = crossover(elites[c1][1], elites[c2][1])\n",
- " pop.append((-1, child))\n",
- " \n",
- " # Evaluate individual \n",
- " fitness = evaluate(pop)\n",
- " elites = fitness[:int(len(pop)*ELITE_RATE)]\n",
- " \n",
- " print(pop[0])\n",
- " print()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Generation: 0\n",
- "(75, [9, 8, 9, 5, 7, 4, 8, 9, 8, 8])\n",
- "\n",
- "Generation: 1\n",
- "(76, [6, 9, 6, 7, 9, 6, 6, 9, 9, 9])\n",
- "\n",
- "Generation: 2\n",
- "(77, [9, 9, 9, 7, 8, 8, 9, 5, 8, 5])\n",
- "\n",
- "Generation: 3\n",
- "(80, [9, 8, 4, 7, 9, 9, 9, 9, 8, 8])\n",
- "\n",
- "Generation: 4\n",
- "(83, [9, 8, 7, 8, 8, 8, 8, 9, 9, 9])\n",
- "\n",
- "Generation: 5\n",
- "(87, [9, 8, 9, 9, 9, 9, 9, 9, 8, 8])\n",
- "\n",
- "Generation: 6\n",
- "(87, [9, 8, 9, 9, 9, 9, 9, 9, 8, 8])\n",
- "\n",
- "Generation: 7\n",
- "(88, [8, 9, 9, 9, 9, 9, 9, 9, 8, 9])\n",
- "\n",
- "Generation: 8\n",
- "(88, [9, 9, 9, 9, 9, 9, 8, 9, 8, 9])\n",
- "\n",
- "Generation: 9\n",
- "(88, [9, 9, 9, 9, 9, 9, 9, 9, 7, 9])\n",
- "\n",
- "Generation: 10\n",
- "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
- "\n",
- "Generation: 11\n",
- "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
- "\n",
- "Generation: 12\n",
- "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
- "\n",
- "Generation: 13\n",
- "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
- "\n",
- "Generation: 14\n",
- "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
- "\n",
- "Generation: 15\n",
- "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
- "\n",
- "Generation: 16\n",
- "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
- "\n",
- "Generation: 17\n",
- "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
- "\n",
- "Generation: 18\n",
- "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
- "\n",
- "Generation: 19\n",
- "(90, [9, 9, 9, 9, 9, 9, 9, 9, 9, 9])\n",
- "\n"
- ]
- }
- ],
- "source": [
- "if __name__ == \"__main__\":\n",
- " main()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.6.2"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Add Comment
Please, Sign In to add comment