C++遺傳算法類(lèi)文件實(shí)例分析
本文所述為C++實(shí)現(xiàn)的遺傳算法的類(lèi)文件實(shí)例。一般來(lái)說(shuō)遺傳算法可以解決許多問(wèn)題,希望本文所述的C++遺傳算法類(lèi)文件,可幫助你解決更多問(wèn)題,并且代碼中為了便于讀者更好的理解,而加入了豐富的注釋內(nèi)容,是新手學(xué)習(xí)遺傳算法不可多得的參考代碼。
具體代碼如下所示:
#include "stdafx.h"
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>//把日期和時(shí)間轉(zhuǎn)換為字符串
using namespace std;
//Parametes setting
#define POPSIZE 200 //population size
#define MAXGENS 1000 //max number of generation
#define NVARS 2 //no of problem variables
#define PXOVER 0.75 //probalility of crossover
#define PMUTATION 0.15 //probalility of mutation
#define TRUE 1
#define FALSE 0
#define LBOUND 0
#define UBOUND 12
#define STOP 0.001
int generation; //current generation no
int cur_best; //best individual
double diff;
FILE *galog; //an output file
struct genotype
{
double gene[NVARS]; //a string of variables基因變量
double upper[NVARS]; //individual's variables upper bound 基因變量取值上確界
double lower[NVARS]; //individual's batiables lower bound 基因變量取值下確界
double fitness; //individual's fitness個(gè)體適應(yīng)值
double rfitness; //relative fitness個(gè)體適應(yīng)值占種群適應(yīng)值比例
double cfitness; //curmulation fitness個(gè)體適應(yīng)值的累加比例
};
struct genotype population[POPSIZE+1];
//population 當(dāng)前種群 population[POPSIZE]用于存放個(gè)體最優(yōu)值并假設(shè)最優(yōu)個(gè)體能存活下去
//在某些遺傳算法中最優(yōu)值個(gè)體并不一定能夠存活下去
struct genotype newpopulation[POPSIZE+1]; //new population replaces the old generation 子種群
/*Declaration of procedures used by the gentic algorithm*/
void initialize(void); //初始化函數(shù)
double randval(double,double); //隨機(jī)函數(shù)
double funtion(double x1,double x2); //目標(biāo)函數(shù)
void evaluate(void); //評(píng)價(jià)函數(shù)
void keep_the_best(void); //保留最優(yōu)個(gè)體
void elitist(void); //當(dāng)前種群與子代種群最優(yōu)值比較
void select(void);
void crossover(void); //基因重組函數(shù)
void swap(double *,double *); //交換函數(shù)
void mutate(void); //基因突變函數(shù)
double report(void); //數(shù)據(jù)記錄函數(shù)
void initialize(void)
{
int i,j;
for(i=0;i<NVARS;i++)
{
for(j=0;j<POPSIZE+1;j++)
{
if(!i)
{
population[j].fitness=0;
population[j].rfitness=0;
population[j].cfitness=0;
}
population[j].lower[i]=LBOUND;
population[j].upper[i]=UBOUND;
population[j].gene[i]=randval(population[j].lower[i],population[j].upper[i]);
}
}
}
//***************************************************************************
//Random value generator:generates a value within bounds
//***************************************************************************
double randval(double low,double high)
{
double val;
val=((double)(rand()%10000)/10000)*(high-low)+low;
return val;
}
//目標(biāo)函數(shù)
double funtion(double x,double y)
{
double result1=sqrt(x*x+y*y)+sqrt((x-12)*(x-12)+y*y)+sqrt((x-8)*(x-8)+(y-6)*(y-6));
return result1;
}
//***************************************************************************
//Evaluation function:evaluate the individual's fitness.評(píng)價(jià)函數(shù)給出個(gè)體適應(yīng)值
//Each time the function is changes,the code has to be recompl
//***************************************************************************
void evaluate(void)
{
int mem;
int i;
double x[NVARS];
for(mem=0;mem<POPSIZE;mem++)
{
for(i=0;i<NVARS;i++)
x[i]=population[mem].gene[i];
population[mem].fitness=funtion(x[0],x[1]);//將目標(biāo)函數(shù)值作為適應(yīng)值
}
}
//***************************************************************************************
//Keep_the_best function:This function keeps track of the best member of the population.
//找出種群中的個(gè)體最優(yōu)值并將其移動(dòng)到最后
//***************************************************************************************
void keep_the_best()
{
int mem;
int i;
cur_best=0;
for(mem=0;mem<POPSIZE;mem++)//找出最高適應(yīng)值個(gè)體
{
if(population[mem].fitness<population[cur_best].fitness)
{
cur_best=mem;
}
}
//將最優(yōu)個(gè)體復(fù)制至population[POSIZE]
if(population[cur_best].fitness<=population[POPSIZE].fitness||population[POPSIZE].fitness<1)//防止出現(xiàn)種群基因退化 故保留歷史最優(yōu)個(gè)體
{
population[POPSIZE].fitness=population[cur_best].fitness;
for(i=0;i<NVARS;i++)
population[POPSIZE].gene[i]=population[cur_best].gene[i];
}
}
//***************************************************************************
//last in the array.If the best individual from the new populatin is better
//than the best individual from the previous population ,then copy the best
//from the new population;else replace the worst individual from the current
//population with the best one from the previous generation.防止種群最優(yōu)值退化
//***************************************************************************
void elitist()
{
int i;
double best,worst;//適應(yīng)值
int best_mem,worst_mem;//序號(hào)
best_mem=worst_mem=0;
best=population[best_mem].fitness;//最高適應(yīng)值初始化
worst=population[worst_mem].fitness;//最低適應(yīng)值初始化
for(i=1;i<POPSIZE;i++)//找出最高和最低適應(yīng)值 算法有待改進(jìn)
{
if(population[i].fitness<best)
{
best=population[i].fitness;
best_mem=i;
}
if(population[i].fitness>worst)
{
worst=population[i].fitness;
worst_mem=i;
}
}
if(best<=population[POPSIZE].fitness)//賦值
{
for(i=0;i<NVARS;i++)
population[POPSIZE].gene[i]=population[best_mem].gene[i];
population[POPSIZE].fitness=population[best_mem].fitness;
}
else
{
for(i=0;i<NVARS;i++)
population[worst_mem].gene[i]=population[POPSIZE].gene[i];
population[worst_mem].fitness=population[POPSIZE].fitness;
}
}
//***************************************************************************
//Select function:Standard proportional selection for maximization problems
//incorporating elitist model--makes sure that the best member survives.篩選函數(shù)并產(chǎn)生子代
//***************************************************************************
void select(void)
{
int mem,i,j;
double sum=0;
double p;
for(mem=0;mem<POPSIZE;mem++)//所有適應(yīng)值求和
{
sum+=population[mem].fitness;
}
for(mem=0;mem<POPSIZE;mem++)
{
population[mem].rfitness=population[mem].fitness/sum;//個(gè)人認(rèn)為還不如建一個(gè)種群類(lèi) 把sum看成類(lèi)成員
}
population[0].cfitness=population[0].rfitness;
for(mem=1;mem<POPSIZE;mem++)
{
population[mem].cfitness=population[mem-1].cfitness+population[mem].rfitness;
}
for(i=0;i<POPSIZE;i++)
{
p=rand()%1000/1000.0;
if(p<population[0].cfitness)
{
newpopulation[i]=population[0];
}
else
{
for(j=0;j<POPSIZE;j++)
if(p>=population[j].cfitness&&p<population[j+1].cfitness)
newpopulation[i]=population[j+1];
}
}
for(i=0;i<POPSIZE;i++)//子代變父代
population[i]=newpopulation[i];
}
//***************************************************************************
//Crossover:performs crossover of the selected parents.
//***************************************************************************
void Xover(int one,int two)//基因重組函數(shù)
{
int i;
int point;
if(NVARS>1)
{
if(NVARS==2)
point=1;
else
point=(rand()%(NVARS-1))+1;//兩個(gè)都重組嗎?
for(i=0;i<point;i++)//只有第一個(gè)基因發(fā)生重組有待改進(jìn)
swap(&population[one].gene[i],&population[two].gene[i]);
}
}
//***************************************************************************
//Swapp: a swap procedure the helps in swappling 2 variables
//***************************************************************************
void swap(double *x,double *y)
{
double temp;
temp=*x;
*x=*y;
*y=temp;
}
//***************************************************************************
//Crossover function:select two parents that take part in the crossover.
//Implements a single point corssover.雜交函數(shù)
//***************************************************************************
void crossover(void)
{
int mem,one;
int first=0;
double x;
for(mem=0;mem<POPSIZE;++mem)
{
x=rand()%1000/1000.0;
if(x<PXOVER)
{
++first;
if(first%2==0)//選擇雜交的個(gè)體對(duì) 雜交有待改進(jìn) 事實(shí)上往往是強(qiáng)者與強(qiáng)者雜交 這里沒(méi)有考慮雌雄與雜交對(duì)象的選擇
Xover(one,mem);
else
one=mem;
}
}
}
//***************************************************************************
//Mutation function:Random uniform mutation.a variable selected for mutation
//變異函數(shù) 事實(shí)基因的變異往往具有某種局部性
//is replaced by a random value between lower and upper bounds of the variables.
//***************************************************************************
void mutate(void)
{
int i,j;
double lbound,hbound;
double x;
for(i=0;i<POPSIZE;i++)
for(j=0;j<NVARS;j++)
{
x=rand()%1000/1000.0;
if(x<PMUTATION)
{
lbound=population[i].lower[j];
hbound=population[i].upper[j];
population[i].gene[j]=randval(lbound,hbound);
}
}
}
//***************************************************************************
//Report function:Reports progress of the simulation.
//***************************************************************************
double report(void)
{
int i;
double best_val;//種群內(nèi)最優(yōu)適應(yīng)值
double avg;//平均個(gè)體適應(yīng)值
//double stddev;
double sum_square;//種群內(nèi)個(gè)體適應(yīng)值平方和
//double square_sum;
double sum;//種群適應(yīng)值
sum=0.0;
sum_square=0.0;
for(i=0;i<POPSIZE;i++)
{
sum+=population[i].fitness;
sum_square+=population[i].fitness*population[i].fitness;
}
avg=sum/(double)POPSIZE;
//square_sum=avg*avg*(double)POPSIZE;
//stddev=sqrt((sum_square-square_sum)/(POPSIZE-1));
best_val=population[POPSIZE].fitness;
fprintf(galog,"%6d %6.3f %6.3f %6.3f %6.3f %6.3f\n",generation,best_val,population[POPSIZE].gene[0],population[POPSIZE].gene[1],avg,sum);
return avg;
}
//***************************************************************************
//main function:Each generation involves selecting the best members,performing
//crossover & mutation and then evaluating the resulting population,until the
//terminating condition is satisfied.
//***************************************************************************
void main(void)
{
int i;
double temp;
double temp1;
if((galog=fopen("data.txt","w"))==NULL)
{
exit(1);
}
generation=1;
srand(time(NULL));//產(chǎn)生隨機(jī)數(shù)
fprintf(galog,"number value x1 x2 avg sum_value\n");
printf("generation best average standard\n");
initialize();
evaluate();
keep_the_best();
temp=report();//記錄,暫存上一代個(gè)體平均適應(yīng)值
do
{
select();//篩選
crossover();//雜交
mutate();//變異
evaluate();//評(píng)價(jià)
keep_the_best();//elitist();
temp1=report();
diff=fabs(temp-temp1);//求浮點(diǎn)數(shù)x的絕對(duì)值
temp=temp1;
generation++;
}while(generation<MAXGENS&&diff>=STOP);
//fprintf(galog,"\n\n Simulation completed\n");
//fprintf(galog,"\n Best member:\n");
printf("\nBest member:\ngeneration:%d\n",generation);
for(i=0;i<NVARS;i++)
{
//fprintf(galog,"\n var(%d)=%3.3f",i,population[POPSIZE].gene[i]);
printf("X%d=%3.3f\n",i,population[POPSIZE].gene[i]);
}
//fprintf(galog,"\n\n Best fitness=%3.3f",population[POPSIZE].fitness);
fclose(galog);
printf("\nBest fitness=%3.3f\n",population[POPSIZE].fitness);
}
感興趣的讀者可以動(dòng)手測(cè)試一下代碼,希望對(duì)大家學(xué)習(xí)C++算法能有所幫助。
上一篇:C++實(shí)現(xiàn)順序排序算法簡(jiǎn)單示例代碼
欄 目:C語(yǔ)言
本文標(biāo)題:C++遺傳算法類(lèi)文件實(shí)例分析
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/3529.html
您可能感興趣的文章
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 01-10深入理解C++中常見(jiàn)的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10深入第K大數(shù)問(wèn)題以及算法概要的詳解
- 01-10c++中inline的用法分析
- 01-10深入N皇后問(wèn)題的兩個(gè)最高效算法的詳解
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類(lèi)算法
- 01-10深入全排列算法及其實(shí)現(xiàn)方法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)


