Main Page | Namespace List | Class Hierarchy | Compound List | File List | Compound Members | File Members

Paes.cpp

Go to the documentation of this file.
00001 
00009 #include <Paes.h>
00010 
00015 Paes::Paes(MultiobjectiveProblem * problemToSolve,
00016            MutationOperator        mutationOperator)  {
00017   problem_          = problemToSolve   ;
00018   mutationOperator_ = mutationOperator ;
00019 
00020   // default values
00021   depth_                         = 4        ; 
00022   maximumArchiveLength_          = 150       ;
00023   numberOfIterations_            = 100000    ;
00024   mutationProbability_           = 0.8      ; 
00025   seed_                          = 1        ; 
00026   printFrequency_                = 100000    ;
00027   distributionIndexForMutation_  = 20.0     ; // for polynomial mutation
00028   perturbationForMutation_       = 10.0     ; // for uniform mutation
00029     
00030   readConfigurationData() ;
00031    
00032   numberOfFitnessEvaluations_ = 0   ;  
00033 
00034   random_.initrandom(seed_) ;
00035   random_.Rseed = random_.randreal2() ;
00036   random_.randomize() ;
00037 
00038   archiveOfSolutions_ = new Population(0, // 0 elements 
00039                                        maximumArchiveLength_ ,
00040                                        &random_,
00041                                        problem_) ;
00042                                         
00043   adaptiveGrid_ = new AdaptiveGrid(depth_, problem_->numberOfFunctions_) ;
00044   if (!adaptiveGrid_) {
00045     cerr << "Paes::Paes-> Not enough memory for the adaptiveGrid object" 
00046          << endl ;
00047     exit(-1) ;
00048   } // if
00049 }  // Paes::Paes
00050 
00051 
00056 void Paes::start()  {
00057   int  result         ;
00058   int  mutations      ;
00059   bool mutantArchived ;
00060   long iterations     ;
00061 
00062   startTime_ = time(NULL) ;
00063   
00064   currentSolution_ = new Individual(problem_, &random_) ;
00065   problem_->evaluate(currentSolution_) ;
00066   numberOfFitnessEvaluations_ ++ ;
00067   addToArchive(new Individual(currentSolution_)) ;
00068   iterations = 0 ;
00069   while (iterations < numberOfIterations_) {
00070     iterations++ ;
00071     if ((iterations % printFrequency_) == 0) {
00072       cout << "ITERATION " << iterations << endl ;
00073       cout << "Archive: " << archiveOfSolutions_->getPopulationSize() << endl ; 
00074       //cout << "Current: " << *currentSolution_ << endl ;
00075       currentSolution_->printFitness() ;
00076       cout << endl ;
00077     } // if
00078     // STEP 1. Copy the current solution
00079     mutantSolution_ = new Individual(currentSolution_) ;
00080     if (mutantSolution_ == NULL) {
00081       cerr << "Paes::start->Error allocating memory for mutantSolution_" 
00082            << endl ;
00083       exit(-2) ;
00084     } // if
00085     // STEP 2. Apply mutation
00086     switch(mutationOperator_){
00087     case BIT_FLIP:
00088       mutations = mutantSolution_->bitFlipMutation(mutationProbability_) ;
00089       break ;
00090     case RANDOM:
00091       mutations = mutantSolution_->randomMutation(mutationProbability_) ;
00092       break ;
00093     case POLYNOMIAL:  
00094       mutations = mutantSolution_->polynomialMutation(mutationProbability_,
00095                                                 distributionIndexForMutation_) ;
00096       break ;
00097     case UNIFORM:
00098       mutations = mutantSolution_->uniformMutation(mutationProbability_,
00099                                                    perturbationForMutation_) ;
00100       break ;
00101     default:
00102       cerr << "Paes::start-> mutant operator " << mutationOperator_    
00103            << " undefined" << endl ;
00104       exit(-1) ;          
00105     } // switch
00106 
00107     if (mutations > 0) {
00108       problem_->evaluate(mutantSolution_) ;
00109       numberOfFitnessEvaluations_ ++ ;
00110         result = currentSolution_->dominanceTest(mutantSolution_) ;
00111       if (result == -1) {
00112         *currentSolution_ = *mutantSolution_ ;
00113         adaptiveGrid_->updateGridLocations(archiveOfSolutions_ ,
00114                                            mutantSolution_) ;
00115         archiveSolution(mutantSolution_) ;
00116       } // if 
00117       else if (result == 0 ) {
00118         //cout << ">> Mutant and Current are non-dominated" << endl ;
00119         result = compareToArchive(mutantSolution_) ;
00120         if (result != -1) { // Mutant is not dominated by the archive
00121           adaptiveGrid_->updateGridLocations(archiveOfSolutions_ ,
00122                                              mutantSolution_) ;         
00123           mutantArchived = archiveSolution(mutantSolution_) ;
00124           if (mutantArchived && (result == 0)) {
00125             int location1 = adaptiveGrid_->findLocation(currentSolution_) ;
00126             int location2 = adaptiveGrid_->findLocation(mutantSolution_) ;
00127             if (adaptiveGrid_->hypercube_[location2] <= 
00128                 adaptiveGrid_->hypercube_[location1])
00129               *currentSolution_ = *mutantSolution_ ;
00130           } // if
00131         } // if
00132         else
00133           delete mutantSolution_ ;
00134       } // else if 
00135       else { // (result == 1)
00136         delete mutantSolution_ ; 
00137         //cout << ">> Current dominates mutant" << endl ;
00138       } // else
00139     } // if
00140     else
00141       delete mutantSolution_ ;
00142   } // for  
00143   
00144   endTime_ = time(NULL) ;
00145 }  // Paes::start
00146 
00147 
00151 void Paes::addToArchive(Individual *solution) {
00152   archiveOfSolutions_->addIndividual(solution) ;
00153 } // Paes::addToArchive
00154 
00160 int Paes::compareToArchive(Individual *solution) {
00161   int counter ;
00162   int result  ;
00163   
00164   counter = 0 ;
00165   result  = 0 ;
00166   
00167   while ((counter < archiveOfSolutions_->getPopulationSize()) && 
00168          (result != -1)) {
00169       result = solution->dominanceTest(archiveOfSolutions_->getIth(counter)) ;
00170     
00171     counter ++ ;
00172   } // while
00173   
00174   return result ;
00175 } // Paes::compareToArchive
00176 
00181 bool Paes::archiveSolution(Individual *solution) {
00182   int result            ;
00183   bool finish           ;
00184   int  counter          ;
00185   bool storeNewSolution ;
00186   
00187   finish           = false ;
00188   storeNewSolution = true  ;
00189   counter          = 0     ;
00190   // CASE 1. The archive is empty
00191   //         Action: add solution to the archive
00192   if (archiveOfSolutions_->getPopulationSize() == 0) {
00193     addToArchive(solution) ;
00194     finish = true ;
00195   } // if
00196   
00197   while ((counter < archiveOfSolutions_->getPopulationSize()) && !finish) {
00198     if (solution->identicalFitness(archiveOfSolutions_->getIth(counter))) {
00199       // There is a solution with the same fitness vector
00200       finish           = true  ;
00201       storeNewSolution = false ;
00202     } // if
00203     else {
00204       result = solution->dominanceTest(archiveOfSolutions_->getIth(counter)) ;
00205       if (result == -1) {
00206         //cout << " The new solution is dominated " << endl ;
00207         //auxiliarArchive_.push(archiveOfSolutions_.pop()) ;
00208         finish           = true  ; 
00209         storeNewSolution = false ;
00210         // The solution is not added to the archive
00211       } // if
00212       else if (result == 1) {
00213         //cout << " The new solution dominates the current solution in the archive" << endl ;
00214         //delete archiveOfSolutions_.pop() ;
00215         archiveOfSolutions_->deleteIth(counter) ;
00216       } // else if
00217       else  {
00218         //cout << " The two solutions are non-dominated " << endl ;
00219         ; // auxiliarArchive_.push(archiveOfSolutions_.pop()) ;
00220       } // else
00221     } // else
00222     counter ++ ;
00223   } // while
00224 
00225   if (storeNewSolution) {
00226     // If the archive is not full, add the solution
00227     if (archiveOfSolutions_->getPopulationSize() < maximumArchiveLength_) 
00228       archiveOfSolutions_->addIndividual(solution) ;
00229     else {
00230       // The archive is full
00231       int i             ;
00232       int location      ;
00233       // If the solution is not in the most crowded region, add it
00234       location = solution->gridLocation_ ;
00235       if (location == adaptiveGrid_->mostCrowdedHypercube_) {
00236         // archiveOfSolutions_->addIndividual(solution) ;
00237         delete solution ;
00238         storeNewSolution = false ;
00239       } // if
00240       else { // Find and replace an individual of the most crowded region 
00241         bool finish ;
00242         finish = false ;
00243         i      = 0     ;
00244         while (!finish) {
00245           location = (archiveOfSolutions_->getIth(i))->gridLocation_ ;
00246           if (location == adaptiveGrid_->mostCrowdedHypercube_) {    
00247             archiveOfSolutions_->deleteIth(i) ;      
00248             archiveOfSolutions_->addIndividual(solution) ;
00249             finish = true ;
00250           } // if
00251           i ++ ;
00252           if (i == archiveOfSolutions_->getPopulationSize())
00253             finish = true ;
00254         } // while
00255       } // else
00256     } // else
00257   } // if
00258   else {
00259     //cout << "     - mutant discarded" << endl ;
00260     delete solution ;
00261   } // else
00262   
00263   return storeNewSolution ;
00264 } // Paes::archiveSolution
00265 
00275 void Paes::printToFiles(char * genotypeFileName,
00276                         char * fitnessFileName) {
00277   archiveOfSolutions_->printGenotype(genotypeFileName) ;
00278   archiveOfSolutions_->printFitness(fitnessFileName) ;
00279 } // Paes::printToFiles
00280 
00284 void Paes::readConfigurationData() {
00285   ifstream configurationFile ;
00286   
00287   configurationFile.open("paes.cfg", ios::in) ;
00288   if (configurationFile.fail()) {
00289     cerr << "Paes::readConfigurationFile-> the file 'paes.cfg' does not exist" ;
00290     cerr << endl ;
00291     exit(-1) ;
00292   } // if
00293   else
00294     cout << "Processing configuration file (paes.cfg) ..." << endl ;
00295 
00296   string key ;
00297   string tmp ;
00298   string value ;
00299   
00300   configurationFile >> key ;
00301 
00302   while (!configurationFile.eof()) {
00303     configurationFile >> tmp ; // " the symbol = "
00304 
00305     if (key.compare("DEPTH") == 0) {
00306       configurationFile >> depth_ ;
00307       cout << key << "\t\t\t" << depth_ << endl ;
00308     }
00309     else if (key.compare("MAXIMUM_ARCHIVE_LENGTH") == 0) {
00310       configurationFile >> maximumArchiveLength_ ;
00311       cout << key << "\t" << maximumArchiveLength_ << endl ;
00312     } 
00313     else if (key.compare("NUMBER_OF_ITERATIONS") == 0) {
00314       configurationFile >> numberOfIterations_ ;
00315       cout << key << "\t" << numberOfIterations_ << endl ;
00316     }    
00317     else if (key.compare("SEED") == 0) {
00318       configurationFile >> seed_ ;
00319       cout << key << "\t\t\t" << seed_ << endl ;
00320     } 
00321     else if (key.compare("MUTATION_PROBABILITY") == 0) {
00322       configurationFile >> mutationProbability_ ;
00323       cout << key << "\t" << mutationProbability_ << endl ;
00324     } 
00325     else if (key.compare("PRINT_FREQUENCY") == 0) {
00326       configurationFile >> printFrequency_ ;
00327       cout << key << "\t" << printFrequency_ << endl ;
00328     } 
00329     configurationFile >> key ;
00330   } // while
00331 
00332   cout << endl ;
00333   
00334   configurationFile.close() ;
00335 } // Paes::readConfigurationData
00336 
00340 void Paes::printStatistics() {
00341   cout << "   RESULTS" << endl ;
00342   cout << "-------------" << endl ;
00343   cout << "Time:        " << endTime_ - startTime_ << " seconds" << endl ;
00344   cout << "Evaluations: " << numberOfFitnessEvaluations_ << endl ;
00345 } // Paes::printStatistics
00346 

Generated on Wed Feb 11 10:38:01 2004 for Paes by doxygen 1.3.3