这是一个非常简单的遗传算法源代码对一特定的应用修正Word文档下载推荐.docx
《这是一个非常简单的遗传算法源代码对一特定的应用修正Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《这是一个非常简单的遗传算法源代码对一特定的应用修正Word文档下载推荐.docx(25页珍藏版)》请在冰豆网上搜索。
#defineNVARS3/*no.ofproblemvariables*/
#definePXOVER0.8/*probabilityofcrossover*/
#definePMUTATION0.15/*probabilityofmutation*/
#defineTRUE1
#defineFALSE0
intgeneration;
/*currentgenerationno.*/
intcur_best;
/*bestindividual*/
FILE*galog;
/*anoutputfile*/
structgenotype/*genotype(GT),amemberofthepopulation*/
{
doublegene[NVARS];
/*astringofvariables*/
doublefitness;
/*GT'
sfitness*/
doubleupper[NVARS];
svariablesupperbound*/
doublelower[NVARS];
svariableslowerbound*/
doublerfitness;
/*relativefitness*/
doublecfitness;
/*cumulativefitness*/
};
structgenotypepopulation[POPSIZE+1];
/*population*/
structgenotypenewpopulation[POPSIZE+1];
/*newpopulation;
*/
/*replacesthe*/
/*oldgeneration*/
/*Declarationofproceduresusedbythisgeneticalgorithm*/
voidinitialize(void);
doublerandval(double,double);
voidevaluate(void);
voidkeep_the_best(void);
voidelitist(void);
voidselect(void);
voidcrossover(void);
voidXover(int,int);
voidswap(double*,double*);
voidmutate(void);
voidreport(void);
/***************************************************************/
/*Initializationfunction:
Initializesthevaluesofgenes*/
/*withinthevariablesbounds.Italsoinitializes(tozero)*/
/*allfitnessvaluesforeachmemberofthepopulation.It*/
/*readsupperandlowerboundsofeachvariablefromthe*/
/*inputfile`gadata.txt'
.Itrandomlygeneratesvalues*/
/*betweentheseboundsforeachgeneofeachgenotypeinthe*/
/*population.Theformatoftheinputfile`gadata.txt'
is*/
/*var1_lower_boundvar1_upperbound*/
/*var2_lower_boundvar2_upperbound...*/
voidinitialize(void)
FILE*infile;
inti,j;
doublelbound,ubound;
if((infile=fopen("
gadata.txt"
"
r"
))==NULL)
{
fprintf(galog,"
\nCannotopeninputfile!
\n"
);
exit
(1);
}
/*initializevariableswithinthebounds*/
for(i=0;
i<
NVARS;
i++)
fscanf(infile,"
%lf"
&
lbound);
ubound);
for(j=0;
j<
POPSIZE;
j++)
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]);
fclose(infile);
}
/***********************************************************/
/*Randomvaluegenerator:
Generatesavaluewithinbounds*/
doublerandval(doublelow,doublehigh)
doubleval;
val=((double)(rand()%1000)/1000.0)*(high-low)+low;
return(val);
/*************************************************************/
/*Evaluationfunction:
Thistakesauserdefinedfunction.*/
/*Eachtimethisischanged,thecodehastoberecompiled.*/
/*Thecurrentfunctionis:
x[1]^2-x[1]*x[2]+x[3]*/
voidevaluate(void)
intmem;
inti;
doublex[NVARS+1];
for(mem=0;
mem<
mem++)
for(i=0;
x[i+1]=population[mem].gene[i];
population[mem].fitness=(x[1]*x[1])-(x[1]*x[2])+x[3];
/*Keep_the_bestfunction:
Thisfunctionkeepstrackofthe*/
/*bestmemberofthepopulation.Notethatthelastentryin*/
/*thearrayPopulationholdsacopyofthebestindividual*/
voidkeep_the_best()
cur_best=0;
/*storestheindexofthebestindividual*/
if(population[mem].fitness>
population[POPSIZE].fitness)
cur_best=mem;
population[POPSIZE].fitness=population[mem].fitness;
/*oncethebestmemberinthepopulationisfound,copythegenes*/
population[POPSIZE].gene[i]=population[cur_best].gene[i];
/****************************************************************/
/*Elitistfunction:
Thebestmemberofthepreviousgeneration*/
/*isstoredasthelastinthearray.Ifthebestmemberof*/
/*thecurrentgenerationisworsethenthebestmemberofthe*/
/*previousgeneration,thelatteronewouldreplacetheworst*/
/*memberofthecurrentpopulation*/
voidelitist()
doublebest,worst;
/*bestandworstfitnessvalues*/
intbest_mem,worst_mem;
/*indexesofthebestandworstmember*/
best=population[0].fitness;
worst=population[0].fitness;
POPSIZE-1;
++i)
if(population[i].fitness>
population[i+1].fitness)
{
if(population[i].fitness>
=best)
best=population[i].fitness;
best_mem=i;
if(population[i+1].fitness<
=worst)
worst=population[i+1].fitness;
worst_mem=i+1;
else
if(population[i].fitness<
worst=population[i].fitness;
worst_mem=i;
if(population[i+1].fitness>
best=population[i+1].fitness;
best_mem=i+1;
/*ifbestindividualfromthenewpopulationisbetterthan*/
/*thebestindividualfromthepreviouspopulation,then*/
/*copythebestfromthenewpopulation;
elsereplacethe*/
/*worstindividualfromthecurrentpopulationwiththe*/
/*bestonefromthepreviousgeneration*/
if(best>
=population[POPSIZE].fitness)
population[POPSIZE].gene[i]=population[best_mem].gene[i];
population[POPSIZE].fitness=population[best_mem].fitness;
else
population[worst_mem].gene[i]=population[POPSIZE].gene[i];
population[worst_mem].fitness=population[POPSIZE].fitness;
}
/**************************************************************/
/*Selectionfunction:
Standardproportionalselectionfor*/
/*maximizationproblemsincorporatingelitistmodel-makes*/
/*surethatthebestmembersurvives*/
voidselect(void)
intmem,i,j,k;
doublesum=0;
doublep;
/*findtotalfitnessofthepopulation*/
sum+=population[mem].fitness;
/*calculaterelativefitness*/
population[mem].rfitness=population[mem].fitness/sum;
population[0].cfitness=population[0].rfitness;
/*calculatecumulativefitness*/
for(mem=1;
population[mem].cfitness=population[mem-1].cfitness+
population[mem].rfitness;
/*finallyselectsurvivorsusingcumulativefitness.*/
p=rand()%1000/1000.0;
if(p<
population[0].cfitness)
newpopulation[i]=population[0];
j++)
if(p>
=population[j].cfitness&
&
p<
population[j+1].cfitness)
newpopulation[i]=population[j+1];
/*onceanewpopulationiscreated,copyitback*/
population[i]=newpopulation[i];
/*Crossoverselection:
selectstwoparentsthattakepartin*/
/*thecrossover.Implementsasinglepointcrossover*/
voidcrossover(void)
inti,mem,one;
intfirst=0;
/*countofthenumberofmemberschosen*/
doublex;
++mem)
x=rand()%1000/1000.0;
if(x<
PXOVER)
++first;
if(first%2==0)
Xover(one,mem);
one=mem;
/*Crossover:
performscrossoverofthetwoselectedparents.*/
voidXover(intone,inttwo)
intpoint;
/*crossoverpoint*/
/*selectcrossoverpoint*/
if(NVARS>
1)
if(NVARS==2)
point=1;
point=(rand()%(NVARS-1))+1;
point;
swap(&
population[one].gene[i],&
population[two].gene[i]);
/*Swap:
Aswapprocedurethathelpsinswapping2variables*/
voidswap(double*x,double*y)
doubletemp;
temp=*x;
*x=*y;
*y=temp;
/*Mutation:
Randomuniformmutation.Avariableselectedfor*/
/*mutationisreplacedbyarandomvaluebetweenlowerand*/
/*upperboundsofthisvariable*/
voidmutate(void)
doublelbound,hbound;
for(j=