Example #1
0
        public ISolution Solve()
        {
            var       totalElapsedEpochs        = 0;
            ISolution bestSolutionSoFar         = null;
            double    bestFitnessSoFar          = 0;
            int       overallEvaluatedSolutions = 0;
            var       sw = new Stopwatch();

            sw.Start();

            do
            {
                var theWorld = new TheWorld(this.problem, this.environmentConfig.PopulationSize);

                var bestSolution = theWorld.EvolveUntil((epochs, epochsWithoutFitnessImprovement, bestFitness, averageFitness) =>
                {
                    if (bestFitness > bestFitnessSoFar)
                    {
                        bestFitnessSoFar = bestFitness;
                        if (logFitnessImprovement)
                        {
                            log.DebugFormat("Fitness improvement! Epoch {0}. Best fitness: {1} - average {2} - Fitness stable from epochs: {3}",
                                            epochs,
                                            bestFitness,
                                            averageFitness,
                                            epochsWithoutFitnessImprovement);
                        }
                        OnFitnessImprovement?.Invoke(bestFitness, averageFitness);
                    }

                    totalElapsedEpochs++;
                    return(computationTerminationManager.Terminated((int)sw.ElapsedMilliseconds, totalElapsedEpochs, epochsWithoutFitnessImprovement));
                });

                overallEvaluatedSolutions += bestSolution.EvaluatedSolutions;

                if ((bestSolutionSoFar == null) || (bestSolutionSoFar.Fitness < bestSolution.Fitness))
                {
                    bestSolutionSoFar = bestSolution;
                }
            } while (!computationTerminationManager.Terminated((int)sw.ElapsedMilliseconds, totalElapsedEpochs));

            return(new Solution(this.problem.Items)
            {
                Fitness = bestSolutionSoFar.Fitness,
                Allocations = bestSolutionSoFar.Allocations,
                EvaluatedSolutions = overallEvaluatedSolutions
            });
        }
Example #2
0
 private void SolvingEnvironment_OnFitnessImprovement(double bestFitness, double averageFitness)
 {
     OnFitnessImprovement?.Invoke(bestFitness, averageFitness);
 }