Individual.cpp
Go to the documentation of this file.00001 00009 #include <Individual.h> 00010 00018 Individual::Individual(MultiobjectiveProblem * problem, Random * random) { 00019 int i ; 00020 00021 problem_ = problem ; 00022 random_ = random ; 00023 chromosome_ = new Chromosome(problem_, random_) ; 00024 fitness_ = new double[problem_->numberOfFunctions_] ; 00025 00026 numberOfPopulations_ = 1 ; 00027 00028 if ((fitness_ == NULL) || (chromosome_ == NULL)) { 00029 cerr << "Individual::Individual-> error when asking for memory" << endl ; 00030 exit(-1) ; 00031 } // if 00032 00033 for (i = 0; i <problem_->numberOfFunctions_; i++) 00034 fitness_[i] = 0.0 ; 00035 00036 rank_ = 0 ; 00037 //crowdingDistance_ = -1 ; 00038 00039 strength_ = -1 ; 00040 strengthRawFitness_ = MAX_REAL ; 00041 strengthFitness_ = -1 ; 00042 density_ = 0 ; 00043 00044 distance_ = MAX_REAL ; 00045 hasBeenSelected_ = false ; 00046 00047 numberOfViolatedConstraints_ = 0 ; 00048 overallConstraintViolation_ = 0 ; 00049 #ifdef __MPI__ 00050 this->calculateSize() ; 00051 #endif 00052 } // Individual::Individual 00053 00060 Individual::Individual(Individual & individual) { 00061 int i ; 00062 00063 problem_ = individual.problem_ ; 00064 random_ = individual.random_ ; 00065 chromosome_ = new Chromosome(individual.chromosome_) ; 00066 fitness_ = new double[individual.problem_->numberOfFunctions_] ; 00067 00068 rank_ = 0 ; 00069 //crowdingDistance_ = -1 ; 00070 00071 strength_ = -1 ; 00072 strengthRawFitness_= MAX_REAL ; 00073 strengthFitness_ = -1 ; 00074 density_ = 0 ; 00075 00076 distance_ = individual.distance_ ; 00077 //hasBeenSelected_ = individual.hasBeenSelected_ ; 00078 location_ = individual.location_ ; 00079 00080 numberOfPopulations_ = 1 ; 00081 00082 numberOfViolatedConstraints_ = 0 ; 00083 overallConstraintViolation_ = 0 ; 00084 if ((fitness_ == NULL) || (chromosome_ == NULL)) { 00085 cerr << "Individual::Individual-> error when asking for memory" << endl ; 00086 exit(-1) ; 00087 } // if 00088 00089 for (i = 0; i <problem_->numberOfFunctions_; i++) 00090 fitness_[i] = individual.fitness_[i] ; 00091 00092 rank_ = individual.rank_ ; 00093 00094 #ifdef __MPI__ 00095 this->calculateSize() ; 00096 #endif 00097 } // Individual::Individual 00098 00105 Individual::Individual(Individual * individual) { 00106 int i ; 00107 00108 problem_ = individual->problem_ ; 00109 random_ = individual->random_ ; 00110 chromosome_ = new Chromosome(individual->chromosome_) ; 00111 00112 fitness_ = new double[individual->problem_->numberOfFunctions_] ; 00113 00114 rank_ = 0 ; 00115 //crowdingDistance_ = -1 ; 00116 00117 strength_ = -1 ; 00118 strengthRawFitness_ = MAX_REAL ; 00119 strengthFitness_ = -1 ; 00120 density_ = 0 ; 00121 00122 distance_ = individual->distance_ ; 00123 //hasBeenSelected_ = individual->hasBeenSelected_ ; 00124 location_ = individual->location_ ; 00125 00126 numberOfPopulations_ = 1 ; 00127 00128 numberOfViolatedConstraints_ = 0 ; 00129 overallConstraintViolation_ = 0 ; 00130 00131 if ((fitness_ == NULL) || (chromosome_ == NULL)) { 00132 cerr << "Individual::Individual-> error when asking for memory" << endl ; 00133 exit(-1) ; 00134 } // if 00135 00136 for (i = 0; i < problem_->numberOfFunctions_; i++) 00137 fitness_[i] = individual->fitness_[i] ; 00138 00139 rank_ = individual->rank_ ; 00140 00141 #ifdef __MPI__ 00142 this->calculateSize() ; 00143 #endif 00144 } // Individual::Individual 00145 00151 Individual::~Individual() { 00152 delete chromosome_ ; 00153 delete [] fitness_ ; 00154 #ifdef __MPI__ 00155 delete [] messageBuffer_ ; 00156 #endif 00157 } // Individual::~Individual 00158 00163 void Individual::setFitness(double * fitness) { 00164 int i ; 00165 for (i = 0; i < problem_->numberOfFunctions_; i++) { 00166 fitness_[i] = fitness[i] ; 00167 //cout << fitness_[i] << " "; 00168 } //for 00169 } // Individual::setFitness 00170 00175 double * Individual::getFitness() const { 00176 return fitness_; 00177 } // getFitness 00178 00187 bool Individual::singlePointCrossover(double crossoverProbability, 00188 Individual * parent, 00189 Individual ** firstChild, 00190 Individual ** secondChild) { 00191 double random ; 00192 bool result ; 00193 00194 Individual * child1 ; 00195 Individual * child2 ; 00196 child1 = new Individual(this) ; 00197 child2 = new Individual(parent) ; 00198 00199 if ((child1 == NULL) || (child2 == NULL)) { 00200 cerr << "Individual::singlePointCrossover->Error creating children" 00201 << endl ; 00202 exit(-1) ; 00203 } // if 00204 00205 result = false ; 00206 00207 switch(problem_->variableType_[0]) { 00208 case BINARY: 00209 case BINARY_REAL: 00210 case BINARY_GRAY_REAL: 00211 case BINARY_INTEGER: 00212 case BINARY_GRAY_INTEGER: 00213 random = random_->rndreal(0.0, 1.0) ; 00214 if (random <= crossoverProbability) { 00215 int cuttingBit ; 00216 bool geneFound ; 00217 int i ; 00218 Gene * swap ; 00219 cuttingBit = random_->rnd(0, chromosome_->bitLength_ - 1) ; 00220 geneFound = false ; 00221 i = 0 ; 00222 while (!geneFound) 00223 if ((chromosome_->gene_[i]->getNumberOfBits()) < cuttingBit) { 00224 cuttingBit -= chromosome_->gene_[i]->getNumberOfBits() ; 00225 i++ ; 00226 } // if 00227 else 00228 geneFound = true ; 00229 child1->chromosome_->gene_[i]->singlePointCrossover(cuttingBit, 00230 child2->chromosome_->gene_[i]) ; 00231 i++ ; 00232 for (; i < chromosome_->numberOfGenes_; i++) { 00233 swap = child1->chromosome_->gene_[i] ; 00234 child1->chromosome_->gene_[i] = child2->chromosome_->gene_[i] ; 00235 child2->chromosome_->gene_[i] = swap ; 00236 } // for 00237 00238 result = true ; 00239 } // if 00240 break ; 00241 case INTEGER: 00242 case REAL: 00243 random = random_->rndreal(0.0, 1.0) ; 00244 if (random <= crossoverProbability) { 00245 int cuttingGene ; 00246 Gene * swap ; 00247 cuttingGene = random_->rnd(0, problem_->numberOfVariables_ - 1) ; 00248 00249 for (int i = cuttingGene; i < problem_->numberOfVariables_; i++) { 00250 swap = child1->chromosome_->gene_[i] ; 00251 child1->chromosome_->gene_[i] = child2->chromosome_->gene_[i] ; 00252 child2->chromosome_->gene_[i] = swap ; 00253 } // for 00254 00255 result = true ; 00256 } // if 00257 break ; 00258 default: 00259 cerr << "Individual::singlePointCrossover-> crossover operator undefined " 00260 << "for individuals of type " << problem_->variableType_[0] 00261 << endl ; 00262 exit(-1) ; 00263 } // switch 00264 00265 00266 *firstChild = child1 ; 00267 *secondChild = child2 ; 00268 00269 return result ; 00270 } // Individual::singlePointCrossover 00271 00272 00282 bool Individual::simulatedBinaryCrossover(double crossoverProbability, 00283 double distributionIndex, 00284 Individual * parent, 00285 Individual ** firstChild, 00286 Individual ** secondChild) { 00287 00288 bool result ; 00289 00290 double random ; 00291 double beta ; 00292 double betaq ; 00293 double alpha ; 00294 double aux ; 00295 double valueY1 ; 00296 double valueY2 ; 00297 double valueX1 ; 00298 double valueX2 ; 00299 double upperBound ; 00300 double lowerBound ; 00301 00302 Individual * child1 ; 00303 Individual * child2 ; 00304 child1 = new Individual(this) ; 00305 child2 = new Individual(parent) ; 00306 00307 if ((child1 == NULL) || (child2 == NULL)) { 00308 cerr << "Individual::simulatedBinaryCrossover->Error creating children" 00309 << endl ; 00310 exit(-1) ; 00311 } // if 00312 00313 result = false ; 00314 00315 switch(problem_->variableType_[0]) { 00316 case REAL: 00317 random = random_->rndreal(0.0, 1.0) ; 00318 if (random <= crossoverProbability) { 00319 for (int i = 0; i < chromosome_->numberOfGenes_; i++) { 00320 random = random_->rndreal(0.0, 1.0) ; 00321 valueX1 = child1->chromosome_->gene_[i]->getRealAllele() ; 00322 valueX2 = child2->chromosome_->gene_[i]->getRealAllele() ; 00323 upperBound = ((RealGene*)child1->chromosome_->gene_[i])->upperBound_ ; 00324 lowerBound = ((RealGene*)child1->chromosome_->gene_[i])->lowerBound_ ; 00325 00326 // A variable is selected with a probability less than 0.5 00327 if (random <= 0.5) { 00328 result = true ; 00329 if (valueX1 < valueX2) { 00330 valueY1 = valueX1 ; 00331 valueY2 = valueX2 ; 00332 } // if 00333 else { 00334 valueY1 = valueX2 ; 00335 valueY2 = valueX1 ; 00336 } // else 00337 00338 if (fabs(valueX1 - valueX2) > 1.0e-14) { 00339 random = random_->rndreal(0.0, 1.0) ; 00340 beta = 1.0 + (2.0*(valueY1-lowerBound)/(valueY2-valueY1)) ; 00341 alpha = 2.0 - pow(beta, -(distributionIndex + 1.0)) ; 00342 if (random <= 1.0/alpha) 00343 betaq = pow((random*alpha), (1.0/(distributionIndex + 1.0))) ; 00344 else 00345 betaq = pow((1.0/(2.0-random*alpha)), 00346 (1.0/(distributionIndex + 1.0))) ; 00347 aux = 0.5*((valueY1 + valueY2) - betaq*(valueY2 - valueY1)) ; 00348 child1->chromosome_->gene_[i]->setRealAllele(aux) ; 00349 00350 beta = 1.0 + (2.0*(upperBound-valueY2)/(valueY2-valueY1)) ; 00351 alpha = 2.0 - pow(beta, -(distributionIndex + 1.0)) ; 00352 if (random <= 1.0/alpha) 00353 betaq = pow((random*alpha), (1.0/(distributionIndex + 1.0))) ; 00354 else 00355 betaq = pow((1.0/(2.0-random*alpha)), 00356 (1.0/(distributionIndex + 1.0))) ; 00357 aux = 0.5*((valueY1 + valueY2) + betaq*(valueY2 - valueY1)) ; 00358 child2->chromosome_->gene_[i]->setRealAllele(aux) ; 00359 00360 valueY1 = child1->chromosome_->gene_[i]->getRealAllele() ; 00361 valueY2 = child2->chromosome_->gene_[i]->getRealAllele() ; 00362 00363 if (valueY1 < lowerBound) 00364 child1->chromosome_->gene_[i]->setRealAllele(lowerBound) ; 00365 if (valueY1 > upperBound) 00366 child1->chromosome_->gene_[i]->setRealAllele(upperBound) ; 00367 00368 if (valueY2 < lowerBound) 00369 child2->chromosome_->gene_[i]->setRealAllele(lowerBound) ; 00370 if (valueY2 > upperBound) 00371 child2->chromosome_->gene_[i]->setRealAllele(upperBound) ; 00372 } // if 00373 else { // nothing to do 00374 } // else 00375 } // if 00376 else { // nothing to do 00377 } // else 00378 } // for 00379 } // if 00380 break ; 00381 case INTEGER: 00382 random = random_->rndreal(0.0, 1.0) ; 00383 if (random <= crossoverProbability) { 00384 for (int i = 0; i < chromosome_->numberOfGenes_; i++) { 00385 random = random_->rndreal(0.0, 1.0) ; 00386 valueX1 = child1->chromosome_->gene_[i]->getRealAllele() ; 00387 valueX2 = child2->chromosome_->gene_[i]->getRealAllele() ; 00388 upperBound = ((RealGene*)child1->chromosome_->gene_[i])->upperBound_ ; 00389 lowerBound = ((RealGene*)child1->chromosome_->gene_[i])->lowerBound_ ; 00390 00391 // A variable is selected with a probability less than 0.5 00392 if (random <= 0.5) { 00393 result = true ; 00394 if (valueX1 < valueX2) { 00395 valueY1 = valueX1 ; 00396 valueY2 = valueX2 ; 00397 } // if 00398 else { 00399 valueY1 = valueX2 ; 00400 valueY2 = valueX1 ; 00401 } // else 00402 00403 if (fabs(valueX1 - valueX2) > 1.0e-14) { 00404 random = random_->rndreal(0.0, 1.0) ; 00405 beta = 1.0 + (2.0*(valueY1-lowerBound)/(valueY2-valueY1)) ; 00406 alpha = 2.0 - pow(beta, -(distributionIndex + 1.0)) ; 00407 if (random <= 1.0/alpha) 00408 betaq = pow((random*alpha), (1.0/(distributionIndex + 1.0))) ; 00409 else 00410 betaq = pow((1.0/(2.0-random*alpha)), 00411 (1.0/(distributionIndex + 1.0))) ; 00412 aux = 0.5*((valueY1 + valueY2) - betaq*(valueY2 - valueY1)) ; 00413 child1->chromosome_->gene_[i]->setRealAllele((int)aux) ; 00414 00415 beta = 1.0 + (2.0*(upperBound-valueY2)/(valueY2-valueY1)) ; 00416 alpha = 2.0 - pow(beta, -(distributionIndex + 1.0)) ; 00417 if (random <= 1.0/alpha) 00418 betaq = pow((random*alpha), (1.0/(distributionIndex + 1.0))) ; 00419 else 00420 betaq = pow((1.0/(2.0-random*alpha)), 00421 (1.0/(distributionIndex + 1.0))) ; 00422 aux = 0.5*((valueY1 + valueY2) + betaq*(valueY2 - valueY1)) ; 00423 child2->chromosome_->gene_[i]->setRealAllele((int)aux) ; 00424 00425 valueY1 = child1->chromosome_->gene_[i]->getRealAllele() ; 00426 valueY2 = child2->chromosome_->gene_[i]->getRealAllele() ; 00427 00428 if (valueY1 < lowerBound) 00429 child1->chromosome_->gene_[i]->setRealAllele(lowerBound) ; 00430 if (valueY1 > upperBound) 00431 child1->chromosome_->gene_[i]->setRealAllele(upperBound) ; 00432 00433 if (valueY2 < lowerBound) 00434 child2->chromosome_->gene_[i]->setRealAllele(lowerBound) ; 00435 if (valueY2 > upperBound) 00436 child2->chromosome_->gene_[i]->setRealAllele(upperBound) ; 00437 } // if 00438 else { // nothing to do 00439 } // else 00440 } // if 00441 else { // nothing to do 00442 } // else 00443 } // for 00444 } // if 00445 break ; 00446 default: 00447 cerr << "Individual::simulatedBinaryCrossover-> crossover operator " 00448 << "undefined for individuals of type " 00449 << problem_->variableType_[0] 00450 << endl ; 00451 exit(-1) ; 00452 } // switch 00453 *firstChild = child1 ; 00454 *secondChild = child2 ; 00455 00456 return result ; 00457 } // Individual::simulatedBinaryCrossover 00458 00468 bool Individual::BLX_Alpha(double crossoverProbability, 00469 double alpha, 00470 Individual * parent, 00471 Individual ** firstChild, 00472 Individual ** secondChild) { 00473 bool result ; 00474 //double betaq ; 00475 double random ; 00476 00477 Individual * child1 ; 00478 Individual * child2 ; 00479 child1 = new Individual(this) ; 00480 child2 = new Individual(parent) ; 00481 double valueY1 ; 00482 double valueY2 ; 00483 double valueX1 ; 00484 double valueX2 ; 00485 double upperValue ; 00486 double lowerValue ; 00487 00488 if ((child1 == NULL) || (child2 == NULL)) { 00489 cerr << "Individual::BLX_alpha->Error creating children" 00490 << endl ; 00491 exit(-1) ; 00492 } // if 00493 00494 result = false ; 00495 random = random_->rndreal(0.0, 1.0) ; 00496 if (random <= crossoverProbability) { 00497 int i ; 00498 00499 for (i = 0; i < chromosome_->numberOfGenes_; i++) { 00500 upperValue = ((RealGene*)child1->chromosome_->gene_[i])->upperBound_ ; 00501 lowerValue = ((RealGene*)child1->chromosome_->gene_[i])->lowerBound_ ; 00502 valueX1 = child1->chromosome_->gene_[i]->getRealAllele() ; 00503 valueX2 = child2->chromosome_->gene_[i]->getRealAllele() ; 00504 00505 double max ; 00506 double min ; 00507 double range ; 00508 00509 if (valueX2 > valueX1) { 00510 max = valueX2 ; 00511 min = valueX1 ; 00512 } // if 00513 else { 00514 max = valueX1 ; 00515 min = valueX2 ; 00516 } // else 00517 00518 range = max - min ; 00519 // Ranges of the new alleles ; 00520 double minRange ; 00521 double maxRange ; 00522 00523 minRange = min - range*alpha; 00524 maxRange = max + range*alpha; 00525 00526 random = random_->rndreal(0.0, 1.0) ; 00527 valueY1 = minRange + random * (maxRange - minRange) ; 00528 00529 random = random_->rndreal(0.0, 1.0) ; 00530 valueY2 = minRange + random * (maxRange - minRange) ; 00531 00532 if (valueY1 < lowerValue) 00533 child1->chromosome_->gene_[i]->setRealAllele(lowerValue) ; 00534 else if (valueY1 > upperValue) 00535 child1->chromosome_->gene_[i]->setRealAllele(upperValue) ; 00536 else 00537 child1->chromosome_->gene_[i]->setRealAllele(valueY1) ; 00538 00539 if (valueY2 < lowerValue) 00540 child2->chromosome_->gene_[i]->setRealAllele(lowerValue) ; 00541 else if (valueY2 > upperValue) 00542 child2->chromosome_->gene_[i]->setRealAllele(upperValue) ; 00543 else 00544 child2->chromosome_->gene_[i]->setRealAllele(valueY2) ; 00545 } // for 00546 00547 result = true ; 00548 } // if 00549 00550 *firstChild = child1 ; 00551 *secondChild = child2 ; 00552 00553 return result ; 00554 } // Individual::BLX_alpha 00555 00556 00566 bool Individual::logicalExplotative(double crossoverProbability, 00567 double lambda, 00568 Individual * parent, 00569 Individual ** firstChild, 00570 Individual ** secondChild) { 00571 bool result ; 00572 //double betaq ; 00573 double random ; 00574 00575 Individual * child1 ; 00576 Individual * child2 ; 00577 child1 = new Individual(this) ; 00578 child2 = new Individual(parent) ; 00579 double valueY1 ; 00580 double valueY2 ; 00581 double valueX1 ; 00582 double valueX2 ; 00583 double upperValue ; 00584 double lowerValue ; 00585 00586 if ((child1 == NULL) || (child2 == NULL)) { 00587 cerr << "Individual::logicalExplotative->Error creating children" 00588 << endl ; 00589 exit(-1) ; 00590 } // if 00591 00592 result = false ; 00593 random = random_->rndreal(0.0, 1.0) ; 00594 if (random <= crossoverProbability) { 00595 int i ; 00596 00597 for (i = 0; i < chromosome_->numberOfGenes_; i++) { 00598 upperValue = ((RealGene*)child1->chromosome_->gene_[i])->upperBound_ ; 00599 lowerValue = ((RealGene*)child1->chromosome_->gene_[i])->lowerBound_ ; 00600 valueX1 = child1->chromosome_->gene_[i]->getRealAllele() ; 00601 valueX2 = child2->chromosome_->gene_[i]->getRealAllele() ; 00602 valueY1 = ((RealGene*)child1->chromosome_->gene_[i])->logicalMFunction( 00603 valueX1, 00604 valueX2, 00605 lambda) ; 00606 valueY2 = ((RealGene*)child1->chromosome_->gene_[i])->logicalMFunction( 00607 valueX1, 00608 valueX2, 00609 1 - lambda) ; 00610 00611 if (valueY1 < lowerValue) 00612 child1->chromosome_->gene_[i]->setRealAllele(lowerValue) ; 00613 else if (valueY1 > upperValue) 00614 child1->chromosome_->gene_[i]->setRealAllele(upperValue) ; 00615 else 00616 child1->chromosome_->gene_[i]->setRealAllele(valueY1) ; 00617 00618 if (valueY2 < lowerValue) 00619 child2->chromosome_->gene_[i]->setRealAllele(lowerValue) ; 00620 else if (valueY2 > upperValue) 00621 child2->chromosome_->gene_[i]->setRealAllele(upperValue) ; 00622 else 00623 child2->chromosome_->gene_[i]->setRealAllele(valueY2) ; 00624 } // for 00625 result = true ; 00626 } // if 00627 00628 *firstChild = child1 ; 00629 *secondChild = child2 ; 00630 00631 return result ; 00632 } // Individual::logicalExplotative 00633 00642 bool Individual::logicalExplorative(double crossoverProbability, 00643 Individual * parent, 00644 Individual ** firstChild, 00645 Individual ** secondChild) { 00646 bool result ; 00647 //double betaq ; 00648 double random ; 00649 00650 Individual * child1 ; 00651 Individual * child2 ; 00652 child1 = new Individual(this) ; 00653 child2 = new Individual(parent) ; 00654 double valueY1 ; 00655 double valueY2 ; 00656 double valueX1 ; 00657 double valueX2 ; 00658 double upperValue ; 00659 double lowerValue ; 00660 00661 if ((child1 == NULL) || (child2 == NULL)) { 00662 cerr << "Individual::logicalExplorative->Error creating children" 00663 << endl ; 00664 exit(-1) ; 00665 } // if 00666 00667 result = false ; 00668 random = random_->rndreal(0.0, 1.0) ; 00669 if (random <= crossoverProbability) { 00670 int i ; 00671 00672 for (i = 0; i < chromosome_->numberOfGenes_; i++) { 00673 upperValue = ((RealGene*)child1->chromosome_->gene_[i])->upperBound_ ; 00674 lowerValue = ((RealGene*)child1->chromosome_->gene_[i])->lowerBound_ ; 00675 valueX1 = child1->chromosome_->gene_[i]->getRealAllele() ; 00676 valueX2 = child2->chromosome_->gene_[i]->getRealAllele() ; 00677 00678 valueY1 = ((RealGene*)child1->chromosome_->gene_[i])->logicalFFunction( 00679 valueX1, 00680 valueX2) ; 00681 valueY2 = ((RealGene*)child1->chromosome_->gene_[i])->logicalSFunction( 00682 valueX1, 00683 valueX2) ; 00684 00685 if (valueY1 < lowerValue) 00686 child1->chromosome_->gene_[i]->setRealAllele(lowerValue) ; 00687 else if (valueY1 > upperValue) 00688 child1->chromosome_->gene_[i]->setRealAllele(upperValue) ; 00689 else 00690 child1->chromosome_->gene_[i]->setRealAllele(valueY1) ; 00691 00692 if (valueY2 < lowerValue) 00693 child2->chromosome_->gene_[i]->setRealAllele(lowerValue) ; 00694 else if (valueY2 > upperValue) 00695 child2->chromosome_->gene_[i]->setRealAllele(upperValue) ; 00696 else 00697 child2->chromosome_->gene_[i]->setRealAllele(valueY2) ; 00698 } // for 00699 result = true ; 00700 } // if 00701 00702 *firstChild = child1 ; 00703 *secondChild = child2 ; 00704 00705 return result ; 00706 } // Individual::logicalExplorative 00707 00708 00718 bool Individual::hamacherExplotative(double crossoverProbability, 00719 double lambda, 00720 Individual * parent, 00721 Individual ** firstChild, 00722 Individual ** secondChild) { 00723 bool result ; 00724 //double betaq ; 00725 double random ; 00726 00727 Individual * child1 ; 00728 Individual * child2 ; 00729 child1 = new Individual(this) ; 00730 child2 = new Individual(parent) ; 00731 double valueY1 ; 00732 double valueY2 ; 00733 double valueX1 ; 00734 double valueX2 ; 00735 double upperValue ; 00736 double lowerValue ; 00737 00738 if ((child1 == NULL) || (child2 == NULL)) { 00739 cerr << "Individual::hamacherExplotative->Error creating children" 00740 << endl ; 00741 exit(-1) ; 00742 } // if 00743 00744 result = false ; 00745 random = random_->rndreal(0.0, 1.0) ; 00746 if (random <= crossoverProbability) { 00747 int i ; 00748 00749 for (i = 0; i < chromosome_->numberOfGenes_; i++) { 00750 upperValue = ((RealGene*)child1->chromosome_->gene_[i])->upperBound_ ; 00751 lowerValue = ((RealGene*)child1->chromosome_->gene_[i])->lowerBound_ ; 00752 valueX1 = child1->chromosome_->gene_[i]->getRealAllele() ; 00753 valueX2 = child2->chromosome_->gene_[i]->getRealAllele() ; 00754 00755 valueY1 = ((RealGene*)child1->chromosome_->gene_[i])->hamacherMFunction( 00756 valueX1, 00757 valueX2, 00758 lambda) ; 00759 valueY2 = ((RealGene*)child1->chromosome_->gene_[i])->hamacherMFunction( 00760 valueX1, 00761 valueX2, 00762 1 - lambda) ; 00763 00764 if (valueY1 < lowerValue) 00765 child1->chromosome_->gene_[i]->setRealAllele(lowerValue) ; 00766 else if (valueY1 > upperValue) 00767 child1->chromosome_->gene_[i]->setRealAllele(upperValue) ; 00768 else 00769 child1->chromosome_->gene_[i]->setRealAllele(valueY1) ; 00770 00771 if (valueY2 < lowerValue) 00772 child2->chromosome_->gene_[i]->setRealAllele(lowerValue) ; 00773 else if (valueY2 > upperValue) 00774 child2->chromosome_->gene_[i]->setRealAllele(upperValue) ; 00775 else 00776 child2->chromosome_->gene_[i]->setRealAllele(valueY2) ; 00777 } // for 00778 result = true ; 00779 } // if 00780 00781 *firstChild = child1 ; 00782 *secondChild = child2 ; 00783 00784 return result ; 00785 } // Individual::hamacherExplotative 00786 00795 bool Individual::hamacherExplorative(double crossoverProbability, 00796 Individual * parent, 00797 Individual ** firstChild, 00798 Individual ** secondChild) { 00799 bool result ; 00800 //double betaq ; 00801 double random ; 00802 00803 Individual * child1 ; 00804 Individual * child2 ; 00805 child1 = new Individual(this) ; 00806 child2 = new Individual(parent) ; 00807 double valueY1 ; 00808 double valueY2 ; 00809 double valueX1 ; 00810 double valueX2 ; 00811 double upperValue ; 00812 double lowerValue ; 00813 00814 if ((child1 == NULL) || (child2 == NULL)) { 00815 cerr << "Individual::hamacherExplorative->Error creating children" 00816 << endl ; 00817 exit(-1) ; 00818 } // if 00819 00820 result = false ; 00821 random = random_->rndreal(0.0, 1.0) ; 00822 if (random <= crossoverProbability) { 00823 int i ; 00824 00825 for (i = 0; i < chromosome_->numberOfGenes_; i++) { 00826 upperValue = ((RealGene*)child1->chromosome_->gene_[i])->upperBound_ ; 00827 lowerValue = ((RealGene*)child1->chromosome_->gene_[i])->lowerBound_ ; 00828 valueX1 = child1->chromosome_->gene_[i]->getRealAllele() ; 00829 valueX2 = child2->chromosome_->gene_[i]->getRealAllele() ; 00830 00831 valueY1 = ((RealGene*)child1->chromosome_->gene_[i])->hamacherFFunction( 00832 valueX1, 00833 valueX2) ; 00834 valueY2 = ((RealGene*)child1->chromosome_->gene_[i])->hamacherSFunction( 00835 valueX1, 00836 valueX2) ; 00837 if (valueY1 < lowerValue) 00838 child1->chromosome_->gene_[i]->setRealAllele(lowerValue) ; 00839 else if (valueY1 > upperValue) 00840 child1->chromosome_->gene_[i]->setRealAllele(upperValue) ; 00841 else 00842 child1->chromosome_->gene_[i]->setRealAllele(valueY1) ; 00843 00844 if (valueY2 < lowerValue) 00845 child2->chromosome_->gene_[i]->setRealAllele(lowerValue) ; 00846 else if (valueY2 > upperValue) 00847 child2->chromosome_->gene_[i]->setRealAllele(upperValue) ; 00848 else 00849 child2->chromosome_->gene_[i]->setRealAllele(valueY2) ; 00850 } // for 00851 result = true ; 00852 } // if 00853 00854 *firstChild = child1 ; 00855 *secondChild = child2 ; 00856 00857 return result ; 00858 } // Individual::hamacherExplorative 00859 00869 bool Individual::algebraicExplotative(double crossoverProbability, 00870 double lambda, 00871 Individual * parent, 00872 Individual ** firstChild, 00873 Individual ** secondChild) { 00874 bool result ; 00875 //double betaq ; 00876 double random ; 00877 00878 Individual * child1 ; 00879 Individual * child2 ; 00880 child1 = new Individual(this) ; 00881 child2 = new Individual(parent) ; 00882 double valueY1 ; 00883 double valueY2 ; 00884 double valueX1 ; 00885 double valueX2 ; 00886 double upperValue ; 00887 double lowerValue ; 00888 00889 if ((child1 == NULL) || (child2 == NULL)) { 00890 cerr << "Individual::algebraicExplotative->Error creating children" 00891 << endl ; 00892 exit(-1) ; 00893 } // if 00894 00895 result = false ; 00896 random = random_->rndreal(0.0, 1.0) ; 00897 if (random <= crossoverProbability) { 00898 int i ; 00899 00900 for (i = 0; i < chromosome_->numberOfGenes_; i++) { 00901 upperValue = ((RealGene*)child1->chromosome_->gene_[i])->upperBound_ ; 00902 lowerValue = ((RealGene*)child1->chromosome_->gene_[i])->lowerBound_ ; 00903 valueX1 = child1->chromosome_->gene_[i]->getRealAllele() ; 00904 valueX2 = child2->chromosome_->gene_[i]->getRealAllele() ; 00905 00906 valueY1 = ((RealGene*)child1->chromosome_->gene_[i])->algebraicMFunction( 00907 valueX1, 00908 valueX2, 00909 lambda) ; 00910 valueY2 = ((RealGene*)child1->chromosome_->gene_[i])->algebraicMFunction( 00911 valueX1, 00912 valueX2, 00913 1 - lambda) ; 00914 00915 if (valueY1 < lowerValue) 00916 child1->chromosome_->gene_[i]->setRealAllele(lowerValue) ; 00917 else if (valueY1 > upperValue) 00918 child1->chromosome_->gene_[i]->setRealAllele(upperValue) ; 00919 else 00920 child1->chromosome_->gene_[i]->setRealAllele(valueY1) ; 00921 00922 if (valueY2 < lowerValue) 00923 child2->chromosome_->gene_[i]->setRealAllele(lowerValue) ; 00924 else if (valueY2 > upperValue) 00925 child2->chromosome_->gene_[i]->setRealAllele(upperValue) ; 00926 else 00927 child2->chromosome_->gene_[i]->setRealAllele(valueY2) ; 00928 } // for 00929 result = true ; 00930 } // if 00931 00932 *firstChild = child1 ; 00933 *secondChild = child2 ; 00934 00935 return result ; 00936 } // Individual::algebraicExplotative 00937 00946 bool Individual::algebraicExplorative(double crossoverProbability, 00947 Individual * parent, 00948 Individual ** firstChild, 00949 Individual ** secondChild) { 00950 bool result ; 00951 //double betaq ; 00952 double random ; 00953 00954 Individual * child1 ; 00955 Individual * child2 ; 00956 child1 = new Individual(this) ; 00957 child2 = new Individual(parent) ; 00958 double valueY1 ; 00959 double valueY2 ; 00960 double valueX1 ; 00961 double valueX2 ; 00962 double upperValue ; 00963 double lowerValue ; 00964 00965 if ((child1 == NULL) || (child2 == NULL)) { 00966 cerr << "Individual::algebraicExplorative->Error creating children" 00967 << endl ; 00968 exit(-1) ; 00969 } // if 00970 00971 result = false ; 00972 random = random_->rndreal(0.0, 1.0) ; 00973 if (random <= crossoverProbability) { 00974 int i ; 00975 00976 for (i = 0; i < chromosome_->numberOfGenes_; i++) { 00977 upperValue = ((RealGene*)child1->chromosome_->gene_[i])->upperBound_ ; 00978 lowerValue = ((RealGene*)child1->chromosome_->gene_[i])->lowerBound_ ; 00979 valueX1 = child1->chromosome_->gene_[i]->getRealAllele() ; 00980 valueX2 = child2->chromosome_->gene_[i]->getRealAllele() ; 00981 00982 valueY1 = ((RealGene*)child1->chromosome_->gene_[i])->algebraicFFunction( 00983 valueX1, 00984 valueX2) ; 00985 valueY2 = ((RealGene*)child1->chromosome_->gene_[i])->algebraicSFunction( 00986 valueX1, 00987 valueX2) ; 00988 00989 if (valueY1 < lowerValue) 00990 child1->chromosome_->gene_[i]->setRealAllele(lowerValue) ; 00991 else if (valueY1 > upperValue) 00992 child1->chromosome_->gene_[i]->setRealAllele(upperValue) ; 00993 else 00994 child1->chromosome_->gene_[i]->setRealAllele(valueY1) ; 00995 00996 if (valueY2 < lowerValue) 00997 child2->chromosome_->gene_[i]->setRealAllele(lowerValue) ; 00998 else if (valueY2 > upperValue) 00999 child2->chromosome_->gene_[i]->setRealAllele(upperValue) ; 01000 else 01001 child2->chromosome_->gene_[i]->setRealAllele(valueY2) ; 01002 } // for 01003 result = true ; 01004 } // if 01005 01006 *firstChild = child1 ; 01007 *secondChild = child2 ; 01008 01009 return result ; 01010 } // Individual::algebraicExplorative 01011 01021 bool Individual::einsteinExplotative(double crossoverProbability, 01022 double lambda, 01023 Individual * parent, 01024 Individual ** firstChild, 01025 Individual ** secondChild) { 01026 bool result ; 01027 //double betaq ; 01028 double random ; 01029 01030 Individual * child1 ; 01031 Individual * child2 ; 01032 child1 = new Individual(this) ; 01033 child2 = new Individual(parent) ; 01034 double valueY1 ; 01035 double valueY2 ; 01036 double valueX1 ; 01037 double valueX2 ; 01038 double upperValue ; 01039 double lowerValue ; 01040 01041 if ((child1 == NULL) || (child2 == NULL)) { 01042 cerr << "Individual::einsteinExplotative->Error creating children" 01043 << endl ; 01044 exit(-1) ; 01045 } // if 01046 01047 result = false ; 01048 random = random_->rndreal(0.0, 1.0) ; 01049 if (random <= crossoverProbability) { 01050 int i ; 01051 01052 for (i = 0; i < chromosome_->numberOfGenes_; i++) { 01053 upperValue = ((RealGene*)child1->chromosome_->gene_[i])->upperBound_ ; 01054 lowerValue = ((RealGene*)child1->chromosome_->gene_[i])->lowerBound_ ; 01055 valueX1 = child1->chromosome_->gene_[i]->getRealAllele() ; 01056 valueX2 = child2->chromosome_->gene_[i]->getRealAllele() ; 01057 01058 valueY1 = ((RealGene*)child1->chromosome_->gene_[i])->einsteinMFunction( 01059 valueX1, 01060 valueX2, 01061 lambda) ; 01062 valueY2 = ((RealGene*)child1->chromosome_->gene_[i])->einsteinMFunction( 01063 valueX1, 01064 valueX2, 01065 1 - lambda) ; 01066 01067 if (valueY1 < lowerValue) 01068 child1->chromosome_->gene_[i]->setRealAllele(lowerValue) ; 01069 else if (valueY1 > upperValue) 01070 child1->chromosome_->gene_[i]->setRealAllele(upperValue) ; 01071 else 01072 child1->chromosome_->gene_[i]->setRealAllele(valueY1) ; 01073 01074 if (valueY2 < lowerValue) 01075 child2->chromosome_->gene_[i]->setRealAllele(lowerValue) ; 01076 else if (valueY2 > upperValue) 01077 child2->chromosome_->gene_[i]->setRealAllele(upperValue) ; 01078 else 01079 child2->chromosome_->gene_[i]->setRealAllele(valueY2) ; 01080 } // for 01081 result = true ; 01082 } // if 01083 01084 *firstChild = child1 ; 01085 *secondChild = child2 ; 01086 01087 return result ; 01088 } // Individual::einsteinExplotative 01089 01098 bool Individual::einsteinExplorative(double crossoverProbability, 01099 Individual * parent, 01100 Individual ** firstChild, 01101 Individual ** secondChild) { 01102 bool result ; 01103 //double betaq ; 01104 double random ; 01105 01106 Individual * child1 ; 01107 Individual * child2 ; 01108 child1 = new Individual(this) ; 01109 child2 = new Individual(parent) ; 01110 double valueY1 ; 01111 double valueY2 ; 01112 double valueX1 ; 01113 double valueX2 ; 01114 double upperValue ; 01115 double lowerValue ; 01116 01117 if ((child1 == NULL) || (child2 == NULL)) { 01118 cerr << "Individual::einsteinExplorative->Error creating children" 01119 << endl ; 01120 exit(-1) ; 01121 } // if 01122 01123 result = false ; 01124 random = random_->rndreal(0.0, 1.0) ; 01125 if (random <= crossoverProbability) { 01126 int i ; 01127 01128 for (i = 0; i < chromosome_->numberOfGenes_; i++) { 01129 upperValue = ((RealGene*)child1->chromosome_->gene_[i])->upperBound_ ; 01130 lowerValue = ((RealGene*)child1->chromosome_->gene_[i])->lowerBound_ ; 01131 valueX1 = child1->chromosome_->gene_[i]->getRealAllele() ; 01132 valueX2 = child2->chromosome_->gene_[i]->getRealAllele() ; 01133 01134 valueY1 = ((RealGene*)child1->chromosome_->gene_[i])->einsteinFFunction( 01135 valueX1, 01136 valueX2) ; 01137 valueY2 = ((RealGene*)child1->chromosome_->gene_[i])->einsteinSFunction( 01138 valueX1, 01139 valueX2) ; 01140 01141 if (valueY1 < lowerValue) 01142 child1->chromosome_->gene_[i]->setRealAllele(lowerValue) ; 01143 else if (valueY1 > upperValue) 01144 child1->chromosome_->gene_[i]->setRealAllele(upperValue) ; 01145 else 01146 child1->chromosome_->gene_[i]->setRealAllele(valueY1) ; 01147 01148 if (valueY2 < lowerValue) 01149 child2->chromosome_->gene_[i]->setRealAllele(lowerValue) ; 01150 else if (valueY2 > upperValue) 01151 child2->chromosome_->gene_[i]->setRealAllele(upperValue) ; 01152 else 01153 child2->chromosome_->gene_[i]->setRealAllele(valueY2) ; 01154 } // for 01155 result = true ; 01156 } // if 01157 01158 *firstChild = child1 ; 01159 *secondChild = child2 ; 01160 01161 return result ; 01162 } // Individual::logicalExplorative 01163 01164 01170 int Individual::bitFlipMutation(double mutationProbability) { 01171 int i ; 01172 int mutations ; 01173 01174 mutations = 0 ; 01175 for (i = 0; i < chromosome_->numberOfGenes_; i++) { 01176 mutations += chromosome_->gene_[i]->bitFlipMutation(mutationProbability) ; 01177 } // for 01178 01179 return mutations ; 01180 } // Individual::bitFlipMutation 01181 01187 int Individual::twoPointsInterchange(double mutationProbability) { 01188 int i ; 01189 int mutations ; 01190 01191 mutations = 0 ; 01192 for (i = 0; i < chromosome_->numberOfGenes_; i++) { 01193 mutations += chromosome_->gene_[i]->twoPointsInterchange(mutationProbability) ; 01194 } // for 01195 01196 return mutations ; 01197 } // Individual::bitFlipMutationn 01198 01205 int Individual::uniformMutation(double mutationProbability, 01206 double perturbation) { 01207 int i ; 01208 int mutations ; 01209 01210 mutations = 0 ; 01211 for (i = 0; i < chromosome_->numberOfGenes_; i++) { 01212 mutations += chromosome_->gene_[i]->uniformMutation(mutationProbability, 01213 perturbation) ; 01214 } // for 01215 01216 return mutations ; 01217 } // Individual::uniformMutation 01218 01227 int Individual::nonUniformMutation(double mutationProbability, 01228 double perturbation, 01229 int iteration, 01230 int maximumNumberOfIterations) { 01231 int i ; 01232 int mutations ; 01233 01234 mutations = 0 ; 01235 for (i = 0; i < chromosome_->numberOfGenes_; i++) { 01236 mutations += chromosome_->gene_[i]->nonUniformMutation(mutationProbability, 01237 perturbation, 01238 iteration, 01239 maximumNumberOfIterations) ; 01240 } // for 01241 01242 return mutations ; 01243 } // Individual::nonUniformMutation 01244 01250 int Individual::randomMutation(double mutationProbability) { 01251 int i ; 01252 int mutations ; 01253 01254 mutations = 0 ; 01255 for (i = 0; i < chromosome_->numberOfGenes_; i++) { 01256 mutations += chromosome_->gene_[i]->randomMutation(mutationProbability) ; 01257 } // for 01258 01259 return mutations ; 01260 } // Individual::randomMutation 01261 01262 01271 int Individual::polynomialMutation(double mutationProbability, 01272 double distributionIndex) { 01273 int i ; 01274 int mutations ; 01275 01276 mutations = 0 ; 01277 for (i = 0; i < chromosome_->numberOfGenes_; i++) { 01278 mutations += chromosome_->gene_[i]->polynomialMutation(mutationProbability, 01279 distributionIndex) ; 01280 } // for 01281 01282 return mutations ; 01283 } // Individual::polynomialMutation 01284 01290 bool Individual::identicalFitness(Individual * individual) { 01291 int i ; 01292 01293 for (i = 0; i < problem_->numberOfFunctions_; i++) 01294 // if (fitness_[i] != individual->fitness_[i]) 01295 if (fabs(fitness_[i] - individual->fitness_[i]) > 0.0000001) 01296 return false ; 01297 01298 return true ; 01299 } // Individual::identicalFitness 01300 01301 01313 int Individual::constraintComparison(Individual * individual) { 01314 int constraintsFirstIndividual ; 01315 int constraintsSecondIndividual ; 01316 01317 int result ; 01318 01319 result = 0 ; 01320 constraintsFirstIndividual = problem_->numberOfNonSatisfiedConstraints( 01321 this) ; 01322 constraintsSecondIndividual = problem_->numberOfNonSatisfiedConstraints( 01323 individual) ; 01324 if (constraintsFirstIndividual < constraintsSecondIndividual) 01325 result = 1 ; // Current dominates 01326 else if (constraintsFirstIndividual > constraintsSecondIndividual) 01327 result = -1 ; // Current is dominated 01328 return result ; 01329 } // Individual::constraintComparison 01330 01338 int Individual::numberOfViolatedConstraintsTest(Individual * individual) { 01339 int result ; 01340 01341 result = 0 ; 01342 if (this->numberOfViolatedConstraints_ > 01343 individual->numberOfViolatedConstraints_) 01344 result = -1 ; 01345 else if (this->numberOfViolatedConstraints_ < 01346 individual->numberOfViolatedConstraints_) 01347 result = 1 ; 01348 01349 return result ; 01350 } // Individual::numberOfViolatedConstraintsTest 01351 01359 int Individual::overallConstraintViolationTest(Individual * individual) { 01360 int result ; 01361 01362 result = 0 ; 01363 if (this->overallConstraintViolation_ > 01364 individual->overallConstraintViolation_) 01365 result = -1 ; 01366 else if (this->overallConstraintViolation_ < 01367 individual->overallConstraintViolation_) 01368 result = 1 ; 01369 01370 return result ; 01371 } // Individual::overallConstraintViolationTest 01372 01380 int Individual::dominanceTest2(Individual * individual) { 01381 int i ; 01382 int last ; 01383 int current ; 01384 int result ; 01385 bool finished ; 01386 01387 finished = false ; 01388 result = 0 ; 01389 01390 last = 0 ; 01391 i = 0 ; 01392 01393 while (!finished) { 01394 if (this->fitness_[i] < individual->fitness_[i]) 01395 if ((individual->fitness_[i] - this->fitness_[i]) >= 1e-7) 01396 current = 1 ; 01397 else 01398 current = 0 ; 01399 else if (this->fitness_[i] > individual->fitness_[i]) 01400 if ((this->fitness_[i] - individual->fitness_[i]) >= 1e-7) 01401 current = -1 ; 01402 else 01403 current = 0 ; 01404 else 01405 current = 0 ; 01406 01407 if ((current != 0) && (current == -last)) { 01408 finished = true ; 01409 result = 0 ; 01410 } // if 01411 //if ((current == 0) || (current == last)){ 01412 else { 01413 if (current != 0) 01414 last = current ; 01415 01416 i ++ ; 01417 if (i == this->problem_->numberOfFunctions_) { 01418 finished = true ; 01419 //if (current != 0) 01420 result = last ; 01421 //else 01422 // result = 1 ; 01423 } // if 01424 } // else 01425 } // while 01426 01427 return result ; 01428 } // Individual::dominanceTest 01429 01438 int Individual::dominanceTest(Individual * individual) { 01439 int value ; 01440 01441 int flag1 = 0 ; 01442 int flag2 = 0 ; 01443 01444 for (int i = 0; i < problem_->numberOfFunctions_ ; i++) 01445 if (this->fitness_[i] < individual->fitness_[i]) 01446 flag1 = 1 ; 01447 else if (this->fitness_[i] > individual->fitness_[i]) 01448 flag2 = 1 ; 01449 01450 if ((flag1 == 1) && (flag2 == 0)) 01451 value = 1 ; 01452 else if ((flag1 == 0) && (flag2 == 1)) 01453 value = -1 ; 01454 else if ((flag1 == 1) && (flag2 == 1)) 01455 value = 0 ; 01456 else 01457 value = 0 ; 01458 01459 return value ; 01460 } // Individual::dominanceTest2 01461 01462 01463 Individual & Individual::operator=(Individual &individual) { 01464 int i ; 01465 01466 problem_ = individual.problem_ ; 01467 *chromosome_ = *(individual.chromosome_) ; 01468 random_ = individual.random_ ; 01469 01470 for (i = 0; i <problem_->numberOfFunctions_; i++) 01471 fitness_[i] = individual.fitness_[i] ; 01472 01473 return *this ; 01474 } // Individual::operator= 01475 01476 bool Individual::operator==(Individual &individual) { 01477 if (*chromosome_ == *individual.chromosome_) 01478 return true ; 01479 else 01480 return false ; 01481 } // Individual::operator== 01482 01483 bool Individual::operator!=(Individual &individual) { 01484 if (*chromosome_ != *individual.chromosome_) 01485 return true ; 01486 else 01487 return false ; 01488 } // Individual::operator== 01489 01490 01491 ostream& operator<< (ostream& outputStream, Individual& individual) { 01492 int i ; 01493 outputStream << *(individual.chromosome_) ; 01494 outputStream << "Fitness: " ; 01495 for (i = 0; i < individual.problem_->numberOfFunctions_; i++) 01496 outputStream << individual.fitness_[i] << " " ; 01497 outputStream << endl ; 01498 01499 return outputStream ; 01500 } // operator<< 01501 01502 void Individual::printFitness() { 01503 int i ; 01504 cout << "Fitness (" << problem_->numberOfFunctions_ << "): " ; 01505 for (i = 0; i < problem_->numberOfFunctions_; i++) 01506 cout << fitness_[i] << " " ; 01507 cout << endl ; 01508 } // Individual::printFitness 01509 01510 void Individual::printGenotype() { 01511 int i ; 01512 for (i = 0; i < problem_->numberOfVariables_; i++) { 01513 chromosome_->gene_[i]->printGenotype() ; 01514 cout << " " ; 01515 } // for 01516 cout << endl ; 01517 } // Individual::printGenotype 01518