├── README.md └── CrowSearchAlgorithm.py /README.md: -------------------------------------------------------------------------------- 1 | # CrowSearchAlgorithmPython 2 | A Python implementation of Crow Search Algorithm 3 | 4 | ## Definition 5 | 6 | Crow Search Algorithm is a search algorithm proposed by Alireza Askarzadeh, in the year 2016, as a new metaheuristic method aimed at solution of the most varied engineering optimization problems. Askarzadeh based his idea on the behavior of crows to develop the algorithm. Therefore this can be classified as bioinspired (inspired by nature). 7 | 8 | A crow is an animal known to collect its food and when necessary to hide this food so that other predators do not have the opportunity to steal your food. This feature is developed by the fact that they watch other birds and where these birds store their stock of food, and as soon as the animals are no longer around, the crows come to the place and steal the food. Therefore, because they have these characteristics, the crows they understand the need to hide their food. 9 | 10 | In this sense, [Askarzadeh (2016)][1] defines 4 principles that form the basis of thinking for the construction of the algorithm: 11 | 12 | - Crows live in flocks; 13 | - Crows memorize the position of their hiding place; 14 | - Crows follow each other to steal food; 15 | - Crows protect your stock (by a probability). 16 | 17 | ## References 18 | 19 | [ASKARZADEH, Alireza. A novel metaheuristic method for solving constrained 20 | engineering optimization problems: Crow search algorithm. Computers & 21 | Structures, v. 169, p. 1-12, 2016.][1] 22 | 23 | [1]: https://www.sciencedirect.com/science/article/abs/pii/S0045794916300475 24 | 25 | -------------------------------------------------------------------------------- /CrowSearchAlgorithm.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------- 2 | # Citation details: 3 | # Alireza Askarzadeh, Anovel metaheuristic method for solving constrained 4 | # engineering optimization problems: Crow search algorithm, Computers & 5 | # Structures, Vol. 169, 1-12, 2016. 6 | # Programmed by Alireza Askarzadeh at Kerman Graduate # 7 | # University of Advanced Technology(KGUT) # 8 | # Date of programming: September 2015 # 9 | # ------------------------------------------------- 10 | # This demo only implements a standard version of CSA for minimization of 11 | # a standard test function(Sphere) on MATLAB 7.6.0 (R2008a). 12 | # ------------------------------------------------- 13 | # Note: 14 | # Due to the stochastic nature of meta-heuristc algorithms, different runs 15 | # may lead to slightly different results. 16 | # ------------------------------------------------- 17 | # Simple transcripted to SciLab by Luan Michel(github.com/Luan-Michel/CrowSearchAlgorithmPython) 18 | # Original in https: # www.mathworks.com/matlabcentral/fileexchange/56127-crow-search-algorithm 19 | 20 | import random #random Function 21 | import numpy #numpy operations 22 | import math #ceil function 23 | 24 | def init(n, pd, l, u): #init the matrix problem 25 | x = [] 26 | for i in range (n): 27 | x.append([]) 28 | for j in range (pd): 29 | x[i].append(l-(l-u)*(random.random())) 30 | return x 31 | 32 | def fitness(xn, n ,pd): #function for fitness calculation 33 | fitness = [] 34 | for i in range(n): 35 | fitness.append(0) 36 | for j in range(pd): 37 | fitness[i] = fitness[i]+pow(xn[(i, j)], 2) 38 | return fitness 39 | 40 | # variables initialization # 41 | pd = 10 #Problem dimension (number of decision variables) 42 | n = 20 #Flock (population) size 43 | ap = 0.1 #Awareness probability 44 | fl = 2 #Flight length (fl) 45 | l = -100 #Lower 46 | u = 100 #Uper 47 | x = numpy.matrix(init(n, pd, l, u)) 48 | xn = x.copy() 49 | ft = numpy.array(fitness(xn, n, pd)) 50 | mem = x.copy() #Memory initialization 51 | fit_mem = ft.copy() #Fitness of memory positions 52 | tmax = 1000 #Max numuber of iterations (itermax) 53 | ffit = numpy.array([]) # Best fit of each iteration 54 | 55 | #Iteration begin 56 | 57 | for t in range(tmax): 58 | 59 | num = numpy.array([random.randint(0, n-1) for _ in range(n)]) # Generation of random candidate crows for following (chasing) 60 | xnew = numpy.empty((n,pd)) 61 | for i in range (n): 62 | if(random.random() > ap): 63 | for j in range (pd): 64 | xnew[(i,j)] = x[(i,j)]+fl*((random.random())*(mem[(num[i],j)]-x[(i,j)])) 65 | else: 66 | for j in range (pd): 67 | xnew[(i, j)] = l-(l-u)*random.random() 68 | xn = xnew.copy() 69 | ft = numpy.array(fitness(xn, n, pd)) #Function for fitness evaluation of new solutions 70 | 71 | #Update position and memory# 72 | for i in range(n): 73 | if(xnew[i].all() > l and xnew[i].all() < u): 74 | x[i] = xnew[i].copy() #Update position 75 | if(ft[i] < fit_mem[i]): 76 | mem[i] = xnew[i].copy() #Update memory 77 | fit_mem[i] = ft[i] 78 | ffit = numpy.append(ffit, numpy.amin(fit_mem)) #Best found value until iteration t 79 | 80 | ngbest, = numpy.where(numpy.isclose(fit_mem, min(fit_mem))) 81 | print(mem[ngbest[0]]) 82 | --------------------------------------------------------------------------------