public void Init()
        {
            var innovations = new InnovationCollection();

              var neuronGenes = Enumerable.Range(0, 3).Select(i => {
            var inId = innovations.GetInitialNeuronInnovationId(i);
            return new NeuronGene(inId, NeuronType.HiddenNeuron);
              }).ToGeneList();

              var synapseGenes = Enumerable.Range(0, 3).Select(i => {
            var inId = innovations.GetSynapseInnovationId(i, i + 1);
            return new SynapseGene(inId, i, i + 1, true);
              }).ToGeneList();

              var protoGenotype = new Genotype(neuronGenes, synapseGenes);

              var phenotypes = Enumerable.Range(0, 100).Select(_ => {
            var gt = Genotype.FromPrototype(protoGenotype);
            var pt = new Phenotype(gt);
            return pt;
              }).ToArray();

              var distanceMetric = new NEAT.DistanceMetric(0.0f, 0.0f, 35.0f);
              var speciation = new NEAT.Speciation(10, 50.0f, 0.2f, distanceMetric);
              species = speciation.Speciate(new NEAT.Specie[0], phenotypes);
        }
Exemplo n.º 2
0
        public void Init()
        {
            var innovations = new InnovationCollection();

            var neuronGenes = Enumerable.Range(0, 3).Select(i => {
                var inId = innovations.GetInitialNeuronInnovationId(i);
                return(new NeuronGene(inId, NeuronType.HiddenNeuron));
            }).ToGeneList();

            var synapseGenes = Enumerable.Range(0, 3).Select(i => {
                var inId = innovations.GetSynapseInnovationId(i, i + 1);
                return(new SynapseGene(inId, i, i + 1, true));
            }).ToGeneList();

            var protoGenotype = new Genotype(neuronGenes, synapseGenes);

            var phenotypes = Enumerable.Range(0, 100).Select(_ => {
                var gt = Genotype.FromPrototype(protoGenotype);
                var pt = new Phenotype(gt);
                return(pt);
            }).ToArray();

            var distanceMetric = new NEAT.DistanceMetric(0.0f, 0.0f, 35.0f);
            var speciation     = new NEAT.Speciation(10, 50.0f, 0.2f, distanceMetric);

            species = speciation.Speciate(new NEAT.Specie[0], phenotypes);
        }
Exemplo n.º 3
0
        public void Init()
        {
            var innovations = new InnovationCollection();

            var neuronGenes = Enumerable.Range(0, 3).Select(i => {
                var inId = innovations.GetInitialNeuronInnovationId(i);
                return(new NeuronGene(inId, NeuronType.HiddenNeuron));
            }).ToGeneList();

            var synapseGenes = Enumerable.Range(0, 3).Select(i => {
                var inId = innovations.GetSynapseInnovationId(i, i + 1);
                return(new SynapseGene(inId, i, i + 1, true));
            }).ToGeneList();

            protoGenotype = new Genotype(neuronGenes, synapseGenes);
        }
        public void Init()
        {
            var innovations = new InnovationCollection();

              var neuronGenes = Enumerable.Range(0, 3).Select(i => {
            var inId = innovations.GetInitialNeuronInnovationId(i);
            return new NeuronGene(inId, NeuronType.HiddenNeuron);
              }).ToGeneList();

              var synapseGenes = Enumerable.Range(0, 3).Select(i => {
            var inId = innovations.GetSynapseInnovationId(i, i + 1);
            return new SynapseGene(inId, i, i + 1, true);
              }).ToGeneList();

              genotype = new Genotype(neuronGenes, synapseGenes);
        }
        public void Init()
        {
            var innovations = new InnovationCollection();

              var neuronGenes = Enumerable.Range(0, 3).Select(i => {
            var inId = innovations.GetInitialNeuronInnovationId(i);
            return new NeuronGene(inId, NeuronType.HiddenNeuron);
              }).ToGeneList();

              var synapseGenes = Enumerable.Range(0, 3).Select(i => {
            var inId = innovations.GetSynapseInnovationId(i, i + 1);
            return new SynapseGene(inId, i, i + 1, true);
              }).ToGeneList();

              var protoGenotype = new Genotype(neuronGenes, synapseGenes);

              phenotypes = Enumerable.Range(0, 100).Select(_ => {
            var gt = Genotype.FromPrototype(protoGenotype);
            var pt = new Phenotype(gt);
            return pt;
              }).ToArray();
        }
Exemplo n.º 6
0
        public void TestInnovationCollection()
        {
            var innovations = new InnovationCollection();

            // Make sure innovation ids are ascending
            var a = innovations.GetInitialNeuronInnovationId(0);
            var b = innovations.GetInitialNeuronInnovationId(1);
            var c = innovations.GetInitialNeuronInnovationId(2);

            Assert.That(a == 0);
            Assert.That(b == 1);
            Assert.That(c == 2);

            var sA = innovations.GetSynapseInnovationId(a, b);
            var sB = innovations.GetSynapseInnovationId(b, c);
            var sC = innovations.GetSynapseInnovationId(c, a);

            Assert.That(sA == 3);
            Assert.That(sB == 4);
            Assert.That(sC == 5);

            var d = innovations.GetNeuronInnovationId(a, b, sA);
            var e = innovations.GetNeuronInnovationId(b, c, sB);
            var f = innovations.GetNeuronInnovationId(c, d, sC);

            Assert.That(d == 6);
            Assert.That(e == 8);
            Assert.That(f == 10);

            // Make sure duplicate innovations receive their original innovation id
            var a0 = innovations.GetInitialNeuronInnovationId(0);
            var b0 = innovations.GetInitialNeuronInnovationId(1);
            var c0 = innovations.GetInitialNeuronInnovationId(2);

            Assert.That(a0 == 0);
            Assert.That(b0 == 1);
            Assert.That(c0 == 2);

            var sA0 = innovations.GetSynapseInnovationId(a0, b0);
            var sB0 = innovations.GetSynapseInnovationId(b0, c0);
            var sC0 = innovations.GetSynapseInnovationId(c0, a0);

            Assert.That(sA0 == 3);
            Assert.That(sB0 == 4);
            Assert.That(sC0 == 5);

            var d0 = innovations.GetNeuronInnovationId(a0, b0, sA0);
            var e0 = innovations.GetNeuronInnovationId(b0, c0, sB0);
            var f0 = innovations.GetNeuronInnovationId(c0, d0, sC0);

            Assert.That(d0 == 6);
            Assert.That(e0 == 8);
            Assert.That(f0 == 10);

            // Test prune
            var genotypes = Enumerable.Range(0, 3).Select(_0 => {
                var neuronGeneA  = new NeuronGene(a, NeuronType.HiddenNeuron);
                var neuronGeneB  = new NeuronGene(b, NeuronType.HiddenNeuron);
                var synapseGeneA = new SynapseGene(sA, a, b, true);
                var synapseGeneB = new SynapseGene(sB, b, c, true);
                var neuronGenes  = new [] { neuronGeneA, neuronGeneB }.ToGeneList();
                var synapseGenes = new [] { synapseGeneA, synapseGeneB }.ToGeneList();
                return(new Genotype(neuronGenes, synapseGenes));
            }).ToList();

            var prunedCount = innovations.Prune(genotypes);

            Assert.That(innovations.Count == 4);
            Assert.That(prunedCount == 5);
        }
        public void TestInnovationCollection()
        {
            var innovations = new InnovationCollection();

              // Make sure innovation ids are ascending
              var a = innovations.GetInitialNeuronInnovationId(0);
              var b = innovations.GetInitialNeuronInnovationId(1);
              var c = innovations.GetInitialNeuronInnovationId(2);
              Assert.That(a == 0);
              Assert.That(b == 1);
              Assert.That(c == 2);

              var sA = innovations.GetSynapseInnovationId(a, b);
              var sB = innovations.GetSynapseInnovationId(b, c);
              var sC = innovations.GetSynapseInnovationId(c, a);
              Assert.That(sA == 3);
              Assert.That(sB == 4);
              Assert.That(sC == 5);

              var d = innovations.GetNeuronInnovationId(a, b, sA);
              var e = innovations.GetNeuronInnovationId(b, c, sB);
              var f = innovations.GetNeuronInnovationId(c, d, sC);
              Assert.That(d == 6);
              Assert.That(e == 8);
              Assert.That(f == 10);

              // Make sure duplicate innovations receive their original innovation id
              var a0 = innovations.GetInitialNeuronInnovationId(0);
              var b0 = innovations.GetInitialNeuronInnovationId(1);
              var c0 = innovations.GetInitialNeuronInnovationId(2);
              Assert.That(a0 == 0);
              Assert.That(b0 == 1);
              Assert.That(c0 == 2);

              var sA0 = innovations.GetSynapseInnovationId(a0, b0);
              var sB0 = innovations.GetSynapseInnovationId(b0, c0);
              var sC0 = innovations.GetSynapseInnovationId(c0, a0);
              Assert.That(sA0 == 3);
              Assert.That(sB0 == 4);
              Assert.That(sC0 == 5);

              var d0 = innovations.GetNeuronInnovationId(a0, b0, sA0);
              var e0 = innovations.GetNeuronInnovationId(b0, c0, sB0);
              var f0 = innovations.GetNeuronInnovationId(c0, d0, sC0);
              Assert.That(d0 == 6);
              Assert.That(e0 == 8);
              Assert.That(f0 == 10);

              // Test prune
              var genotypes = Enumerable.Range(0, 3).Select(_0 => {
            var neuronGeneA = new NeuronGene(a, NeuronType.HiddenNeuron);
            var neuronGeneB = new NeuronGene(b, NeuronType.HiddenNeuron);
            var synapseGeneA = new SynapseGene(sA, a, b, true);
            var synapseGeneB = new SynapseGene(sB, b, c, true);
            var neuronGenes = new []{neuronGeneA, neuronGeneB}.ToGeneList();
            var synapseGenes = new []{synapseGeneA, synapseGeneB}.ToGeneList();
            return new Genotype(neuronGenes, synapseGenes);
              }).ToList();

              var prunedCount = innovations.Prune(genotypes);
              Assert.That(innovations.Count == 4);
              Assert.That(prunedCount == 5);
        }
    IEnumerator Start()
    {
        var logPath = string.Format("logs_{0}", DateTime.Now.Ticks);
        Debug.LogFormat("Logging to {0}", logPath);

        if (!Directory.Exists(logPath)) {
          Directory.CreateDirectory(logPath);
        }

        elitesLog = File.CreateText(Path.Combine(logPath, "elites.csv"));
        populationLog = File.CreateText(Path.Combine(logPath, "populations.csv"));
        generationLog = File.CreateText(Path.Combine(logPath, "generations.csv"));
        speciesLog = File.CreateText(Path.Combine(logPath, "species.csv"));

        var populationSize = 100;
        var innovations = new InnovationCollection();

        var mutations = new MutationCollection();
        mutations.Add(0.01f, new AddNeuronMutator(innovations)); // 0.1%
        mutations.Add(0.05f, new AddSynapseMutator(innovations)); // 1%
        mutations.Add(0.05f, new ToggleSynapseMutator(0.125f));
        mutations.Add(0.20f, new PerturbNeuronMutator(0.5f, 0.25f)); // 98% vvv
        mutations.Add(0.20f, new PerturbSynapseMutator(0.5f, 0.25f));
        mutations.Add(0.20f, new ReplaceNeuronMutator(0.5f));
        mutations.Add(0.20f, new ReplaceSynapseMutator(0.5f));

        var eliteSelector = new EliteSelector();

        var crossover = new MultipointCrossover();
        var offspringSelector = new OffspringSelector(crossover);

        var distanceMetric = new DistanceMetric(2.0f, 2.0f, 1.0f);
        var speciation = new Speciation(10, 6.0f, 0.3f, distanceMetric);

        var neuronGenes = new []{
          new NeuronGene(innovations.GetInitialNeuronInnovationId(0), NeuronType.UpperNeuron),
          new NeuronGene(innovations.GetInitialNeuronInnovationId(1), NeuronType.LowerNeuron),
          new NeuronGene(innovations.GetInitialNeuronInnovationId(2), NeuronType.PositionNeuron),
          new NeuronGene(innovations.GetInitialNeuronInnovationId(3), NeuronType.SpeedNeuron),
        }.ToGeneList();
        var synapseGenes = new GeneList<SynapseGene>();
        var protoGenotype = new Genotype(neuronGenes, synapseGenes);

        var genotypes = new GenotypeStream(protoGenotype)
          .Take(populationSize).ToArray();

        var species = new Specie[0];
        var generation = 0;

        var elitePhenotypes = new List<Phenotype>();
        var offspringPhenotypes = genotypes.Select(gt => new Phenotype(gt)).ToList();

        while (true) {
          yield return StartCoroutine(EvaluatePopulation(offspringPhenotypes));

          var phenotypes = new List<Phenotype>(elitePhenotypes.Count + offspringPhenotypes.Count);
          phenotypes.AddRange(elitePhenotypes);
          phenotypes.AddRange(offspringPhenotypes);

          Assert.AreEqual(phenotypes.Count, populationSize,
        "Population size must remain constant");

          var longest = phenotypes.OrderByDescending(pt => pt.BestDuration).First();
          Debug.LogFormat("[{0}] Fitness: {1}, Duration: {2}s ({3}, {4}) (Longest)",
        generation, longest.MeanFitness, longest.MeanDuration,
        longest.Genotype.NeuronCount,
        longest.Genotype.SynapseCount);

          var best = phenotypes.OrderByDescending(pt => pt.Fitness).First();
          Debug.LogFormat("[{0}] Fitness: {1}, Duration: {2}s ({3}, {4}) (Best)",
        generation, best.MeanFitness, best.MeanDuration,
        best.Genotype.NeuronCount,
        best.Genotype.SynapseCount);

          elitesLog.WriteLine(string.Join(",", new string[]{
        generation.ToString(),
        best.Fitness.ToString(),
        best.BestDuration.ToString(),
        JSON.Serialize(best.Genotype.ToJSON()),
          }));

          var meanComplexity = phenotypes.Aggregate(0.0f,
        (sum, pt) => sum + (float)pt.Genotype.Complexity,
        (sum) => sum / (float)phenotypes.Count);

          var meanNeuronCount = phenotypes.Aggregate(0.0f,
        (sum, pt) => sum + (float)pt.Genotype.NeuronCount,
        (sum) => sum / (float)phenotypes.Count);

          var meanSynapseCount = phenotypes.Aggregate(0.0f,
        (sum, pt) => sum + (float)pt.Genotype.SynapseCount,
        (sum) => sum / (float)phenotypes.Count);

          var meanFitness = phenotypes.Aggregate(0.0f,
        (sum, pt) => sum + (float)pt.Fitness,
        (sum) => sum / (float)phenotypes.Count);

          var stdevFitness = phenotypes.Aggregate(0.0f,
        (sum, pt) => sum + Mathf.Pow(pt.Fitness - meanFitness, 2.0f),
        (sum) => Mathf.Sqrt(sum / (float)phenotypes.Count));

          var stdevComplexity = phenotypes.Aggregate(0.0f,
        (sum, pt) => sum + Mathf.Pow((float)pt.Genotype.Complexity - meanComplexity, 2.0f),
        (sum) => Mathf.Sqrt(sum / (float)phenotypes.Count));

          var stdevNeuronCount = phenotypes.Aggregate(0.0f,
        (sum, pt) => sum + Mathf.Pow((float)pt.Genotype.NeuronCount - meanNeuronCount, 2.0f),
        (sum) => Mathf.Sqrt(sum / (float)phenotypes.Count));

          var stdevSynapseCount = phenotypes.Aggregate(0.0f,
        (sum, pt) => sum + Mathf.Pow((float)pt.Genotype.SynapseCount - meanSynapseCount, 2.0f),
        (sum) => Mathf.Sqrt(sum / (float)phenotypes.Count));

          species = speciation.Speciate(species, phenotypes.ToArray());

          Debug.LogFormat("[{0}] Species Count: {1} (Threshold: {2})",
        generation, species.Length, speciation.DistanceThreshold);

          var meanAdjustedFitness = phenotypes.Aggregate(0.0f,
        (sum, pt) => sum + (float)pt.AdjustedFitness,
        (sum) => sum / (float)phenotypes.Count);

          // standard deviation:
          // take the square root of the average of the squared deviations of the values from their average value
          var stdevAdjustedFitness = phenotypes.Aggregate(0.0f,
        (sum, pt) => sum + Mathf.Pow(pt.AdjustedFitness - meanAdjustedFitness, 2.0f),
        (sum) => Mathf.Sqrt(sum / (float)phenotypes.Count));

          generationLog.WriteLine(new []{
        generation,
        best.Fitness,
        best.MeanDuration,
        meanAdjustedFitness, stdevAdjustedFitness,
        meanFitness, stdevFitness,
        meanComplexity, stdevComplexity,
        meanNeuronCount, stdevNeuronCount,
        meanSynapseCount, stdevSynapseCount
          }.Stringify());

          foreach (var sp in species) {
        speciesLog.WriteLine(new []{
          generation,
          sp.SpeciesId, sp.Count,
          sp.BestFitness,
          sp.MeanFitness,
          sp.MeanAdjustedFitness,
          sp.MeanComplexity,
        }.Stringify());

        foreach (var pt in sp) {
          populationLog.WriteLine(new []{
            generation,
            sp.SpeciesId,
            pt.Fitness,
            pt.AdjustedFitness,
            pt.BestDuration,
            pt.Genotype.Complexity,
            pt.Genotype.NeuronCount,
            pt.Genotype.SynapseCount,
          }.Stringify());
        }
          }

          var eliteCount = species.Length;
          elitePhenotypes = eliteSelector.Select(species, eliteCount);

          var offspringCount = populationSize - elitePhenotypes.Count;
          var offspringGenotypes = offspringSelector.Select(species, offspringCount);

          var mutationResults = mutations.Mutate(offspringGenotypes);
          Debug.LogFormat("[{0}] Mutations: Added Neurons: {1}, Added Synapses: {2}, Perturbed Neurons: {3}, Perturbed Synapses: {4}, Replaced Neurons: {5}, Replaced Synapses: {6}, Toggled Synapses: {7}, Pruned Synapses: {8}, Orphaned Neurons: {9}",
        generation,
        mutationResults.addedNeurons,
        mutationResults.addedSynapses,
        mutationResults.perturbedNeurons,
        mutationResults.perturbedSynapses,
        mutationResults.replacedNeurons,
        mutationResults.replacedSynapses,
        mutationResults.toggledSynapses,
        mutationResults.prunedSynapses,
        mutationResults.orphanedNeurons);

          offspringPhenotypes = offspringGenotypes.Select(gt => new Phenotype(gt)).ToList();

          generation++;

          // Flush these so we can preview results while it runs
          generationLog.Flush();
          populationLog.Flush();
          speciesLog.Flush();
        }
    }