# Cuckoo search, problem with Levy flight

In cuckoo search, in some point you have to generate new solution using formula $$x_k(t+1) = x_k(t) + \alpha L(s, \lambda),$$ where $$L(s,\lambda)=\frac{\lambda\Gamma(\lambda)\sin(\pi\lambda/2)}{\pi}\frac{1}{s^{\lambda+1}},$$ $$s$$ is number from Levy distribution. Formula for $$s$$: $$s=\frac{U}{|V|^{1/\lambda}},$$ where $$U\sim N(0, 1), V\sim N(0, \sigma^2)$$ and $$\sigma^2=\left(\frac{\Gamma(1+\lambda)\cdot\sin(\pi\lambda/2)}{\lambda\Gamma((1+\lambda)/2)}\right)^{1/\lambda}.$$ I tried to implement this algorithm in Python, here is my code:

from math import * from scipy import special import numpy as np lam = 1.5 alfa = 0.01 U = np.random.normal(0, 1, 1)[0] sigma = (gamma(1 + lam) * sin(pi * lam/2)/(lam*gamma((1+lam)/2)))**(1/lam) V = np.random.normal(0, sigma, 1)[0] s = U/abs(V)**(1/lam) L = lam * gamma(lam) * sin(pi*lam/2)/((s**(1 + lam))*pi)  print(L) 

It doesn’t always work, I mean $$L$$ is not a number sometimes. Where is the mistake? I am using the book mentioned below.

Xin-She Yang: Nature-Inspired Optimization Algorithms