遗传算法文档格式.docx

上传人:b****5 文档编号:20370834 上传时间:2023-01-22 格式:DOCX 页数:13 大小:17.67KB
下载 相关 举报
遗传算法文档格式.docx_第1页
第1页 / 共13页
遗传算法文档格式.docx_第2页
第2页 / 共13页
遗传算法文档格式.docx_第3页
第3页 / 共13页
遗传算法文档格式.docx_第4页
第4页 / 共13页
遗传算法文档格式.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

遗传算法文档格式.docx

《遗传算法文档格式.docx》由会员分享,可在线阅读,更多相关《遗传算法文档格式.docx(13页珍藏版)》请在冰豆网上搜索。

遗传算法文档格式.docx

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<

Pc)

++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;

Pm)

/*findtheboundsonthevariabletobemutated*/

lbound=population[i].lower[j];

hbound=population[i].upper[j];

population[i].gene[j]=randval(lbound,hbound);

/*Re

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 少儿英语

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1