多峰问题。

import random
import math
import numpy as np
import copy

#定义全局变量
global Xmin #定义域下限
global Xmax #定义域上限
Xmin = 0
Xmax = 7

class chrom:
def init(self,chrom_one,x_decode,y_decode,thisFitness):
self.chrom_one = chrom_one
self.x_decode = x_decode
self.y_decode = y_decode
self.thisFitness = thisFitness

#解码
def decode(chromosome,Xmin,Xmax):
chromosome.reverse()
chrom_x = chromosome[0:3]
chrom_y = chromosome[3:]
x = 0
y = 0
for i in range(len(chrom_x)):
#循环生成十进制
x = x + chrom_x[i] * (pow(2,i))
y = y + chrom_y[i] * (pow(2,i))
#控制种群在0,10的区间里
#xx = Xmin + x * (Xmax - Xmin) / (pow(2, 10) - 1)
#yy = Xmin +y * (Xmax - Xmin) / (pow(2, 10) - 1)
return x,y

#计算适应度值,也就是函数值。
def fitness(x,y):

sum = 0
for i in range(1, 6):
    sum = sum + (i * math.cos((i + 1) * x + i)) * (i * math.cos((i + 1) * y + i))
return sum
#return x**2+y**2

#初始化种群
def initpop(NP,chrom_L):
‘’’

:param NP:种群数目
:param chrom_L: 染色体长度
:return: 种群
'''
chrom_list = []
for i in range(NP):
    # 生成一个6长度的染色体
    chrom_one = [random.randint(0, 1) for i in range(chrom_L)]
    # 对该染色体进行解码
    chrom_x_decode,chrom_y_decode = decode(chrom_one, Xmin, Xmax)
    # 计算该染色体的适应度值
    chrom_fit = fitness(chrom_x_decode,chrom_y_decode)
    chrom_list.append(chrom(chrom_one,chrom_x_decode,chrom_y_decode, chrom_fit))
return chrom_list

#两点交叉
def Intersection(pop):
odd = pop[0::2]
even = pop[1::2]
new_pop = []
a, b = np.random.choice(len(pop[0].chrom_one), 2)
for i in range(len(odd)):
a1 = odd[i].chrom_one
a2 = even[i].chrom_one
if a < b:
a1_1 = a1[0:a] + a2[a:b - 1] + a1[b - 1:]
a2_1 = a2[0:a] + a1[a:b - 1] + a2[b - 1:]
elif a > b:
a1_1 = a1[0:b] + a2[b:a - 1] + a1[a - 1:]
a2_1 = a2[0:b] + a1[b:a - 1] + a2[a - 1:]
elif a == b:
a1_1 = a1
a2_1 = a2
this_decode1_x,this_decode1_y = decode(a1_1,Xmin,Xmax)
this_fitness1 = fitness(this_decode1_x,this_decode1_y)
new_pop.append(chrom(a1_1, this_decode1_x,this_decode1_y, this_fitness1))
this_decode2_x,this_decode2_y = decode(a2_1,Xmin,Xmax)
this_fitness2 = fitness(this_decode2_x,this_decode2_y)
new_pop.append(chrom(a2_1, this_decode2_x,this_decode2_y, this_fitness2))
return new_pop

#变异
def variation(pop):
new_pop = []
a = np.random.choice(len(pop[0].chrom_one), 1)
for i in range(len(pop)):
a1 = pop[i].chrom_one
if a1[a[0]] == 1:
a1[a[0]] = a1[a[0]] - 1
else:
a1[a[0]] = a1[a[0]] + 1
this_decode_x,this_decode_y = decode(a1,Xmin,Xmax)
this_fitness = fitness(this_decode_x,this_decode_y)
new_pop.append(chrom(a1, this_decode_x,this_decode_y, this_fitness))
return new_pop

#选择排序
def chance_sort(POP):
for i in range(len(POP)):
for j in range(len(POP) - 1):
if POP[i].thisFitness > POP[j].thisFitness:
POP[i], POP[j] = POP[j], POP[i]
return POP

#种群进化
def evolution(POP,NP,G):
#初始化种群
POP_all = []
for i in range(G):
POP_all = POP_all + POP
if np.random.random() < 0.8: # 以Pc的概率进行交叉结合
POP = Intersection(POP)
if np.random.random() < 0.1: # 以Pm的概率进行变异
POP = variation(POP)
POP_all = POP_all + POP
POP_end = chance_sort(POP_all)
POP = POP_end[0:NP]
POP_all = []
return POP

NP = 100 #种群数目
chrom_L = 6 #染色体长度
G = 2000 #最大进化代数
pc = 0.8 #交叉概率
pm = 0.1 #变异概率

#初始化种群
chrom_list = initpop(NP,chrom_L)
#开始进行迭代进化
best_result = evolution(chrom_list,NP,G)
#print(len(best_pop))
print(‘目前的最优种群为:’)

依次输出一下,查看结果如何。

for i in range(len(best_result)):
print(‘染色体:’,best_result[i].chrom_one)
print(‘x:’,best_result[i].x_decode)
print(‘y:’,best_result[i].y_decode)
print(‘适应度值:’,best_result[i].thisFitness)
print(’------------------------------’)
print(‘迭代100次后,最优解为\nx=’,best_result[len(best_result)-1].x_decode,’\ny=’,best_result[len(best_result)-1].y_decode)
print(‘最优解:’,best_result[len(best_result)-1].thisFitness)

最后再绘制的函数曲线看一看

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

创建一个图像窗口

fig = plt.figure()

在图像窗口添加3d坐标轴

ax = Axes3D(fig)

def func(x,y):

sum = 0
for i in range(1,6):
    sum = sum +(i*np.cos((i+1)*x+i))*(i*np.cos((i+1)*y+i))
return sum
#return x**2+y**2

x = np.linspace(0, 7, 100)
y = np.linspace(0, 7, 100)
x,y = np.meshgrid(x,y)
z = func(x,y)
ax.plot_surface(x,y,z,rstride = 1, cmap=‘rainbow’,cstride = 1)

scatter_x = np.array([ind.x_decode for ind in best_result])
scatter_y = np.array([ind.y_decode for ind in best_result])
scatter_z = np.array([ind.thisFitness for ind in best_result])
ax.scatter(scatter_x,scatter_y,scatter_z,c=‘black’)
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值