网上找不太到混沌映射的代码,所以本文参考21种混沌映射的相关公式,用python进行了实现,并填装成函数形式,可直接进行调用,主要实现了混沌映射生成初始种群。其中,21种混沌映射的公式可以参考这篇博客:改进策略:21种混沌映射方法-混沌初始化(附matlab代码)_如何使用混沌映射生成初始种群-CSDN博客
以后有时间可能会进行具体分析,以下是python代码,供以相互学习。
import numpy as np
'''
Chebyshev:Chebyshev混沌映射
Circle 混沌映射
Gauss/mouse 混沌映射
Iterative 混沌映射
Logistic 混沌映射
Piecewise 混沌映射
Sine 混沌映射
Singer 混沌映射
Sinusoidal 混沌映射
Tent 混沌映射
Fuch 混沌映射
SPM 混沌映射
ICMIC 混沌映射
Tent_Logistic_Cosine 混沌映射
Sine_Tent_Cosine 混沌映射
Logistic_Sine_Cosine 混沌映射
Henon 混沌映射
Cubic 混沌映射
Logistic-Tent 混沌映射
Bernoulli 混沌映射
Kent 混沌映射
'''
def Chebyshev(num,a=4,x0=None,size=1):
x=[]
if x0 is None:
x0 = np.random.uniform(-1,1,size=size)
for i in range(num):
x0 = np.cos(a*(np.cos(x0)**(-1)))
x.append(x0.copy())
return x
def Circle(num,a=0.5,b=0.2,x0=None,size=1):
x = []
if x0 is None:
x0 = np.random.uniform(0,1,size=size)
for i in range(num):
x0 = np.mod(x0+b-(a/(2*np.pi))*np.sin(2*np.pi*x0) ,1)
x.append(x0.copy())
return x
def Gauss_mouse(num,x0=None,size=1):
x = []
if x0 is None:
x0 = np.random.uniform(0,1,size=size)
for i in range(num):
for dim in range(size):
if x0[dim] == 0:
x0[dim]=1
else:
x0[dim]=np.mod(1/x0[dim],1)
x.append(x0.copy())
return x
def Iterative(num,a=0.7,x0=None,size=1):
x = []
if x0 is None:
x0 = np.random.uniform(-1,1,size=size)
for i in range(num):
x0 = np.sin((a*np.pi)/x0)
x.append(x0.copy())
return x
def Logistic(num,a=4,x0=None,size=1):
x = []
if x0 is None:
x0 = np.random.uniform(0,1,size=size)
for i in range(num):
x0 = a*x0*(1-x0)
x.append(x0.copy())
return x
def Piecewise(num,P=0.3,x0=None,size=1):
x = []
if x0 is None:
x0 = np.random.uniform(0,1,size=size)
for i in range(num):
for dim in range(size):
if 0<=x0[dim] < P:
x0[dim]=x0[dim]/P
elif P<=x0[dim]<=0.5:
x0[dim]=(x0[dim]-P)/(0.5-P)
elif 0.5<x0[dim]<=1-P:
x0[dim]=(1-P-x0[dim])/(0.5-P)
elif 1-P<x0[dim]<=1:
x0[dim]=(1-x0[dim])/P
x.append(x0.copy())
return x
def Sine(num,a=4,x0=None,size=1):
x = []
if x0 is None:
x0 = np.random.uniform(0,1,size=size)
for i in range(num):
x0 = (a/4)*np.sin(np.pi*x0)
x.append(x0.copy())
return x
def Singer(num,a=1.07,x0=None,size=1):
x = []
if x0 is None:
x0 = np.random.uniform(0,1,size=size)
for i in range(num):
x0 = a*(7.86*x0-23.31*(x0**2)+28.75*(x0**3)-13.302875*(x0**4))
x.append(x0.copy())
return x
def Sinusoidal(num,a=2.3,x0=None,size=1):
x = []
if x0 is None:
x0 = np.random.uniform(0.45,1,size=size)
for i in range(num):
x0 = a*(x0**2)*np.sin(np.pi*x0)
x.append(x0.copy())
return x
def Tent(num,a=0.7,x0=None,size=1):
x = []
if x0 is None:
x0 = np.random.uniform(0,1,size=size)
for i in range(num):
for dim in range(size):
if x0[dim]<a:
x0[dim]=x0[dim]/a
elif a<=x0[dim]:
x0[dim]=(1-x0[dim])/(1-a)
x.append(x0.copy())
return x
def Fuch(num,x0=None,size=1):
x = []
if x0 is None:
x0 = np.random.uniform(-1,1,size=size)
for i in range(num):
x0=np.cos(1/(x0**2))
x.append(x0.copy())
return x
def SPM(num,n=0.4,u=0.3,x0=None,size=1):
x = []
if x0 is None:
x0 = np.random.uniform(0,1,size=size)
for i in range(num):
for dim in range(size):
if 0<=x0[dim]<n:
x0[dim] = np.mod(x0[dim] / n + u * np.sin(np.pi * x0[dim]) + np.random.uniform(0, 1), 1)
elif n<=x0[dim]<0.5:
x0[dim] = np.mod((x0[dim] / n)/(0.5-n) + u * np.sin(np.pi * x0[dim]) + np.random.uniform(0, 1), 1)
elif 0.5<=x0[dim]<1-n:
x0[dim] = np.mod(((1-x0[dim]) / n)/(0.5-n) + u * np.sin(np.pi * (1-x0[dim])) + np.random.uniform(0, 1), 1)
elif 1-n<=x0[dim]<=1:
x0[dim] = np.mod((1-x0[dim]) / n + u * np.sin(np.pi * (1-x0[dim])) + np.random.uniform(0, 1), 1)
x.append(x0.copy())
return x
def ICMIC(num,a=5,x0=None,size=1):
x = []
if x0 is None:
x0 = np.random.uniform(-1,1,size=size)
for i in range(num):
x0 = np.sin(a/x0)
x.append(x0.copy())
return x
def Tent_Logistic_Cosine(num,x0=None,size=1):
x = []
if x0 is None:
x0 = np.random.uniform(0,1,size=size)
for i in range(num):
for dim in range(size):
r = np.random.uniform(0,1)
if x0[dim]<0.5:
x0[dim] = np.cos(np.pi * (2 * r * x0[dim] + 4 * (1 - r) * x0[dim] * (1 - x0[dim]) - 0.5))
elif 0.5<=x0[dim]<=1:
x0[dim] = np.cos(np.pi * (2 * r * (1-x0[dim]) + 4 * (1 - r) * x0[dim] * (1 - x0[dim]) - 0.5))
x.append(x0.copy())
return x
def Sine_Tent_Cosine(num,x0=None,size=1):
x = []
if x0 is None:
x0 = np.random.uniform(0,1,size=size)
for i in range(num):
for dim in range(size):
r = np.random.uniform(0, 1)
if x0[dim] < 0.5:
x0[dim] = np.cos(np.pi * (r * np.sin(np.pi * x0[dim]) + 2 * (1 - r) * x0[dim] - 0.5))
elif 0.5 <= x0[dim] <= 1:
x0[dim] = np.cos(np.pi * (r * np.sin(np.pi * x0[dim]) + 2 * (1 - r) * (1-x0[dim]) - 0.5))
x.append(x0.copy())
return x
def Logistic_Sine_Cosine(num,x0=None,size=1):
x = []
if x0 is None:
x0 = np.random.uniform(0,1,size=size)
for i in range(num):
r = np.random.uniform(0,1)
x0 = np.cos(np.pi * (4*r*x0*(1-x0)+(1-r)*np.sin(np.pi*x0)-0.5))
x.append(x0.copy())
return x
def Henon(num,a=1.4,b=0.3,x0=None,y0=None,size=1):
x = []
if x0 is None or y0 is None:
x0 = np.random.uniform(0,1,size=size)
y0 = np.random.uniform(0,1,size=size)
for i in range(num):
x1 = 1 + y0 - a * (x0**2)
y1 = b * x0
x0,y0 = x1,y1
x.append(x0.copy())
return x
def Cubic(num,a=2.595,x0=None,size=1):
x = []
if x0 is None:
x0 = np.random.uniform(0,1,size=size)
for i in range(num):
x0 = a * x0 * (1 - (x0**2))
x.append(x0.copy())
return x
def Logistic_Tent(num,x0=None,size=1):
x = []
if x0 is None:
x0 = np.random.uniform(0,1,size=size)
for i in range(num):
r = np.random.uniform(0,1)
for dim in range(size):
if x0[dim]<0.5:
x0[dim] = np.mod(r * x0[dim] * (1 - x0[dim]) + ((4 - r) * x0[dim]) / 2, 1)
elif 0.5<=x0[dim]<=1:
x0[dim] = np.mod(r * x0[dim] * (1 - x0[dim]) + ((4 - r) * (1-x0[dim])) / 2, 1)
x.append(x0.copy())
return x
def Bernoulli(num,a=0.3,x0=None,size=1):
x = []
if x0 is None:
x0 = np.random.uniform(0,1,size=size)
for i in range(num):
for dim in range(size):
if x0[dim]<=1-a:
x0[dim] = (x0[dim])/(1-a)
elif 1-a<x0[dim]<=1:
x0[dim] = (x0[dim]-1+a)/a
x.append(x0.copy())
return x
def Kent(num,a=0.3,x0=None,size=1):
x = []
if x0 is None:
x0 = np.random.uniform(0,1,size=size)
for i in range(num):
for dim in range(size):
if x0[dim]<=a:
x0[dim]=x0[dim]/a
elif a<x0[dim]<1:
x0[dim] = (1-x0[dim])/(1-a)
x.append(x0.copy())
return x