void GeneticAlgorithm::mutate(Genotype& genotype) { // Generate a dummy genotype with which to test mutations with. Genotype dummyGenotype = genotype; do { // Whilst the mutated genome are acceptable to the user... // Generate a dummy genotype with which to test mutations with. dummyGenotype = genotype; // Apply mutations to each gene in the dummy genome. for (int gene = 0; gene < dummyGenotype.myGenes.size(); gene++) { // Generate prototype mutations until one fits into [0, 1]. double newValue = 0; do { newValue = dummyGenotype.getGene(gene) + myRand.randNorm(0.0, myMutationVariance); } while ((newValue < 0) || (newValue > 1)); // Apply the mutation. dummyGenotype.setGene(gene, newValue); } } while(myGenomeChecker.checkGenomeIsValid(dummyGenotype) == false); // The genome is valid, so overwrite the original genotype. genotype = dummyGenotype; }
void GeneticAlgorithm::verifyAndPush( Genotype g ) { vector<int>::iterator geneIterator; int points = 0; for(geneIterator = g.getGene()->begin(); geneIterator != g.getGene()->end(); ++geneIterator) { points += *geneIterator; } if(points <= pointLimit) population.push_back(g); }
void GeneticAlgorithm::mutate ( ) { int transfer; int first; double x; vector<int>::iterator geneIterator; for (int i = 0; i < population.size(); i++ ) { transfer = 0; Genotype g; g.setGene(*population[i].getGene()); for (geneIterator = g.getGene()->begin(); geneIterator != g.getGene()->end(); ++geneIterator) { x = rand ( ) % 1000 / 1000.0; if ( x < PMUTATION ) { ++transfer; if ( transfer % 2 == 1 ) first = distance(g.getGene()->begin(),geneIterator); else { if(g.getGene()->at(first) < *geneIterator) { ++g.getGene()->at(first); --*geneIterator; } else if(g.getGene()->at(first) > *geneIterator) { --g.getGene()->at(first); ++*geneIterator; } else { ++g.getGene()->at(first); ++*geneIterator; } } } } verifyAndPush(g); } return; }
void GeneticAlgorithm::Xover ( int a, int b ) { int pointA; // parent A crossover point int pointB; // parent B crossover point // // Select the crossover point. // // A B //point -- [XXX[P)XX) // pointA = rand () % ( population[a].getGene()->size() ); //cout<< pointA << "\n"; pointB = (pointA % NTOOLS) + NTOOLS * ( rand () % ( population[b].getGene()->size() / NTOOLS) ) ; //cout<< pointB << "\n"; // // Splice the parents together // Genotype g; g.assignGene(population[a].spliceGene(0, pointA), population[b].spliceGene(pointB, population[b].getGene()->size())); vector<int>::iterator geneIterator; int points = 0; for(geneIterator = g.getGene()->begin(); geneIterator != g.getGene()->end(); ++geneIterator) { points += *geneIterator; } if(points <= pointLimit) population.push_back(g); /* Method to allow 4 parents and cut/splice cout<<one<<two<< point[0][0]<< "\n"; point[0][0] = ( rand ( ) % ( population[one].getGene()->size() - 3 ) ) + 1; cout<< point[0][0]<< "\n"; point[1][0] = (point [0][0] % NTOOLS) + NTOOLS * ( rand ( ) % ( population[two].getGene()->size() / NTOOLS) ) ; cout<< point[0][0]<< "\n"; point[2][0] = (point [0][0] % NTOOLS) + NTOOLS * ( rand ( ) % ( population[three].getGene()->size() / NTOOLS) ) ; cout<< point[0][0]<< "\n"; point[3][0] = (point [0][0] % NTOOLS) + NTOOLS * ( rand ( ) % ( population[four].getGene()->size() / NTOOLS) ) ; cout<< point[0][0]<< "\n"; //point[0][1] point[0][1] = (point[0][0]) + ( rand ( ) % ( population[one].getGene()->size() - 2 - point[0][0]) ) + 1; cout<< point[0][0]<< "\n"; //point[1][1] if(point[1][0] > (point[0][1] % NTOOLS) ) point[1][1] = (point [0][1] % NTOOLS) + NTOOLS * ( rand ( ) % ( (population[two].getGene()->size() / NTOOLS) - (point[1][0] / NTOOLS) - 1) + (point[1][0] / NTOOLS) + 1) ; else point[1][1] = (point [0][1] % NTOOLS) + NTOOLS * ( rand ( ) % ( (population[two].getGene()->size() / NTOOLS) - (point[1][0] / NTOOLS) ) + point[1][0] / NTOOLS) ; cout<< point[0][0]<< "\n"; //point[2][1] if(point[2][0] > (point[0][1] % NTOOLS) ) point[2][1] = (point [0][1] % NTOOLS) + NTOOLS * ( rand ( ) % ( (population[three].getGene()->size() / NTOOLS) - (point[2][0] / NTOOLS) - 1) + (point[2][0] / NTOOLS) + 1) ; else point[2][1] = (point [0][1] % NTOOLS) + NTOOLS * ( rand ( ) % ( (population[three].getGene()->size() / NTOOLS) - (point[2][0] / NTOOLS) ) + point[2][0] / NTOOLS ) ; //point[3][1] if(point[3][0] > (point[0][1] % NTOOLS) ) point[3][1] = (point [0][1] % NTOOLS) + NTOOLS * ( rand ( ) % ( (population[four].getGene()->size() / NTOOLS) - (point[3][0] / NTOOLS) - 1) + (point[3][0] / NTOOLS) + 1) ; else point[3][1] = (point [0][1] % NTOOLS) + NTOOLS * ( rand ( ) % ( (population[four].getGene()->size() / NTOOLS) - (point[3][0] / NTOOLS) ) + point[3][0] / NTOOLS ) ; //point[0][2] point[0][2] = (point[0][1]) + ( rand ( ) % ( population[one].getGene()->size() - 1 ) ) + 1; //point[1][2] if(point[1][1] > (point[0][2] % NTOOLS) ) point[1][2] = (point [0][2] % NTOOLS) + NTOOLS * ( rand ( ) % ( (population[two].getGene()->size() / NTOOLS) - (point[1][1] / NTOOLS) - 1) + (point[1][1] / NTOOLS) + 1) ; else point[1][2] = (point [0][2] % NTOOLS) + NTOOLS * ( rand ( ) % ( (population[two].getGene()->size() / NTOOLS) - (point[1][1] / NTOOLS) ) + point[1][1] / NTOOLS ) ; //point[2][2] if(point[2][1] > (point[0][2] % NTOOLS) ) point[2][2] = (point [0][2] % NTOOLS) + NTOOLS * ( rand ( ) % ( (population[three].getGene()->size() / NTOOLS) - (point[2][1] / NTOOLS) - 1) + (point[2][1] / NTOOLS) + 1) ; else point[2][2] = (point [0][2] % NTOOLS) + NTOOLS * ( rand ( ) % ( (population[three].getGene()->size() / NTOOLS) - (point[2][1] / NTOOLS) ) + point[2][1] / NTOOLS ) ; //point[3][2] if(point[3][1] > (point[0][2] % NTOOLS) ) point[3][2] = (point [0][2] % NTOOLS) + NTOOLS * ( rand ( ) % ( (population[four].getGene()->size() / NTOOLS) - (point[3][1] / NTOOLS) - 1) + (point[3][1] / NTOOLS) + 1) ; else point[3][2] = (point [0][2] % NTOOLS) + NTOOLS * ( rand ( ) % ( (population[four].getGene()->size() / NTOOLS) - (point[3][1] / NTOOLS) ) + point[3][1] / NTOOLS ) ; for (int i = 0; i < 4; i++ ) { struct genotype g; g.fitness = 0; g.cfitness = 0; g.rfitness = 0; std::list<int>::iterator beginIterator; std::list<int>::iterator endIterator; //Part 1 of new gene endIterator = population[one].gene.begin(); std::advance(endIterator, point[0][0] + 1); for(beginIterator = population[one].gene.begin(); beginIterator != endIterator; ++beginIterator) { g.gene.push_back(*beginIterator); } //Part 2 beginIterator = population[two].gene.begin(); endIterator = population[two].gene.begin(); std::advance(beginIterator, point[1][0]); std::advance(endIterator, point[1][1] + 1); for(; beginIterator != endIterator; ++beginIterator) { g.gene.push_back(*beginIterator); } //Part 3 beginIterator = population[three].gene.begin(); endIterator = population[three].gene.begin(); std::advance(beginIterator, point[2][1]); std::advance(endIterator, point[2][2] + 1); for(; beginIterator != endIterator; ++beginIterator) { g.gene.push_back(*beginIterator); } //Part 4 beginIterator = population[four].gene.begin(); std::advance(beginIterator, point[3][2]); for(; beginIterator != population[four].gene.end(); ++beginIterator) { g.gene.push_back(*beginIterator); } population.push_back(g); //r8_swap ( &population[one].gene[i], &population[two].gene[i] ); }*/ return; }