Implemented fully documented Cuckoo Search Optimization (CSO) algorithm via Levy flights (basic model) using Python.
- Numpy
- matplotlib
Once the installation is finished (download or cloning), go the cso folder and follow the below simple guidelines to execute CSO effectively (either write the code in command line or in a python editor).
>>> from cso import CSO
Next, a fitness function (or cost function) is required. I have included five different fitness functions for example purposes namely fitness_1
, fitness_2
, fitness_3
, fitness_4
and fitness_5
.
Minimize:
f(x) = (x2 + y - 11)2 + (x + y2 - 7)2
Optimum solution
: x = 3 ; y = 2
Minimize:
f(x) = (x + 2y - 7)2 + (2x + y - 5)2
Optimum solution
: x = 1 ; y = 3
Minimize:
f(x) = (1.5 - x - xy)2 + (2.25 - x + xy2)2 + (2.625 - x + xy3)2
Optimum solution
: x = 3 ; y = 0.5
Maximize:
f(x) = 2xy + 2x - x2 - 2y2
Optimum solution
: x = 2 ; y = 1
Minimize:
f(x) = -sin(x)(sin2m(x2/π)) - sin(y)(sin2m(2y2/π))
When the bound is (x,y) ∈ (0,5) x (0,5) and m=10 [1]
Optimum solution
: x = 2.20319 ; y = 1.57049
>>> from fitness import fitness_1, fitness_2, fitness_3, fitness_4, fitness_5
Now, if you want, you can provide bound values for all the particles (not mandatory) and optimize (minimize or maximize) the fitness function using CSO:
NOTE: a bool variable min=True
(default value) for MINIMIZATION PROBLEM and min=False
for MAXIMIZATION PROBLEM
>>> CSO(fitness=fitness_5, bound=[(0,5),(0,5)]).execute()
You will see the following similar output (there can be other minima as well):
OPTIMUM SOLUTION
> [2.2028966, 1.5707915]
OPTIMUM FITNESS
> -1.8013034
When fitness_4 is used, observe that min=False
since it is a Maximization problem.
>>> CSO(fitness=fitness_4, bound=[(-4,4),(-4,4)], min=False).execute()
You will see the following similar output:
OPTIMUM SOLUTION
> [2.0, 1.0]
OPTIMUM FITNESS
> 2.0
Incase you want to print the fitness value for each iteration, set verbose=True
(here Tmax=50
is the
maximum iteration)
>>> CSO(fitness=fitness_2, Tmax=50, verbose=True).execute()
You will see the following similar output:
Iteration: 0 | best global fitness (cost): 4.2060194
Iteration: 1 | best global fitness (cost): 4.2060194
Iteration: 2 | best global fitness (cost): 4.2060194
Iteration: 3 | best global fitness (cost): 4.2060194
Iteration: 4 | best global fitness (cost): 4.2060194
Iteration: 5 | best global fitness (cost): 3.0228358
Iteration: 6 | best global fitness (cost): 2.0454478
Iteration: 7 | best global fitness (cost): 1.647782
Iteration: 8 | best global fitness (cost): 0.2005788
Iteration: 9 | best global fitness (cost): 0.1981048
Iteration: 10 | best global fitness (cost): 0.1981048
Iteration: 11 | best global fitness (cost): 0.1981048
Iteration: 12 | best global fitness (cost): 0.1981048
Iteration: 13 | best global fitness (cost): 0.1981048
Iteration: 14 | best global fitness (cost): 0.1981048
Iteration: 15 | best global fitness (cost): 0.1981048
Iteration: 16 | best global fitness (cost): 0.1981048
Iteration: 17 | best global fitness (cost): 0.1981048
Iteration: 18 | best global fitness (cost): 0.0547217
Iteration: 19 | best global fitness (cost): 0.0367725
Iteration: 20 | best global fitness (cost): 0.0367725
Iteration: 21 | best global fitness (cost): 0.0367725
Iteration: 22 | best global fitness (cost): 0.0367725
Iteration: 23 | best global fitness (cost): 0.0367725
Iteration: 24 | best global fitness (cost): 0.0367725
Iteration: 25 | best global fitness (cost): 0.0367725
Iteration: 26 | best global fitness (cost): 0.0367725
Iteration: 27 | best global fitness (cost): 0.0367725
Iteration: 28 | best global fitness (cost): 0.0367725
Iteration: 29 | best global fitness (cost): 0.0367725
Iteration: 30 | best global fitness (cost): 0.0367725
Iteration: 31 | best global fitness (cost): 0.0367725
Iteration: 32 | best global fitness (cost): 0.0367725
Iteration: 33 | best global fitness (cost): 0.0367725
Iteration: 34 | best global fitness (cost): 0.0367725
Iteration: 35 | best global fitness (cost): 0.0367725
Iteration: 36 | best global fitness (cost): 0.0367725
Iteration: 37 | best global fitness (cost): 0.0196146
Iteration: 38 | best global fitness (cost): 0.0087851
Iteration: 39 | best global fitness (cost): 0.0087851
Iteration: 40 | best global fitness (cost): 0.0087851
Iteration: 41 | best global fitness (cost): 0.0087851
Iteration: 42 | best global fitness (cost): 0.0087851
Iteration: 43 | best global fitness (cost): 0.0087851
Iteration: 44 | best global fitness (cost): 0.0087851
Iteration: 45 | best global fitness (cost): 0.0087851
Iteration: 46 | best global fitness (cost): 0.0068151
Iteration: 47 | best global fitness (cost): 0.0068151
Iteration: 48 | best global fitness (cost): 0.0068151
Iteration: 49 | best global fitness (cost): 0.0068151
OPTIMUM SOLUTION
> [0.9965802, 3.0395979]
OPTIMUM FITNESS
> 0.0068151
Now, incase you want to plot the fitness value for each iteration, then set plot=True
(here Tmax=50
is the
maximum iteration)
>>> CSO(fitness=fitness_2, Tmax=50, plot=True).execute()
You will see the following similar output:
OPTIMUM SOLUTION
> [0.9965802, 3.0395979]
OPTIMUM FITNESS
> 0.0068151
In case you are interested in implementing Particle Swarm Optimization using Python, you can visit this link!
[1] X. Yang and Suash Deb, "Cuckoo Search via Lévy flights," 2009 World Congress on Nature & Biologically Inspired Computing (NaBIC), 2009, pp. 210-214, doi: 10.1109/NABIC.2009.5393690.