遗传算法的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
分享到:
评论

你可能感兴趣的:(C++,c,算法,C#,J#)