00001
00009 #include <Paes.h>
00010
00015 Paes::Paes(MultiobjectiveProblem * problemToSolve,
00016 MutationOperator mutationOperator) {
00017 problem_ = problemToSolve ;
00018 mutationOperator_ = mutationOperator ;
00019
00020
00021 depth_ = 4 ;
00022 maximumArchiveLength_ = 150 ;
00023 numberOfIterations_ = 100000 ;
00024 mutationProbability_ = 0.8 ;
00025 seed_ = 1 ;
00026 printFrequency_ = 100000 ;
00027 distributionIndexForMutation_ = 20.0 ;
00028 perturbationForMutation_ = 10.0 ;
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,
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 }
00049 }
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
00075 currentSolution_->printFitness() ;
00076 cout << endl ;
00077 }
00078
00079 mutantSolution_ = new Individual(currentSolution_) ;
00080 if (mutantSolution_ == NULL) {
00081 cerr << "Paes::start->Error allocating memory for mutantSolution_"
00082 << endl ;
00083 exit(-2) ;
00084 }
00085
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 }
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 }
00117 else if (result == 0 ) {
00118
00119 result = compareToArchive(mutantSolution_) ;
00120 if (result != -1) {
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 }
00131 }
00132 else
00133 delete mutantSolution_ ;
00134 }
00135 else {
00136 delete mutantSolution_ ;
00137
00138 }
00139 }
00140 else
00141 delete mutantSolution_ ;
00142 }
00143
00144 endTime_ = time(NULL) ;
00145 }
00146
00147
00151 void Paes::addToArchive(Individual *solution) {
00152 archiveOfSolutions_->addIndividual(solution) ;
00153 }
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 }
00173
00174 return result ;
00175 }
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
00191
00192 if (archiveOfSolutions_->getPopulationSize() == 0) {
00193 addToArchive(solution) ;
00194 finish = true ;
00195 }
00196
00197 while ((counter < archiveOfSolutions_->getPopulationSize()) && !finish) {
00198 if (solution->identicalFitness(archiveOfSolutions_->getIth(counter))) {
00199
00200 finish = true ;
00201 storeNewSolution = false ;
00202 }
00203 else {
00204 result = solution->dominanceTest(archiveOfSolutions_->getIth(counter)) ;
00205 if (result == -1) {
00206
00207
00208 finish = true ;
00209 storeNewSolution = false ;
00210
00211 }
00212 else if (result == 1) {
00213
00214
00215 archiveOfSolutions_->deleteIth(counter) ;
00216 }
00217 else {
00218
00219 ;
00220 }
00221 }
00222 counter ++ ;
00223 }
00224
00225 if (storeNewSolution) {
00226
00227 if (archiveOfSolutions_->getPopulationSize() < maximumArchiveLength_)
00228 archiveOfSolutions_->addIndividual(solution) ;
00229 else {
00230
00231 int i ;
00232 int location ;
00233
00234 location = solution->gridLocation_ ;
00235 if (location == adaptiveGrid_->mostCrowdedHypercube_) {
00236
00237 delete solution ;
00238 storeNewSolution = false ;
00239 }
00240 else {
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 }
00251 i ++ ;
00252 if (i == archiveOfSolutions_->getPopulationSize())
00253 finish = true ;
00254 }
00255 }
00256 }
00257 }
00258 else {
00259
00260 delete solution ;
00261 }
00262
00263 return storeNewSolution ;
00264 }
00265
00275 void Paes::printToFiles(char * genotypeFileName,
00276 char * fitnessFileName) {
00277 archiveOfSolutions_->printGenotype(genotypeFileName) ;
00278 archiveOfSolutions_->printFitness(fitnessFileName) ;
00279 }
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 }
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 ;
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 }
00331
00332 cout << endl ;
00333
00334 configurationFile.close() ;
00335 }
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 }
00346