程序設(shè)計及其應(yīng)用Java實(shí)訓(xùn)指導(dǎo)書_第1頁
程序設(shè)計及其應(yīng)用Java實(shí)訓(xùn)指導(dǎo)書_第2頁
程序設(shè)計及其應(yīng)用Java實(shí)訓(xùn)指導(dǎo)書_第3頁
程序設(shè)計及其應(yīng)用Java實(shí)訓(xùn)指導(dǎo)書_第4頁
程序設(shè)計及其應(yīng)用Java實(shí)訓(xùn)指導(dǎo)書_第5頁
已閱讀5頁,還剩51頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

程序設(shè)計及應(yīng)用實(shí)訓(xùn)指引書

(JavaI吾日)

昆明理工大學(xué)信息工程與自動化學(xué)院自動化系

張志坤李勇編

8月

、題目

題目:運(yùn)用遺傳算法仿真掰猴桃組培苗變異

二、目

(1)掌握J(rèn)ava語言開發(fā)環(huán)境搭建,掌握J(rèn)ava基本編程知識。

(2)學(xué)習(xí)遺傳算法基本原理,運(yùn)用遺傳算法進(jìn)行狒猴桃遺傳變異模仿。

(3)運(yùn)用excel進(jìn)行遺傳變異數(shù)據(jù)分析,掌握數(shù)據(jù)分析辦法

三、指引教師

張志坤、李勇

刪地點(diǎn)

昆明理工大學(xué)呈貢校區(qū)信自樓445。

五、時間及進(jìn)度安排

時間:一短學(xué)期(8月31日至9月9日)

進(jìn)度安排:

日期環(huán)節(jié)講程安排摘要

-8-31布置設(shè)計任務(wù)及環(huán)境搭建

-9-1程序構(gòu)造設(shè)計

-9-2程序編寫指引

-9-5查閱資料

-9-6查閱資料

-9-7程序編寫、分析數(shù)據(jù)

-9-8報告書編寫及指引

-9-9程序、報告檢查及成績評估

六、設(shè)備及器材

設(shè)備器材:計算機(jī)

軟件:(1)JDK1.7及以上;(2)Eclipse-standard-luna;

七、成果規(guī)定

實(shí)訓(xùn)同窗規(guī)定完畢(一)、(二)、(三)、(四)、(七)

程序設(shè)計及應(yīng)用同窗規(guī)定完畢(一)、(二)、(三)、(四)、(五)、(六)、(七)

(一)、遺傳算法基本原理

遺傳算法(GeneticAlgorithm)是模仿達(dá)爾文生物進(jìn)化論自然選取和遺傳學(xué)機(jī)理生物進(jìn)

化過程計算模型,是一種通過模仿自然進(jìn)化過程搜索最優(yōu)解辦法。已被人們廣泛地應(yīng)用于組

合優(yōu)化、機(jī)器學(xué)習(xí)、信號解決、自適應(yīng)控制和人工生命等領(lǐng)域,是當(dāng)代關(guān)于智能計算中核心

技術(shù)。

遺傳算法基本運(yùn)算過程如下:

(1)初始化:設(shè)立進(jìn)化代數(shù)計數(shù)器t=0,設(shè)立最大進(jìn)化代數(shù)T,使用M個個體作為初

始群體

P(0)a

(2)個體評價:計算群體P(t)中各個個體適應(yīng)度。

(3)選取運(yùn)算:將選取算子作用于群體。選取目是把優(yōu)化個體直接遺傳或通過配對交

叉產(chǎn)生新個體再遺傳到下一代。選取操作是建立在群體中個體適應(yīng)度評估基本上,運(yùn)用輪盤

賭進(jìn)行選取。

(4)交叉運(yùn)算:將交叉算子作用于群體。遺傳算法中起核心作用就是交叉算子。

(5)變異運(yùn)算:將變異算子作用于群體。即是對群體中個體串某些基因座上基因值作

變動。群體P(t)通過選取、交叉、變異運(yùn)算之后得到下一代群體P(t+1)。

(6)終結(jié)條件判斷:若t=T,則以進(jìn)化過程中所得到具備最大適應(yīng)度個體作為最優(yōu)解

輸出,終結(jié)計算。

圖1遺傳算法解決問題環(huán)節(jié)

遺傳算法進(jìn)行問題求解實(shí)質(zhì)是,在解空間中不斷遺傳,而遺傳過程中通過適應(yīng)度值,選

取更適應(yīng)問題解作為幸存者和后裔,逐漸逼近最優(yōu)解。

1、選?。?/p>

從群體中選取優(yōu)勝個體,裁減劣質(zhì)個體操作叫選取。選取算子有時又稱為再生算子

(reproductionoperator)。選取目是把優(yōu)化個體(或解值接遺傳到下一代或通過配對交叉產(chǎn)生

新個體再遺傳到下一代。選取操作是建立在群體中個體適應(yīng)度評估基本上,當(dāng)前慣用選取算

子有如下幾種:適應(yīng)度比例辦法、隨機(jī)遍歷抽樣法、局部選取法等。其中輪盤賭選取法

(roulettewheelselection)是最簡樸也是最慣用選取辦法。在該辦法中,各個個體選取概率

和其適應(yīng)度值成比例。顯然,概率反映了個體i適應(yīng)度在整個群體個體適應(yīng)度總和中所占比

例。個體適應(yīng)度越大。其被選取概率就越高、反之亦然。計算出群體中各個個體選取概率后,

為了選取交配個體,需要進(jìn)行多輪選取。每一輪產(chǎn)生一種[0,1]之間均勻隨機(jī)數(shù),將該隨機(jī)

數(shù)作為選取指針來擬定被選個體。個體被選后,可隨機(jī)地構(gòu)成交配對,以供背面交叉操作。

2、交叉

在自然界生物進(jìn)化過程中起核心作用是生物遺傳基因重組(加上變異)。同樣,遺傳算法

中起核心作用是遺傳操作交叉算子。所謂交叉是指把兩個父代個體某些構(gòu)造加以替代重組而

生成新個體操作。通過交叉,遺傳算法搜索能力得以奔騰提高。交叉算子依照交叉率招種群

中兩個個體隨機(jī)地互換某些基因,可以產(chǎn)生新基因組合,盼望將有益基因組合在一起。依照

編碼表達(dá)辦法不同,可以有如下算法;

1)實(shí)值重組(realvaluedrecombination)

2)離散重組(discreterecombination)

3)中間重組(intermediaterecombination)

4)線性重組(linearrecombination)

5)擴(kuò)展線性重組(extendedlinearrecombination)<,

6)二進(jìn)制交叉(binaryvaluedcrossover)

7)單點(diǎn)交叉(single-pointcrossover)

8)多點(diǎn)交叉(multiple-pointcrossover)

9)均勻交叉(uniformcrossover)

10)洗牌交叉(shufflecrossover)

11)縮小代理交叉(crossoverwithreducedsurrogate)o

最慣用交叉算子為單點(diǎn)交叉(one-pointcrossover)。詳細(xì)操作是:在個體串中隨機(jī)設(shè)定一種

交叉點(diǎn),實(shí)行交叉時,該點(diǎn)前或后兩個個體某些構(gòu)造進(jìn)行互換,并生成兩個新個體。下面給

出了單點(diǎn)交叉一種例子:

個體A:1001個11131001000新個體

個體B:0011T000^0011111新個體

3、變異

變異算子基本內(nèi)容是對群體中個體串某些基因座上基因值作變動。根據(jù)個體編碼表達(dá)辦

法不同,可以有如下算法;

a)實(shí)值變異

b)二進(jìn)制變異。

普通來說,變異算子操作基本環(huán)節(jié)如下:

a)對群中所有個體以事先設(shè)定變異概率判斷與否進(jìn)行變異

b)對進(jìn)行變異個體隨機(jī)選取變異位進(jìn)行變異。

遺傳算法引入變異目有兩個:?是使遺傳算法具備局部隨機(jī)搜索能力。當(dāng)遺傳算法通過

交又算子已接近最優(yōu)解鄰域時,運(yùn)用變異算子這種局部隨機(jī)搜索能力可以加速向最優(yōu)解收

斂。顯然,此種狀況下變異概率應(yīng)取較小值,否則接近最優(yōu)解積木塊會因變異而遭到破壞。

二是使遺傳算法可維持群體多樣性,以防止浮現(xiàn)未成熟收斂現(xiàn)象。此時收斂概率應(yīng)取較大值。

遺傳算法中,交叉算子因其全局搜索能力而作為重要算子,變異算子因其局部搜索能力

而作為輔助算了?。遺傳算法通過交叉和變異這對互相配合又互相競爭操作而使其具備兼顧全

局和局部均衡搜索能力。所謂互相配合.是指當(dāng)群體在進(jìn)化中陷于搜索空間中某個超平面而

僅靠交叉不能掙脫時,通過變異操作可有助于這種掙脫。所謂互相競爭,是指當(dāng)通過交叉已

形成所盼望積木塊時,變異操作有也許破壞這些積木塊。如何有效地配合使用交叉和變異操

作,是當(dāng)前遺傳算法一種重要研究內(nèi)容。

基本變異算子是指對群體中個體碼串隨機(jī)挑選一種或各種基因座并對這些基因座基因

值做變動(以變異概率P.做變動),(0,1)二值碼串中基本變異操作如卜.:

基因位下方標(biāo)有*號基因發(fā)生變異。

變異率選用普通受種群大小、染色體長度等因素影響,普通選用很小值,普通取0.001

—O.lo

4、終結(jié)條件

當(dāng)最優(yōu)個體適應(yīng)度達(dá)到給定閾值,或者最優(yōu)個體適應(yīng)度和群體適應(yīng)度不再上升時,或者

迭代次數(shù)達(dá)到預(yù)設(shè)代數(shù)時,算法終結(jié)。預(yù)設(shè)代數(shù)普通設(shè)立為100-500代。

(二)、Java開發(fā)環(huán)境搭建

1、JDK獲取

從Oracle公司網(wǎng)站上可以免費(fèi)獲取各種版本JDK,Oracle公司網(wǎng)址.com。注意這次設(shè)計

實(shí)用是JDK1.7

2、JDK安裝及環(huán)境變量配備

JDK有安裝版和直接解壓版“

安裝版安裝辦法如下:

(1)找到下載安裝包,雙擊運(yùn)營。注意:如果是32位系統(tǒng)運(yùn)營

jdk-7u60-windows-i586.exe,64位操作系統(tǒng)運(yùn)營jdk-7u60-windows-x64.exe.

7jdk-o

旃共享查看▼。

⑥▼Ti)>這臺電腦>Z.V3Q&(E:)>solft?丹程》Java,jdkV63?-jdk-P

,最近訪問的位置,□名稱修福日期大小

國jdk-7u60-windows-i586.exe2014/6/2610:23130979KI

公OneDrive

圖jdk-7u60-windows-x64.exe2014/6/2610:21132.758Kf

(2)按照系統(tǒng)提示安裝JDK,需注意是再安裝時安裝途徑盡量不要再C盤,并且安裝

途徑不要包括空格和中文,

(3)環(huán)境變量配備

WindowsXP:在桌面右擊“我電腦”,然后單擊“屬性”,在“系統(tǒng)屬性”對話框中選

取“高檔”選項(xiàng)卡,然后單擊“環(huán)境變量”按鈕,打開“環(huán)境變量”對話框,找到“系統(tǒng)變

量”中名為Path行,單擊編輯,將JDK中bin目錄安裝途徑加入到環(huán)境變量中。為了編程

以便可以新建一種classpath環(huán)境變量,并將JDK中l(wèi)ib目錄安裝途徑加入到該環(huán)境變星中。

Windows7,windows8,windows8.1:在桌面右擊“我電腦”,然后單擊“屬性”,在“系

統(tǒng)”對話框中選取“高檔系統(tǒng)配備”,在打開“系統(tǒng)屬性”對話框中選取“高檔”選項(xiàng)卡,

然后單擊“環(huán)境變量”按狙,打開“環(huán)境變量”對話框,找到“系統(tǒng)變量”中名為Path行,

單擊編輯,將JDK中bin目錄安裝途徑加入到環(huán)境變量中。為了編程以便可以新建一種

classpath環(huán)境變量,并將JDK中l(wèi)ib目錄安裝途徑加入到該環(huán)境變量中。

SI&51S:C^wndow$\$y5tem32\and^xe

MicrosoftUindows[版本6.1.7601]

版權(quán)所有<c>20B9MicrosoftCorporation^保留所有權(quán)利.

C:XUsersx?ars>jaua-version

javdvtjrsiu”?H?6?8_45”

Java<TH>SEFuntineEnvironment(build1.6.0_45-b86>

JavaHotSpot<TH>64-BitServerUM(build20.45-b01,nixednode)

CNsersMmrs〉______________________________________

F

3、Eclipse獲取與安裝

Eclipse是IBM開源項(xiàng)目,可以從Eclipse官方網(wǎng)站()免費(fèi)獲取。

將下載壓縮包解壓至?種不包括中文字符目錄下,即完畢安裝。

雙擊eclipse.exe打開eclipse。

(三)、遺傳算法編寫指引

采用4位DNA編碼,M條基因,交叉2帶計算。采用四進(jìn)制編碼。0,1,2,3分別代表G,

A=1;

A,T,C即:T=2;G=();C=3O用編碼隨機(jī)產(chǎn)生一種小數(shù)XWI。1],進(jìn)過篩選適

應(yīng)度后,進(jìn)行交叉、變異計算后輸出計算成果

規(guī)定學(xué)生依照下面指引內(nèi)容,配備下面代碼,并可以對?的運(yùn)營,閱讀并理解下面代碼含

義。

1、包定義

packagegeneTest;

2、基因類(Gene)

publicclassGeneimplementsCloneable{

protectedStringcllele;//等位片段

publicStringgetAllele(){

returnallele;

)

publicvoidsetAllele(Stringallele){

this.allele=allele;

)

@Override

publicObjectclone()throwsCloneNotSupportedException{

Geneg=null;

try(

g=(Gene)super.clone():

g.allele=newString(allele);

}catch(CloneNotSupportedExceptione){

System.out.println(e.getMessage()):

returng;

)

)

3、基因型類(GeneType)

publicclassGeneTypeimplementsCloneable{

protectedGene[]gene;//基因序列片段

publicGene[]getGene(){

returngene;

)

publicvoidsetGene(Gene[]gene){

this.gene=gene;

)

publicGeneType(GeneTypegeneType){

superf);

this.gene=geneType.getGene();

)

publicGeneType(){

)

@Override

publicObjectclone()throwsCloneNotSupportedException{

GeneTypeg=null;

try(

g=(GeneType)super.clone();

g.gene=gene.clone();

}catch(CloneNotSupportedExceptione){

System.out.println(e.getMessage());

)

returng;

)

)

4、體現(xiàn)型類(PhenoType)

publicclassPhenoTypeimplementsCloneable{

protecteddoublex;//體現(xiàn)相應(yīng)數(shù)值

publicdoublegetX(){

returnx;

)

publicvoidsetX(doublex){

this.x=x;

)

publicPhenoType(PhenoTypephoneType){

this.x=phoneType.getX{);

)

publicPhenoType(){

)

publicPhenoType(doublex){

this.x=x;

)

@Override

publicObjectclone()throwsCloneNotSupportedException{

PhenoTypeg=null;

try(

g=(PhenoType)super.clone();

}catch(CloneNotSupportedExceptione){

System.out.println(e.getMessage()):

)

returng;

)

)

5、染色體類(Chromsome)

publicclassChromsorreimplementsCloneable{

protectedGeneTypegeneType;//基因型

protectedPhenoTypephoneType;//體現(xiàn)型

publicGeneTypegetGeneType(){

returngeneType;

)

publicvoidsetGeneType(GeneTypegeneType){

this.geneType=geneType;

)

publicPhenoTypegetPhoneType(){

returnphoneType;

)

publicvoidsetPhoneType(PhenoTypephoneType){

this.phoneType=phoneType;

)

@Override

publicObjectclone()throwsCloneNotSupportedException{

Chromsomeg=null;

try(

g=(Chromsome)super.clone();

g.genelype=newGeneType(geneType);

g.phoneType=newPhenoType(phoneType);

}catch(CloneNotSupportedExceptione){

System.out.println(e.getMessage()):

)

returng;

)

publicStringtoString(){

Stringstr="":

for(inti=0:i<geneType.gene.length;i++)

{str+=geneType.getGene()[i].getAllele()+"

returnstr;

)

)

6、個體類(抽象Individuab詳細(xì)類Rosenbrocklndividual)

publicabstractclassindividualimplementsCloneable{

protectedChromsome[]chrom;//染色體序列

protecteddoublefitness;//適應(yīng)度

protecteddoublerelativeFitness;//相對適應(yīng)度

protecteddoubletargetValue;//目的函數(shù)值

publicChromsomef]getChrom(){

returnchrom;

)

publicvoidsetChrom(Chromsome(]chrom){

this.chrom-chrom;

)

publicdoublegetFitness(){

returnfitness;

)

publicvoidsetFitness(doublefitness){

this.fitness=fitness;

)

publicdoublegetRelativeFitness(){

returnrelativeFitness;

)

publicvoidsetRelativeFitness(doublerelativeFitness){

this.relativeFitness=relativeFitness;

)

publicdoublegetTargetValue(){

returntargetValue;

)

publicvoidsetTargetValuefdoubletargetValue){

this.targetValue=targetvalue;

)

〃抽象辦法

publicabstractIndividualrandomlyGeneratlndividual(CodeScalescale);//隨機(jī)生成個

publicabstractIndividualgeneratelndividualByCode(CodeScalescale,String[]code);//

依照編碼序列生成個體

publicabstractIndividualcoding(CodeScalescalejndividualindiv);//對個體進(jìn)行編碼

publicabstractIndividualdecode(CodeScalescalejndividualindiv);//對個體進(jìn)行解碼

publicabstractIndividualcalFitness(lndividualindiv);//計算適應(yīng)度

publicabstractIndividualcalTargetValue(lndividualindiv);//計算目的函數(shù)

@Override

publicObjectclone()throwsCloneNotSupportedException{

Individualg=null;

try(

g=(Indvidual)super.clone();

g.chrom=chrom.clone():

}catch(CloneNotSupportedExceptione){

System.out.println(e.getMessage());

)

returng;

)

publicStringtoString(){

Stringstr=

for(inti=0;i<chrom.length;i++){

str+=chrom[i].toString();

)

str+="\n";

for(inti=0;i<chrom.length;i++){

str+=chrom[i].getPhoneType().getX()+"

)

str+=fitness+""+relativeFitness+"\n";

returnstr;

)

)

publicclassRosenbrocklndividualextendsIndividualimplementsCloneable{

@Override

publicIndividualrandomlyGeneratlndividualfCodeScalescale){

doublexl=Math.random()*4.096-2.048;

doublex2=Math.random()*4.096-2.048;

Individualindiv=newRosenbrocklndividual();

ChromsomeOchrom=newChromsome[2];

chrom[0]=newChromsomef);

chrom[l]=newChromsome();

PhenoTypephenoTypel=newPhenoType();

phenoTypelsetX(xl);

PhenoTypephenoType2=newPhenoType();

phenoType2setX(x2);

chrom[0].phoneType=phenoTypel;

chrom[l].phoneType=phenoType2;

indiv.setChrom(chrom);

indiv=indiv.:oding(scale,indiv);

returnindiv;

)

@Override

publicIndividualgeneratelndividualByCode(Code$calescale,String口code){

〃一種香蕉個體有兩個染色體,需要對兩個染色體進(jìn)行編碼

GeneTypegeneTypel=newGeneType();;

GeneTypegeneType2=newGeneType();;

Gene[]gl=newGene[scale.geneTypeLength];

Gene[]g2=newGene[scale.geneTypeLength];

for(inti=O;i<scale.geneTypeLength;i++){

Genegenel=newGene();

genel.setAllele(code[i]);

gl[i]=genel;

Genegene2=newGene();

gene2.setAllele(code[i+scale.geneTypeLength]);

g2[i]=gene2;

)

geneTypel.setGene(gl);

geneType2.setGene(g2);

Chromsomechroml=newChromsome();

chroml.setGeneType(geneTypel);

Chromsomechrom2=newChromsome();

chrom2.setGeneType(geneType2);

Chromsome|]chrom=newChromsome[2];

chrom[0]=chroml;

chrom[l]=chrom2;

Individualindiv=newRosenbrocklndividualf);

indiv.setChrom(chrom);

indiv=decodefscale,indiv);

returnindiv;

)

@Override

publicIndividualcoding(CodeScalescale.Individualindiv){

〃一種香蕉個體有兩個染色體,需要對兩個染色體進(jìn)行編碼

doublexl=indiv.chrom[0].getPhoneType().gstX();

doublex2=indiv.chrom[l].getPhoneType().getX{);

Chromsome[]chrom=newChromsome[2];

chrom[0]=indiv.chrom[0];

GeneTypegeneTypel=newGeneType();

Gene[]gl=newGene[scale.geneTypeLength];

for(inti=0;i<scale.geneTypeLength:i++){

Genegene=newGene();

gene.setAllele(byLengthSpilt(scale,codingVariable(scale,xl))[i]);

gl[i]=gene;

)

geneTypel.s?tGene(gl);

chrom[0].setGeneType(geneTypel);

chrom[l]=indiv.chrom[l];

GeneTypegeneType2=newGeneTypef);

Gene[]g2=newGene[scale.geneTypeLength];

for(inti=0;i<scale.geneTypeLength;i++){

Genegene=newGene();

gene.setAllele(byLengthSpilt(scale,codingVariable(scale,x2))[i]);

g2[i]=gene;

)

geneType2.S5tGene(g2);

chrom[l].setGeneType(geneType2);

Individualindividual=newRosenbrocklndividual();

individual=indiv;

individual.setChrom(chrom);

returnindividual;

)

privateStringcodingVariable(CodeScalescale,doublex){

doublelength=(double)scale.geneTypeLength*scale.geneLength;

doubley=(((x+2.048)*(Math.pow(2.0,length)-1.0))/4.096);

Stringcode=lnteger.toBinaryString((int)y);

for(inti=code.length();i<length;i++){

code="0"+code;

)

returncode;

)

publicstaticString!)byLengthSpilt(CodeScalescale,Stringstr){

intj=(int)str.length()/scale.geneLength;

String[]subStr=newString。];

for(inti=0;i<j;i++){

subStr[i]=str.substring(i*scale.geneLength,i*scale.geneLength

+scale.geneLength);

)

returnsubSt,;

)

@Override

publicIndividualdecode(CodeScalescale,Individualindiv){

〃一種香蕉個體有兩個染色體,需要對兩個染色體進(jìn)行解碼

Stringcodel=

Stringcode2=

GeneTypegeneTypel=indiv.chrom[0].getGeneType();

GeneTypegeneType2=indiv.chrom[l].getGeneType();

for(inti=O;i<geneTypel.getGene().length;i++){

codel=codel+geneTypel.getGene()[i].getAllele();

)

for(inti=0;i<geneType2.getGene(),length;i++){

code2=code2+geneType2.getGene()[i].getAllele();

)

doublexl=decodeHandle(scale,codel);

Chromsome|]chrom=newChromsome[2];

chrom[0]=indiv.chromfO];

chrom[0].setPhoneType(newPhenoType(xl));

doublex2=decodeHandle(scale,code2);

chrom[l]=indiv.chrom[l];

chrom[l].setPhoneType(newPhenoType(x2));

Individualindividual=newRosenbrocklndividual();

individual=indiv;

individual.setChrom(chrom);

returnindividual;

)

publicdoubledecodeHandle(CodeScalescale,Stringcode){

doubley=(double)lnteger.parselnt(code,2);

doublelength=(double)scale.geneTypeLength*scale.geneLength;

doublex=(4.096*y/(Math.pow(2.0,length)-1.0))-2.048;

returnx;

)

@Override

publicIndividualcalFitness(lndividualindiv){

Individualindividual=newRosenbrocklndividual();

individual=indiv;

individual=calTargetValue(indiv);

individual.setFitness(individual.getTargetValue());

returnindividual;

)

@Override

publicIndividualcalTargetValue(lndividualindiv){

Individualindividual=newRosenbrocklndividual();

individual=indiv;

doubletargetValue=rosenbrockfindiv.chromfOl.getPhoneTypeO.getXO,

indiv.chrom[l].getPhoneType().getX());

individual.setTargetValue(targetValue);

returnindividual;

)

publicstaticdoublerosenbrockfdoublexl,doublex2){

doublefun;

fun=100*Math.pow((xl*xl-x2),2)+Math.pow((l-xl),2);

returnfun;

)

(?Override

publicObjectclonef)throwsCloneNotSupportedCxception{

Rosenbrocklndividualg=null;

try(

g=(Rosenbrocklndividual)super.clone();

}catch(CloneNotSupportedExceptione){

System.out.println(e.getMessage()):

)

returng;

)

)

7、種群類(抽象類Population,詳細(xì)類RosenbrockPopulation)

publicabstractclassPopulationimplementsCloneable{

protectedintgenerationld;//種群代數(shù)

protectedIndividual口pop;//種群

protecteddoubleaverageFitness://平均適應(yīng)度

protecteddoubletotalFitness://平均適應(yīng)度

protectedIndividualbestindividual;//當(dāng)前代適應(yīng)度最佳個體

protectedintbestIndex;//當(dāng)前代適應(yīng)度最佳個體相應(yīng)下標(biāo)

protectedIndividualworstindividual;//當(dāng)前代適應(yīng)度最差個體

protectedIndividualcurrentBesIndividual;//當(dāng)前適應(yīng)度最佳個體

publicintgetBestndex(){

returnbestindex;

)

publicvoidsetBestlndex(intbestindex){

this.bestindex=bestindex;

)

publicintgetGenerationld(){

returngenerationld;

)

publicvoidsetGenerationld(intgenerationld){

this.generationld=generationld;

)

publicIndividual口getPopO{

returnpop;

}

publicvoidsetPop(lndividual[]pop){

this.pop=pop;

)

publicdoublegetAverageFitness(){

returnaverageFitness;

)

publicvoidsetAverageFitnessfdoubleaverageFitness){

this.averageFitness=averageFitness;

)

publicdoublegetTotalFitness(){

returntotalFitness;

)

publicvoidsetTotalFitness(doubletotalFitness){

this.totalFitness=totalFitness;

)

publicIndividualgetBestlndividual(){

returnbestindividual;

)

publicvoidsetBestlndividualflndividualbestindividual){

this.bestlndividual=bestindividual;

)

publicIndividualgetWorstlndividualf){

returnworstindividual;

)

publicvoid<;pfWcr<;tlndividuAl(lndividuAlwor<;tlndividiJAl){

this.worstlndividual=worstindividual;

)

publicIndividualgetCurrentBeslndividual(){

returncurrentBesIndividual;

)

publicvoidsetCurrentBeslndividual(lndividualcurrentBesIndividual){

this.currentBesIndividual=currentBesIndividual;

)

//抽象辦法

publicabstractPopulationevaluate(CodeScalescale,Populationp);//評估種群

publicabstractPopulationcalTotalFitness(CodeScalescale,Populationp);//計算總適

應(yīng)度

publicabstractPcpulationcalAverageFitness(CodeScalescale,Populationp);//i|算平

均適應(yīng)度

publicabstractPcpulationcalRelativeFitness(CodeScalescale,Populationp);//計算相

對適應(yīng)度

publicabstractPcpulationfindBestAndWorstlndivdualfCodeScalescale,

Populationp);//尋找當(dāng)前種群中最優(yōu)個體和最差個體,尋找當(dāng)前最優(yōu)個體

publicabstractPopulationrandomlylnitPopulation(CodeScalescale);//隨機(jī)初始化種

(?Override

publicObjectclone()throwsCloneNotSupportedException{

Populationg=null;

try(

g=(Population)super.clone();

g.pop=pop.clone();

}catch(CloneNotSupportedExceptione){

System.out.println(e.getMessage());

)

returng;

)

publicStringtoStringO{

Stringstr=

for(inti=0;i<pop.length;i++){

str+=pDp[i].toString();

)

returnstr;

)

)

publicclassRosenbrockPopulationextendsPopulationmplementsCloneable{

(?Override

publicPopulationevaluate(CodeScalescale,Populationp){

//計算總適應(yīng)度

Populationpop=newRosenbrockPopulation();

pop=calTotalFitness(scale?p);

//計算平及適應(yīng)度

pop=calAverageFitness(scale,p);

//計算相對適應(yīng)度

pop=calRelativeFitnessfscale,p);

//尋找最優(yōu)個體、最差、當(dāng)前代最優(yōu)個體

pop=findBestAndWorstlndividual(scale,p);

returnpop;

)

@Override

publicPopulationcalTotalFitness(CodeScalescale.Populationp){

doubletotalFitness=0.0;

for(inti=0:i<scale.populationsize;i++){

totalFitness=totalFitness+p.getPop()[i].getFitness();

)

Populationpop=newRosenbrockPopulation();

pop=p;

pop.setTotalFitness(totalFitness);

returnpop;

)

(?Override

publicPopulationcalAverageFitness(CodeScalescale,Populationp){

doubleaverageFitness=p.totalFitness/scale.populationsize;

Populationpop=newRosenbrockPopulation();

pop=p;

pop.setAverageFitness(averageFitness);

returnpop;

)

@Override

publicPopulationcalRelativeFitness(CodeScalescale,Populationp){

Individual口individual=newlndividual[scale.populationSize];

for(inti=0;i<scale.populationsize;i++){

doublerelativeFitness=p.pop[i].fitness/p.totalFitness;

Individualindiv=newRosenbrocklndividual();

indiv=p.pop[i];

indiv.se:RelativeFitness(relativeFitness);

individual1]=indiv;

)

Populationpop=newRosenbrockPopulation();

pop=p;

pop.setPop(individual);

returnpop;

)

@Override

publicPopulationfindBestAndWorstlndividual(CodeScalescale,Populationp){

intbestindex=0;

Individualbestindividual=newRosenbrocklndividual();

Individualworstindividual=newRosenbrocklndividualf);

IndividualcurrentBesIndividual=newRosenbrocklndividual();

bestindividual=p.pop[0];

worstindividual=p.pop[0];

if(p.generationld==0){//第一代

currentBesIndividual=p.pop[0];

)

for(inti=0;i<scale.populationsize;i++){

if(p.pop[i].getFitness()>bestlndividual.getFitnessf)){

bestindividual=p.pop[i];

bestindex=i;

}

if(p.pop[i].getFitness()<worstlndividual.getFitnessO){

wcrstlndividual=p.popfi);

|

if(bestindividual.getFitness。>currentBeslndividual.getFitness()){

currentBesIndividual=bestindividual;

)

)

Populationpop=newRosenbrockPopulation();

pop=p;

pop.setBestlndex(bestlndex);

pop.setBestlndividual(bestlndividual);

pop.setCurrentBeslndividual(currentBeslndividual);

pop.setWorstlndividual(worstlndividual);

returnpop;

)

(?Override

publicPopulationrandomlylnitPopulation(CodeScalescale){

Individualindividual=newRosenbrocklndividual();

Individualindiv[]=newIndividual[scale.populationsize];

for(inti=0;i<scale.populationSize;i++){

indiv[i]=individual.randomlyGeneratlndvidual(scale);

indiv[i]=individual.calTargetValue(indiv[i]);

indiv[i]=individual.calFitness(indiv[i));

)

Populationp=newRosenbrockPopulation();

p.generationld=0;

p.pop=indiv;

p=p.evaluate(scale,p);

returnp;

)

@Override

publicObjectclone()throwsCloneNotSupportedException{

Populationg=null;

try(

g=(Population)super.clone();

}catch(CloneNotSupportedExceptione){

System.out.println(e.getMessage());

)

returng;

)

)

8、遺傳類(抽象類GeneticAlgorithm,詳細(xì)類RosenbrockGeneticAlgorithm)

publicabstractclassGeneticAlgorithm{

protecteddoublecrossoverRate;//交叉概率

protecteddoublemutateRate;//變異概率

protectedCodeScalescale;〃編碼規(guī)模

protectedPopulationfirstPopulation;//第一代種群

publicdoublegetCrossoverRate(){

returncrossoverRate;

)

publicvoidsetCrcssoverRate(doublecrossoverRate){

this.crossoverRate=crossoverRate;

)

publicdoublegetMutateRate(){

returnmutateRate;

)

publicvoidsetMutateRate(doublemutateRate){

this.mutateRate=mutateRate;

)

publicCodeScalegetScale(){

returnscale;

)

publicvoidsetScale(CodeScalescale){

this.scale=scale;

)

publicPopulationgetFirstPopulation(){

returnfirstPopulation;

)

publicvoidsetFirstPopulationfPopulationfirstPopulation){

this.firstPopulation=firstPopulation;

)

〃抽象辦法

publicabstractPcpulationselect(GeneticAlgorithmga,Populationp);〃選取

publicabstractPopulationcrossover(GeneticAlgorithmga,Populationp);//交叉

publicabstractPopulationmutatefGeneticAlgorithmga,Populationp);//變異

publicabstractPopulationevolve(GeneticAlgorithmga,Populationp);//進(jìn)化

publicabstractbooleanisEvolutionDone(GeneticAlgorithmga,Populationp);//判斷進(jìn)

化與否完畢

publicabstractvoidexecutive(GeneticAlgorithmga);〃執(zhí)行

)

publicclassRosenbrockGeneticAlgorithmextendsGeneticAlgorithm{

(?Override

publicPopulationselect(GeneticAlgorithmga,Populationp){

doubledrouletteWheel://賭盤

Individual口childPop=newlndividual[ga.scale.populationSize];

//產(chǎn)生賭盤

rouletteWheel=newdoublefga.scale.populationsize);

rouletteWheel[0]=p.pop[0].relativeFitness;

for(inti=1;i<ga.scale.populationsize-1:i++){

rouletteWheel[i]=p.pop[i].relativeFitness+rouletteWheel[i-1];

)

rouletteWheel[ga.scale.populationsize-1]=1;

//進(jìn)行賭盤選取,產(chǎn)生新種群

for(inti=0;i<ga.scale.populationSize;i++){

doublernd=rand();

for(intj=0;j<ga.scale.populationSize;j++){

if(rnd<rouletteWheel[j]){

childPop[i]=p.popO];

break;

)

}

)

PopulationciildPopulation=newRosenbrockPopulation();

try(

childPopulation=(Population)p.clonef);

}catch(CloneNotSupportedExceptione){

e.printStackTrace();

)

childPopulation.setPop(childPop);

childPopulation.setGenerationld(p.genera

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論