遺傳算法解決二次函數(shù)問題_第1頁
遺傳算法解決二次函數(shù)問題_第2頁
遺傳算法解決二次函數(shù)問題_第3頁
遺傳算法解決二次函數(shù)問題_第4頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

學習好資料歡迎下載學習好資料歡迎下載學習好資料歡迎下載<遺傳算法>技術文檔問題:

利用遺傳算法求解區(qū)間[0,

31]上的二次函數(shù)y=x2的最大值

VS2005C++環(huán)境C++面向對象的思想設計遺傳算法各參數(shù)變量的說明(1)shortbitString;//個體的二進制編碼(2)shortfitness;//個體的適應度(3)staticconstshortbitCount=5;//個體二進制碼的長度(4)staticconstshortpopuScale=10;//種群規(guī)模(5)#defineCROSS_MUTATE_PRO_COMP100//交叉、變異的基數(shù)(6)staticconstshortpc=40;//交叉?zhèn)€體數(shù)相對于基數(shù)(7)staticconstshortpm=5;//變異個體數(shù)相對于基數(shù)(8)IndividualpopuArray[popuScale];//種群(9)shortgen=0;//種群進化的代數(shù)遺傳操作的設計思想

分別設計了一個個體類、種群類,種群由個體組成,這是類庫設計,對用戶來說,分別使用這兩個類來實現(xiàn)遺傳算法,其中選擇中采用輪盤賭來選擇下一代。(1)、個體類設計如下:#ifndef__INDIVIDUAL_H__#define__INDIVIDUAL_H__#include<iostream>classIndividual{private: shortbitString;//個體的二進制編碼 shortfitness;//個體的適應度public: staticconstshortbitCount=5;//個體二進制碼的長度 Individual(); Individual(shortn); Individual(shortbitStr,intfit); ~Individual(); shortGetFitness()const; shortGetBitString()const; //voidsetBitString(); //voidsetFitness(); booloperator>(constIndividual&indi)const; booloperator==(constIndividual&indi)const; friendstd::ostream&operator<<(std::ostream&os,constIndividual&indi); voidShowIndividual(void)const;};#endif(2)種群類設計如下:#ifndef__POPULATION_H__#define__POPULATION_H__#include"individual.h"#defineCROSS_MUTATE_PRO_COMP100//交叉、變異的基數(shù)classPopulation{public: staticconstshortpopuScale=10;//種群規(guī)模private: staticconstshortpc=40;//交叉?zhèn)€體數(shù)相對于基數(shù) staticconstshortpm=5;//變異個體數(shù)相對于基數(shù) IndividualpopuArray[popuScale];public: Population(); voidSelection();//選擇 voidCrossover();//交叉 voidMutation();//變異 constIndividual&GetbestIndividual()const; constIndividual&GetRepresatationIndividual()const; friendstd::ostream&operator<<(std::ostream&os,constPopulation&p); voidShowPopulation()const; ~Population();};#endif(3)用戶區(qū)程序代碼(遺傳算法實現(xiàn)):#include<iostream>#include"individual.h"#include"population.h"voidGA(void);intmain(){ GA();}voidGA(void){ std::cout<<"Pleaseenterhowmanygenerationsyouwantthepopulationtoevolve!"<<std::endl; shortgen=0;//種群進化的代數(shù) std::cin>>gen; Populationbirds; for(inti=0;i<gen;i++) { std::cout<<"the"<<i<<"thgeneration:"<<std::endl; std::cout<<birds<<std::endl; birds.Selection(); std::cout<<"afterselectionthepopulationis:"<<std::endl; std::cout<<birds<<std::endl; birds.Crossover(); std::cout<<"aftercrossoverthepopulationis:"<<std::endl; std::cout<<birds<<std::endl; birds.Mutation(); std::cout<<"aftermutationthepopulationis:"<<std::endl; std::cout<<birds<<std::endl; std::cout<<std::endl<<std::endl; } std::cout<<"After"<<gen<<"evolutionthebestfitnessIndividualbirdis:"<<std::endl; std::cout<<birds.GetbestIndividual()<<std::endl; std::cout<<"After"<<gen<<"evolutiontherepresetationalIndividualbirdis:"<<std::endl; std::cout<<birds.GetRepresatationIndividual()<<std::endl;}程序流程圖

開始開始Gen=0編碼隨機產(chǎn)生N個初始個體滿足終止條件?計算群體中各個體適應度從左至右依次執(zhí)行遺傳算子j=0j=0j=0根據(jù)適應度選擇復制個體選擇兩個交叉?zhèn)€體選擇個體變異點執(zhí)行變異執(zhí)行交叉執(zhí)行復制將復制的個體添入新群體中將交叉后的兩個新個體添入新群體中將變異后

溫馨提示

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

評論

0/150

提交評論