• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

Population.cpp

Go to the documentation of this file.
00001 
00009 #include <Population.h>
00010 
00021 Population::Population(string name,
00022                        int initialPopulationSize, 
00023                        int maximumPopulationSize,
00024                        Random * random          ,   
00025                        MultiobjectiveProblem * problem) {   
00026   name_                  = name                  ;
00027   problem_               = problem               ;
00028   populationSize_        = initialPopulationSize ;
00029   maximumPopulationSize_ = maximumPopulationSize ;
00030   random_                = random                ;
00031   
00032   population_   = new Individual * [maximumPopulationSize_] ;
00033 
00034   if (!population_) {
00035     cerr << "Population::Population " << name_ 
00036          << "-> error when asking for memory " 
00037          << endl ;
00038     exit(-1) ;
00039   } // if
00040   
00041   int i ;
00042   for (i = 0; i < populationSize_; i++) 
00043     population_[i] = new Individual(problem_, random) ;
00044 } // Population::Population
00045 
00051 Population::~Population(void) {
00052   for (int i = 0; i < populationSize_ ; i++)
00053     delete population_[i] ;
00054   delete [] population_   ;
00055 } // Population::~Population
00056 
00061 int Population::getPopulationSize() const {
00062   return populationSize_;
00063 } // getPopulationSize
00064   
00065  
00070 int Population::getMaximumPopulationSize() const {
00071   return maximumPopulationSize_ ;
00072 } // getMaximumPopulationSize
00073   
00079 Individual * Population::getIth(int index) const {
00080   if ((index < populationSize_) && (index >= 0))
00081     return population_[index];
00082   else {
00083     cerr << "Population(" << name_ << ")::getIth - Index out of range "
00084          << "when getting a copy of " << "an individual" ;
00085     cerr << endl ;
00086     exit(-1) ;
00087   } // else
00088 } // Population::getIth
00089 
00090 
00095 Individual * Population::getRandom() const {
00096   int index ;
00097 
00098   index = random_->rnd(0, populationSize_-1) ;
00099   if ((index < populationSize_) && (index >= 0))
00100     return population_[index];
00101   else {
00102     cerr << "Population(" << name_ << ")::getRandom - Index out of range "
00103          << "when getting a copy of " << "an individual" ;
00104     cerr << endl ;
00105     exit(-1) ;
00106   } // else
00107 } // Population::getIth
00108 
00115 Individual * Population::extractIth(int index) {
00116   Individual * result ;
00117   
00118   result = population_[index] ;
00119   if ((index < populationSize_) && (index >= 0)) {
00120     if (population_[index]->numberOfPopulations_ > 1)
00121       population_[index]->numberOfPopulations_ -- ;
00122     int i ;
00123     for (i = index; i < (populationSize_ - 1); i++)
00124       population_[i] = population_[i + 1] ;
00125     populationSize_ -- ;
00126   } // if
00127   else {
00128     cerr << "Population(" << name_ << ")::extractIth - Index " 
00129          << index << " out of range " ;
00130     cerr << endl ;
00131     exit(-1) ;
00132   } // else
00133   
00134   return result ;
00135 } // Population::extractIth
00136 
00142 void Population::setIth(int index, Individual * individual) {
00143   if ((index < populationSize_) && (index >= 0))
00144     population_[index] = individual ;
00145   else {
00146     cerr << "Population(" << name_ << ")::setIth - Index out of range "
00147          << "when inserting an " << "individual" ;
00148     cerr << endl ;
00149     exit(-1) ;
00150   } // else  
00151 } // setIth
00152 
00158 void Population::setFitness(int index, double * fitness) {
00159   population_[index]->setFitness(fitness);
00160 } // setFitness
00161 
00162 
00167 void Population::addIndividual(Individual * individual) {
00168   if (populationSize_ < maximumPopulationSize_) {
00169     population_[populationSize_] = individual ;
00170     populationSize_ += 1 ;
00171   } // if
00172   else {
00173     cerr << "Population(" << name_ << ")::addIndividual->The population "
00174          << "size has reached its "<< "maximum size (" 
00175          << maximumPopulationSize_ << ")" << endl ;
00176     exit(-1) ;
00177   } // else
00178 } // Population::addIndividual
00179 
00180   
00186 void Population::deleteIth(int index) {
00187 //cout << "Delete element " << index 
00188 //     << " of " << populationSize_ 
00189 //     << " populations: " << population_[index]->numberOfPopulations_ 
00190 //     << endl ;
00191   if ((index < populationSize_) && (index >= 0)) {
00192     if (population_[index]->numberOfPopulations_ > 1) 
00193       population_[index]->numberOfPopulations_ -- ;
00194     else {    
00195       delete population_[index] ;
00196     } // else
00197     int i ;
00198     for (i = index; i < (populationSize_ - 1); i++)
00199       population_[i] = population_[i + 1] ;
00200     populationSize_ -- ;
00201   } // if
00202   else {
00203     cerr << "Population(" << name_ << ")::deleteIth - Index " 
00204          << index << " out of range " ;
00205     cerr << endl ;
00206     exit(-1) ;
00207   } // else
00208 } // Population::deleteIth
00209 
00213 void Population::removeAllIndividuals() {
00214   //int i ;
00215   //int j = populationSize_ ;
00216   //cout << "Elements to delete: " << j << endl ;
00217   //for (i = 0; i < j; i++)
00218   //  deleteIth(0) ;
00219   while (populationSize_ > 0) {
00220     //cout << *(getIth(populationSize_-1)) << endl ;
00221     deleteIth(populationSize_ - 1) ;
00222   } // while
00223 } // Population::removeAllIndividuals
00224 
00228 void Population::reset() {
00229   populationSize_ = 0 ;
00230 } // Population::reset
00231 
00232 
00236 void Population::swap(int index1, int index2) {
00237   Individual * tmp ;
00238   tmp                 = population_[index1] ;
00239   population_[index1] = population_[index2] ;
00240   population_[index2] = tmp                 ;
00241 } // Population::reset
00242 
00243 
00248 void Population::printFitness(char * fileName) {
00249   ofstream outputFile ;
00250   int      i          ;
00251   int      j          ;
00252 
00253   outputFile.open(fileName) ;
00254   outputFile.precision(35) ;
00255   
00256   for (j = 0 ; j < getPopulationSize(); j++) {
00257     for (i = 0; i < problem_->numberOfFunctions_ ; i++)
00258       outputFile << getIth(j)->getFitness()[i] 
00259                  << "\t" ;
00260     //outputFile << getIth(j)->distance_ ;                 
00261     outputFile << endl ;
00262   } // for
00263 
00264   outputFile.close() ;      
00265 } // Population::printFitness
00266 
00271 void Population::printGenotype(char * fileName) {
00272   ofstream outputFile ;
00273   int      i          ;
00274   int      j          ;
00275 
00276   outputFile.open(fileName, ios::out) ;
00277   outputFile.precision(35) ;
00278   
00279   for (j = 0 ; j < getPopulationSize(); j++) {
00280     for (i = 0; i < problem_->numberOfVariables_ ; i++) {
00281       getIth(j)->chromosome_->gene_[i]->writeGenotype(outputFile) ;
00282       outputFile << "\t" ;
00283     } // for
00284     outputFile << endl ;
00285   } // for 
00286   
00287   outputFile.close() ;      
00288 } // Population::printGenotype
00289 
00296 Individual * Population::strengthAndCrowdingTournamentSelection() {
00297   int random ;
00298   
00299   Individual * individual1 ;
00300   Individual * individual2 ;
00301   
00302   random = random_->rnd(0,populationSize_- 1) ; 
00303   individual1 = this->getIth(random) ;
00304   random = random_->rnd(0,populationSize_- 1) ; 
00305   individual2 = this->getIth(random) ;
00306 
00307   if (individual1->strengthRawFitness_ < individual2->strengthRawFitness_)
00308     return individual1 ;
00309   else if (individual1->strengthRawFitness_ > individual2->strengthRawFitness_)
00310     return individual2 ;
00311   else {
00312     if (individual1->distance_ > individual2->distance_)
00313       return individual1 ;
00314     else
00315       return individual2 ;
00316   } // else
00317 } // Population::strengthAndCrowdingTournamentSelection()
00318 
00319 
00325 void Population::sortByFitness(int functionIdentifier) {
00326   Individual * swap ;
00327 
00328   if ((functionIdentifier >= problem_->numberOfFunctions_) ||
00329       (functionIdentifier < 0)) {
00330     cerr << "Population::sortByFitness->the function identifier " 
00331          << functionIdentifier << " is invalid. Must be between 1 and "
00332          << problem_->numberOfFunctions_ << endl ;
00333     exit(-1) ;
00334   } // if
00335    
00336   // bubble sort loop
00337   for (int i = (populationSize_-1); i > 0 ;i--)
00338     for (int j = 0; j < i; j++) {
00339       if (population_[j]->fitness_[functionIdentifier] >
00340           population_[j + 1]->fitness_[functionIdentifier]){
00341         swap             = population_[j]   ;
00342         population_[j]   = population_[j+1] ;
00343         population_[j+1] = swap             ;
00344       } // if
00345     } // for
00346 } // Population::sortByObjective
00347 
00351 void Population::sortByStrengthRawFitnessAndCrowding() {
00352   Individual * swap ;
00353   // bubble sort loop
00354   for (int i = (populationSize_-1); i > 0 ;i--)
00355     for (int j = 0; j < i; j++) {
00356       if ((population_[j]->strengthRawFitness_ > 
00357            population_[j+1]->strengthRawFitness_) ||
00358           ((population_[j]->strengthRawFitness_ == 
00359             population_[j+1]->strengthRawFitness_) && 
00360            (population_[j]->distance_ < 
00361             population_[j+1]->distance_))){
00362         swap             = population_[j]   ;
00363         population_[j]   = population_[j+1] ;
00364         population_[j+1] = swap             ;
00365       } // if
00366     } // sort
00367 } // Population::sortByStrengthFitnessAndCrowding
00368 
00369 
00373 void Population::sortByStrengthFitness() {
00374   Individual * swap ;
00375   // bubble sort loop
00376   for (int i = (populationSize_-1); i > 0 ;i--)
00377     for (int j = 0; j < i; j++) {
00378       if (population_[j]->strengthFitness_ > 
00379           population_[j+1]->strengthFitness_){
00380         swap             = population_[j]   ;
00381         population_[j]   = population_[j+1] ;
00382         population_[j+1] = swap             ;
00383       } // if
00384     } // for
00385 } // Population::sortByStrengthFitness
00386 
00390 void Population::sortByRankAndCrowding() {
00391   Individual * swap ;
00392   // bubble sort loop
00393   for (int i = (populationSize_-1); i > 0 ;i--)
00394     for (int j = 0; j < i; j++) {
00395       if ((population_[j]->rank_ > population_[j+1]->rank_) ||
00396           ((population_[j]->rank_ == population_[j+1]->rank_) && 
00397            (population_[j]->distance_ < 
00398             population_[j+1]->distance_))){
00399         swap             = population_[j]   ;
00400         population_[j]   = population_[j+1] ;
00401         population_[j+1] = swap             ;
00402       } // if
00403     } // sort
00404 } // Population::sortByRankAndCrowding
00405 
00409 void Population::crowdingDistanceAssignment() {
00410   int    lastIndividual ;
00411   double distance       ;
00412    
00413   if (this->getPopulationSize() > 0) {  
00414     lastIndividual = this->getPopulationSize() - 1 ;
00415     for (int i = 0; i < this->getPopulationSize(); i++)
00416       this->getIth(i)->distance_ = 0 ;
00417     
00418     for (int i = 0 ; i < problem_->numberOfFunctions_; i++) {     
00419       this->sortByFitness(i) ;    
00420       double difference = this->getIth(lastIndividual)->fitness_[i] -
00421                           this->getIth(0)->fitness_[i] ;
00422       if (difference < 0.0) {
00423         cerr << "Population(" << name_ << ")::crowdingDistanceAssignment "
00424              << "- Distance < 0" ;
00425         cerr << endl ;
00426         exit(-1) ;
00427       } // if
00428 
00429       for (int j = 0; j < lastIndividual ; j++) {
00430         distance = this->getIth(j)->distance_ ;
00431         if ((j == 0) || (j == (lastIndividual-1))) 
00432           distance = MAX_REAL ; 
00433           //distance = 100 * this->getIth(lastIndividual)->fitness_[i] ;
00434         else
00435           distance += fabs(this->getIth(j+1)->fitness_[i] -
00436                            this->getIth(j-1)->fitness_[i]) / difference ;
00437         this->getIth(j)->distance_ = distance ;
00438       } // for
00439     } // for
00440   } // if
00441 } // Population::crowdingDistanceAssignment
00442 
00448 void Population::maxiMinDistanceAssignment() {
00449   double distance             ;
00450   int   lastIndividual ;
00451   
00452   Population * tempPopulation = new Population("tmp", 
00453                                                0, 
00454                                                this->populationSize_, 
00455                                                this->random_, 
00456                                                this->problem_) ;
00457      
00458   if (this->getPopulationSize() > 0) {  
00459     lastIndividual = this->getPopulationSize() - 1 ;
00460     for (int i = 0; i < this->getPopulationSize(); i++)
00461       this->getIth(i)->distance_ = 0 ;
00462     
00463     // STEP 1. Get the extreme solutions
00464     for (int i = 0 ; i < problem_->numberOfFunctions_; i++) {     
00465       this->sortByFitness(i) ;    
00466       this->getIth(0)->distance_ = MAX_REAL ;
00467       tempPopulation->addIndividual(this->extractIth(0)) ;
00468     } // for 
00469     
00470     double maxDistance           ;
00471     int    individualMaxDistance ;
00472     while (this->getPopulationSize() > 0) {
00473       // STEP 2. Obtain the distances to tempPopulation
00474       maxDistance           = 0.0 ;
00475       individualMaxDistance = 0 ; 
00476       for (int i = 0; i < getPopulationSize(); i++) {
00477         this->getIth(i)->distance_ = MAX_REAL ;
00478         tempPopulation->minimumDistanceToPopulation(this->getIth(i)) ;
00479         if (this->getIth(i)->distance_ > maxDistance) {
00480           maxDistance = this->getIth(i)->distance_ ;
00481           individualMaxDistance = i ;
00482         } // if
00483       } // for
00484       // STEP 3. The individual with maximum distance is selected
00485       tempPopulation->addIndividual(this->extractIth(individualMaxDistance)) ;
00486     } // while
00487     // STEP 4. Restoring original population
00488     while (tempPopulation->populationSize_ > 0)
00489       this->addIndividual(tempPopulation->extractIth(0)) ;
00490   } // if
00491   delete tempPopulation ;
00492 } // Population::crowdingDistanceAssignment
00493 
00494 
00498 void Population::minimumDistanceToPopulation(Individual * individual) {
00499   // Intially, the distance of an individual is MAX_REAL. Otherwise, it is 
00500   // assumed that we are measuring the distance to more than one population 
00501   double minimumDistance = individual->distance_ ;
00502 
00503   for (int i = 0; i < this->getPopulationSize(); i++) {
00504     double tmp = 0.0 ;
00505 
00506     for (int j = 0; j < problem_->numberOfVariables_; j++)
00507       tmp += pow(individual->chromosome_->gene_[j]->getRealAllele() - 
00508                  this->getIth(i)->chromosome_->gene_[j]->getRealAllele(),
00509                  2) ;      
00510  
00511 /* 
00512     for (int j = 0; j < individual->chromosome_->numberOfGenes_ ; j++)
00513       tmp += pow(individual->fitness_[j] - 
00514                  population->getIth(i)->fitness_[j],
00515                  2) ;      
00516 */
00517     if (minimumDistance > tmp)
00518       minimumDistance = tmp ;  
00519   } // for
00520   
00521   individual->distance_ = minimumDistance ;
00522 } // Population::distanceToPopulation
00523 
00528 void Population::strengthAssignment() {
00529   if (this->getPopulationSize() > 0) {
00530     // STEP 1. Initialize strength and raw fitness values
00531     for (int i = 0; i < this->getPopulationSize(); i++) {
00532       this->getIth(i)->strength_   = 0 ;
00533     } // for
00534   
00535     // STEP 2. Obtain the strength values of the individuals in the archive and
00536     //         in the population
00537     for (int i = 0; i < (this->getPopulationSize() - 1); i++) {
00538       int result ;
00539       for (int j = i + 1; j < (this->getPopulationSize()) ; j++) {
00540         //result = this->getIth(i)->numberOfViolatedConstraintsTest(this->getIth(j)) ;
00541         result = this->getIth(i)->overallConstraintViolationTest(this->getIth(j)) ;
00542         if (result == 0)
00543           result = this->getIth(i)->dominanceTest(this->getIth(j)) ;
00544         if (result == 1) {
00545           //cout << i << " dominates " << j << endl ;
00546           this->getIth(i)->strength_ ++ ;          
00547         }                               
00548         else if (result == -1) {
00549           //cout << j << " dominates " << i << endl ;
00550           this->getIth(j)->strength_ ++ ;
00551         }
00552       } // for
00553     } // for
00554   } // if
00555 } // Population::strengthAssignment
00556 
00562 void Population::strengthRawFitnessAssignment() {
00563   if (this->getPopulationSize() > 0) {
00564     // STEP 1. Initialize strength and raw fitness values
00565     for (int i = 0; i < this->getPopulationSize(); i++) {
00566       getIth(i)->strengthRawFitness_   = 0 ;
00567     } // for
00568   
00569     // STEP 2. Obtain the strength values of the individuals in the archive and
00570     //         in the population
00571     for (int i = 0; i < (getPopulationSize() - 1); i++) {
00572       int result ;
00573       for (int j = i + 1; j < getPopulationSize() ; j++) {
00574         //result = this->getIth(i)->numberOfViolatedConstraintsTest(this->getIth(j)) ;
00575         result = this->getIth(i)->overallConstraintViolationTest(this->getIth(j)) ;
00576         if (result == 0)
00577           result = getIth(i)->dominanceTest(getIth(j)) ;
00578         if (result == -1)
00579           getIth(i)->strengthRawFitness_ += getIth(j)->strength_ ;   
00580         else if (result == 1)                                        
00581           getIth(j)->strengthRawFitness_ += getIth(i)->strength_ ; 
00582       } // for  
00583     } // for 
00584   } // if
00585 } // Population::strengthRawFitnessAssignment
00586 
00593 Individual *
00594 Population::crowdedComparison(Individual * individual1, 
00595                                            Individual * individual2) {
00596   if (individual1->rank_ < individual2->rank_)
00597     return individual1 ;
00598   else if (individual1->rank_ > individual2->rank_)
00599     return individual2 ;
00600   else {
00601     if (individual1->distance_ > individual2->distance_)
00602       return individual1 ;
00603     else
00604       return individual2 ;
00605   } // else
00606 } // Population::crowdedComparison
00607 
00612 int 
00613 Population::removeDominatedIndividuals() {
00614   Ranking    * ranking         ;
00615   Population * dummyPopulation ;
00616   int counter ;
00617 
00618   ranking = new Ranking(this) ;
00619   dummyPopulation = new Population("dummy", 
00620                                    0, 
00621                                    maximumPopulationSize_, 
00622                                    random_, 
00623                                    problem_) ;
00624   if ((ranking == NULL) || (dummyPopulation == NULL)) {
00625     cerr << "Population::removeDominatedIndividuals-> error creating objects" 
00626          << endl ;
00627     exit(-1) ;
00628   } // if
00629 
00630   ranking->rankPopulation(populationSize_) ;
00631   ranking->copyFrontToPopulation(0, 
00632                 ranking->nonDominatedFront_[0]->getPopulationSize(),
00633                 dummyPopulation) ;
00634 
00635   counter = this->getPopulationSize() - dummyPopulation->getPopulationSize() ;
00636   this->removeAllIndividuals() ;
00637   while (dummyPopulation->getPopulationSize() > 0) {
00638     int last = dummyPopulation->getPopulationSize() - 1 ;
00639     this->addIndividual(dummyPopulation->extractIth(last)) ;
00640   } // while
00641 
00642   delete ranking ;
00643   delete dummyPopulation ;
00644 
00645   return counter ;
00646 } // Population::removeDominatedIndividuals
00647 
00648 
00655 Individual * 
00656 Population::rankingAndCrowdingTournamentSelection() {
00657   int random ;
00658   
00659   Individual * individual1 ;
00660   Individual * individual2 ;
00661   
00662   random = random_->rnd(0,populationSize_- 1) ; 
00663   individual1 = this->getIth(random) ;
00664   random = random_->rnd(0,populationSize_- 1) ; 
00665   individual2 = this->getIth(random) ;
00666 
00667   return crowdedComparison(individual1, individual2) ;
00668 } // Population::rankingTournamentSelection()
00669 
00675 Individual * Population::tournamentSelection() {
00676   int random ;
00677   int result ; 
00678   Individual * individual1 ;
00679   Individual * individual2 ;
00680   
00681   random = random_->rnd(0,populationSize_- 1) ; 
00682   individual1 = this->getIth(random) ;
00683   random = random_->rnd(0,populationSize_- 1) ; 
00684   individual2 = this->getIth(random) ;
00685 
00686   result = individual1->numberOfViolatedConstraintsTest(individual2) ;
00687   if (result == 0)
00688     result = individual1->dominanceTest(individual2) ;
00689   if (result == -1) // Individual1 is dominated
00690     return individual2 ;
00691   else if (result == 1) // Individual 2 is dominated
00692     return individual1 ; 
00693   else { // Both are non-dominated
00694     if (random_->rndreal(0.0, 1.0) < 0.5)
00695       return individual1 ;
00696     else
00697       return individual2 ;
00698   } // else
00699 } // Population::tournamentSelection()
00700 
00712 bool Population::thereIsAnEqualFitnessIndividual(Individual * individual,
00713                                                  int        * position) {
00714   bool found ;
00715   int  index ;
00716   found = false ;
00717   index = 0 ;
00718 
00719   while ((!found) && (index < populationSize_)) {
00720     if (individual->identicalFitness(population_[index]))
00721       found = true ;    
00722     index ++ ;
00723   } // while
00724 
00725   if (position != NULL) 
00726     *position = index - 1 ;
00727     
00728   if (found)
00729     return true ;
00730   else
00731     return false ;
00732 } // Population::thereIsAnEqualFitnessIndividual
00733 
00743 bool Population::individualIsInPopulation(Individual * individual, 
00744                                           int *        position) {
00745   bool found ;
00746   int  index ;
00747 
00748   found = false ;
00749   index = 0 ;
00750   while ((!found) && (index < populationSize_)) {
00751     if (*individual == *(population_[index]))
00752       found = true ;
00753     index ++ ;
00754   } // while
00755 
00756   if (position != NULL) 
00757     *position = index - 1 ;
00758 
00759   if (found)
00760     return true ;
00761   else
00762     return false ;
00763 } // Population::individualIsInPopulation
00764 
00771 int Population::testIfIndividualDominates(Individual * individual) {
00772   int counter ;
00773   int result  ;
00774   
00775   counter = 0 ;
00776   result  = 0 ;
00777   
00778   while ((counter < this->getPopulationSize()) && 
00779          (result != -1)) {
00780       result = individual->dominanceTest(this->getIth(counter)) ;
00781     
00782     counter ++ ;
00783   } // while
00784   
00785   return result ;
00786 } // Population::testIfIndividualDominatesPopulation
00787 
00797  void Population::clustering(int clusters, Population ** centroids) {
00798    *centroids = new Population("Centroids",0, populationSize_, random_, problem_) ;
00799    if (*centroids == NULL) {
00800      cerr << "Population(" << name_ << ")::clustering-> error creating "
00801           << "the centroids population" 
00802           << endl ;
00803      exit(-1) ;
00804    } // if  
00805    if (clusters > 0) {
00806      if (populationSize_ <= clusters) {
00807        for (int i = 0 ; i < populationSize_; i++)
00808          (*centroids)->addIndividual(new Individual(getIth(i))) ; 
00809      } // if
00810      else {
00811        // Step 1. At the beginning there are as many centroids as individuals
00812        int numberOfCentroids = populationSize_ ;
00813        // Step 2. Initialize the centroids population 
00814        for (int i = 0; i < populationSize_; i++) 
00815          (*centroids)->addIndividual(new Individual(getIth(i))) ;
00816        // Step 3. Iterate until the required number of centroids  
00817        while (numberOfCentroids > clusters) {
00818 //cout << "Number of centroids: " << numberOfCentroids << endl ;       
00819          double minimumDistance = MAX_REAL ;
00820          int x, y = -1 ; // Identifiers of the individuals at minimum distance
00821          // Step 3.1. Obtain the distance between pairs of individuals
00822          for (int i = 0; i < (numberOfCentroids - 1); i++) {
00823            for (int j = i + 1; j < (numberOfCentroids - 1) ; j++) {
00824              double distance = 0.0 ;
00825              for (int k = 0; k < problem_->numberOfVariables_; k++)
00826                distance += pow(((RealGene *)((*centroids)->getIth(i)->chromosome_->gene_[k]))->allele_ - 
00827                                ((RealGene *)((*centroids)->getIth(j)->chromosome_->gene_[k]))->allele_,
00828                            2) ; 
00829              if (distance < minimumDistance) {
00830                minimumDistance = distance ;
00831                x = i ;
00832                y = j ;
00833              } // if  
00834            } // for  
00835          } // for  
00836          // Step 3.2. Merge the individuals with minimal distance. 
00837          //           The individual x is the resulting centroid
00838          for (int i = 0; i < problem_->numberOfVariables_; i++) {
00839            double tmp1 ;
00840            double tmp2 ;
00841            tmp1 = ((RealGene *)((*centroids)->getIth(x)->chromosome_->gene_[i]))->allele_ ;
00842            tmp2 = ((RealGene *)((*centroids)->getIth(y)->chromosome_->gene_[i]))->allele_ ;
00843            tmp1 = (tmp1 + tmp2) / 2.0 ;
00844            ((RealGene *)((*centroids)->getIth(x)->chromosome_->gene_[i]))->allele_ = tmp1 ;
00845          } // for
00846          // Step 3.3 The individual y is removed
00847          (*centroids)->deleteIth(y) ;
00848 //cout << "Centroids population: " << (*centroids)->getPopulationSize() << endl ;       
00849          // Step 3.4. Decrement the centroids counter
00850          numberOfCentroids -- ;
00851        } // while  
00852      } // else     
00853    } // if
00854  } // Population::clustering 

Our Software

orangebox Mallba

orangebox ssGA

orangebox JGDS

orangebox xxGA

orangebox JCell

orangebox MHTB

orangebox DEME

orangebox JMetal

orangebox More...

orangebox Go Back