?? population.cpp
字號:
/***************************************************************/* Single & Multi-Objective Real-Coded Genetic Algorithms Code *//* Author: Kumara Sastry *//* Illinois Genetic Algorithms Laboratory (IlliGAL) *//* Deparment of General Engineering *//* University of Illinois at Urbana-Champaign *//* 104 S. Mathews Ave, Urbana, IL 61801 *//***************************************************************/#include "population.hpp"Population::Population() { // Things to be done: // Initialization of member variables: // 1. Guys, newGuys -> Depending upon the type of GA // 2. mpool -> new int[popSize] // 3. bestInd -> new Individual // 4. selection = new kindOfSelection(this); // 5. crossover = new kindOfCrossover(); // If (TypeOfGA == SGA) // 1. Initialize all statistics to new double; int ii; guys = new Individual*[globalSetup->populationSize]; newGuys = new Individual*[globalSetup->populationSize]; if (globalSetup->gaType==SGA) { bestInd = new Individual; freezeMask = new int[globalSetup->noOfDecisionVariables]; for(ii = 0; ii < globalSetup->noOfDecisionVariables; ii++) freezeMask[ii] = OFF; for (ii=0; ii<globalSetup->populationSize; ii++) { guys[ii] = new Individual; newGuys[ii] = new Individual(guys[ii]); } } else { bestInd = new NsgaIndividual; for (ii=0; ii<globalSetup->populationSize; ii++) { guys[ii] = new NsgaIndividual; newGuys[ii] = new NsgaIndividual(guys[ii]); } } mpool = new int[globalSetup->populationSize]; switch (globalSetup->selectionType) { case TournamentWOR: selection = new TournamentSelection(*((int *)globalSetup->selectionParameters), this); break; case SUS: selection = new StochasticUniversalSelection(this); break; case Truncation: selection = new TruncationSelection(*((int *)globalSetup->selectionParameters), this); break; case TournamentWR: selection = new TournamentSelectionWithReplacement(*((int *)globalSetup->selectionParameters), this); break; case RouletteWheel: selection = new RouletteWheelSelection(this); break; default: exit(0); } switch (globalSetup->xOverType) { case OnePoint: crossover = new OneTwoPointCrossover(1); break; case TwoPoint: crossover = new OneTwoPointCrossover(2); break; case Uniform: if (globalSetup->xOverParameters==NULL) crossover = new UniformCrossover(); else crossover = new UniformCrossover(((double *)globalSetup->xOverParameters)[0]); break; case SBX: if (globalSetup->xOverParameters==NULL) crossover = new SimulatedBinaryCrossover(); else crossover = new SimulatedBinaryCrossover(((double *)globalSetup->xOverParameters)[0]); break; default: exit(0); } if(globalSetup->loadPopulation) { loadPopulationFromFile(); } // Initialization of the other stuff if (globalSetup->gaType==SGA) { bestobj = new double; worstobj = new double; avgobj = new double; maxfit = new double; minfit = new double; avgfit = new double; varfit = new double; bestFitChange = new double; avgFitChange = new double; fitVarChange = new double; bestObjChange = new double; avgObjChange = new double; switch (globalSetup->localSearchMethod) { case NoLocalSearch: break; case SimplexSearch: localSearch = new Simplex; break; // case Complex: localSearch = new Complex(); // break; // case MOM: localSearch = new methodOfMultipliers(); // break; default: exit(0); } computeObjStatistics(); mapObjectiveToFitness(); computeFitnessStatistics(); if((globalSetup->selectionType == SUS)|| (globalSetup->selectionType == RouletteWheel)|| (globalSetup->nichingType == Sharing)) { scaleFitness(); if(globalSetup->nichingType == Sharing) shareFitness(); computeFitnessStatistics(); } replacePopulation(); computeObjStatistics(); mapObjectiveToFitness(); computeFitnessStatistics(); }}std::ostream &operator<< (std::ostream &out, const Population &pop ){ int ii, jj; if (globalSetup->gaType==SGA) { if(globalSetup->nichingType == NoNiching) { out << "Best Objective - "<<*(pop.bestobj)<< std::endl <<"Worst Objective - "<<*(pop.worstobj)<< std::endl<<"Average Objective - "<< *(pop.avgobj)<< std::endl; out << "Maximum Fitness - "<<*(pop.maxfit)<<std::endl<<"Average Fitness - "<<*(pop.avgfit)<<std::endl; out <<"The best individual"<<std::endl<<*(pop.bestInd)<<std::endl; } else { out << "% "; for(jj = 0; jj < globalSetup->noOfDecisionVariables; jj++) out << "var #" << jj << "\t"; out << std::endl; for(ii = 0; ii < globalSetup->populationSize; ii++) { for(jj = 0; jj < globalSetup->noOfDecisionVariables; jj++) out << (*pop[ii])[jj] << "\t"; out << pop[ii]->getFitness() << std::endl; } } } else { out << "% "; for(jj = 0; jj < globalSetup->finalNoOfObjectives; jj++) out << "obj #" << jj << "\t"; out << std::endl; for(ii = 0; ii < globalSetup->populationSize; ii++) { for(jj = 0; jj < globalSetup->finalNoOfObjectives; jj++) out << ((NsgaIndividual *)(pop[ii]))->getObjective(jj) << "\t"; out << std::endl; } } return out;}Population::~Population(){ int ii; for(ii = 0; ii < globalSetup->populationSize; ii++){ delete guys[ii]; delete newGuys[ii]; } delete [] guys; delete [] newGuys; delete bestInd; delete []mpool; delete selection; delete crossover; if (globalSetup->gaType == SGA) { delete []freezeMask; delete bestobj; delete worstobj; delete avgobj; delete maxfit; delete minfit; delete avgfit; delete varfit; delete bestFitChange; delete avgFitChange; delete fitVarChange; delete bestObjChange; delete avgObjChange; }}/*============================================================** Function Name: Population::doRTS()** Function Task: This method performs Restricted Tournament Selection** For each new individual, random select W individuals and find the individual which closest to the new individual based on phenotypic distance. If the closest individual is better than the new individual, replace the new individual with the closest individual.** Reference: Harik, G. (1994). "Finding Multiple Solutions In Problems of Bounded Difficulty", Illigal Report No. 94002. http://www-illigal.ge.uiuc.edu/techreps.php3. (ftp://ftp-illigal.ge.uiuc.edu/pub/papers/IlliGALs/94002.ps.Z).** Output: None** Functions Called:**========================================================== */void Population::doRTS(void){ int windowSize = ((int *)globalSetup->nichingParameters)[0]; int ii, jj, kk,closest, rndGuy; double minDist, distance, maxDist; Individual **tempGuys; tempGuys = new Individual*[globalSetup->populationSize]; for(ii = 0; ii < globalSetup->populationSize; ii++) tempGuys[ii] = new Individual(guys[ii]); maxDist = 0; for(ii = 0; ii < globalSetup->noOfDecisionVariables; ii++) maxDist += ((globalSetup->variableRanges[ii][1] - globalSetup->variableRanges[ii][0])*(globalSetup->variableRanges[ii][1] - globalSetup->variableRanges[ii][0])); for(ii = 0; ii < globalSetup->populationSize; ii++) { minDist = maxDist; for(jj = 0; jj < windowSize; jj++) { distance = 0.0; rndGuy = myRandom.boundedIntegerRandom(0, globalSetup->populationSize); for(kk = 0; kk < globalSetup->noOfDecisionVariables; kk++) distance += ((*tempGuys[rndGuy])[kk]-(*newGuys[ii])[kk])* ((*tempGuys[rndGuy])[kk]-(*newGuys[ii])[kk]); if(distance < minDist) { closest = rndGuy; minDist = distance; } } if(isBetter(newGuys[ii],tempGuys[closest])) *tempGuys[closest] = *newGuys[ii]; } for(ii = 0; ii < globalSetup->populationSize; ii++) { *(newGuys[ii]) = *(tempGuys[ii]); delete tempGuys[ii]; } delete []tempGuys;}/*============================================================** Function Name: Population::shareFitness()** Function Task: This method performs shared niching which consists** of the following steps. Calculate the number of individuals sharing** each niche using a phenotypic distance metric. An individual is** considered to be in the same niche as another if phenotypic distance** between is less than sigmaShare which is specified in th eglobal setup** as a parameter. shareAlpha is another parameter used for calculating** the sharing value which is used to update the niche count for each of** the individuals. The shared fitness of each individaul is calculate by** dividing is fitness by its niche count.** Output: None** Functions Called: Population::setFitness(double)** Reference: Goldberg, D.E. & Richardson, J. (1987). "Genetic Algorithms with Sharing for Multimodal Function Optimization", Genetic Algorithms and their Applications: Proceedings of the Second International Conference on Genetic Algorithms, 41-49. (TCGA No. 01170).**========================================================== */void Population::shareFitness(){ double sigmaShare = ((double *)globalSetup->nichingParameters)[0]; double shareAlpha = ((double *)globalSetup->nichingParameters)[1]; double *sharingValue; int ii, jj, kk, ll, arraySize, transIndex; double tempDist, nicheCount; double phenoValue1, phenoValue2, sharedFitness; arraySize = ((globalSetup->populationSize)*(globalSetup->populationSize-1))/2; sharingValue = new double[arraySize]; ll = 0; for(ii = 0; ii < globalSetup->populationSize-1; ii++) { nicheCount = 1.0; for(jj = ii + 1; jj < globalSetup->populationSize; jj++) { tempDist = 0.0; for(kk = 0; kk < globalSetup->noOfDecisionVariables; kk++) { phenoValue1 = (*newGuys[ii])[kk]; phenoValue2 = (*newGuys[jj])[kk]; tempDist += ((phenoValue1-phenoValue2)*(phenoValue1-phenoValue2)); } tempDist = sqrt(tempDist); if(tempDist < sigmaShare) sharingValue[ll] = 1.0 - pow((tempDist/(sigmaShare)), shareAlpha); else sharingValue[ll] = 0.0; nicheCount += sharingValue[ll++]; } for(jj = 0; jj < ii; jj++) { transIndex = (globalSetup->populationSize)*jj + (ii-jj-1) - (jj*(jj+1)/2); nicheCount += sharingValue[transIndex]; } sharedFitness = (newGuys[ii]->getFitness())/nicheCount; newGuys[ii]->setFitness(sharedFitness); } delete []sharingValue;}/*============================================================** Function Name: Population::freeze(int)** Function Task: Given a locus this method sets the freezemask** for the corresponding location on the chromosome.** It then calls Individual::freeze(int,) for each individual** to set value of the frozen gene in all individuals to the** value of the corresponding gene in the best individual.** This feature is diabled when Niching is on.** Output:None** Functions called: Individual::freeze(int,)**========================================================*/void Population::freeze(int locus){ int ii; if((globalSetup->gaType == SGA)&& (globalSetup->nichingType == NoNiching)&& (freezeMask[locus] == OFF)) { freezeMask[locus] = ON; for(ii = 0; ii < globalSetup->populationSize; ii++) { guys[ii]->freeze(locus, (*bestInd)[locus]); newGuys[ii]->freeze(locus, (*bestInd)[locus]); } }}/*============================================================** Function Name: Population::flood(int)** Function Task: Given a locus this method sets the freezemask** of the corresponding location on the chromosome.** It then calls Individual::flood(int,) for each individual** to set value of the flooded genes to some random value.** This feature is diabled when Niching is on.** Output:None** Functions called: Individual::flood(int)**========================================================*/void Population::flood(int locus){ int ii; freezeMask[locus]=OFF; if((globalSetup->gaType == SGA)&& (globalSetup->nichingType == NoNiching)) { for(ii = 0; ii < globalSetup->populationSize; ii++) { guys[ii]->flood(locus); newGuys[ii]->flood(locus); } }}/*============================================================** Function Name: Population::computeObjStatistics(void)** Function Task: this method generates statistics according to** objective function values. It finds out the average objective** value for the population and the best and worst objective** values. This depends on whether the type of optimization in the** problem is minimization or maximization.** Output:None** Functions called: Individual::getObjective()**========================================================*/void Population::computeObjStatistics(void){ int ii, bestGuy, worstGuy; double oneOverPopulationSize; double oldBestObj, oldAvgObj; oneOverPopulationSize = 1.0/(1.0*(globalSetup->populationSize)); oldBestObj = *bestobj; oldAvgObj = *avgobj; bestGuy = worstGuy = 0; *avgobj = oneOverPopulationSize*(newGuys[0]->getObjective()); for(ii = 1; ii < globalSetup->populationSize; ii++) { *avgobj += oneOverPopulationSize*(newGuys[ii]->getObjective()); if(isBetter(newGuys[ii], newGuys[bestGuy])) bestGuy = ii; if(isBetter(newGuys[worstGuy], newGuys[ii])) worstGuy = ii; } *bestobj = newGuys[bestGuy]->getObjective(); *worstobj = newGuys[worstGuy]->getObjective(); *bestObjChange = fabs(oldBestObj - *bestobj); *avgObjChange = fabs(oldAvgObj - *avgobj);}/*============================================================** Function Name: Population::computeFitnessStatistics(void)** Function Task: this method generates statistics according to** fitness function values. It finds out the average fitness** value for the population and the maximum and minimum fitness** values as well as the fitness variance.** Output:None** Functions called: Individual::getFitness()**========================================================*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -