遗传算法的优化(运筹优化5-启发式优化算法之遗传算法)
遗传算法的优化(运筹优化5-启发式优化算法之遗传算法)class GA: def __init__(self size chrom_size cp mp gen_max num): #size种群数量 #chrom_size染色体长度 #cp交叉概率 #mp变异概率 #gen_max进化最大世代数 #num变量数 self.individuals = [] self.fitness = [] self.selector_probability = [] self.new_individuals = [] self.num = num self.elitist = {'chromosome': [0] * num 'fitness': 0 'age': 0}
[雾霾]补上节划过的水...
- 直接上问题:求解函数
待求解的目标函数
2.将目标函数定义适应度函数(刚发现可以插入代码块,这看起来更给力,抱歉之前太业余了,没发现这功能的强大)
def fitness(l):
x = l[0]
y = l[1]
k = l[2]
Z = (0.5 - (np.sin(np.sqrt(x ** 2 y ** 2)) ** 2 - 0.5) / (1 0.001 * (x ** 2 y ** 2) ** 2)) / (
0.001 * (k ** 2 y ** 2) ** 2)
return Z
如取x y k分别为1,2,3 即l = [1 2 3]
运行print('计算l=[1 2 3]的适应度值为:' fitness([1 2 3]))
得到:
适应度计算示例
3.构建GA算法
class GA:
def __init__(self size chrom_size cp mp gen_max num):
#size种群数量
#chrom_size染色体长度
#cp交叉概率
#mp变异概率
#gen_max进化最大世代数
#num变量数
self.individuals = []
self.fitness = []
self.selector_probability = []
self.new_individuals = []
self.num = num
self.elitist = {'chromosome': [0] * num 'fitness': 0 'age': 0}
self.size = size
self.chromosome_size = chrom_size
self.crossover_probability = cp
self.mutation_probability = mp
self.generation_max = gen_max
self.age = 0
v = 2 ** self.chromosome_size - 1
for i in range(self.size):
t = []
s = []
for j in range(num):
t.append(random.randint(0 v))
s.append(0)
self.individuals.append(t)
self.new_individuals.append(s)
self.fitness.append(0)
self.selector_probability.append(0)
#==========================================以上为变量初始化
# 染色体解码
def decode(self interval chromosome):
s = []
for i in range(self.num):
d = interval[i][1] - interval[i][0]
n = float(2 ** self.chromosome_size - 1)
t = interval[i][0] chromosome[i] * d / n
s.append(t)
return s
例如:x y k的取值范围分别为 [[-10.0 10.0] [-10.0 10.0] [-10.0 10.0]],则处理结果时将[69 359 3948]解码为[-9.663003663003662 -8.246642246642246 9.282051282051281]
#计算每组染色的适应度值
def fitness_func(self interval chromosome):
l = self.decode(interval chromosome)
func = fitness(l)
return func
def evaluate(self interval):
sp = self.selector_probability
for i in range(self.size):
self.fitness[i] = self.fitness_func(interval self.individuals[i])
ft_sum = sum(self.fitness)
for i in range(self.size):
sp[i] = self.fitness[i] / float(ft_sum)
for i in range(1 self.size):
sp[i] = sp[i] sp[i - 1]
#例如初始化选择概率均为0,[0 0 0 0 0 0 ...]计算后为[0.08128979780831412 0.4931327413520626 0.05090787956742299 ...]
# 轮盘赌法选择
def select(self):
(t i) = (random.random() 0)
for p in self.selector_probability:
if p > t:
break
i = i 1
return i
# 交叉
def cross(self chrom1 chrom2):
p = random.random()
n = 2 ** self.chromosome_size - 1
if chrom1 != chrom2 and p < self.crossover_probability:
t = random.randint(1 self.chromosome_size - 1)
mask = n << t
(r1 r2) = (chrom1 & mask chrom2 & mask)
mask = n >> (self.chromosome_size - t)
(l1 l2) = (chrom1 & mask chrom2 & mask)
(chrom1 chrom2) = (r1 l2 r2 l1)
return (chrom1 chrom2)
# 变异
def mutate(self chrom):
p = random.random()
if p < self.mutation_probability:
t = random.randint(1 self.chromosome_size)
mask1 = 1 << (t - 1)
mask2 = chrom & mask1
if mask2 > 0:
chrom = chrom & (~mask2)
else:
chrom = chrom ^ mask1
return chrom
# 每代中保留最佳个体
def reproduct_elitist(self):
j = -1
for i in range(self.size):
if self.elitist['fitness'] < self.fitness[i]:
j = i
self.elitist['fitness'] = self.fitness[i]
if (j >= 0):
self.elitist['chromosome'][0] = self.individuals[j][0]
self.elitist['chromosome'][1] = self.individuals[j][1]
self.elitist['age'] = self.age
#每次所有个体的计算
def evolve(self interval):
indvs = self.individuals
new_indvs = self.new_individuals
self.evaluate(interval)
i = 0
while True:
idv1 = self.select()
idv2 = self.select()
while True:
t = random.randint(0 self.num - 1)
s = random.randint(0 self.num - 1)
if t != s:
(idv1_x idv1_y) = (indvs[idv1][t] indvs[idv1][s])
(idv2_x idv2_y) = (indvs[idv2][t] indvs[idv2][s])
break
(idv1_x idv2_x) = self.cross(idv1_x idv2_x)
(idv1_y idv2_y) = self.cross(idv1_y idv2_y)
(idv1_x idv1_y) = (self.mutate(idv1_x) self.mutate(idv1_y))
(idv2_x idv2_y) = (self.mutate(idv2_x) self.mutate(idv2_y))
(new_indvs[i][t] new_indvs[i][s]) = (idv1_x idv1_y)
(new_indvs[i 1][t] new_indvs[i 1][s]) = (idv2_x idv2_y)
i = i 2
if i >= self.size:
break
self.reproduct_elitist()
for i in range(self.size):
for j in range(self.num):
self.individuals[i][j] = self.new_individuals[i][j]
def run(self interval):
for i in range(self.generation_max):
self.evolve(interval)
fitn = self.elitist['fitness']
x = self.elitist['chromosome']
x = self.decode(interval x)
return x fitn
4. 程序运行
(1)种群数量为 10,染色体长度为 15,交叉概率为 0.7,变异概率为 0.2 进化最大世代数为 30 变量数3
if __name__ == '__main__':
pop = GA (10 12 0.7 0.2 30 3)
interval = [[-10.0 10.0] [-10.0 10.0] [-10.0 10.0]]
t = pop.run(interval)
print(t)
plt.plot(range(pop.generation_max) pop.gene_fitn_best 'b')
plt.show()
算法收敛结果:
收敛曲线1
输出结果:([3.7875457875457883 0.0073260073260073 -10.0] 315395913.31734186)
(2) 种群数量为 20,染色体长度为 12,交叉概率为 0.7,变异概率为 0.2 进化最大世代数为 80 变量数3
if __name__ == '__main__':
pop = GA (20 12 0.7 0.2 80 3)
interval = [[-10.0 10.0] [-10.0 10.0] [-10.0 10.0]]
t = pop.run(interval)
print(t)
plt.plot(range(pop.generation_max) pop.gene_fitn_best 'r')
plt.show()
收敛曲线2
输出结果:([0.0024420024420024333 -0.0024420024420024333 -10.0] 7029921141473.557)
感兴趣的点个赞吧[中国赞]
后续找个案例上来