首页 技术 正文
技术 2022年11月14日
0 收藏 386 点赞 3,506 浏览 2388 个字

代码地址:

https://github.com/guojun007/real_sga

本部分是采用实数编码的标准遗传算法,整体流程与上一篇二进制编码的基本一致,

主要区别在于本部分的交叉操作为模拟二进制交叉,即SBX ,

变异操作 为 多项式变异

real_sga/crossover/crossover.py

#实数编码,SBX交叉
def crossover(population, pcross_real, V, minRealVal, maxRealVal, eta_c):
for i in xrange(0, len(population), 2):
#如果随机概率大于交叉概率则不进行交叉操作
if random.random()>pcross_real:
continue #对两个个体执行SBX交叉操作
for j in xrange(V):
#对某自变量交叉
ylow=minRealVal[j]
yup=maxRealVal[j]
y1=population[i][j]
y2=population[i+1][j]
r=random.random()
if r<=0.5:
betaq=(2*r)**(1.0/(eta_c+1.0))
else:
betaq=(0.5/(1.0-r))**(1.0/(eta_c+1.0)) child1=0.5*( (1+betaq)*y1+(1-betaq)*y2 )
child2=0.5*( (1-betaq)*y1+(1+betaq)*y2 )
child1=min(max(child1, ylow), yup)
child2=min(max(child2, ylow), yup) population[i][j]=child1
population[i+1][j]=child2

以上代码是根据相关论文所写,是原始方式的化简版本。

以下给出官方的原始代码的 Python2.7   重构版。

#实数编码,SBX交叉
def crossover(population, pcross_real, V, minRealVal, maxRealVal, eta_c):
for i in xrange(0, len(population), 2):
#如果随机概率大于交叉概率则不进行交叉操作
if random.random()>pcross_real:
continue
#对两个个体执行SBX交叉操作
for j in xrange(V):
#判断是否对某自变量交叉
if random.random()>0.5:
continue
#如果两个体某自变量相等则不操作
if population[i][j]==population[i+1][j]:
continue
#对某自变量交叉
y1=min(population[i][j], population[i+1][j])
y2=max(population[i][j], population[i+1][j])
ylow=minRealVal[j]
yup=maxRealVal[j]
r=random.random()
beta=1.0+(2.0*(y1-ylow)/(y2-y1))
alpha=2.0-beta**( -(eta_c+1.0) )
if r<=(1.0/alpha):
betaq=(r*alpha)**(1.0/(eta_c+1.0))
else:
betaq=(1.0/(2.0-r*alpha))**(1.0/(eta_c+1.0))
child1=0.5*( (y1+y2)-betaq*(y2-y1) ) beta=1.0+(2.0*(yup-y2)/(y2-y1))
alpha=2.0-beta**( -(eta_c+1.0) )
if r<=(1.0/alpha):
betaq=(r*alpha)**(1.0/(eta_c+1.0))
else:
betaq=(1.0/(2.0-r*alpha))**(1.0/(eta_c+1.0))
child2=0.5*( (y1+y2)-betaq*(y2-y1) ) child1=min(max(child1, ylow), yup)
child2=min(max(child2, ylow), yup)
population[i][j]=child1
population[i+1][j]=child2

多项式变异:

real_sga/mutation/mutation.py

#Routine for real polynomial mutation of an individual
#实数编码的常规多项式变异def mutation(population, pmut_real, V, minRealVal, maxRealVal, eta_m):
for i in xrange(len(population)):
for j in xrange(V):
r=random.random()
#对个体某变量进行变异
if r<=pmut_real:
y=population[i][j]
ylow=minRealVal[j]
yup=maxRealVal[j]
delta1=1.0*(y-ylow)/(yup-ylow)
delta2=1.0*(yup-y)/(yup-ylow)
#delta=min(delta1, delta2)
r=random.random()
mut_pow=1.0/(eta_m+1.0)
if r<=0.5:
xy=1.0-delta1
val=2.0*r+(1.0-2.0*r)*(xy**(eta_m+1.0))
deltaq=val**mut_pow-1.0
else:
xy=1.0-delta2
val=2.0*(1.0-r)+2.0*(r-0.5)*(xy**(eta_m+1.0))
deltaq=1.0-val**mut_pow
y=y+deltaq*(yup-ylow)
y=min(yup, max(y, ylow))
population[i][j]=y

标准遗传算法(实数编码 python实现)模拟二进制交叉SBX  多项式变异

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,993
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845