private void FillBestFitArray(Solution[] generation) { for (int slot = 1; slot <= generation.Length - 2; slot++) { int fitness = generation[slot + 1].score - generation[slot].score; bestFitArray[slot] = new FitnessObject(slot, fitness); } }
public int Compare(FitnessObject <T> x, FitnessObject <T> y) { int to_return = 0; if (x.Fitness < y.Fitness) { to_return = 1; } else if (x.Fitness > y.Fitness) { to_return = -1; } return(to_return); }
//Finds and returns the solution with the highest fitness in a given generation. public Solution FindBestFit(Solution[] generation) { //create array of fitness objects bestFitArray = new FitnessObject[generation.Length]; //set up ends of the bestFitArray, to avoid overflowing. int firstFitness = generation.First().score; bestFitArray[0] = new FitnessObject(0, firstFitness); int lastFitness = generation.Last().score - generation[generation.Count() - 2].score; bestFitArray[generation.Count() - 1] = new FitnessObject(generation.Length - 1, lastFitness); //Fill the BestFitArray, then sort it by each item's fitness score. FillBestFitArray(generation); IEnumerable <FitnessObject> bestFitSorted = bestFitArray.OrderBy(item => item.fitness); //Use the BestFitArray to determine which solution in the generation is the best fit. Then, return that solution. int slotOfBestFitObject = bestFitSorted.Last().generationSlot; return(generation[slotOfBestFitObject]); }