vector<Chromosome*> BoltzmannSelection::execute(vector<Chromosome*> _selectionPool, uint _selectionCount, vector<Chromosome*>& _unselected){ assert(_selectionPool.size() >= _selectionCount); mFitnessVals.clear(); mMaxFit = 0; calculateFitness(_selectionPool); calculateMaxFitness(); vector<Chromosome*> output; while(output.size() < _selectionCount) output.push_back(selectChromosome(_selectionPool)); _unselected = _selectionPool; return output; }
void doReproduce(){ struct Chromosome XoverPop[4]; // Small population of 4 individuals XoverPop[0] = Population[selectParent()]; // Select parent 1 XoverPop[1] = Population[selectParent()]; // Select parent 2 /*** Crossover ***/ int GeneNr; for(GeneNr = 0; GeneNr < GENETIC_PURSUERS;GeneNr++){ // Take one gene from each parent XoverPop[2].gene[GeneNr ] = XoverPop[0].gene[GeneNr]; XoverPop[3].gene[GeneNr ] = XoverPop[1].gene[GeneNr]; XoverPop[2].gene[GeneNr+1] = XoverPop[1].gene[GeneNr+1]; XoverPop[3].gene[GeneNr+1] = XoverPop[0].gene[GeneNr+1]; GeneNr++; } /*** Mutation ***/ doMutation(&XoverPop[2]); // Mutate the generated individual doMutation(&XoverPop[3]); // Mutate the generated individual /*** sort to add best individes ***/ int i = 0; for(i=2;i<4;i++) calculateFitness(&XoverPop[i]); // Calculate fitness for children sortPopulation(XoverPop, 4); // Sort after fitness addToNewPopulation(XoverPop[0]); // Add best individual to new population addToNewPopulation(XoverPop[1]); // Add second best individual to new population }
/*** Algorithm ***/ void genAlg(int *solution) { // Main call function for Genetic Algorithm int currentGeneration=0, currentPopulation=0; int isSolved = 0; int k,i,j; int isChanged = 1; for(;GENETIC_POPULATION_SIZE<GENETIC_POPULATION_MAX_SIZE;GENETIC_POPULATION_SIZE+=GENETIC_INCREMENT_POPULATION){ for(i = GENETIC_POPULATION_SIZE; i < GENETIC_POPULATION_SIZE+GENETIC_INCREMENT_POPULATION; i++){ // Reset all genes. for(j = 0; j < GENETIC_PURSUERS; j++) for(k=0;k<GENETIC_STEPS;k++) Population[i].gene[j].allele[k] = 4; Population[i].inS4=999; // Reset fitness value (S4), just set to high value Population[i].chrSteps=999; // Reset fitness value (steps), set to high value Population[i].fitnessScore = 1; // Low value = bad solution, must be positive for(j = 0; j < GENETIC_PURSUERS; j++) for(k = 0; k < 2; k++) Population[i].gene[j].start[k] = Population[0].gene[j].start[k]; int GeneNr=0; for(GeneNr = 0; GeneNr < GENETIC_PURSUERS; GeneNr++){ // For each Gene getRandom(&(Population[i].gene[GeneNr]),0); // Generate a random step sequence } } if(isChanged == 1){ i = 0; isChanged = 0; } else{ i = GENETIC_POPULATION_SIZE; } for(; i < GENETIC_POPULATION_SIZE+GENETIC_INCREMENT_POPULATION; i++){ // Calculate fitness for population calculateFitness(&Population[i]); if(Population[i].inS4 == 0){ isSolved = 1; if(Population[i].chrSteps < GENETIC_STEPS){ isChanged = 1; GENETIC_STEPS = Population[i].chrSteps; } } } if(isSolved==1 && GENETIC_POPULATION_SIZE>GENETIC_MIN_POP_SIZE) break; } printf("genAlg\n"); printf("**** Genetic Algorithm info ****\n"); printf("Population size:\t%d\n", GENETIC_POPULATION_SIZE); printf("Pursuers:\t\t%d\n", GENETIC_PURSUERS); printf("Maximum steps:\t\t%d\n", GENETIC_STEPS); printf("Maximum generations:\t%d\n", GENETIC_GENERATIONS); printf("Convergence %%:\t\t%d\n", abs(GENETIC_CONVERGENCE_PERCENT*100)); printf("Mutation %%:\t\t%d\n", abs(GENETIC_MUTATION_PROBABILITY*100)); sortPopulation(Population, GENETIC_POPULATION_SIZE); // Sort the population after fitness for(currentGeneration = 0; currentGeneration < GENETIC_GENERATIONS; currentGeneration++){ /*** New generation ***/ /* calculateFitness(&Population[0]); printStates(); printf("Generation %d\n", currentGeneration); */ addToNewPopulation(Population[0]); // Elite selection addToNewPopulation(Population[1]); // Elite selection while(NewPopLocation < GENETIC_POPULATION_SIZE) doReproduce(); // Reproduction sortPopulation(New_Population, GENETIC_POPULATION_SIZE); // Sort the new population after fitness /*** Swap populations ***/ int i; for(i = 0; i < GENETIC_POPULATION_SIZE;i++) Population[i] = New_Population[i]; // Overwrite old population with new memcpy(Population, New_Population, sizeof New_Population); /* if(currentGeneration%1==0){ printf("Best Fitness: %f, States: %d, steps: %d\n", Population[0].fitnessScore, Population[0].inS4, Population[0].chrSteps); printf("Compare Fitness (%d): %f, States: %d, steps: %d\n", abs(GENETIC_POPULATION_SIZE*GENETIC_CONVERGENCE_PERCENT), Population[abs(GENETIC_POPULATION_SIZE*GENETIC_CONVERGENCE_PERCENT)].fitnessScore, Population[abs(GENETIC_POPULATION_SIZE*GENETIC_CONVERGENCE_PERCENT)].inS4, Population[abs(GENETIC_POPULATION_SIZE*GENETIC_CONVERGENCE_PERCENT)].chrSteps); printf("Worst Fitness (%d): %f, States: %d, steps: %d\n", GENETIC_POPULATION_SIZE, Population[GENETIC_POPULATION_SIZE-1].fitnessScore, Population[GENETIC_POPULATION_SIZE-1].inS4, Population[GENETIC_POPULATION_SIZE-1].chrSteps); } */ if(Population[0].inS4 == 0 && Population[abs(GENETIC_POPULATION_SIZE*GENETIC_CONVERGENCE_PERCENT)].inS4 == 0) // If a complete solution has been found, check if convergence level is reached if((Population[0].chrSteps==Population[abs(GENETIC_POPULATION_SIZE*GENETIC_CONVERGENCE_PERCENT)].chrSteps)) // If 90% of the population has the same number of steps, no need to continue to do more generations. if(Population[0].fitnessScore==Population[abs(GENETIC_POPULATION_SIZE*GENETIC_CONVERGENCE_PERCENT)].fitnessScore) // Redundant check, check fitness score break; NewPopLocation=0; } printf("%d generations used.\n", currentGeneration); printf("Best Fitness: %f, States: %d, steps: %d\n", Population[0].fitnessScore, Population[0].inS4, Population[0].chrSteps); printf("Compare Fitness: %f, States: %d, steps: %d\n", Population[abs(GENETIC_POPULATION_SIZE*GENETIC_CONVERGENCE_PERCENT)].fitnessScore, Population[abs(GENETIC_POPULATION_SIZE*GENETIC_CONVERGENCE_PERCENT)].inS4, Population[abs(GENETIC_POPULATION_SIZE*GENETIC_CONVERGENCE_PERCENT)].chrSteps); printf("Worst Fitness: %f, States: %d, steps: %d\n", Population[GENETIC_POPULATION_SIZE-1].fitnessScore, Population[GENETIC_POPULATION_SIZE-1].inS4, Population[GENETIC_POPULATION_SIZE-1].chrSteps); solution[0]=GENETIC_PURSUERS; // Write number of pursuers to solution solution[1]=GENETIC_STEPS; // Write maximum number of steps to solution if(Population[0].chrSteps<=MAX_SIZE_SOLUTION){ doDecode(&Population[0], solution); // Decode solution to positions solution[1]=calculateStates(solution); // Calculate states, and update steps to minimum } else solution[1]=Population[0].chrSteps; // If the solution was to long, or not found, no need to return path /*** Free allocated memory ***/ free(Population); free(New_Population); /*** Free memory for NodeMatrix ***/ for(i = 0; i < ROWS;i++) free(NodeMatrix[i]); free(NodeMatrix); return; }