遗传算法的c程序




程序代码:(代码标记[code]...[ / code])

#include
< iostream.h >
#include
< math.h >
#include
< time.h >
// #include<graphics.h>
#include < stdlib.h >
#include
< string .h >

#define maxpop100
#define maxstring32

typedef
struct ... {
charchrom[maxstring];
floatx,fitness;
intparent1,parent2,xsite;
}
pp;

pp
* oldpop, * newpop, * p1;
int popsize,lchrom,gen,maxgen,nmutation,ncross,jcross,maxpp,minpp,jrand;
float pcross,pmutation,sumfitness,avg,max,min,seed,rj[maxpop],oldrand[maxpop];
double coef;

float objfunc( float );
int select();
int flip( float );
int crossover( char * , char * , int );
char mutation();
void generation();
void initialize();
void report();
void initpop();
void initdata();
void initreport();
double decode( char * );
float random1();
void randomize1();
void pause();

void test( char x)
... {cout<<x<<' ';}

float objfunc( float x1)
// computeobjectfitness;
... {
floaty;
y
=3.14*x1;
y
=sin(2.0*y);
returny*y;
}


void statistic(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;
}


void generation()
// 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);
}


void initdata()
// 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;
}


void initreport()
... {
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();
}


void initpop()
... {
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
}


void initialize()
... {
initdata();
//test('a');
coef=pow(2.00,lchrom)-0.1;
initpop();
statistic(oldpop);
initreport();
}


void report( int gen)
... {
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<<' ';
}


int select()
... {
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;
}


double decode( char * pp)
// decodechromtotherealnumber
... {
intj;
doublett,tt1;
tt1
=1.0;
tt
=0.0;
for(j=lchrom-1;j>-1;j--)...{
//for(j=31;j>-1;j--){
if(pp[j]=='1')tt=tt+tt1;
tt1
=2.0*tt1;
}

tt
=tt/coef;
//tt=tt/(pow(2.0,32)-0.1);
returntt;
}


void pause()
... {
intj,j1;
intx1;
x1
=0;
for(j=1;j<=25;j++)
for(j1=1;j1<2;j1++)x1=x1+1;
}


char mutation( char ch)
// mutationoperate
... {
intmutate,j;
mutate
=flip(pmutation);
if(mutate)
...{
nmutation
=nmutation+1;//???nmutation??
if(ch)ch=0;
elsech=1;
}

if(ch)return'1';
elsereturn'0';
}


int crossover( char * parent1, char * parent2, int k5)
// crossoveroperate
... {
inti,j,j1;
if(flip(pcross))
...{
jcross
=rand()%(lchrom-1);
ncross
=ncross+1;//???ncross?
}

elsejcross=lchrom;
if(jcross!=lchrom)
...{
for(j=0;j<jcross;j++)...{
newpop[k5].chrom[j]
=mutation(parent1[j]);
newpop[k5
+1].chrom[j]=mutation(parent2[j]);
}

for(j=jcross;j<lchrom;j++)...{
newpop[k5].chrom[j]
=mutation(parent2[j]);
newpop[k5
+1].chrom[j]=mutation(parent1[j]);
}

}
//endif
else
...{
for(j=0;j<lchrom;j++)
...{
newpop[k5].chrom[j]
=mutation(parent1[j]);
newpop[k5
+1].chrom[j]=mutation(parent2[j]);
}

}
//endelse
return1;
}


void randomize1()
// resettherandomnumbergenerator
... {
inti;
srand((unsigned)time(NULL));
//randomize();//???????generateerrors"randomizeundeclared"
for(i=0;i<lchrom;i++);
oldrand[i]
=(rand()%1000)/1000.0;
jrand
=0;
}


float random1()
// generatearandomnumber
... {
jrand
=jrand+1;
if(jrand>=lchrom)
...{
jrand
=0;
randomize1();
}

returnoldrand[jrand];
}


int flip( float probability)
// 贝努利实验
... {
floatppp;
ppp
=(rand()%1000)/1000.0;
if(ppp<=probability)return1;
return0;
}


void main()
... {
doubledetest;
chartestch[32];
for(inttesti=0;testi<32;testi++)...{
if(rand()%2==1)
testch[testi]
='1';
elsetestch[testi]='0';
}

detest
=decode(testch);
cout
<<detest<<' ';


charc;
longintgen,k,j;
floatoldmax;
intoldmaxpp;
//pp*oldpop,*newpop,*p1;
//oldpop=newpp[maxpop];
//newpop=newpp[maxpop];

/**//*oldpop=newpp[maxpop];
newpop=newpp[maxpop];

p1=newpp;
for(k=0;k<maxpop;k++)
for(j=0;j<maxstring;j++){
oldpop[k].chrom[j]='0';
newpop[k].chrom[j]='0';
}
*/

gen
=0;
initialize();
p1
=newpop;
newpop
=oldpop;
statistic(newpop);
report(gen);
newpop
=p1;
cin
>>c;
do...{
gen
=gen+1;
oldmax
=max;
oldmaxpp
=maxpp;
generation();
statistic(newpop);
if(max<oldmax)...{
for(j=0;j<lchrom;j++)
newpop[minpp].chrom[j]
=oldpop[oldmaxpp].chrom[j];
newpop[minpp].x
=oldpop[oldmaxpp].x;
newpop[minpp].fitness
=oldpop[oldmaxpp].fitness;
statistic(newpop);
}

//report(gen);
p1=oldpop;
oldpop
=newpop;
newpop
=p1;
//cin>>c;
}
while(gen<maxgen);
report(gen);
free(p1);
free(oldpop);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值