遗传算法文档格式.docx
- 文档编号:20370834
- 上传时间:2023-01-22
- 格式:DOCX
- 页数:13
- 大小:17.67KB
遗传算法文档格式.docx
《遗传算法文档格式.docx》由会员分享,可在线阅读,更多相关《遗传算法文档格式.docx(13页珍藏版)》请在冰豆网上搜索。
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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 遗传 算法
![提示](https://static.bdocx.com/images/bang_tan.gif)