// Single point crossover Individual Individual::crossover(Individual parent) { unsigned int xPoint, nBits; uint32 chThis, chParent, mask, off; chThis = getChromosome().bitArray; chParent = parent.getChromosome().bitArray; nBits = sizeof( uint32 ); xPoint = Utility::getRandomUI32(0, nBits); mask = ~0 >> xPoint; if ( Utility::getRandomF64() < 0.5 ) off = ( chThis & ~mask ) | ( chParent & mask ); else off = ( chThis & mask ) | ( chParent & ~mask ); return Individual( off ); }
std::vector < Individual > Individual::cruzaAcentuada (Individual& spouse) { std::vector < Individual > result; std::vector < bool > parent1 = getChromosome(); std::vector < bool > parent2 = spouse.getChromosome(); size_t size = parent1.size(); size_t genNumber = chromosome.size(); std::vector < bool > child1_ch, child2_ch, child1_marks(size, false), child2_marks(size, false); bool cross = false; std::vector <bool>::iterator mark_it = crossPoints.begin(); std::vector <bool>::iterator mark_spouse_it = spouse.crossPoints.begin(); for( size_t i = 0; i < size; i++, mark_it++, mark_spouse_it++ ) { if (*mark_it || *mark_spouse_it) { cross = !cross; } if(!cross) { if(*mark_it) { child2_marks.at(i) = true; } if (*mark_spouse_it) { child1_marks.at(i) = true; } child1_ch.push_back( parent1.at(i) ); child2_ch.push_back( parent2.at(i) ); } else { if(*mark_it) { child1_marks.at(i) = true; } if (*mark_spouse_it) { child2_marks.at(i) = true; } child1_ch.push_back( parent2.at(i) ); child2_ch.push_back( parent1.at(i) ); } } Individual child1 (child1_ch,genNumber); Individual child2 (child2_ch,genNumber); child1.crossPoints = child1_marks; child2.crossPoints = child2_marks; result.push_back( child1 ); result.push_back( child2 ); return result; }
std::vector < Individual > Individual::cruzaUnPunto (Individual& spouse, size_t p) { std::vector < Individual > result; std::vector < bool > parent1 = getChromosome(); std::vector < bool > parent2 = spouse.getChromosome(); std::vector < bool > child1, child2; size_t size = parent1.size(); size_t genNumber = chromosome.size(); for( size_t i = 0; i < size; i++ ) { if (i < p) { child1.push_back( parent1.at(i) ); child2.push_back( parent2.at(i) ); } else { child1.push_back( parent2.at(i) ); child2.push_back( parent1.at(i) ); } } result.push_back( Individual(child1,genNumber) ); result.push_back( Individual(child2,genNumber) ); return result; }
std::vector < Individual > Individual::cruzaDosPuntos (Individual& spouse, size_t x, size_t y) { std::vector < Individual > result; std::vector < bool > parent1 = getChromosome(); std::vector < bool > parent2 = spouse.getChromosome(); std::vector < bool > child1, child2; size_t size = parent1.size(); size_t genNumber = chromosome.size(); size_t t = x; x = x<y?x:y; y = y<t?t:y; for( size_t i = 0; i < size; i++ ) { if (i < x || i >= y) { child1.push_back( parent1.at(i) ); child2.push_back( parent2.at(i) ); } else { child1.push_back( parent2.at(i) ); child2.push_back( parent1.at(i) ); } } result.push_back( Individual(child1,genNumber) ); result.push_back( Individual(child2,genNumber) ); return result; }