// Calculates the fitness of a chromosome. public static double CalculateFitness(Chromosome chromosome) { var distanceToTravel = 0.0; if (!FITNESS_CONSIDERS_PARTITIONS) { distanceToTravel = CalculateDistance(chromosome); } else { var greedy_mTSP = new GreedyAlgorithm(Utils.fittestToCitiesList(CITIES, chromosome), DEPOT, NUMBER_OF_DRONES); distanceToTravel = greedy_mTSP.solve().Item1; } return 1 - distanceToTravel / 10000; }
static void ga_OnRunComplete(object sender, GaEventArgs e) { var fittest = getFittestChromosome(e.Population); // Call the algorithm to generate the mTSP solution. var greedy_mTSP = new GreedyAlgorithm(Utils.fittestToCitiesList(CITIES, fittest), DEPOT, NUMBER_OF_DRONES); solution = greedy_mTSP.solve().Item1; partitionPoints = greedy_mTSP.solve().Item2; timeElapsed = STOPWATCH.ElapsedMilliseconds; fittestChromosome = fittest; Console.WriteLine("Final solution (MinMax cost): {0}", solution); Console.WriteLine("Time elapsed: {0} ms", STOPWATCH.ElapsedMilliseconds); }
// Callback for when a generation completes its iteration. private static void ga_OnGenerationComplete(object sender, GaEventArgs e) { // We print in the console the current results of the algorithm for every 100 generations // when not in calibration mode. When calibration, we print only for the last generation. if ((!IS_CALIBRATION && e.Generation % 100 == 0) || e.Generation == NUMBER_OF_GENERATIONS) { var fittest = getFittestChromosome(e.Population); var distanceToTravel = 0.0; if (!FITNESS_CONSIDERS_PARTITIONS) { distanceToTravel = CalculateDistance(fittest); } else { var greedy_mTSP = new GreedyAlgorithm(Utils.fittestToCitiesList(CITIES, fittest), DEPOT, NUMBER_OF_DRONES); distanceToTravel = greedy_mTSP.solve().Item1; } Console.WriteLine("Generation: {0}, Fitness: {1}, Distance: {2}", e.Generation, fittest.Fitness, distanceToTravel); } }