Created
November 21, 2017 14:57
-
-
Save fedden/d44062ba109e2732637063e00772accd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"np.set_printoptions(precision=3, suppress=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"scrolled": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"attempt 0\n", | |
" * fly solution: [ 7 8 10 2 9 3 5 4 6 1]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 1\n", | |
" * fly solution: [10 9 8 7 2 1 5 4 6 3]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 2\n", | |
" * fly solution: [ 8 7 9 10 2 3 5 1 6 4]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 3\n", | |
" * fly solution: [ 2 9 8 7 10 1 5 3 6 4]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 4\n", | |
" * fly solution: [ 7 10 8 2 9 3 1 6 4 5]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 5\n", | |
" * fly solution: [ 9 7 8 10 2 4 3 6 1 5]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 6\n", | |
" * fly solution: [ 7 8 9 10 2 6 5 4 3 1]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 7\n", | |
" * fly solution: [10 2 8 9 7 5 3 6 4 1]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 8\n", | |
" * fly solution: [ 2 10 8 7 9 5 6 4 3 1]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 9\n", | |
" * fly solution: [ 7 2 9 10 8 4 5 6 3 1]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 10\n", | |
" * fly solution: [ 9 8 10 2 7 1 6 3 5 4]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 11\n", | |
" * fly solution: [10 7 9 8 2 4 3 5 1 6]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 12\n", | |
" * fly solution: [10 2 8 7 9 4 1 6 5 3]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 13\n", | |
" * fly solution: [ 8 7 10 9 2 5 4 1 6 3]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 14\n", | |
" * fly solution: [ 7 8 10 9 2 6 5 1 3 4]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 15\n", | |
" * fly solution: [ 9 7 10 2 8 3 6 1 4 5]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 16\n", | |
" * fly solution: [ 9 8 7 10 2 3 6 4 1 5]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 17\n", | |
" * fly solution: [ 9 7 8 10 2 6 5 4 1 3]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 18\n", | |
" * fly solution: [10 7 9 2 8 1 3 6 5 4]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 19\n", | |
" * fly solution: [ 9 7 10 8 2 1 5 3 6 4]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 20\n", | |
" * fly solution: [ 9 2 7 8 10 5 6 4 1 3]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 21\n", | |
" * fly solution: [ 9 7 10 8 2 3 5 6 4 1]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 22\n", | |
" * fly solution: [ 9 10 7 8 2 4 3 6 1 5]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 23\n", | |
" * fly solution: [10 8 9 2 7 4 5 1 3 6]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 24\n", | |
" * fly solution: [ 8 2 7 10 9 6 3 5 1 4]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 25\n", | |
" * fly solution: [10 8 7 9 2 5 4 1 6 3]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 26\n", | |
" * fly solution: [ 7 8 9 2 10 1 3 5 4 6]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 27\n", | |
" * fly solution: [ 9 2 10 8 7 4 5 6 3 1]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 28\n", | |
" * fly solution: [ 2 7 8 10 9 1 6 4 3 5]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 29\n", | |
" * fly solution: [ 8 10 9 7 2 5 3 1 6 4]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 30\n", | |
" * fly solution: [ 8 2 10 7 9 1 4 3 6 5]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 31\n", | |
" * fly solution: [ 7 2 9 10 8 5 4 6 1 3]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 32\n", | |
" * fly solution: [ 8 7 10 9 2 1 4 3 6 5]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 33\n", | |
" * fly solution: [ 7 10 8 2 9 6 4 3 5 1]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 34\n", | |
" * fly solution: [ 7 9 10 8 2 3 5 1 6 4]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 35\n", | |
" * fly solution: [ 7 2 10 9 8 3 1 4 6 5]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 36\n", | |
" * fly solution: [10 7 2 8 9 5 3 6 1 4]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 37\n", | |
" * fly solution: [ 7 9 8 10 2 6 5 1 3 4]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 38\n", | |
" * fly solution: [ 7 9 10 2 8 3 5 6 4 1]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 39\n", | |
" * fly solution: [ 2 9 10 8 7 4 5 6 1 3]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 40\n", | |
" * fly solution: [10 8 7 2 9 4 5 6 3 1]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 41\n", | |
" * fly solution: [ 9 8 2 10 7 4 5 6 3 1]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 42\n", | |
" * fly solution: [ 8 9 2 7 10 6 1 5 3 4]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 43\n", | |
" * fly solution: [ 8 9 10 7 2 6 3 5 4 1]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 44\n", | |
" * fly solution: [ 9 10 2 8 7 5 4 3 1 6]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 45\n", | |
" * fly solution: [ 7 10 2 9 8 6 3 5 4 1]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 46\n", | |
" * fly solution: [ 7 2 9 8 10 4 5 6 1 3]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 47\n", | |
" * fly solution: [ 9 2 7 10 8 5 4 1 6 3]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 48\n", | |
" * fly solution: [ 2 10 8 7 9 3 1 4 6 5]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n", | |
"\n", | |
"attempt 49\n", | |
" * fly solution: [ 7 8 9 2 10 6 1 3 5 4]\n", | |
" * difference sum: 0\n", | |
" * difference product: 0\n" | |
] | |
} | |
], | |
"source": [ | |
"def create_random_population(size):\n", | |
" return np.random.random((size, 10))\n", | |
"\n", | |
"\n", | |
"def get_fitness(fly):\n", | |
" '''Less is better'''\n", | |
" fly_sorted_indices = np.argsort(fly)\n", | |
" fly_solution = fly_sorted_indices + 1\n", | |
" \n", | |
" assert np.sum(fly_solution) == 55\n", | |
" assert len(np.unique(fly_solution)) == 10\n", | |
" \n", | |
" difference_one = abs(36 - np.sum(fly_solution[:5]))\n", | |
" difference_two = abs(360 - np.product(fly_solution[-5:]))\n", | |
" return difference_one + difference_two\n", | |
"\n", | |
"\n", | |
"for iteration in range(50):\n", | |
" size = 100\n", | |
" disturbance_threshold = 0.01\n", | |
"\n", | |
" lower = -1.0\n", | |
" upper = 1.0\n", | |
" iteration_amount = 100\n", | |
"\n", | |
" population = create_random_population(size)\n", | |
"\n", | |
" all_time_best = None\n", | |
" all_time_best_score = np.finfo(np.float32).max\n", | |
"\n", | |
" best_neighbour = np.zeros_like(population[0])\n", | |
"\n", | |
" done = False\n", | |
"\n", | |
" while not done:\n", | |
"\n", | |
" fitnesses = np.array([get_fitness(p) for p in population])\n", | |
"\n", | |
" swarms_best_index = np.argmin(fitnesses)\n", | |
" swarms_best = population[swarms_best_index]\n", | |
"\n", | |
" if np.amin(fitnesses) < all_time_best_score:\n", | |
" all_time_best_score = np.amin(fitnesses)\n", | |
" all_time_best = swarms_best\n", | |
"\n", | |
" if all_time_best_score == 0:\n", | |
" done = True\n", | |
" break\n", | |
"\n", | |
" r = np.random.uniform(0.0, 1.0, population.shape)\n", | |
"\n", | |
" for i, p in enumerate(population):\n", | |
" left = (i - 1) if i is not 0 else len(population) - 1\n", | |
" right = (i + 1) if i is not (len(population) - 1) else 0\n", | |
"\n", | |
" best_neighbour = population[left] if fitnesses[left] < fitnesses[right] else population[right]\n", | |
"\n", | |
" for x in range(len(p)):\n", | |
" if r[i][x] < disturbance_threshold:\n", | |
" p[x] = np.random.uniform(lower, upper)\n", | |
" else:\n", | |
" update = swarms_best[x] - best_neighbour[x]\n", | |
" p[x] = best_neighbour[x] + np.random.uniform(0.0, 1.0) * update\n", | |
"\n", | |
"\n", | |
" solution = np.argsort(all_time_best) + 1\n", | |
" print('\\nattempt', iteration)\n", | |
" print(' * fly solution: ', solution)\n", | |
" print(' * difference sum: ', abs(36 - np.sum(solution[:5])))\n", | |
" print(' * difference product: ', abs(360 - np.product(solution[-5:])))\n" | |
] | |
} | |
], | |
"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.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment