版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、GA包遗传算法最大化使用遗传算法的适应度函数。默认求最大值。用法:ga(type=c(binary,real-valued,permutation),fitness,.,min,max,nBits,population二gaControl(type)population,selection=gaControl(type)selection,crossover二gaControl(type)crossover,mutation=gaControl(type)mutation,popSize=50,pcrossover=0.8,pmutation=0.1,elitism=base:max(1,ro
2、und(popSize*0.05),maxiter=100,run=maxiter,maxfitness=Inf,names=NULL,suggestions=NULL,keepBest=FALSE,parallel=FALSE,monitor=gaMonitor,seed=NULL)参数说明 type:解得编码类型- binary:二进制编码- real-valued:实数浮点编码- permutation:问题涉及到重新排序的列表,字符串编码。可求解TSP问题 fitness:适应度函数 min:解得下界(多元变量为一个向量) max:解得上界(多元变量为一个向量) nBits:一个种群用
3、二进制编码的长度是多少(长度越大代表精度越高) population:初始种群 selection:选择 crossover:交叉 crossover:变异 popsize:种群大小 pcrossover:交叉概率(默认0.8) pmutation:变异概率(默认0.1) elitism:代沟(默认情况下,前5%个体将在每个迭代中保留) maxiter:最大迭代次数(默认100) maxfitness:适应度函数的上界,GA搜索后中断 keepBest:是否保留每一代的最优解 parallel:是否采用并行运算 monitor:绘图用的,监控遗传算法的运行状况 seed:一个整数值包含随机数发
4、生器的状态。这个参数可以用来复制GA搜索的结果。 通过gaControl设置默认的遗传算子。检索当前设置操作:gaControl(binary)gaControl(real-valued)gaControl(permutation)举例1.一元函数:|x|+cos(x)图像library(GA)#Loadingrequiredpackage:foreach#Loadingrequiredpackage:iterators#PackageGAversion2.2#Typecitation(GA)forcitingthisRpackageinpublications.f-function(x)ab
5、s(x)+cos(x)curve(f,-20,20)fitness-function(x)-f(x)#由于这个函数默认求解最大值,所以我们求的最大值GA-ga(type=real-valued,fitness=fitness,min=-20,max=20)GA#一个GA类型调用其属性腌符号#Anobjectofclassga#-20,max=20)maxiterpopulationfitnesssolution#Call:#ga(type=real-valued,fitness=fitness,min=#Availableslots:#1calltypemin#5nBitsnamespopSi
6、ze#9runmaxitersuggestions#13elitismpcrossoverpmutation#17summarybestSolfitnessValuesummary(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=
7、100#Fitnessfunctionvalue=-1.000193#Solution=#x1#1,0.0001927091plot(GA)#画出每代最佳的适应度函数值与每代的平均适应度函数值Illi0204060180I100Generationcurve(f,-20,20)abline(v=GAsolution,lty=3)Median1lgooOLvnfflAtntnE匚lLL2.一元函数:(X4+X)cos(X)跟踪f-funetion(x)(xA2+x)*cos(x)#TOx10curve(f,-10,10)x#编写自己的跟踪功能,点代表一个解monitor-function(obj
8、)curve(f,-10,10,main=paste(iteration=,objiter)points(objpopulation,objfitness,pch=20,col=2)rug(objpopulation,col=2)Syssleep(0.2)monito#Notrun:GA-ga(type=real-valued,fitness=f,min=-10,max=10,r=monitor)iteration=1Xiteration=2Xiteration=3Xiteration=4Xiteration=5Xiteration=6Xiteration=7Xiteration=8Xiter
9、ation=9Xiteration=10Xiteration=11iteration=12Xiteration=13Xiteration=14xiteration=15Xiteration=16xiteration=17Xiteration=18xiteration=19Xiteration=20xiteration=21Xiteration=22xiteration=23Xiteration=24xiteration=25Xiteration=26xiteration=27Xiteration=28xiteration=29Xiteration=30xiteration=31Xiterati
10、on=32xiteration=33Xiteration=34xiteration=35Xiteration=36xiteration=37Xiteration=38xiteration=39Xiteration=40xiteration=41Xiteration=42xiteration=43Xiteration=44xiteration=45Xiteration=46xiteration=47Xiteration=48xiteration=49Xiteration=50xiteration=51Xiteration=52xiteration=53Xiteration=54xiteratio
11、n=55Xiteration=56xiteration=57Xiteration=58xiteration=59Xiteration=60xiteration=61Xiteration=62xiteration=63Xiteration=64xiteration=65Xiteration=66xiteration=67Xiteration=68xiteration=69Xiteration=70xiteration=71Xiteration=72xiteration=73Xiteration=74xiteration=75Xiteration=76xiteration=77Xiteration
12、=78xiteration=79Xiteration=80xiteration=81Xiteration=82xiteration=83Xiteration=84xiteration=85Xiteration=86xiteration=87Xiteration=88xiteration=89Xiteration=90xiteration=91Xiteration=92xiteration=93Xiteration=94xiteration=95Xiteration=96xiteration=97Xiteration=98xsummary(GA)#+#|GeneticAlgorithm|#+#G
13、Asettings:#Type=real-valued#Populationsize=50#Numberofgenerations=100#Elitism=2#Crossoverprobability=0.8#Mutationprobability=0.1#Searchdomain=#x1#Min-10#Max10#GAresults:#Iterations=100#Fitnessfunctionvalue=47.70562#Solution=#x1#1,6.560576monitor(GA)abline(v=GAsolution,lty=3)Rastrigin-function(x1,x2)
14、20+x1A2+x2A2-10*(cos(2*pi*x1)+cos(2*pi*x2)x1-seq(-5.12,5.12,by=0.1)x2-seq(-5.12,5.12,by=0.l)f-outer(x1,x2,Rastrigin)persp3D(x1,x2,f,theta=50,phi=20)2-420ooo864o2xGA-ga(type=real-valued,fitness=function(x)-Rastrigin(x1,2),min=c(-5.12,-5.12),max=c(5.12,5.12),popSize=50,maxiter100)#Iter1#Iter2#Iter3#It
15、er4#Iter5#Iter6#Iter7#Iter8#Iter9#Iter10#Iter11#Iter12#Iter13#Iter14#Iter15#Iter16#Iter17#Iter18#Iter19#Iter20#Iter21#Iter22#Iter23#Iter24#Iter25#Iter26#Iter27#Iter28#Iter29#Iter30#Iter31#Iter32#Iter33#Iter34#Iter35#Iter36#Iter37#Iter38#Iter39#Iter40#Iter41#Iter42#Iter43#Iter44#Iter45MeanMeanMeanMea
16、nMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMeanMean39.27992|Best=-10.4074231.0111|Best=-10.4074226.18011Best=-2.91429320.67291Best=-2.91429315.1307|Best=-2.65879919.99986|Best=-1.7217271
17、8.2604Best=-1.72172717.1881Best=-1.04802917.54574-14.164316.2831913.6596311.870157.83977111.134810.5660712.2144813.2479411.3487311.8221613.6860614.3082716.022315.6546311.672857.9149654.4819996.2481985.4831815.5809084.2274215.3003622.9817982.2627363.0290854.6045546.6314023.7474846.9932525.8874995.201
18、9844.7809124.5265626.1322987.34724Best-1.048029Best-1.048029Best-0.4797405Best-0.4797405Best=-0.1781359Best-0.1781359Best-0.1781359Best-0.1781359Best-0.1781359Best-0.1211027Best-0.1211027Best-0.1112675Best=-0.1112675Best-0.1112675Best-0.09198465Best-0.09198465Best-0.04204961Best-0.0003423884Best-0.0
19、003423884Best-0.0003423884Best-0.0003423884Best-0.0003423884Best-0.0003423884Best-0.0002433344Best-0.0002433344Best-0.0002433344Best-0.0002433344Best-0.0002433344Best-0.0002433344Best-0.0002433344Best-0.0002433344Best-0.0002433344Best-0.0002433344Best-0.0002433344Best=-1.048029Best=-1.048029|Best=-0
20、.0002433344#Iter46Mean-5.142651Best=-0.0002433344#Iter47Mean-4.627236Best=-0.0002433344#Iter48Mean-3.455913Best=-0.0002433344#Iter49Mean-5.630922Best=-0.0002433344#Iter50Mean-3.851845Best=-0.0002433344#Iter51Mean-2.834154Best=-0.0002433344#Iter52Mean-4.332291Best=-0.0001900013#Iter53Mean-6.833839Bes
21、t=-0.0001900013#Iter54Mean-7.181622Best=-0.0001900013#Iter55Mean-4.769373Best=-0.0001900013#Iter56Mean-3.684805Best=-0.0001900013#Iter57Mean-3.353003Best=-0.0001900013#Iter58Mean-4.455591Best=-0.0001900013#Iter59Mean-4.625651Best=-0.0001900013#Iter60Mean-4.464767Best=-0.0001437472#Iter61Mean-6.60411
22、8Best=-0.0001437472#Iter62Mean-3.275529Best=-0.0001437472#Iter63Mean-1.914675Best=-0.0001437472#Iter64Mean-2.418252Best=-0.0001437472#Iter65Mean-1.298085Best=-0.0001437472#Iter66Mean-2.243991Best=-0.0001437472#Iter67Mean-1.148524Best=-9.664828e-05#Iter68Mean-0.650877Best=-9.664828e-05#Iter69Mean-3.5
23、54919Best=-9.664828e-05#Iter70Mean-1.86329|Best=-9.664828e-05#Iter71Mean-1.896993Best=-9.664828e-05#Iter72Mean-3.957162Best=-9.664828e-05#Iter73Mean-3.655072Best=-9.664828e-05#Iter74Mean-3.664436Best=-9.664828e-05#Iter75Mean-4.751035Best=-9.664828e-05#Iter76Mean-3.658323Best=-9.664828e-05#Iter77Mean
24、-1.789493Best=-9.664828e-05#Iter78Mean-4.643992Best=-9.664828e-05#Iter79Mean-5.266699Best=-9.664828e-05#Iter80Mean-4.878061Best=-9.664828e-05#Iter81Mean-7.787437Best=-9.664828e-05#Iter82Mean-9.124848Best=-9.664828e-05#Iter83Mean-11.40333Best=-9.664828e-05#Iter84Mean-9.988992Best=-9.664828e-05#Iter85
25、Mean-8.851527Best=-9.664828e-05#Iter86Mean-7.240974Best=-9.664828e-05#Iter87Mean-7.269581Best=-9.664828e-05#Iter88Mean-6.899626Best=-9.664828e-05#Iter89Mean-5.597969Best=-9.664828e-05#Iter90Mean-4.74366|Best=-9.664828e-05#Iter91Mean-2.997341Best=-9.664828e-05#Iter92Mean-5.535812Best=-9.664828e-05#It
26、er93Mean-5.361464Best=-8.873609e-05#Iter94Mean-8.108462Best=-8.873609e-05#Iter95Mean-8.058939Best=-8.873609e-05#Iter=96#Iter=97#Iter=98#Iter=99#Iter=100Mean=-3.677557Mean=-6.304357Mean=-5.293325Mean=-6.088588|Mean=-5.860671Best=Best=Best=Best=|Best=8.873609e-058.873609e-056.569813e-066.569813e-06-6.
27、569813e-06summary(GA)#+#|GeneticAlgorithm|#+#GAsettings:#Type=real-valued#Populationsize=50#Numberofgenerations=100#Elitism=2#Crossoverprobability=0.8#Mutationprobability=0.1#Searchdomain#x1x2#Min-5.12-5.12#Max5.125.12#GAresults:#Iterations=100#Fitnessfunctionvalue=-6.569813e-06#Solution=#x1x2#1,-0.
28、0001763244-4.499996e-05plot(GA)0ovor0.entoASSfu匚lLL020406080100Generation4.对例3采用并行遗传算法,不知道为什么进行并行运算老师出错,附上代码20+X12+X22-10(cos(2兀X1)+cos(2nX2)Rastrigin-function(x1,x2)20+x1A2+x2A2-10*(cos(2*pi*x1)+cos(2*pi*x2)systemtime(GA1-ga(type=real-valued,fitness=function(x)-Rastrigin(x1,x2),min=c(-5.12,-5.12),m
29、ax=c(5.12,5.12),popSize=50,maxiter=100,monitor=FALSE,seed=12345)#usersystemelapsed#0.150.000.16GA1fitness#最后一代的种群适应度值#1-2.225098e-04-9.550619e-02-4.538940e-01-2.723767e+00-1.128641e+01#6-3.855740e-01-6.463223e-03-1.297195e-01-1.518032e-01-6.268278e+00#11-7.217925e-02-6.039854e-02-1.906179e+01-7.1196
30、03e-02-1.550847e+01#16-1.642796e+01-7.030944e-02-3.891715e-02-1.934336e+01-1.966749e-01#21-3.962517e+01-6.423513e-04-1.855974e-02-1.751280e+00-4.427695e+00#26-1.712183e-01-2.023108e+01-2.154413e-05-7.804141e+00-2.980788e-01#31-4.597700e-02-2.096966e-02-6.463223e-03-9.039056e-03-4.899872e-01#36-6.285
31、863e+00-7.376722e-06-8.060323e+00-2.271313e+00-4.822281e-02#41-1.750354e+00-7.342478e-02-1.040912e-01-2.121289e+00-2.728822e+01#46-5.028242e-01-1.446651e+01-1.968160e-01-7.295192e-02-1.062578e+01GA1fitnessValue#最佳适应度值#1-7.376722e-06GA1solution#最佳解#x1x2#1,-0.00013937520.0001332557# system.time(GA2-ga
32、(type=real-valued,fitness=function(x)# -Rastrigin(x1,x2),min=c(-5.12,-5.12),max=c(5.12,5.12),# popSize=50,maxiter=100,monitor=FALSE,seed=12345,parallel# TRUE)5类型permutation的用法,解决TSP问题library(xlsx)city=readxlsx(city.xlsx,1,header=F)#各个城市坐标city#展示数据#X1X2#113042312#236391315#341772244#437121399#5348815
33、35#633261556#732381229#841961004#94312790#104386570#1130071970#1225621756#1327881491#1423811676#151332695#1637151678#1739182179#1840612370#1937802212#2036762578#2140292838#2242632931#2334291908#2435072367#2533942643#2634393201#2729353240#2831403550#2925452357#3027782826#3123702975n=dim(city)1#多少个城市j
34、uli=dist(city)m_juli=as.matrix(juli)#距离矩阵bb=0f-function(x)for(iin1:n)if(i=n)bb=bb+m_julixi,x1elsebb=bb+m_julixi,xi+1-return(bb)systemtime(GA3-ga(type=permutation,fitness=function(x)-f(x),min=1,max=31,popSize=100,maxiter=200,monitor=FALSE)#usersystemelapsed#3.120.003.12as.numeric(GA3solution)#路线图#198
35、2416561123191731822212024252628273029#2431115141213710GA3fitnessValue#最优路线的长度,注意我们求的是于(x)的最大值#1-15483.39=19,*图形对比library(ggplot2)c=ascharacter(rownames(city)citys=cbind(city,c)#names(citys)#1X1X2cp=ggplot(data=citys,aes(x=X1,y=X2)+geom_point(shapesize=4,col=red)+theme_bw()size=pp=p+geom_text(data=ci
36、tys,aes(x=X1,y=X2,label=c),4,vjust=1.5)ppp=pp+geom_path(linetype=2)+ggtitle(初始顺序)library(dplyr)jie=data.frame(最优解=as.factor(GA3solution)jie#最优解#最优解#19#28#32#44#516#65#76#811#923#1019#1117#123#1318#1422#1521#1620#1724#1825#1926#2028#2127#2230#2329#2431#251#2615#2714#2812#2913#307#3110citys#X1X2c#1130423121#2363913152#3417722443#4371213994#53488153
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- T/CRRA 0301-2023绿色再生塑料 产销监管链
- T/TMAC 231-2025电子器件用金刚石热沉片
- T/GDLC 022-2024温室气体 产品碳足迹 量化方法与要求 凝胶糖果
- T/CI 479-2024区域复合水灾害特征变异识别规程
- T/CABC 13-2024产品数字护照 动力蓄电池唯一编码
- T/CRHA 088-2024病理免疫组织化学检测质控品要求
- 2025~2026学年黑龙江齐齐哈尔市龙江县人教版上册第一次学情自测教学质量抽测六年级数学试卷【附答案】
- 2025-2026学年吉林省榆树市初中政治八年级期末上册自测高频题(附答案)
- 2027届江苏省南通市田家炳中学七年级数学第一学期期末检测模拟试题含解析
- 前台接待访客登记、引导与通知流程方案
- 2026年皖豫天一大联考高三10月英语试题(含答案)
- 2026-2027学年四年级上学期统编版语文单元练习卷 第一单元-1(含答案)
- 【应用方案】2026智慧农业农业机器人智能感知与精准作业一体化设计方案
- 高标准农田建设项目初步设计技术规程(NYT 5490-2026 )
- CSCO非小细胞肺癌诊疗指南(2026版)
- 压缩空气储能地下洞库施工方案
- T∕CCEAS008-2026 建设工程造价咨询成果文件质量标准
- (2026年)护理查对制度课件
- 第10课《养成遵纪守法好习惯》第1框《增强遵纪守法意识》-【中职思政】《职业道法与法治》课件(高教版2023基础模块)
- 口腔侵入性操作目录
- 班主任基本功大赛-情景答辩23题(中学组)
评论
0/150
提交评论