python 遗传算法求函数极值的实现代码

作者:zzzzjh 时间:2023-08-29 11:36:11 

废话不多说,大家直接看代码吧!


"""遗传算法实现求函数极大值—Zjh"""
import numpy as np
import random
import matplotlib.pyplot as plt
class Ga():
"""求出二进制编码的长度"""
def __init__(self):
 self.boundsbegin = -2
 self.boundsend = 3
 precision = 0.0001 # 运算精确度
 self.Bitlength = int(np.log2((self.boundsend - self.boundsbegin)/precision))+1#%染色体长度
 self.popsize = 50# 初始种群大小
 self.Generationmax = 12# 最大进化代数
 self.pcrossover = 0.90# 交叉概率
 self.pmutation = 0.2# 变异概率
 self.population=np.random.randint(0,2,size=(self.popsize,self.Bitlength))

"""计算出适应度"""
def fitness(self,population):
 Fitvalue=[]
 cumsump = []
 for i in population:
  x=self.transform2to10(i)#二进制对应的十进制
  xx=self.boundsbegin + x * (self.boundsend - self.boundsbegin) / (pow(2,self.Bitlength)-1)
  s=self.targetfun(xx)
  Fitvalue.append(s)
 fsum=sum(Fitvalue)
 everypopulation=[x/fsum for x in Fitvalue]
 cumsump.append(everypopulation[0])
 everypopulation.remove(everypopulation[0])
 for j in everypopulation:
  p=cumsump[-1]+j
  cumsump.append(p)
 return Fitvalue,cumsump
"""选择两个基因,准备交叉"""
def select(self,cumsump):
 seln=[]
 for i in range(2):
  j = 1
  r=np.random.uniform(0,1)
  prand =[x-r for x in cumsump]
  while prand[j] < 0:
   j = j + 1
  seln.append(j)
 return seln
"""交叉"""
def crossover(self, seln, pc):
 d=self.population[seln[1]].copy()
 f=self.population[seln[0]].copy()
 r=np.random.uniform()
 if r<pc:
  print('yes')
  c=np.random.randint(1,self.Bitlength-1)
  print(c)
  a=self.population[seln[1]][c:]
  b=self.population[seln[0]][c:]
  d[c:]=b
  f[c:]=a
  print(d)
  print(f)
  g=d
  h=f
 else:
  g=self.population[seln[1]]
  h=self.population[seln[0]]
 return g,h
"""变异操作"""
def mutation(self,scnew,pmutation):
 r=np.random.uniform(0, 1)
 if r < pmutation:
  v=np.random.randint(0,self.Bitlength)
  scnew[v]=abs(scnew[v]-1)
 else:
  scnew=scnew
 return scnew

"""二进制转换为十进制"""
def transform2to10(self,population):
 #x=population[-1] #最后一位的值
 x=0
 #n=len(population)
 n=self.Bitlength
 p=population.copy()
 p=p.tolist()
 p.reverse()
 for j in range(n):
  x=x+p[j]*pow(2,j)
 return x #返回十进制的数
"""目标函数"""
def targetfun(self,x):
 #y = x∗(np.sin(10∗(np.pi)∗x))+ 2
 y=x*(np.sin(10*np.pi*x))+2
 return y

if __name__ == '__main__':
Generationmax=12
gg=Ga()
scnew=[]
ymax=[]
#print(gg.population)
Fitvalue, cumsump=gg.fitness(gg.population)
Generation = 1
while Generation < Generationmax +1:
 Fitvalue, cumsump = gg.fitness(gg.population)
 for j in range(0,gg.popsize,2):
  seln = gg.select( cumsump) #返回选中的2个个体的序号
  scro = gg.crossover(seln, gg.pcrossover) #返回两条染色体
  s1=gg.mutation(scro[0],gg.pmutation)
  s2=gg.mutation(scro[1],gg.pmutation)
  scnew.append(s1)
  scnew.append(s2)
 gg.population = scnew
 Fitvalue, cumsump = gg.fitness(gg.population)
 fmax=max(Fitvalue)
 d=Fitvalue.index(fmax)
 ymax.append(fmax)
 x = gg.transform2to10(gg.population[d])
 xx = gg.boundsbegin + x * (gg.boundsend - gg.boundsbegin) / (pow(2, gg.Bitlength) - 1)
 Generation = Generation + 1
Bestpopulation = xx
Targetmax = gg.targetfun(xx)
print(xx)
print(Targetmax)

x=np.linspace(-2,3,30)
y=x*(np.sin(10*np.pi*x))+2
plt.scatter(2.65,4.65,c='red')
plt.xlim(0,5)
plt.ylim(0,6)
plt.plot(x,y)
plt.annotate('local max', xy=(2.7,4.8), xytext=(3.6, 5.2),arrowprops=dict(facecolor='black', shrink=0.05))
plt.show()

一个函数求极值的仿真的作业,参考了别人的matlab代码,用python复现了一遍,加深印象!

来源:https://blog.csdn.net/zzzzjh/article/details/80633573

标签:python,遗传算法,函数,极值
0
投稿

猜你喜欢

  • Python之字典添加元素的几种方法

    2022-11-13 13:37:23
  • Python利用networkx画图绘制Les Misérables人物关系

    2021-03-31 07:41:54
  • MySQL数据库性能优化之索引优化

    2012-05-08 07:16:37
  • python向企业微信发送文字和图片消息的示例

    2021-09-18 15:42:08
  • 分享6个Go处理字符串的技巧小结

    2024-01-30 17:39:07
  • Python提取支付宝和微信支付二维码的示例代码

    2021-04-08 19:38:42
  • Python 使用@property对属性进行数据规范性校验的实现

    2021-03-31 00:37:10
  • 解读Python中字典的key都可以是什么

    2023-09-23 05:29:37
  • python3.7.3版本和django2.2.3版本是否可以兼容

    2022-08-18 09:31:23
  • Python 调用VC++的动态链接库(DLL)

    2023-06-19 09:01:44
  • sqlserver锁表、解锁、查看销表的方法

    2024-01-12 15:57:38
  • Python使用tkinter模块实现GUI界面的学生信息管理系统流程分步详解

    2022-04-06 18:09:53
  • python3+PyQt5 实现Rich文本的行编辑方法

    2023-09-29 14:37:47
  • SQL Server开发智能提示插件SQL Prompt介绍

    2024-01-23 01:02:54
  • 详解python函数的闭包问题(内部函数与外部函数详述)

    2023-01-22 20:53:01
  • Python集合之set和frozenset的使用详解

    2021-06-26 22:41:20
  • Python docx库代码演示

    2021-12-11 12:18:02
  • Pandas如何对Categorical类型字段数据统计实战案例

    2023-03-01 20:02:26
  • 详解python日期时间处理

    2021-08-20 17:07:53
  • Python OpenCV直方图均衡化详解

    2022-11-03 17:35:35
  • asp之家 网络编程 m.aspxhome.com