R语言GA遗传算法精品毕业设计完整版文档格式.docx
- 文档编号:13402779
- 上传时间:2022-10-10
- 格式:DOCX
- 页数:71
- 大小:377.68KB
R语言GA遗传算法精品毕业设计完整版文档格式.docx
《R语言GA遗传算法精品毕业设计完整版文档格式.docx》由会员分享,可在线阅读,更多相关《R语言GA遗传算法精品毕业设计完整版文档格式.docx(71页珍藏版)》请在冰豆网上搜索。
二进制编码
–real-valued:
实数浮点编码
–permutation:
问题涉及到重新排序的列表,字符串编码。
可求解TSP问题
•fitness:
适应度函数
•min:
解得下界(多元变量为一个向量)
•max:
解得上界(多元变量为一个向量)
•nBits:
一个种群用二进制编码的长度是多少(长度越大代表精度越高)
•population:
初始种群
•selection:
选择
•crossover:
交叉
变异
•popsize:
种群大小
•pcrossover:
交叉概率(默认0.8)
•pmutation:
变异概率(默认0.1)
•elitism:
代沟(默认情况下,前5%个体将在每个迭代中保留)
•maxiter:
最大迭代次数(默认100)
•maxfitness:
适应度函数的上界,GA搜索后中断
•keepBest:
是否保留每一代的最优解
•parallel:
是否采用并行运算
•monitor:
绘图用的,监控遗传算法的运行状况
•seed:
一个整数值包含随机数发生器的状态。
这个参数可以用来复制GA搜索的结果。
•通过gaControl设置默认的遗传算子。
检索当前设置操作:
–gaControl("
)
举例
1.一元函数:
|x|+cos(x)
•图像
library(GA)
##Loadingrequiredpackage:
foreach
iterators
##Package'
GA'
version2.2
##Type'
citation("
GA"
)'
forcitingthisRpackageinpublications.
f<
-function(x)abs(x)+cos(x)
curve(f,-20,20)
•求解最小值
fitness<
-function(x)-f(x)#由于这个函数默认求解最大值,所以我们求-f(x)的最大值
GA<
-ga(type="
fitness=fitness,min=-20,max=20)
GA#一个GA类型调用其属性用@符号
##Anobjectofclass"
ga"
##
##Call:
##ga(type="
##Availableslots:
##[1]"
call"
"
type"
min"
max"
##[5]"
nBits"
names"
popSize"
iter"
##[9]"
run"
maxiter"
suggestions"
population"
##[13]"
elitism"
pcrossover"
pmutation"
fitness"
##[17]"
summary"
bestSol"
fitnessValue"
solution"
summary(GA)
##+-----------------------------------+
##|GeneticAlgorithm|
##GAsettings:
##Type=real-valued
##Populationsize=50
##Numberofgenerations=100
##Elitism=2
##Crossoverprobability=0.8
##Mutationprobability=0.1
##Searchdomain=
##x1
##Min-20
##Max20
##GAresults:
##Iterations=100
##Fitnessfunctionvalue=-1.000193
##Solution=
##[1,]0.0001927091
plot(GA)#画出每代最佳的适应度函数值与每代的平均适应度函数值
abline(v=GA@solution,lty=3)
2.一元函数:
(X^2+X)cos(X)跟踪
-function(x)(x^2+x)*cos(x)#-10<
x<
10
curve(f,-10,10)
##编写自己的跟踪功能,点代表一个解
monitor<
-function(obj){
curve(f,-10,10,main=paste("
iteration="
obj@iter))
points(obj@population,obj@fitness,pch=20,col=2)
rug(obj@population,col=2)
Sys.sleep(0.2)
}
##Notrun:
fitness=f,min=-10,max=10,monitor=monitor)
##End(Notrun)orifyouwanttosuppressthetracingGA<
-ga(type=
##'
real-valued'
fitness=f,min=-10,max=10,monitor=NULL)
##Min-10
##Max10
##Fitnessfunctionvalue=47.70562
##[1,]6.560576
monitor(GA)
3.二元函数:
Rastrigin<
-function(x1,x2){
20+x1^2+x2^2-10*(cos(2*pi*x1)+cos(2*pi*x2))
x1<
-seq(-5.12,5.12,by=0.1)
x2<
-outer(x1,x2,Rastrigin)
persp3D(x1,x2,f,theta=50,phi=20)
filled.contour(x1,x2,f,color.palette=jet.colors)
fitness=function(x)-Rastrigin(x[1],x[2]),
min=c(-5.12,-5.12),max=c(5.12,5.12),popSize=50,maxiter=100)
##Iter=1|Mean=-39.27992|Best=-10.40742
##Iter=2|Mean=-31.0111|Best=-10.40742
##Iter=3|Mean=-26.18011|Best=-2.914293
##Iter=4|Mean=-20.67291|Best=-2.914293
##Iter=5|Mean=-15.1307|Best=-2.658799
##Iter=6|Mean=-19.99986|Best=-1.721727
##Iter=7|Mean=-18.2604|Best=-1.721727
##Iter=8|Mean=-17.1881|Best=-1.048029
##Iter=9|Mean=-17.54574|Best=-1.048029
##Iter=10|Mean=-14.1643|Best=-1.048029
##Iter=11|Mean=-16.28319|Best=-1.048029
##Iter=12|Mean=-13.65963|Best=-1.048029
##Iter=13|Mean=-11.87015|Best=-0.4797405
##Iter=14|Mean=-7.839771|Best=-0.4797405
##It
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 GA 遗传 算法 精品 毕业设计 完整版