资源简介
人工免疫算法,用于计算函数的极值问题,收敛性极佳,迭代不到10次就可以找到最优解,而且与理论解完全吻合
代码片段和文件信息
import numpy as np
import matplotlib.pylab as plt
import timeit
import xlwt
def initialize_pop(mn):
pop = np.random.randint(02(mn))
return pop
def value2to10(x):
val = np.zeros(x.shape[0])
for i in range(3):
val = val + x[: i] * (2 ** (2-i))
for i in range(3 x.shape[1]):
val = val + x[:i]*(2**-(x.shape[1]-i-1))
return val
def affinityFunction(x):
#f = x + 10*np.sin(5*x) + 7*np.cos(4*x)
#f = - x**2 + 2*x +3
f = 1/3*x**3-5*x**2+9*x
return f
def bubbleSort(x y):
for i in range(len(y)):
for j in range(len(y)-i-1):
if y[j] < y[j+1]:
y[j] y[j+1] = y[j+1] y[j]
x[j:] x[j+1:] = x[j+1:] x[j:]
return x
def activate(pop m n Fn Ncl):
y = affinityFunction(value2to10(pop))
sortpop = bubbleSort(pop y)
for i in range(int(m*(1-Fn))):
ca = np.tile(sortpop[i :] (Ncl 1))
for j in range(1 Ncl):
ind = np.ceil((n-1)*np.random.rand(3))
for k in range(3):
if ca[j int(ind[k])] == 0:
评论
共有 条评论