程序代码:(代码标记[code]...[/code])
#include<iostream.h>
#include<math.h>
#include<time.h>
//#include<graphics.h>
#include<stdlib.h>
#include<string.h>
#definemaxpop100
#definemaxstring32

typedefstruct...{
charchrom[maxstring];
floatx,fitness;
intparent1,parent2,xsite;
}pp;
pp*oldpop,*newpop,*p1;
intpopsize,lchrom,gen,maxgen,nmutation,ncross,jcross,maxpp,minpp,jrand;
floatpcross,pmutation,sumfitness,avg,max,min,seed,rj[maxpop],oldrand[maxpop];
doublecoef;
floatobjfunc(float);
intselect();
intflip(float);
intcrossover(char*,char*,int);
charmutation();
voidgeneration();
voidinitialize();
voidreport();
voidinitpop();
voidinitdata();
voidinitreport();
doubledecode(char*);
floatrandom1();
voidrandomize1();
voidpause();
voidtest(charx)
...{cout<<x<<' ';}
floatobjfunc(floatx1)
//computeobjectfitness;
...{
floaty;
y=3.14*x1;
y=sin(2.0*y);
returny*y;
}
voidstatistic(pp*pop)
//statisticthefitnessofpopulation
...{
intj;
sumfitness=pop[0].fitness;
max=pop[0].fitness;
min=pop[0].fitness;
maxpp=0;
minpp=0;
for(j=1;j<popsize;j++)...{
sumfitness=sumfitness+pop[j].fitness;
if(pop[j].fitness>max)...{
max=pop[j].fitness;
maxpp=j;
}
if(pop[j].fitness<min)...{
min=pop[j].fitness;
minpp=j;
}
}//endfor
avg=sumfitness/(float)popsize;
}
voidgeneration()
//updateageneration;
...{
intj,mate1,mate2;
j=0;
do...{
mate1=select();
mate2=select();
crossover(oldpop[mate1].chrom,oldpop[mate2].chrom,j);
newpop[j].x=(float)decode(newpop[j].chrom);
newpop[j].fitness=objfunc(newpop[j].x);
newpop[j].parent1=mate1;
newpop[j].parent2=mate2;
newpop[j].xsite=jcross;//recodethecrosspoint;
newpop[j+1].x=(float)decode(newpop[j+1].chrom);
newpop[j+1].fitness=objfunc(newpop[j+1].x);
newpop[j+1].parent1=mate1;
newpop[j+1].parent2=mate2;
newpop[j+1].xsite=jcross;
j=j+2;
}while(j<popsize);
}
voidinitdata()
//inputcontrolparameters
...{
intch,j;
cout<<"*********SGADATAENTRYANDINITIALIZATION******* ";
cout<<"Enterpopulationsize:";
cin>>popsize;
//cout<<"Enterchromosomelength:";
//cin>>lchrom;
lchrom=32;
cout<<"Entermaxgenerations";
cin>>maxgen;
cout<<"Entercrossoverprobability:";
cin>>pcross;
cout<<"Entermutationprobability:";
cin>>pmutation;
//randomize1();
nmutation=0;
ncross=0;
}
voidinitreport()
...{
cout<<"Populationsize:"<<popsize<<' ';
//cout<<"Chromosomelength:"<<lchrom<<' ';
cout<<"Maximum#ofgeneration:"<<maxgen<<' ';
cout<<"Crossoverprobability:"<<pcross<<' ';
cout<<"Mutationprobability:"<<pmutation<<' ';
cout<<"----------------------------------------------------- ";
cout<<"InitialPopulationMaximumFitness:"<<max<<' ';
cout<<"InitialPopulationAverageFitness:"<<avg<<' ';
cout<<"InitialPopulationMinimunFitness:"<<min<<' ';
cout<<"InitialPopulationSumofFitness:"<<sumfitness<<' ';
pause();
}
voidinitpop()
...{
srand((unsigned)time(NULL));
intbit;
oldpop=newpp[popsize];
newpop=newpp[popsize];
intj,j1;
for(j=0;j<popsize;j++)...{
for(j1=0;j1<lchrom;j1++)...{
if(rand()%2==1)
oldpop[j].chrom[j1]='1';
elseoldpop[j].chrom[j1]='0';
}
oldpop[j].x=(float)decode(oldpop[j].chrom);
oldpop[j].fitness=objfunc(oldpop[j].x);
oldpop[j].parent1=0;
oldpop[j].parent2=0;
oldpop[j].xsite=0;
}//endfor
}
voidinitialize()
...{
initdata();
//test('a');
coef=pow(2.00,lchrom)-0.1;
initpop();
statistic(oldpop);
initreport();
}
voidreport(intgen)
...{
intk,j;
cout<<"****************************************************** ";
cout<<"Generation:"<<gen<<' ';
cout<<"#parentsxsitestringxfitness ";
for(j=0;j<popsize;j++)
...{
cout<<j<<" "<<newpop[j].parent1<<' '<<newpop[j].parent2<<' '<<newpop[j].xsite<<' ';
for(k=0;k<lchrom;k++)cout<<newpop[j].chrom[k];
cout<<' '<<newpop[j].x<<' '<<newpop[j].fitness<<' ';
}
cout<<"******************************************************** ";
cout<<"ResultGen:"<<gen<<' ';
cout<<"AVG="<<avg<<' '<<"MIN="<<min<<' '<<"MAX="<<max<<' ';
cout<<"ncross="<<ncross<<' '<<"nmutation="<<nmutation<<' ';
}
intselect()
...{
doublerand1,partsum;
intj;
partsum=0.0;
j=0;
rand1=random1()*sumfitness;
do...{
partsum=partsum+oldpop[j].fitness;
j=j+1;
}while((partsum<rand1)&&(j<popsize));
returnj-1;
}
doubledecode(char*pp)
//decodechromtotherealnumber

本文介绍了一个使用C语言实现的遗传算法示例,通过定义种群规模、染色体长度及最大迭代次数等参数来优化目标函数。该算法包含了选择、交叉与变异等基本遗传操作,并记录了每一代的最佳解。

被折叠的 条评论
为什么被折叠?



