Ssmo.cpp
Go to the documentation of this file.00001 00009 #include <Ssmo.h> 00010 00015 Ssmo::Ssmo(MultiobjectiveProblem * problemToSolve, 00016 MutationOperator mutationOperator) : 00017 MoEA(problemToSolve, mutationOperator, NO_CROSSOVER) { 00018 problem_ = problemToSolve ; 00019 00020 distributionIndexForMutation_ = 20.0 ; // Between 0.5 and 100 00021 00022 populationSize_ = 100 ; 00023 maximumPopulationSize_ = 200 ; 00024 qualityReferenceSetSize_ = 10 ; 00025 diversityReferenceSetSize_ = 10 ; 00026 maximumNumberOfIterations_ = 15 ; 00027 maximumNumberOfFitnessEvaluations_ = 20000 ; 00028 00029 numberOfSubranges_ = 4 ; 00030 improvementIterations_ = 10 ; 00031 00032 mutationProbability_ = problem_->calculateMutationProbability() ; 00033 00034 random_.initrandom(time(NULL)) ; 00035 //random_.initrandom(2) ; 00036 random_.Rseed = random_.randreal2() ; 00037 random_.randomize() ; 00038 00039 readConfigurationData() ; 00040 00041 population_ = new Population("Population", 00042 0, 00043 maximumPopulationSize_, 00044 &random_, 00045 problem_); 00046 dummyPopulation_ = new Population("DummyPopulation_", 00047 0, 00048 maximumPopulationSize_, 00049 &random_, 00050 problem_); 00051 00052 qualityReferenceSet_ = new Population("QualityReferenceSet", 00053 0, 00054 qualityReferenceSetSize_, 00055 &random_, 00056 problem_) ; 00057 diversityReferenceSet_ = new Population("DiversityReferenceSet", 00058 0, 00059 diversityReferenceSetSize_, 00060 &random_, 00061 problem_) ; 00062 00063 improvedCombinedSolutions_ = new Population("ImprovedCombinedSolutions", 00064 0, 00065 10000, 00066 &random_, 00067 problem_) ; 00068 00069 combinedSolutions_ = new Population("CombinedSolutions", 00070 0, 00071 10000, 00072 &random_, 00073 problem_) ; 00074 00075 if (!population_ || !qualityReferenceSet_ || !diversityReferenceSet_ || 00076 !improvedCombinedSolutions_ || !combinedSolutions_) { 00077 cerr << "Ssmo::Ssmo->Error creating populations" << endl ; 00078 exit(-1) ; 00079 } // if 00080 } // Ssmo::Ssmo 00081 00085 Ssmo::~Ssmo() { 00086 } // Paes::~Ssmo 00087 00091 void Ssmo::start() { 00092 Individual * newIndividual ; 00093 Individual * improvedIndividual ; 00094 00095 cout << "Ssmo start method" << endl ; 00096 00097 startTime_ = time(NULL) ; 00098 00099 // STEP 1 00100 for (int i = 0; i < populationSize_; i++) { 00101 newIndividual = diversificationGeneration() ; 00102 problem_->evaluateConstraints(newIndividual) ; 00103 problem_->evaluate(newIndividual) ; 00104 numberOfFitnessEvaluations_ ++ ; 00105 00106 improvedIndividual = improvement(newIndividual) ; 00107 addImprovedIndividual(population_, improvedIndividual) ; 00108 } // for 00109 00110 // STEP 2 00111 int newIndividuals ; 00112 numberOfIterations_ = 0 ; 00113 00114 while (numberOfFitnessEvaluations_ < maximumNumberOfFitnessEvaluations_) { 00115 //)for (int iterations = 0; 00116 // iterations < maximumNumberOfIterations_; 00117 // iterations++) { 00118 numberOfIterations_ ++ ; 00119 int subIterations = 0 ; 00120 00121 referenceSetUpdate(true) ; 00122 removeIndividuals(population_, populationSize_) ; 00123 00124 newIndividuals = subsetGeneration() ; 00125 00126 while (newIndividuals > 0) { 00127 int size = combinedSolutions_->getPopulationSize() ; 00128 00129 for (int i = 0; i < size; i++) { 00130 if ((numberOfFitnessEvaluations_ % printFrequency_) == 0) { 00131 cout << "Evaluations: " << numberOfFitnessEvaluations_ 00132 << "\t iteration: " << numberOfIterations_ << ","<< subIterations 00133 << "\t Individuals: " << newIndividuals << endl ; 00134 printToFiles("VAR", "FUN") ; 00135 } // if 00136 00137 newIndividual = (Individual *)combinedSolutions_->extractIth(0) ; 00138 00139 problem_->evaluate(newIndividual) ; 00140 problem_->evaluateConstraints(newIndividual) ; 00141 numberOfFitnessEvaluations_ ++ ; 00142 00143 improvedIndividual = improvement(newIndividual) ; 00144 improvedCombinedSolutions_->addIndividual(improvedIndividual) ; 00145 } // for 00146 referenceSetUpdate(false) ; 00147 newIndividuals = subsetGeneration() ; 00148 00149 subIterations++ ; 00150 if (numberOfFitnessEvaluations_ > maximumNumberOfFitnessEvaluations_) 00151 newIndividuals = 0 ; 00152 } // while 00153 00154 while (qualityReferenceSet_->getPopulationSize() > 0) { 00155 Individual * individual ; 00156 individual = (Individual*)qualityReferenceSet_->extractIth(0) ; 00157 individual->hasBeenSelected_ = false ; 00158 addImprovedIndividual(population_, individual) ; 00159 } // while 00160 int v = removeDominatedIndividuals(population_) ; 00161 diversityReferenceSet_->removeAllIndividuals() ; 00162 00163 while (population_->getPopulationSize() < (populationSize_ - 1)) { 00164 newIndividual = diversificationGeneration() ; 00165 00166 problem_->evaluate(newIndividual) ; 00167 problem_->evaluateConstraints(newIndividual) ; 00168 numberOfFitnessEvaluations_ ++ ; 00169 00170 improvedIndividual = improvement(newIndividual) ; 00171 addImprovedIndividual(population_, improvedIndividual) ; 00172 } // while 00173 } // while 00174 removeDominatedIndividuals(population_) ; 00175 endTime_ = time(NULL) ; 00176 00177 cout << "Fitness evaluations: " << numberOfFitnessEvaluations_ << endl ; 00178 } // Ssmo::start 00179 00187 void Ssmo::distanceToPopulation(Population * population, 00188 Individual * individual) { 00189 // Intially, the distance of an individual is MAX_REAL. Otherwise, it is 00190 // assumed that we are measuring the distance to more than one population 00191 double minimumDistance = individual->distance_ ; 00192 00193 for (int i = 0; i < population->getPopulationSize(); i++) { 00194 double tmp = 0.0 ; 00195 00196 for (int j = 0; j < problem_->numberOfVariables_; j++) 00197 tmp += pow(individual->chromosome_->gene_[j]->getRealAllele() - 00198 population->getIth(i)->chromosome_->gene_[j]->getRealAllele(), 00199 2) ; 00200 /* 00201 for (int j = 0; j < problem_->numberOfFunctions_; j++) 00202 tmp += pow(individual->fitness_[j] - 00203 population->getIth(i)->fitness_[j], 00204 2) ; 00205 */ 00206 00207 if (minimumDistance > tmp) 00208 minimumDistance = tmp ; 00209 } // for 00210 00211 individual->distance_ = minimumDistance ; 00212 } // Ssmo::distanceToPopulation 00213 00223 void Ssmo::addImprovedIndividual(Population * population, 00224 Individual * individual) { 00225 if (!population_->thereIsAnEqualFitnessIndividual(individual, NULL)) { 00226 population->addIndividual(individual) ; 00227 00228 if (population->getPopulationSize() == population->maximumPopulationSize_) { 00229 removeIndividuals(population_, populationSize_) ; 00230 } // if 00231 } // if 00232 else { 00233 delete individual ; 00234 } // else 00235 } // Ssmo::addImprovedIndividual 00236 00237 00244 void Ssmo::printToFiles(char * genotypeFileName, 00245 char * fitnessFileName) { 00246 population_->sortByFitness(0) ; 00247 cout << "----------------- " << population_->getPopulationSize() << endl ; 00248 printVariablesToFile(genotypeFileName, population_) ; 00249 printFunctionValuesToFile(fitnessFileName, population_) ; 00250 } // Ssmo::printToFiles 00251 00252 00256 void Ssmo::printStatistics() { 00257 cout << " RESULTS" << endl ; 00258 cout << "-------------" << endl ; 00259 cout << "Time : " << (long)(endTime_ - startTime_) << " seconds" << endl ; 00260 cout << "Iterations : " << numberOfIterations_ << endl ; 00261 cout << "Evaluations: " << numberOfFitnessEvaluations_ << endl ; 00262 cout << "Mutations: : " << numberOfMutations_ << endl ; 00263 // 00264 } // Ssmo::printStatistics 00265 00269 void Ssmo::readConfigurationData() { 00270 ifstream configurationFile ; 00271 00272 configurationFile.open("Ssmo.cfg", ios::in) ; 00273 if (configurationFile.fail()) { 00274 cerr << "Ssmo::readConfigurationFile-> the file 'Ssmo.cfg' does " 00275 << "not exist" ; 00276 cerr << endl ; 00277 exit(-1) ; 00278 } // if 00279 else 00280 cout << "Processing configuration file (Ssmo.cfg) ..." << endl ; 00281 00282 string key ; 00283 string tmp ; 00284 string value ; 00285 00286 configurationFile >> key ; 00287 00288 while (!configurationFile.eof()) { 00289 configurationFile >> tmp ; // " the symbol = " 00290 00291 if (key.compare("MAXIMUM_NUMBER_OF_ITERATIONS") == 0) { 00292 configurationFile >> maximumNumberOfIterations_ ; 00293 cout << key << "\t" << maximumNumberOfIterations_ << endl ; 00294 } 00295 else if (key.compare("MAXIMUM_NUMBER_OF_EVALUATIONS") == 0) { 00296 configurationFile >> maximumNumberOfFitnessEvaluations_ ; 00297 cout << key << "\t" << maximumNumberOfFitnessEvaluations_ << endl ; 00298 } 00299 else if (key.compare("POPULATION_SIZE") == 0) { 00300 configurationFile >> populationSize_ ; 00301 cout << key << "\t" << populationSize_ << endl ; 00302 } 00303 else if (key.compare("MAXIMUM_POPULATION_SIZE") == 0) { 00304 configurationFile >> maximumPopulationSize_ ; 00305 cout << key << "\t" << maximumPopulationSize_ << endl ; 00306 } 00307 else if (key.compare("QUALITY_REFERENCE_SET_SIZE") == 0) { 00308 configurationFile >> qualityReferenceSetSize_ ; 00309 cout << key << "\t" << qualityReferenceSetSize_ << endl ; 00310 } 00311 else if (key.compare("DIVERSITY_REFERENCE_SET_SIZE") == 0) { 00312 configurationFile >> diversityReferenceSetSize_ ; 00313 cout << key << "\t" << diversityReferenceSetSize_ << endl ; 00314 } 00315 else if (key.compare("DISTRIBUTION_INDEX_FOR_MUTATION") == 0) { 00316 configurationFile >> distributionIndexForMutation_ ; 00317 cout << key << "\t" << distributionIndexForMutation_ << endl ; 00318 } 00319 else if (key.compare("PRINT_FREQUENCY") == 0) { 00320 configurationFile >> printFrequency_ ; 00321 cout << key << "\t" << printFrequency_ << endl ; 00322 } 00323 else if (key.compare("NUMBER_OF_SUBRANGES") == 0) { 00324 configurationFile >> numberOfSubranges_ ; 00325 cout << key << "\t" << numberOfSubranges_ << endl ; 00326 } 00327 else if (key.compare("IMPROVEMENT_ITERATIONS") == 0) { 00328 configurationFile >> improvementIterations_ ; 00329 cout << key << "\t" << improvementIterations_ << endl ; 00330 } 00331 configurationFile >> key ; 00332 } // while 00333 00334 configurationFile.close() ; 00335 } // Ssmo::readConfigurationData 00336