public Chromosome Run(IProblem problem, Parameters parameters) { Parameters = parameters; chromosomeFactory = parameters.ChromosomeFactory; heuristics = parameters.Heuristics; var population = Initialization(problem, parameters); int counter = 0; var watch = System.Diagnostics.Stopwatch.StartNew(); watch.Start(); bool conv = false; Chromosome theBestOne = population[0], localBest; do { population = Evolve(problem, parameters, population); population = Improve(problem, parameters, population); if (conv = IsConvergent(population, parameters)) { population = Restart(problem, population, parameters); } localBest = GetTheBestChromosome(population); theBestOne = theBestOne.CompareTo(localBest) < 0 ? theBestOne : localBest; } while (!StopCondition(++counter, parameters)); //Chromosome theBestSolution = GetTheBestChromosome(population); Console.WriteLine("\nThe best solution: " + theBestOne.ToString()); return(theBestOne); }
/// <summary> /// Runs the memetic algorithm to solve the problem. /// </summary> /// <param name="problem">The problem.</param> /// <param name="parameters">The parameters.</param> /// <returns>The best found solution.</returns> public async Task <Chromosome> Run(IProblem problem, Parameters parameters, Action <string, Chromosome, int, float, long, int> callback, string id, bool genetic) { Parameters = parameters; chromosomeFactory = parameters.ChromosomeFactory; heuristics = parameters.Heuristics; Population population = new Population(parameters.PopulationSize); Chromosome solution = null; int index = 0; while (index < parameters.PopulationSize) { solution = parameters.ChromosomeFactory.RandomSolution(parameters.GeneCount, problem); if (!population.Contains(solution)) { population[index] = solution; ++index; } } var watch = System.Diagnostics.Stopwatch.StartNew(); watch.Start(); Chromosome theBestOne = population[0], localBest; int iteration = 0, restarts = 0; await Task.Run(() => { do { if (source.IsCancellationRequested) { break; } population = Evolve(problem, parameters, population); if (IsConvergent(population, parameters)) { population = Restart(problem, population, parameters); ++restarts; } localBest = GetTheBestChromosome(population); theBestOne = theBestOne.CompareTo(localBest) < 0 ? theBestOne : localBest; callback(id, theBestOne, iteration, (theBestOne as Solution).TotalDistance(), watch.ElapsedMilliseconds, restarts); } while (!StopCondition(++iteration, parameters)); }, source.Token); source.Dispose(); source = new CancellationTokenSource(); return(theBestOne); }
public Task <Chromosome> Run(IProblem problem, Parameters parameters, Action <string, Chromosome, int, float, long, int> callback, string id) { chromosomeFactory = parameters.ChromosomeFactory; heuristics = parameters.Heuristics; var population = Initialization(problem, parameters); int iteration = 0, restarts = 0; var watch = System.Diagnostics.Stopwatch.StartNew(); watch.Start(); Chromosome theBestOne = population[0], localBest; var result = Task.Run(() => { do { if (source.IsCancellationRequested) { break; } population = Evolve(problem, parameters, population); population = Improve(problem, parameters, population); if (IsConvergent(population, parameters)) { population = Restart(problem, population, parameters); ++restarts; } localBest = GetTheBestChromosome(population); theBestOne = theBestOne.CompareTo(localBest) < 0 ? theBestOne : localBest; callback(id, theBestOne, iteration, (theBestOne as Solution).TotalDistance(), watch.ElapsedMilliseconds, restarts); } while (!StopCondition(++iteration, parameters)); return(theBestOne); }, source.Token); //Chromosome theBestSolution = GetTheBestChromosome(population); source.Dispose(); source = new CancellationTokenSource(); return(result); }
/// <summary> /// Creates a new fitness landscape. /// </summary> /// <param name="problem">The problem instance.</param> /// <param name="chromosomeFactory">A chromosome factory.</param> /// <param name="fitnessFunction">The fitness function.</param> public Landscape(IProblem problem, AbstractChromosomeFactory chromosomeFactory, FitnessFunction fitnessFunction) { Problem = problem; ChromosomeFactory = chromosomeFactory; FitnessFunction = fitnessFunction; }
public void SetUp() { reader = new VrptwProblemReader(); factory = new SolutionFactory(); fitness = new FitnessFunction(5000, 1); }