//--------------------------------Roulette------------------------------------------- // // selects a chromosome from the population via roulette wheel selection //------------------------------------------------------------------------------------ private static string Roulette(float total_fitness, chromo[] Population) { //generate a random number between 0 & total fitness count float Slice = (float)(r.NextDouble() * total_fitness); //go through the chromosones adding up the fitness so far float FitnessSoFar = 0.0f; for (int i = 0; i < POP_SIZE; i++) { FitnessSoFar += Population[i].fitness; //if the fitness so far > random number return the chromo at this point if (FitnessSoFar >= Slice) return Population[i].bits; } return ""; }
static void Main(string[] args) { //seed the random number generator r = new Random(); //just loop endlessly until user gets bored :0) while (true) { //storage for our population of chromosomes. chromo[] Population = new chromo[POP_SIZE]; //get a target number from the user. (no error checking) float Target; System.Console.Out.WriteLine("\nInput a target number: "); Target = float.Parse(System.Console.In.ReadLine()); //first create a random population, all with zero fitness. for (int i = 0; i < POP_SIZE; i++) { Population[i] = new chromo(GetRandomBits(CHROMO_LENGTH), 0.0f); } int GenerationsRequiredToFindASolution = 0; //we will set this flag if a solution has been found bool bFound = false; //enter the main GA loop while (!bFound) { //this is used during roulette wheel sampling float TotalFitness = 0.0f; // test and update the fitness of every chromosome in the // population for (int i = 0; i < POP_SIZE; i++) { Population[i].fitness = AssignFitness(Population[i].bits, Target); TotalFitness += Population[i].fitness; } // check to see if we have found any solutions (fitness will be 999) for (int i = 0; i < POP_SIZE; i++) { if (Population[i].fitness == 999.0f) { System.Console.Out.WriteLine("\nSolution found in " + GenerationsRequiredToFindASolution + " generations!"); PrintChromo(Population[i].bits); bFound = true; break; } } // create a new population by selecting two parents at a time and creating offspring // by applying crossover and mutation. Do this until the desired number of offspring // have been created. //define some temporary storage for the new population we are about to create chromo[] temp = new chromo[POP_SIZE]; int cPop = 0; //loop until we have created POP_SIZE new chromosomes while (cPop < POP_SIZE) { // we are going to create the new population by grabbing members of the old population // two at a time via roulette wheel selection. string offspring1 = Roulette(TotalFitness, Population); string offspring2 = Roulette(TotalFitness, Population); //add crossover dependent on the crossover rate Crossover(ref offspring1, ref offspring2); //now mutate dependent on the mutation rate Mutate(ref offspring1); Mutate(ref offspring2); //add these offspring to the new population. (assigning zero as their //fitness scores) temp[cPop++] = new chromo(offspring1, 0.0f); temp[cPop++] = new chromo(offspring2, 0.0f); }//end loop //copy temp population into main population array for (int i = 0; i < POP_SIZE; i++) { Population[i] = temp[i]; } ++GenerationsRequiredToFindASolution; // exit app if no solution found within the maximum allowable number // of generations if (GenerationsRequiredToFindASolution > MAX_ALLOWABLE_GENERATIONS) { System.Console.Out.WriteLine("No solutions found this run!"); bFound = true; } } }//end while }