/**************************************************************************//*Thisisasimplegeneticalgorithmimplementationwherethe*//*evaluationfunctiontakespositivevaluesonlyandthe*//*fitnessofanindividualisthesameasthevalueofthe*//*objectivefunction*//**************************************************************************/#includestdio.h#includestdlib.h#includemath.h/*Changeanyoftheseparameterstomatchyourneeds*/#definePOPSIZE50/*populationsize*/#defineMAXGENS1000/*max.numberofgenerations*/#defineNVARS3/*no.ofproblemvariables*/#definePXOVER0.8/*probabilityofcrossover*/#definePMUTATION0.15/*probabilityofmutation*/#defineTRUE1#defineFALSE0intgeneration;/*currentgenerationno.*/intcur_best;/*bestindividual*/FILE*galog;/*anoutputfile*/structgenotype/*genotype(GT),amemberofthepopulation*/{doublegene[NVARS];/*astringofvariables*/doublefitness;/*GT'sfitness*/doubleupper[NVARS];/*GT'svariablesupperbound*/doublelower[NVARS];/*GT'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;iNVARS;i++){fscanf(infile,%lf,&lbound);fscanf(infile,%lf,&ubound);for(j=0;jPOPSIZE;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;memPOPSIZE;mem++){for(i=0;iNVARS;i++)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(){intmem;inti;cur_best=0;/*storestheindexofthebestindividual*/for(mem=0;memPOPSIZE;mem++){if(population[mem].fitnesspopulation[POPSIZE].fitness){cur_best=mem;population[POPSIZE].fitness=population[mem].fitness;}}/*oncethebestmemberinthepopulationisfound,copythegenes*/for(i=0;iNVARS;i++)population[POPSIZE].gene[i]=population[cur_best].gene[i];}/****************************************************************//*Elitistfunction:Thebestmemberofthepreviousgeneration*//*isstoredasthelastinthearray.Ifthebestmemberof*//*thecurrentgenerationisworsethenthebestmemberofthe*//*previousgeneration,thelatteronewouldreplacetheworst*//*memberofthecurrentpopulation*//****************************************************************/voidelitist(){inti;doublebest,worst;/*bestandworstfitnessvalues*/intbest_mem,worst_mem;/*indexesofthebestandworstmember*/best=population[0].fitness;worst=population[0].fitness;for(i=0;iPOPSIZE-1;++i){if(population[i].fitnesspopulation[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){worst=population[i].fitness;worst_mem=i;}if(population[i+1].fitness=best){best=population[i+1].fitness;best_mem=i+1;}}}/*ifbestindividualfromthenewpopulationisbetterthan*//*thebestindividualfromthepreviouspopulation,then*//*copythebestfromthenewpopulati