Example #1
0
        public void HandlePopulationLevelStagnationTest_SingleSpecies()
        {
            UnitTests.RandomStub randomStub = new UnitTests.RandomStub();
            Simulation           sim        = new Simulation(randomStub, gen, 20);

            // Create some fake species and genomes
            Species species1 = new Species(randomStub);

            Genome gen1 = new Genome(randomStub);

            gen1.OriginalFitness = 10;
            species1.AddGenome(gen1);

            // Remove default species/genomes and add fake ones
            sim.Species.Clear();
            sim.Species.Add(species1);

            sim.OrderSpeciesByOriginalFitness();
            sim.HandlePopulationLevelStagnation();

            Assert.AreEqual(20, species1.Offspring);
            Assert.AreEqual(20, species1.ChampionOffspring);

            Assert.AreEqual(0, sim.GenerationsSinceLastUpdate);
        }
Example #2
0
        public void OrderSpeciesTest_SameOrder(int fitness1, int fitness2, int fitness3)
        {
            Simulation sim = new Simulation(r, gen, 1);

            // Create some fake species and genomes
            Species species1 = new Species(r);
            Species species2 = new Species(r);
            Species species3 = new Species(r);

            Genome gen1 = new Genome(r);

            gen1.OriginalFitness = fitness1;
            species1.AddGenome(gen1);

            Genome gen2 = new Genome(r);

            gen2.OriginalFitness = fitness2;
            species2.AddGenome(gen2);

            Genome gen3 = new Genome(r);

            gen3.OriginalFitness = fitness3;
            species3.AddGenome(gen3);

            // Remove default species/genomes and add fake ones
            sim.Species.Clear();
            sim.Species.AddRange(new Species[] { species1, species2, species3 });


            sim.OrderSpeciesByOriginalFitness();

            Assert.IsTrue(sim.Species[0] == species1);
            Assert.IsTrue(sim.Species[1] == species2);
            Assert.IsTrue(sim.Species[2] == species3);
        }
Example #3
0
 /// <summary>
 /// Pushes Genomes into Species based on Compatibility-Distance
 /// </summary>
 private void Speciate()
 {
     // Each existing species is represented by a random genome inside
     // the species from the previous generation.
     //
     // A given genome g in the current generation is placed in the first species
     // in which g is compatible with the representative genome of that species.
     // This way, species do not overlap.
     //
     // If g is not compatible with any existing species,
     // a new species is created with g as its representative.
     for (int i = 0; i < species.Count; i++)
     {
         species[i].Clear();                    // Clear previous Species and set new Mascots from previous Generation
     }
     for (int i = 0; i < population.Count; i++) // For each Genome in Population
     {
         Genome g = population[i];
         bool   addedToSpecies = false;
         for (int j = 0; j < species.Count; j++) // Loop through Species (in order) to find first closest match
         {
             Species s = species[j];
             if (Functions.CompatibilityDistance(g, s.Mascot, c1, c2, c3) < compatibilityThreshold)
             {
                 s.AddGenome(g); // Add to Speciest
                 addedToSpecies = true;
                 break;          // Break from Species-Loop
             }
         }
         if (!addedToSpecies) // Create new Species with g as Mascot
         {
             Species s = new Species(g);
             s.AddGenome(g); // Add g as member
             species.Add(s);
         }
     }
     // FitnessSharing: f'(i) = f(i) / Species.Count (reduced calculation due to speciation)
     for (int i = 0; i < species.Count; i++) // For Each Species
     {
         Species species1 = species[i];
         if (species1.Count > 1)                              // If Species has more than 1 member
         {
             for (int j = 0; j < species1.Genomes.Count; j++) // For Each Genomes
             {
                 Genome g = species1[j];
                 g.SetFitness(g.Fitness / species1.Count); // Update Fitness
             }
         }
     }
 }
Example #4
0
        public void HandlePopulationLevelStagnationTest_OddPopulationSize()
        {
            UnitTests.RandomStub randomStub = new UnitTests.RandomStub();
            Simulation           sim        = new Simulation(randomStub, gen, 21);

            // Create some fake species and genomes
            Species species1 = new Species(randomStub);
            Species species2 = new Species(randomStub);
            Species species3 = new Species(randomStub);
            Species species4 = new Species(randomStub);

            Genome gen1 = new Genome(randomStub);

            gen1.OriginalFitness = 10;
            species1.AddGenome(gen1);

            Genome gen2 = new Genome(randomStub);

            gen2.OriginalFitness = 0;
            species2.AddGenome(gen2);

            Genome gen3 = new Genome(randomStub);

            gen3.OriginalFitness = 20;
            species3.AddGenome(gen3);

            Genome gen4 = new Genome(randomStub);

            gen4.OriginalFitness = 1;
            species4.AddGenome(gen4);

            // Remove default species/genomes and add fake ones
            sim.Species.Clear();
            sim.Species.AddRange(new Species[] { species1, species2, species3, species4 });

            sim.OrderSpeciesByOriginalFitness();
            sim.HandlePopulationLevelStagnation();

            Assert.AreEqual(10, species1.Offspring);
            Assert.AreEqual(10, species1.ChampionOffspring);

            Assert.AreEqual(0, species2.Offspring);

            Assert.AreEqual(11, species3.Offspring);
            Assert.AreEqual(11, species3.ChampionOffspring);

            Assert.AreEqual(0, species4.Offspring);

            Assert.AreEqual(0, sim.GenerationsSinceLastUpdate);
        }
Example #5
0
        public void CalculateSpeciesOffspringTest_TotalFitnessGreaterThanZero()
        {
            UnitTests.RandomStub randomStub = new UnitTests.RandomStub();
            Simulation           sim        = new Simulation(randomStub, gen, 21);

            // Create some fake species and genomes
            Species species1 = new Species(randomStub);
            Species species2 = new Species(randomStub);
            Species species3 = new Species(randomStub);
            Species species4 = new Species(randomStub);

            Genome gen1 = new Genome(randomStub);

            gen1.OriginalFitness = 5;
            gen1.Fitness         = 5;
            species1.AddGenome(gen1);

            Genome gen2 = new Genome(randomStub);

            gen2.OriginalFitness = 10;
            gen2.Fitness         = 10;
            species2.AddGenome(gen2);

            Genome gen3 = new Genome(randomStub);

            gen3.OriginalFitness = 15;
            gen3.Fitness         = 15;
            species3.AddGenome(gen3);

            Genome gen4 = new Genome(randomStub);

            gen4.OriginalFitness = 20;
            gen4.Fitness         = 20;
            species4.AddGenome(gen4);

            // Remove default species/genomes and add fake ones
            sim.Species.Clear();
            sim.Species.AddRange(new Species[] { species1, species2, species3, species4 });

            sim.OrderSpeciesByOriginalFitness();
            sim.CalculateSpeciesOffspring(50.0f);

            Assert.AreEqual(2, species1.Offspring);
            Assert.AreEqual(4, species2.Offspring);
            Assert.AreEqual(6, species3.Offspring);
            Assert.AreEqual(9, species4.Offspring);
        }
Example #6
0
        public void CalculateSpeciesOffspringTest_TotalFitnessZero_Expected_AllSpeciesHaveSameOffspring_ButOne()
        {
            UnitTests.RandomStub randomStub = new UnitTests.RandomStub();
            Simulation           sim        = new Simulation(randomStub, gen, 22);

            // Create some fake species and genomes
            Species species1 = new Species(randomStub);
            Species species2 = new Species(randomStub);
            Species species3 = new Species(randomStub);
            Species species4 = new Species(randomStub);

            Genome gen1 = new Genome(randomStub);

            gen1.OriginalFitness = 10;
            species1.AddGenome(gen1);

            Genome gen2 = new Genome(randomStub);

            gen2.OriginalFitness = 0;
            species2.AddGenome(gen2);

            Genome gen3 = new Genome(randomStub);

            gen3.OriginalFitness = 20;
            species3.AddGenome(gen3);

            Genome gen4 = new Genome(randomStub);

            gen4.OriginalFitness = 1;
            species4.AddGenome(gen4);

            // Remove default species/genomes and add fake ones
            sim.Species.Clear();
            sim.Species.AddRange(new Species[] { species1, species2, species3, species4 });

            sim.OrderSpeciesByOriginalFitness();
            sim.CalculateSpeciesOffspring(0.0f);

            Assert.AreEqual(7, sim.Species[0].Offspring);
            for (int i = 1; i < sim.Species.Count; ++i)
            {
                Assert.AreEqual(5, sim.Species[i].Offspring);
            }
        }
Example #7
0
        public void ObliterateWorstSpeciesTest()
        {
            Simulation sim = new Simulation(r, gen, 1);

            // Create some fake species and genomes
            Species species1 = new Species(r);
            Species species2 = new Species(r);
            Species species3 = new Species(r);

            Genome gen1 = new Genome(r);

            gen1.OriginalFitness = 0;
            species1.AddGenome(gen1);
            species1.Age = 14;

            Genome gen2 = new Genome(r);

            gen2.OriginalFitness = 0;
            species2.AddGenome(gen2);
            species2.Age = 30;

            Genome gen3 = new Genome(r);

            gen3.OriginalFitness = 2;
            species3.AddGenome(gen3);
            species3.Age = 30;

            // Remove default species/genomes and add fake ones
            sim.Species.Clear();
            sim.Species.AddRange(new Species[] { species1, species2, species3 });

            sim.OrderSpeciesByOriginalFitness();

            sim.EpochID = 30;

            sim.PenalizeNonImprovingSpecies();

            Assert.AreEqual(false, species1.ShouldBePenalized);
            Assert.AreEqual(true, species2.ShouldBePenalized);
            Assert.AreEqual(false, species3.ShouldBePenalized);
        }
Example #8
0
        public void RemoveEmptySpeciesTest()
        {
            Simulation sim = new Simulation(r, gen, 1);

            // Create some fake species and genomes
            Species species1 = new Species(r);
            Species species2 = new Species(r);
            Species species3 = new Species(r);
            Species species4 = new Species(r);

            Genome gen1 = new Genome(r);

            gen1.OriginalFitness = 0;
            species1.AddGenome(gen1);
            species1.Age = 14;

            Genome gen2 = new Genome(r);

            gen2.OriginalFitness = 0;
            species2.AddGenome(gen2);
            species2.Age = 30;

            Genome gen3 = new Genome(r);

            gen3.OriginalFitness = 2;
            species3.AddGenome(gen3);
            species3.Age = 30;

            // Remove default species/genomes and add fake ones
            sim.Species.Clear();
            sim.Species.AddRange(new Species[] { species1, species2, species3, species4 });

            Assert.AreEqual(4, sim.Species.Count);
            sim.RemoveEmptySpecies();
            Assert.AreEqual(3, sim.Species.Count);

            Assert.AreEqual(species1, sim.Species[0]);
            Assert.AreEqual(species2, sim.Species[1]);
            Assert.AreEqual(species3, sim.Species[2]);
        }
Example #9
0
        public Generation MakeFirstGeneration(InnovationGenerator generator, InnovationGenerator genomeGenerator,
                                              int initialPopulationSize, int selectionAlgorithm, int reproductionsPerGenome, int nBest)
        {
            // check if there is a Lua implementation of this
            LuaFunction func = LuaState["MakeFirstGeneration"] as LuaFunction;

            if (func != null)
            {
                try
                {
                    return((Generation)func.Call(generator, initialPopulationSize)?.First());
                }
                catch (Exception e)
                {
                    logger.Error(e, "Error running lua code for first generation.");
                }
            }

            Generation generation = new Generation(0, selectionAlgorithm, reproductionsPerGenome, nBest);
            Genome     genome     = new Genome(0);

            int inputs  = Data.Constants.NetworkInputs;
            int outputs = Data.Constants.NetworkOutputs;

            List <int> inputIds = new List <int>(inputs);

            // input nodes
            for (int i = 0; i < inputs; i++)
            {
                int currentId = generator.Innovate();
                inputIds.Add(currentId);
                genome.AddNode(new Node(currentId, NodeType.Input, int.MinValue));
            }

            // output nodes
            for (int i = 0; i < outputs; i++)
            {
                int currentId = generator.Innovate();
                genome.AddNode(new Node(currentId, NodeType.Output, int.MaxValue));

                foreach (int hiddenId in inputIds)
                {
                    genome.AddConnection(new Connection(hiddenId, currentId, Double() * 4f - 2f, true, generator.Innovate()));
                }
            }

            Species original = new Species(genome);

            generation.Species.Add(original);

            for (int i = 0; i < initialPopulationSize; i++)
            {
                Genome g = genome.Copy();
                g.Mutate(generator);
                g.Id = genomeGenerator.Innovate();

                original.AddGenome(g);
            }

            return(generation);
        }