// This function uses the fitness function given in FitnessFunction.cs to evaluate all paths of the current generation public void evaluation() { // Creates a Simulation and a rating for each path in the current population and adds it to the ratings List for (int i = 0; i < populationSize; i++) { // Creates a new simulation and runs it Variables.simulation = new Simulation(Variables.vehicle, population[i].Path); Variables.simulation.run(); // Gets the rating for the current simulation population[i].Rating = FitnessFunction.fitness(Variables.simulation.getPath()); if (Variables.popDebugging) { population[i].Collisions = Variables.debugCollisions; population[i].Distances = Variables.debugDistance; } // Writes the path to the global Variable for drawing Variables.path = population[i].Path; // Draws. glcontrol.Refresh(); } }
// This function is only used once (Generation 0) and populates the algorithm with randomly generated paths. public void initialize() { double rating; // Generates an initial population of size <PopulationSize> along with their fitness for (int i = 0; i < populationSize; i++) { // Generates a random path of length 20 and saves it to the global path EZPathFollowing.PathPrimitives.generatePath(); // Creates a Simulation for this path Variables.simulation = new Simulation(Variables.vehicle, Variables.path); Variables.simulation.run(); // Rates the Path and adds the rating to the ratings List rating = FitnessFunction.fitness(Variables.simulation.getPath()); // Adds the path, genome and rating to the population population[i] = new Population(Variables.path, Variables.genome, rating); // Draws. glcontrol.Refresh(); } }