Exemplo n.º 1
0
        protected override double CalculateFitness()
        {
            if (!Genotype.IsValidConfiguration())
            {
                return(0);
            }

            var fitnessAvg = 0.0;
            var runtimeAvg = TimeSpan.Zero;

            for (int i = 0; i < FitnessRuns; i++)
            {
                var stopwatch = new Stopwatch();
                stopwatch.Start();
                fitnessAvg += targetEA.Evolve().Winner.Fitness;
                stopwatch.Stop();
                runtimeAvg += stopwatch.Elapsed;
            }

            fitnessAvg /= FitnessRuns;
            runtimeAvg  = TimeSpan.FromTicks(runtimeAvg.Ticks / FitnessRuns);

            TargetEARuntimeAverage = runtimeAvg;
            TargetEAFitnessAverage = fitnessAvg;

            return(targetEA.EAConfiguration.Mode == EAMode.MaximizeFitness
                ? fitnessAvg / runtimeAvg.TotalSeconds
                : 1 / ((fitnessAvg + 1) * runtimeAvg.TotalSeconds));
        }