├── .gitignore ├── LICENSE ├── README.md └── cuckoo_search └── cuckoo_search.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 沈键 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Optimization_Algorithms 2 | Optimization algorithms coded in python! 3 | -------------------------------------------------------------------------------- /cuckoo_search/cuckoo_search.py: -------------------------------------------------------------------------------- 1 | # ---------------------------------------------------------------------- 2 | # cuckoo_search via levy flight for global optimization 3 | # -- coded by SJ2050 in 2019 4 | #----------------------------------------------------------------------- 5 | import numpy as np 6 | import scipy.special as sc_special 7 | 8 | version = '1.0.0' 9 | 10 | def cuckoo_search(n, m, fit_func, lower_boundary, upper_boundary, iter_num = 100,pa = 0.25, beta = 1.5, step_size = 0.1): 11 | """ 12 | Cuckoo search function 13 | --------------------------------------------------- 14 | Input parameters: 15 | n: Number of nests 16 | m: Number of dimensions 17 | fit_func: User defined fitness evaluative function 18 | lower_boundary: Lower bounary (example: lower_boundary = (-2, -2, -2)) 19 | upper_boundary: Upper boundary (example: upper_boundary = (2, 2, 2)) 20 | iter_num: Number of iterations (default: 100) 21 | pa: Possibility that hosts find cuckoos' eggs (default: 0.25) 22 | beta: Power law index (note: 1 < beta < 2) (default: 1.5) 23 | step_size: Step size scaling factor related to the problem's scale (default: 0.1) 24 | Output: 25 | The best solution and its value 26 | """ 27 | # get initial nests' locations 28 | nests = generate_nests(n, m, lower_boundary, upper_boundary) 29 | fitness = calc_fitness(fit_func, nests) 30 | 31 | # get the best nest and record it 32 | best_nest_index = np.argmax(fitness) 33 | best_fitness = fitness[best_nest_index] 34 | best_nest = nests[best_nest_index].copy() 35 | 36 | for _ in range(iter_num): 37 | nests = update_nests(fit_func, lower_boundary, upper_boundary, nests, best_nest, fitness, step_size) 38 | nests = abandon_nests(nests, lower_boundary, upper_boundary, pa) 39 | fitness = calc_fitness(fit_func, nests) 40 | 41 | max_nest_index = np.argmax(fitness) 42 | max_fitness = fitness[max_nest_index] 43 | max_nest = nests[max_nest_index] 44 | 45 | if (max_fitness > best_fitness): 46 | best_nest = max_nest.copy() 47 | best_fitness = max_fitness 48 | 49 | return (best_nest, best_fitness) 50 | 51 | def generate_nests(n, m, lower_boundary, upper_boundary): 52 | """ 53 | Generate the nests' locations 54 | --------------------------------------------------- 55 | Input parameters: 56 | n: Number of nests 57 | m: Number of dimensions 58 | lower_boundary: Lower bounary (example: lower_boundary = (-2, -2, -2)) 59 | upper_boundary: Upper boundary (example: upper_boundary = (2, 2, 2)) 60 | Output: 61 | generated nests' locations 62 | """ 63 | lower_boundary = np.array(lower_boundary) 64 | upper_boundary = np.array(upper_boundary) 65 | nests = np.empty((n, m)) 66 | 67 | for each_nest in range(n): 68 | nests[each_nest] = lower_boundary + np.array([np.random.rand() for _ in range(m)]) * (upper_boundary - lower_boundary) 69 | 70 | return nests 71 | 72 | def update_nests(fit_func, lower_boundary, upper_boundary, nests, best_nest, fitness, step_coefficient): 73 | """ 74 | This function is to get new nests' locations and use new better one to replace the old nest 75 | --------------------------------------------------- 76 | Input parameters: 77 | fit_func: User defined fitness evaluative function 78 | lower_boundary: Lower bounary (example: lower_boundary = (-2, -2, -2)) 79 | upper_boundary: Upper boundary (example: upper_boundary = (2, 2, 2)) 80 | nests: Old nests' locations 81 | best_nest: Nest with best fitness 82 | fitness: Every nest's fitness 83 | step_coefficient: Step size scaling factor related to the problem's scale (default: 0.1) 84 | Output: 85 | Updated nests' locations 86 | """ 87 | lower_boundary = np.array(lower_boundary) 88 | upper_boundary = np.array(upper_boundary) 89 | n, m = nests.shape 90 | # generate steps using levy flight 91 | steps = levy_flight(n, m, 1.5) 92 | new_nests = nests.copy() 93 | 94 | for each_nest in range(n): 95 | # coefficient 0.01 is to avoid levy flights becoming too aggresive 96 | # and (nest[each_nest] - best_nest) could let the best nest be remained 97 | step_size = step_coefficient * steps[each_nest] * (nests[each_nest] - best_nest) 98 | step_direction = np.random.rand(m) 99 | new_nests[each_nest] += step_size * step_direction 100 | # apply boundary condtions 101 | new_nests[each_nest][new_nests[each_nest] < lower_boundary] = lower_boundary[new_nests[each_nest] < lower_boundary] 102 | new_nests[each_nest][new_nests[each_nest] > upper_boundary] = upper_boundary[new_nests[each_nest] > upper_boundary] 103 | 104 | new_fitness = calc_fitness(fit_func, new_nests) 105 | nests[new_fitness > fitness] = new_nests[new_fitness > fitness] 106 | 107 | return nests 108 | 109 | def abandon_nests(nests, lower_boundary, upper_boundary, pa): 110 | """ 111 | Some cuckoos' eggs are found by hosts, and are abandoned.So cuckoos need to find new nests. 112 | --------------------------------------------------- 113 | Input parameters: 114 | nests: Current nests' locations 115 | lower_boundary: Lower bounary (example: lower_boundary = (-2, -2, -2)) 116 | upper_boundary: Upper boundary (example: upper_boundary = (2, 2, 2)) 117 | pa: Possibility that hosts find cuckoos' eggs 118 | Output: 119 | Updated nests' locations 120 | """ 121 | lower_boundary = np.array(lower_boundary) 122 | upper_boundary = np.array(upper_boundary) 123 | n, m = nests.shape 124 | for each_nest in range(n): 125 | if (np.random.rand() < pa): 126 | step_size = np.random.rand() * (nests[np.random.randint(0, n)] - nests[np.random.randint(0, n)]) 127 | nests[each_nest] += step_size 128 | # apply boundary condtions 129 | nests[each_nest][nests[each_nest] < lower_boundary] = lower_boundary[nests[each_nest] < lower_boundary] 130 | nests[each_nest][nests[each_nest] > upper_boundary] = upper_boundary[nests[each_nest] > upper_boundary] 131 | 132 | return nests 133 | 134 | def levy_flight(n, m, beta): 135 | """ 136 | This function implements Levy's flight. 137 | --------------------------------------------------- 138 | Input parameters: 139 | n: Number of steps 140 | m: Number of dimensions 141 | beta: Power law index (note: 1 < beta < 2) 142 | Output: 143 | 'n' levy steps in 'm' dimension 144 | """ 145 | sigma_u = (sc_special.gamma(1+beta)*np.sin(np.pi*beta/2)/(sc_special.gamma((1+beta)/2)*beta*(2**((beta-1)/2))))**(1/beta) 146 | sigma_v = 1 147 | 148 | u = np.random.normal(0, sigma_u, (n, m)) 149 | v = np.random.normal(0, sigma_v, (n, m)) 150 | 151 | steps = u/((np.abs(v))**(1/beta)) 152 | 153 | return steps 154 | 155 | def calc_fitness(fit_func, nests): 156 | """ 157 | calculate each nest's fitness 158 | --------------------------------------------------- 159 | Input parameters: 160 | fit_func: User defined fitness evaluative function 161 | nests: Nests' locations 162 | Output: 163 | Every nest's fitness 164 | """ 165 | n, m = nests.shape 166 | fitness = np.empty(n) 167 | 168 | for each_nest in range(n): 169 | fitness[each_nest] = fit_func(nests[each_nest]) 170 | 171 | return fitness 172 | 173 | if __name__=='__main__': 174 | def fit_func(nest): 175 | x, y = nest 176 | return 3*(1-x)**2*np.e**(-x**2-(y+1)**2) - 10*(x/5-x**3-y**5)*np.e**(-x**2-y**2) - (np.e**(-(x+1)**2-y**2))/3 177 | 178 | best_nest, best_fitness = cuckoo_search(25, 2, fit_func, [-3, -3], [3, 3], step_size = 0.4) 179 | 180 | print('最大值为:%.5f, 在(%.5f, %.5f)处取到!'%(best_fitness, best_nest[0], best_nest[1])) 181 | --------------------------------------------------------------------------------