收藏 分销(赏)

程序设计及其应用Java实训指导书.doc

上传人:a199****6536 文档编号:2832015 上传时间:2024-06-07 格式:DOC 页数:56 大小:402.04KB
下载 相关 举报
程序设计及其应用Java实训指导书.doc_第1页
第1页 / 共56页
程序设计及其应用Java实训指导书.doc_第2页
第2页 / 共56页
程序设计及其应用Java实训指导书.doc_第3页
第3页 / 共56页
程序设计及其应用Java实训指导书.doc_第4页
第4页 / 共56页
程序设计及其应用Java实训指导书.doc_第5页
第5页 / 共56页
点击查看更多>>
资源描述

1、程序设计及应用实训指引书(Java语言)昆明理工大学信息工程与自动化学院自动化系张志坤 李勇编8月一、题目题目:运用遗传算法仿真猕猴桃组培苗变异二、目(1)掌握Java语言开发环境搭建,掌握Java基本编程知识。(2)学习遗传算法基本原理,运用遗传算法进行猕猴桃遗传变异模仿。(3)运用excel进行遗传变异数据分析,掌握数据分析办法三、指引教师张志坤、李勇四、地点昆明理工大学呈贡校区信自楼445。五、时间及进度安排时间:短学期(8月31日至9月9日)进度安排:日 期环节进程安排摘要-8-31布置设计任务及环境搭建-9-1程序构造设计-9-2程序编写指引-9-5查阅资料-9-6查阅资料-9-7程

2、序编写、分析数据-9-8报告书编写及指引-9-9程序、报告检查及成绩评估六、设备及器材设备器材:计算机软件:(1)JDK 1.7及以上;(2)Eclipse-standard-luna;七、成果规定实训同窗规定完毕(一)、(二)、(三)、(四)、(七)程序设计及应用同窗规定完毕(一)、(二)、(三)、(四)、(五)、(六)、(七)(一)、遗传算法基本原理遗传算法(Genetic Algorithm)是模仿达尔文生物进化论自然选取和遗传学机理生物进化过程计算模型,是一种通过模仿自然进化过程搜索最优解办法。已被人们广泛地应用于组合优化、机器学习、信号解决、自适应控制和人工生命等领域,是当代关于智能

3、计算中核心技术。遗传算法基本运算过程如下:(1)初始化:设立进化代数计数器t=0,设立最大进化代数T,使用M个个体作为初始群体P(0)。(2)个体评价:计算群体P(t)中各个个体适应度。(3)选取运算:将选取算子作用于群体。选取目是把优化个体直接遗传或通过配对交叉产生新个体再遗传到下一代。选取操作是建立在群体中个体适应度评估基本上,运用轮盘赌进行选取。(4)交叉运算:将交叉算子作用于群体。遗传算法中起核心作用就是交叉算子。(5)变异运算:将变异算子作用于群体。即是对群体中个体串某些基因座上基因值作变动。群体P(t)通过选取、交叉、变异运算之后得到下一代群体P(t+1)。(6)终结条件判断:若t

4、=T,则以进化过程中所得到具备最大适应度个体作为最优解输出,终结计算。拟定实际问题参数集对参数集进行编码初始种群满足停止规则?遗传操作产生新一代种群结束1位串编码参数2计算目的函数值3函数值向适应度值映射4适应度值调节评价种群三个基本算子1选取2交叉3变异图1 遗传算法解决问题环节遗传算法进行问题求解实质是,在解空间中不断遗传,而遗传过程中通过适应度值,选取更适应问题解作为幸存者和后裔,逐渐逼近最优解。1、选取:从群体中选取优胜个体,裁减劣质个体操作叫选取。选取算子有时又称为再生算子(reproduction operator)。选取目是把优化个体(或解)直接遗传到下一代或通过配对交叉产生新个

5、体再遗传到下一代。选取操作是建立在群体中个体适应度评估基本上,当前惯用选取算子有如下几种:适应度比例办法、随机遍历抽样法、局部选取法等。其中轮盘赌选取法 (roulette wheel selection)是最简朴也是最惯用选取办法。在该办法中,各个个体选取概率和其适应度值成比例。显然,概率反映了个体i适应度在整个群体个体适应度总和中所占比例。个体适应度越大。其被选取概率就越高、反之亦然。计算出群体中各个个体选取概率后,为了选取交配个体,需要进行多轮选取。每一轮产生一种0,1之间均匀随机数,将该随机数作为选取指针来拟定被选个体。个体被选后,可随机地构成交配对,以供背面交叉操作。2、交叉在自然界

6、生物进化过程中起核心作用是生物遗传基因重组(加上变异)。同样,遗传算法中起核心作用是遗传操作交叉算子。所谓交叉是指把两个父代个体某些构造加以替代重组而生成新个体操作。通过交叉,遗传算法搜索能力得以奔腾提高。交叉算子依照交叉率将种群中两个个体随机地互换某些基因,可以产生新基因组合,盼望将有益基因组合在一起。依照编码表达办法不同,可以有如下算法:1)实值重组(real valued recombination)2)离散重组(discrete recombination)3)中间重组(intermediate recombination)4)线性重组(linear recombination)5)扩

7、展线性重组(extended linear recombination)。6)二进制交叉(binary valued crossover)7)单点交叉(single-point crossover)8)多点交叉(multiple-point crossover)9)均匀交叉(uniform crossover)10)洗牌交叉(shuffle crossover)11)缩小代理交叉(crossover with reduced surrogate)。最惯用交叉算子为单点交叉(one-point crossover)。详细操作是:在个体串中随机设定一种交叉点,实行交叉时,该点前或后两个个体某些构造

8、进行互换,并生成两个新个体。下面给出了单点交叉一种例子:个体A:1 0 0 1 1 1 1 1 0 0 1 0 0 0 新个体个体B:0 0 1 1 0 0 0 0 0 1 1 1 1 1 新个体3、变异变异算子基本内容是对群体中个体串某些基因座上基因值作变动。根据个体编码表达办法不同,可以有如下算法:a)实值变异b)二进制变异。普通来说,变异算子操作基本环节如下:a)对群中所有个体以事先设定变异概率判断与否进行变异b)对进行变异个体随机选取变异位进行变异。遗传算法引入变异目有两个:一是使遗传算法具备局部随机搜索能力。当遗传算法通过交叉算子已接近最优解邻域时,运用变异算子这种局部随机搜索能力可

9、以加速向最优解收敛。显然,此种状况下变异概率应取较小值,否则接近最优解积木块会因变异而遭到破坏。二是使遗传算法可维持群体多样性,以防止浮现未成熟收敛现象。此时收敛概率应取较大值。遗传算法中,交叉算子因其全局搜索能力而作为重要算子,变异算子因其局部搜索能力而作为辅助算子。遗传算法通过交叉和变异这对互相配合又互相竞争操作而使其具备兼顾全局和局部均衡搜索能力。所谓互相配合.是指当群体在进化中陷于搜索空间中某个超平面而仅靠交叉不能挣脱时,通过变异操作可有助于这种挣脱。所谓互相竞争,是指当通过交叉已形成所盼望积木块时,变异操作有也许破坏这些积木块。如何有效地配合使用交叉和变异操作,是当前遗传算法一种重要

10、研究内容。基本变异算子是指对群体中个体码串随机挑选一种或各种基因座并对这些基因座基因值做变动(以变异概率P.做变动),(0,1)二值码串中基本变异操作如下:基因位下方标有*号基因发生变异。变异率选用普通受种群大小、染色体长度等因素影响,普通选用很小值,普通取0.0010.1。4、终结条件当最优个体适应度达到给定阈值,或者最优个体适应度和群体适应度不再上升时,或者迭代次数达到预设代数时,算法终结。预设代数普通设立为100-500代。(二)、Java开发环境搭建1、JDK获取从Oracle公司网站上可以免费获取各种版本JDK,Oracle公司网址.com。注意这次设计实用是JDK 1.72、JDK

11、安装及环境变量配备JDK有安装版和直接解压版。安装版安装办法如下:(1)找到下载安装包,双击运营。注意:如果是32位系统运营jdk-7u60-windows-i586.exe,64位操作系统运营jdk-7u60-windows-x64.exe。(2)按照系统提示安装JDK,需注意是再安装时安装途径尽量不要再C盘,并且安装途径不要包括空格和中文。(3)环境变量配备Windows XP:在桌面右击“我电脑”,然后单击“属性”,在“系统属性”对话框中选取“高档”选项卡,然后单击“环境变量”按钮,打开“环境变量”对话框,找到“系统变量”中名为Path行,单击编辑,将JDK中bin目录安装途径加入到环境

12、变量中。为了编程以便可以新建一种classpath环境变量,并将JDK中lib目录安装途径加入到该环境变量中。Windows 7,windows 8,windows 8.1:在桌面右击“我电脑”,然后单击“属性”,在“系统”对话框中选取“高档系统配备”,在打开“系统属性”对话框中选取“高档”选项卡,然后单击“环境变量”按钮,打开“环境变量”对话框,找到“系统变量”中名为Path行,单击编辑,将JDK中bin目录安装途径加入到环境变量中。为了编程以便可以新建一种classpath环境变量,并将JDK中lib目录安装途径加入到该环境变量中。3、Eclipse获取与安装Eclipse是IBM开源项目

13、,可以从Eclipse官方网站( )免费获取。将下载压缩包解压至一种不包括中文字符目录下,即完毕安装。双击eclipse.exe打开eclipse。(三)、遗传算法编写指引采用4位DNA编码,M条基因,交叉2带计算。采用四进制编码。0,1,2,3分别代表G,A,T,C即:。用编码随机产生一种小数,进过筛选适应度后,进行交叉、变异计算后输出计算成果规定学生依照下面指引内容,配备下面代码,并可以对的运营,阅读并理解下面代码含义。1、包定义package geneTest;2、基因类(Gene)public class Gene implements Cloneable protected Stri

14、ng allele;/ 等位片段public String getAllele() return allele;public void setAllele(String allele) this.allele = allele;Overridepublic Object clone() throws CloneNotSupportedException Gene g = null;try g = (Gene)super.clone(); g.allele = new String(allele); catch(CloneNotSupportedException e ) System.out.

15、println(e.getMessage(); return g;3、基因型类(GeneType)public class GeneType implements Cloneableprotected Gene gene;/ 基因序列片段public Gene getGene() return gene;public void setGene(Gene gene) this.gene = gene;public GeneType(GeneType geneType) super();this.gene = geneType.getGene();public GeneType() Overrid

16、epublic Object clone() throws CloneNotSupportedException GeneType g = null;try g = (GeneType)super.clone(); g.gene = gene.clone(); catch(CloneNotSupportedException e ) System.out.println(e.getMessage(); return g;4、体现型类(PhenoType)public class PhenoType implements Cloneableprotected double x;/ 体现相应数值p

17、ublic double getX() return x;public void setX(double x) this.x = x;public PhenoType(PhenoType phoneType) this.x=phoneType.getX();public PhenoType() public PhenoType(double x) this.x = x;Overridepublic Object clone() throws CloneNotSupportedException PhenoType g = null;try g = (PhenoType)super.clone(

18、); catch(CloneNotSupportedException e ) System.out.println(e.getMessage(); return g;5、染色体类(Chromsome)public class Chromsome implements Cloneableprotected GeneType geneType;/ 基因型protected PhenoType phoneType;/ 体现型public GeneType getGeneType() return geneType;public void setGeneType(GeneType geneType)

19、 this.geneType = geneType;public PhenoType getPhoneType() return phoneType;public void setPhoneType(PhenoType phoneType) this.phoneType = phoneType;Overridepublic Object clone() throws CloneNotSupportedException Chromsome g = null;try g = (Chromsome)super.clone(); g.geneType = new GeneType(geneType)

20、; g.phoneType = new PhenoType(phoneType); catch(CloneNotSupportedException e ) System.out.println(e.getMessage(); return g;public String toString() String str=; for(int i = 0 ;i geneType.gene.length ;i+) str += geneType.getGene()i.getAllele()+ ; return str; 6、个体类(抽象Individual,详细类RosenbrockIndividual

21、)public abstract class Individual implements Cloneableprotected Chromsome chrom;/ 染色体序列protected double fitness;/ 适应度protected double relativeFitness;/ 相对适应度protected double targetValue;/ 目的函数值public Chromsome getChrom() return chrom;public void setChrom(Chromsome chrom) this.chrom = chrom;public do

22、uble getFitness() return fitness;public void setFitness(double fitness) this.fitness = fitness;public double getRelativeFitness() return relativeFitness;public void setRelativeFitness(double relativeFitness) this.relativeFitness = relativeFitness;public double getTargetValue() return targetValue;pub

23、lic void setTargetValue(double targetValue) this.targetValue = targetValue;/抽象办法public abstract Individual randomlyGeneratIndividual(CodeScale scale);/ 随机生成个体public abstract Individual generateIndividualByCode(CodeScale scale,String code);/ 依照编码序列生成个体public abstract Individual coding(CodeScale scale

24、,Individual indiv);/ 对个体进行编码public abstract Individual decode(CodeScale scale,Individual indiv);/ 对个体进行解码public abstract Individual calFitness(Individual indiv);/ 计算适应度public abstract Individual calTargetValue(Individual indiv);/ 计算目的函数Overridepublic Object clone() throws CloneNotSupportedException

25、Individual g = null;try g = (Individual)super.clone(); g.chrom = chrom.clone(); catch(CloneNotSupportedException e ) System.out.println(e.getMessage(); return g;public String toString() String str = ;for (int i = 0;i chrom.length;i+) str += chromi.toString();str+=n;for (int i = 0;i chrom.length;i+)

26、str += chromi.getPhoneType().getX()+ ;str+=fitness+ +relativeFitness+n;return str;public class RosenbrockIndividual extends Individual implements CloneableOverridepublic Individual randomlyGeneratIndividual(CodeScale scale) double x1 = Math.random() * 4.096 - 2.048; double x2 = Math.random() * 4.096

27、 - 2.048;Individual indiv=new RosenbrockIndividual();Chromsome chrom = new Chromsome2;chrom0 = new Chromsome();chrom1 = new Chromsome();PhenoType phenoType1=new PhenoType();phenoType1.setX(x1);PhenoType phenoType2=new PhenoType();phenoType2.setX(x2);chrom0.phoneType=phenoType1;chrom1.phoneType=pheno

28、Type2;indiv.setChrom(chrom);indiv = indiv.coding(scale,indiv);return indiv;Overridepublic Individual generateIndividualByCode(CodeScale scale,String code) /一种香蕉个体有两个染色体,需要对两个染色体进行编码GeneType geneType1 = new GeneType();GeneType geneType2 =new GeneType();Gene g1 = new Genescale.geneTypeLength;Gene g2 =

29、 new Genescale.geneTypeLength;for(int i=0;iscale.geneTypeLength;i+)Gene gene1 =new Gene();gene1.setAllele(codei);g1i=gene1;Gene gene2 =new Gene();gene2.setAllele(codei+scale.geneTypeLength);g2i=gene2;geneType1.setGene(g1);geneType2.setGene(g2);Chromsome chrom1 = new Chromsome();chrom1.setGeneType(ge

30、neType1);Chromsome chrom2 = new Chromsome();chrom2.setGeneType(geneType2);Chromsome chrom= new Chromsome2;chrom0=chrom1;chrom1=chrom2;Individual indiv=new RosenbrockIndividual();indiv.setChrom(chrom);indiv = decode(scale,indiv);return indiv;Overridepublic Individual coding(CodeScale scale,Individual

31、 indiv) /一种香蕉个体有两个染色体,需要对两个染色体进行编码double x1 = indiv.chrom0.getPhoneType().getX();double x2 = indiv.chrom1.getPhoneType().getX();Chromsome chrom = new Chromsome2;chrom0 = indiv.chrom0;GeneType geneType1 = new GeneType();Gene g1 = new Genescale.geneTypeLength;for (int i = 0;i scale.geneTypeLength;i+)

32、Gene gene = new Gene();gene.setAllele(byLengthSpilt(scale,codingVariable(scale,x1)i);g1i = gene;geneType1.setGene(g1);chrom0.setGeneType(geneType1);chrom1 = indiv.chrom1;GeneType geneType2 = new GeneType();Gene g2 = new Genescale.geneTypeLength;for (int i = 0;i scale.geneTypeLength;i+) Gene gene = n

33、ew Gene();gene.setAllele(byLengthSpilt(scale,codingVariable(scale,x2)i);g2i = gene;geneType2.setGene(g2);chrom1.setGeneType(geneType2);Individual individual = new RosenbrockIndividual();individual = indiv;individual.setChrom(chrom);return individual;private String codingVariable(CodeScale scale,doub

34、le x) double length =(double) scale.geneTypeLength*scale.geneLength;double y = (x + 2.048) * (Math.pow(2.0,length) - 1.0) / 4.096);String code = Integer.toBinaryString(int) y);for (int i = code.length();i length;i+) code = 0 + code;return code;public static String byLengthSpilt(CodeScale scale,Strin

35、g str) int j = (int) str.length() / scale.geneLength;String subStr = new Stringj;for (int i = 0;i j;i+) subStri = str.substring(i * scale.geneLength,i * scale.geneLength+ scale.geneLength);return subStr;Overridepublic Individual decode(CodeScale scale,Individual indiv) /一种香蕉个体有两个染色体,需要对两个染色体进行解码Stri

36、ng code1 = ;String code2 = ;GeneType geneType1 = indiv.chrom0.getGeneType();GeneType geneType2 = indiv.chrom1.getGeneType();for(int i=0;igeneType1.getGene().length;i+)code1 = code1+geneType1.getGene()i.getAllele();for(int i=0;igeneType2.getGene().length;i+)code2 = code2+geneType2.getGene()i.getAllel

37、e();double x1 = decodeHandle(scale,code1);Chromsome chrom = new Chromsome2;chrom0 = indiv.chrom0;chrom0.setPhoneType(new PhenoType(x1);double x2 = decodeHandle(scale,code2);chrom1 = indiv.chrom1;chrom1.setPhoneType(new PhenoType(x2);Individual individual = new RosenbrockIndividual();individual = ind

38、iv;individual.setChrom(chrom);return individual;public double decodeHandle(CodeScale scale,String code) double y = (double) Integer.parseInt(code,2);double length =(double) scale.geneTypeLength*scale.geneLength;double x = (4.096 * y / (Math.pow(2.0,length) - 1.0) - 2.048;return x;Overridepublic Indi

39、vidual calFitness(Individual indiv) Individual individual = new RosenbrockIndividual();individual = indiv;individual = calTargetValue(indiv);individual.setFitness(individual.getTargetValue();return individual;Overridepublic Individual calTargetValue(Individual indiv) Individual individual = new Rose

40、nbrockIndividual();individual = indiv;double targetValue = rosenbrock(indiv.chrom0.getPhoneType().getX(),indiv.chrom1.getPhoneType().getX();individual.setTargetValue(targetValue);return individual;public static double rosenbrock(double x1,double x2) double fun;fun = 100 * Math.pow(x1 * x1 - x2),2) +

41、 Math.pow(1 - x1),2);return fun;Overridepublic Object clone() throws CloneNotSupportedException RosenbrockIndividual g = null;try g = (RosenbrockIndividual)super.clone(); catch(CloneNotSupportedException e ) System.out.println(e.getMessage(); return g;7、种群类(抽象类Population,详细类RosenbrockPopulation)publ

42、ic abstract class Population implements Cloneable protected int generationId;/ 种群代数protected Individual pop;/ 种群protected double averageFitness;/ 平均适应度protected double totalFitness;/ 平均适应度protected Individual bestIndividual;/ 当前代适应度最佳个体protected int bestIndex;/ 当前代适应度最佳个体相应下标protected Individual worstIndividual;/ 当

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 应用文书 > 技术指导

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服