Beispiel #1
0
 public SpeciesFitness(float fitness, GenomeWrapper bestMember)
 {
     Fitness    = fitness;
     BestMember = bestMember;
 }
Beispiel #2
0
        public void Evaluate()
        {
            species.Clear();
            HighestScore  = float.MinValue;
            FittestGenome = null;

            foreach (var g in Genomes)
            {
                var foundSpecies = false;
                foreach (var s in species.Where(
                             s => Genome.CalculateCompatibilityDistance(g.Genome, s.Mascot.Genome, C1, C2, C3) < DT))
                {
                    s.Members.Add(g);
                    g.Species    = s;
                    foundSpecies = true;
                    break;
                }

                if (!foundSpecies)
                {
                    var newSpecies = new Species(g);
                    species.Add(newSpecies);
                    g.Species = newSpecies;
                }

                var score = EvaluateGenome(g.Genome);
                g.Fitness = g.Fitness * 0.5f + score;

                if (!(HighestScore <= score))
                {
                    continue;
                }
                HighestScore = score;
                if (FittestGenome != null)
                {
                    FittestGenome.Best = false;
                }
                FittestGenome = g;
                g.Best        = true;
            }

            species.RemoveAll(s => s.Members.Count == 0);

            if (species.Count > populationSize / 5)
            {
                var orderedSpecies = species.OrderBy(s => s.CalculateSpeciesFitness().Fitness).ToArray();
                var speciesIndex   = 0;
                while (species.Count > populationSize / 5)
                {
                    species.Remove(orderedSpecies[speciesIndex]);
                    speciesIndex++;
                }
            }

            Genomes.Clear();

            foreach (var speciesFitness in species.Select(s => s.CalculateSpeciesFitness())
                     .Where(f => !f.BestMember.Best))
            {
                Genomes.Add(new GenomeWrapper(new Genome(speciesFitness.BestMember.Genome)));
            }
            Genomes.Add(new GenomeWrapper(new Genome(FittestGenome.Genome))
            {
                Best = true
            });

            while (Genomes.Count < populationSize)
            {
                var s = GetRandomSpecies();

                var p1 = GetRandomGenomeInSpecies(s);
                var p2 = GetRandomGenomeInSpecies(s);

                var mostFitParent  = p1.Fitness > p2.Fitness ? p1 : p2;
                var leastFitParent = p1.Fitness > p2.Fitness ? p2 : p1;

                var child = Genome.Crossover(mostFitParent.Genome, leastFitParent.Genome);

                if (RandomnessHandler.RandomZeroToOne() < WeightMutationRate)
                {
                    child.WeightMutation();
                }
                if (RandomnessHandler.RandomZeroToOne() < AddConnectionRate)
                {
                    child.AddConnectionMutation(connectionInnovation, ConnectionMutationMaxAttempts);
                }
                if (RandomnessHandler.RandomZeroToOne() < ToggleConnectionRate)
                {
                    child.ToggleConnectionMutation();
                }
                if (RandomnessHandler.RandomZeroToOne() < AddNodeRate)
                {
                    child.AddNodeMutation(nodeInnovation, connectionInnovation);
                }

                Genomes.Add(new GenomeWrapper(child)
                {
                    Fitness = leastFitParent.Fitness
                });
            }
        }
Beispiel #3
0
 public void Reset()
 {
     Mascot = Members[RandomnessHandler.Random.Next(Members.Count)];
     Members.Clear();
     LastCalculatedFitness = null;
 }