Exemplo n.º 1
0
    public static Genome Crossover(Genome parent1, Genome parent2, Random r)      //Creates a child genome from two parent genomes. Parent 1 has higher fitness.
    {
        Genome child = new Genome();

        List <Genome.NodeGene> parent1nodes = parent1.GetNodes();
        Dictionary <int, Genome.ConnectionGene> parent1connections = parent1.GetConnections();
        Dictionary <int, Genome.ConnectionGene> parent2connections = parent2.GetConnections();

        foreach (Genome.NodeGene p1node in parent1nodes)
        {
            child.AddNode(p1node);
        }

        foreach (Genome.ConnectionGene p1con in parent1connections.Values)
        {
            if (parent2connections.ContainsKey(p1con.GetInnovation()))
            {
                child.AddConnection(r.Next(100) < 50 ? p1con : parent2connections[p1con.GetInnovation()]);
            }
            else
            {
                child.AddConnection(p1con);
            }
        }

        return(child);
    }
Exemplo n.º 2
0
    private void Start()
    {
        for (int i = 0; i < 2; i++)
        {
            Node node = new Node(Node.NodeType.INPUT, i + 1);
            genome.AddNode(node);
        }

        genome.AddNode(new Node(Node.NodeType.OUTPUT, 3));
        genome.AddNode(new Node(Node.NodeType.HIDDEN, 4));


        genome.AddConnection(new Connection(1, 4, 0.2f, true, 1));
        genome.AddConnection(new Connection(2, 4, 0.4f, true, 2));
        genome.AddConnection(new Connection(4, 3, 1f, true, 3));
        genome.AddConnection(new Connection(1, 3, -0.3f, true, 4));

        History.SetInnovationDebug(4);

        /*
         * for (int i = 0; i < 30; i++)
         * {
         *  genome.Mutate();
         * }
         *
         */

        ForwardProp();
        GetComponent <GenomePrinter>().Draw(genome);
    }
Exemplo n.º 3
0
    public void Start()
    {
        vision.Add(0);
        vision.Add(1);
        vision.Add(2);
        for (int i = 0; i < 3; i++)
        {
            Node node = new Node(Node.NodeType.INPUT, i + 1);
            genome.AddNode(node);
        }
        genome.AddNode(new Node(Node.NodeType.OUTPUT, 4));
        genome.AddNode(new Node(Node.NodeType.OUTPUT, 5));
        genome.AddNode(new Node(Node.NodeType.OUTPUT, 6));
        //genome.AddNode(new Node(Node.NodeType.HIDDEN, 5));
        //genome.AddNode(new Node(Node.NodeType.HIDDEN, 6));


        genome.AddConnection(new Connection(1, 4, 0f, true, 1));
        genome.AddConnection(new Connection(2, 5, 0f, true, 2));
        genome.AddConnection(new Connection(3, 6, 0f, true, 3));

        foreach (Connection c in genome.Connections.Values)
        {
            History.AddConnectionToInnovationHistoryDebug(c);
        }

        gameObject.GetComponent <GenomePrinter>().Draw(genome);
        Genome.DebugPrint(genome);

        History.SetInnovationDebug(3);
    }
Exemplo n.º 4
0
        public void AddConnectionsOneByOneFeedforward()
        {
            // ReSharper disable once RedundantArgumentDefaultValue
            var parameters = new NetworkParameters(3, 1, NetworkType.FeedForward)
            {
                InitialConnectionDensity = 1f
            };
            var tracker =
                new InnovationTracker(NetworkParameters.BiasCount + parameters.SensorCount + parameters.EffectorCount);

            var genome             = new Genome(parameters, tracker, false);
            var currentConnections = new List <string>
            {
                "0b->4e", "1s->4e", "2s->4e", "3s->4e"
            };

            AssertCurrentConnections(genome, currentConnections);

            genome.SplitConnection(0); // 0 -> 4 into 0 -> 5 -> 4
            currentConnections.ChangeCollection(new[] { "0b->4e" }, new[] { "0b->5h", "5h->4e" });
            AssertCurrentConnections(genome, currentConnections);

            genome.SplitConnection(4); // 0 -> 5 -> 4 into 0 -> 6 -> 5 -> 4
            currentConnections.ChangeCollection(new[] { "0b->5h" }, new[] { "0b->6h", "6h->5h" });
            AssertCurrentConnections(genome, currentConnections);

            genome.SplitConnection(1); // 2 -> 4 into 2 -> 7 -> 4
            currentConnections.ChangeCollection(new[] { "2s->4e" }, new[] { "2s->7h", "7h->4e" });
            AssertCurrentConnections(genome, currentConnections);

            var initialLayers     = genome.NetworkTopology.LayerRanges.ToArray();
            var initialLinksCount = genome.NetworkTopology.Links.Sum(l => l.Count);

            genome.AddConnection(15); // 5h->7h
            AssertNoChanges();

            genome.AddConnection(6); // 7h->5h
            AssertNoChanges();

            genome.AddConnection(9); // 5h->6h
            AssertNoChanges();

            genome.AddConnection(9); // 7h->6h
            AssertNoChanges();


            genome.AddConnection(12); // 6h->7h
            genome.NetworkTopology.Links.Sum(l => l.Count).Should().Be(initialLinksCount + 1, "added allowed connection");
            genome.NetworkTopology.LayerRanges.Should().BeEquivalentTo(initialLayers, "expected no changes in layer structure");


            void AssertNoChanges()
            {
                genome.NetworkTopology.Links.Sum(l => l.Count).Should().Be(initialLinksCount, "expected no changes after adding recurrent connections");
                genome.NetworkTopology.LayerRanges.Should().BeEquivalentTo(initialLayers, "expected no changes after adding recurrent connections");
            }
        }
Exemplo n.º 5
0
        public void SanityCheck()
        {
            // Arrange
            var parameters = new NetworkParameters(3, 1)
            {
                InitialConnectionDensity = 1f
            };
            var tracker =
                new InnovationTracker(NetworkParameters.BiasCount + parameters.SensorCount + parameters.EffectorCount);

            var genome = new Genome(parameters, tracker, false);

            // Act

            // Assert
            Assert.Equal(0, genome.VacantConnectionCount);

            // Act
            genome.RemoveConnection(1);

            // Assert
            Assert.Equal(1, genome.VacantConnectionCount);

            // Act
            genome.AddConnection(0);

            // Assert
            Assert.Equal(0, genome.VacantConnectionCount);
        }
Exemplo n.º 6
0
    public void Initialize(int input, int output)
    {
        Genome genome = new Genome();

        for (int i = 0; i < input; i++)
        {
            Node node = new Node(Node.NodeType.INPUT, i + 1);
            genome.AddNode(node);
        }
        for (int i = input; i < output + input; i++)
        {
            Node node = new Node(Node.NodeType.OUTPUT, i + 1);
            genome.AddNode(node);
        }

        int inno = 0;

        for (int i = 1; i <= input; i++)
        {
            for (int j = input + 1; j <= output + input; j++)
            {
                genome.AddConnection(new Connection(i, j, 0f, true, ++inno));
            }
        }

        _brain       = genome;
        _initialized = true;
    }
Exemplo n.º 7
0
        public void CheckLinksAmbiguityHandling()
        {
            // Arrange
            var parameters = new NetworkParameters(3, 1)
            {
                InitialConnectionDensity = 1f
            };
            var tracker =
                new InnovationTracker(NetworkParameters.BiasCount + parameters.SensorCount + parameters.EffectorCount);

            var first  = new Genome(parameters, tracker, false);
            var second = new Genome(parameters, tracker, false);

            // Act
            Assert.True(first.NeatChromosome[1].SourceId == 1 && first.NeatChromosome[1].TargetId == 4);
            Assert.True(second.NeatChromosome[1].SourceId == 1 && second.NeatChromosome[1].TargetId == 4);
            first.SplitConnection(1);
            second.SplitConnection(1);
            while (second.VacantConnectionCount > 0) // make fully connected network to ensure our link is there
            {
                second.AddConnection(0);
            }

            // Assert
            var linkGene = Assert.Single(second.NeatChromosome, gene => gene.SourceId == 1 && gene.TargetId == 4);

            Assert.DoesNotContain(first.NeatChromosome, gene => gene.Id == linkGene.Id);
        }
Exemplo n.º 8
0
    public void Start()
    {
        Genome parent1 = new Genome();

        for (int i = 0; i < 3; i++)
        {
            Node node = new Node(Node.NodeType.INPUT, i + 1);
            parent1.AddNodeCopy(node);
        }
        parent1.AddNodeCopy(new Node(Node.NodeType.OUTPUT, 4));
        parent1.AddNodeCopy(new Node(Node.NodeType.HIDDEN, 5));


        parent1.AddConnection(new Connection(1, 4, 1f, true, 1));
        parent1.AddConnection(new Connection(2, 4, 1f, false, 2));
        parent1.AddConnection(new Connection(3, 4, 1f, true, 3));
        parent1.AddConnection(new Connection(2, 5, 1f, true, 4));
        parent1.AddConnection(new Connection(5, 4, 1f, true, 5));
        parent1.AddConnection(new Connection(1, 5, 1f, true, 8));

        History.SetInnovationDebug(8);


        Genome parent2 = new Genome();

        for (int i = 0; i < 3; i++)
        {
            Node node = new Node(Node.NodeType.INPUT, i + 1);
            parent2.AddNodeCopy(node);
        }
        parent2.AddNodeCopy(new Node(Node.NodeType.OUTPUT, 4));
        parent2.AddNodeCopy(new Node(Node.NodeType.HIDDEN, 5));
        parent2.AddNodeCopy(new Node(Node.NodeType.HIDDEN, 6));

        parent2.AddConnection(new Connection(1, 4, -1f, true, 1));
        parent2.AddConnection(new Connection(2, 4, -1f, false, 2));
        parent2.AddConnection(new Connection(3, 4, -1f, true, 3));
        parent2.AddConnection(new Connection(2, 5, -1f, true, 4));
        parent2.AddConnection(new Connection(5, 4, -1f, false, 5));
        parent2.AddConnection(new Connection(5, 6, -1f, true, 6));
        parent2.AddConnection(new Connection(6, 4, -1f, true, 7));
        parent2.AddConnection(new Connection(3, 5, -1f, true, 9));
        parent2.AddConnection(new Connection(1, 6, -1f, true, 10));

        History.SetInnovationDebug(10);


        Genome child = Genome.CrossOver(parent1, parent2, Genome.Fitter.Parent2);

        gameObject.GetComponent <GenomePrinter>().Draw(child);
        Genome.DebugPrint(child);
    }
Exemplo n.º 9
0
        public void SetUp()
        {
            innovationHistory = new InnovationHistory();

            parent1 = new Genome(3, 1, innovationHistory);
            parent1.AddConnection(parent1.GetNode(1), parent1.GetNode(4)).InnovationNr.ShouldBe(1);
            parent1.AddConnection(parent1.GetNode(2), parent1.GetNode(4)).InnovationNr.ShouldBe(2);
            parent1.AddConnection(parent1.GetNode(3), parent1.GetNode(4)).InnovationNr.ShouldBe(3);
            parent1.MutateAddNode(parent1.GetNode(2), parent1.GetNode(4)); // InnovationNr 4 + 5

            parent2 = new Genome(3, 1, innovationHistory);
            parent2.AddConnection(parent2.GetNode(1), parent2.GetNode(4)).InnovationNr.ShouldBe(1);
            parent2.AddConnection(parent2.GetNode(2), parent2.GetNode(4)).InnovationNr.ShouldBe(2);
            parent2.AddConnection(parent2.GetNode(3), parent2.GetNode(4)).InnovationNr.ShouldBe(3);
            parent2.MutateAddNode(parent2.GetNode(2), parent2.GetNode(4)); // InnovationNr 4 + 5
            parent2.MutateAddNode(parent2.GetNode(5), parent2.GetNode(4)); // InnovationNr 6 + 7

            parent1.AddConnection(parent1.GetNode(1), parent1.GetNode(5)).InnovationNr.ShouldBe(8);
            parent2.AddConnection(parent2.GetNode(3), parent2.GetNode(5)).InnovationNr.ShouldBe(9);
            parent2.AddConnection(parent2.GetNode(1), parent2.GetNode(6)).InnovationNr.ShouldBe(10);
        }
Exemplo n.º 10
0
    public Genome Reproduce(Genome parentB)
    {
        //Create a reference for a new Genome child
        Genome child = population.CreateGenome();

        //Create a dict of genes picking them from the 2 parents
        Dictionary <int, Gene> g = new Dictionary <int, Gene>();

        g = GetGenes();
        foreach (KeyValuePair <int, Gene> i in parentB.GetGenes())
        {
            if (g.ContainsKey(i.Key))
            {
                //Choose a random number between 0 and 1
                int n = Random.Range(0, 2);
                // If it is 0 select the gene from the first parent -> do nothing
                // If it is 1 select the gene from the second parent -> overwrite the current value in the dict
                if (n == 1)
                {
                    g[i.Key] = i.Value;
                }
            }
        }
        child.SetGenes(g);

        // Create a dict of ConnectionGene picking them from the 2 parents
        Dictionary <int, ConnectionGene> c = new Dictionary <int, ConnectionGene>();

        c = GetConnectionGenes();
        foreach (KeyValuePair <int, ConnectionGene> i in parentB.GetConnectionGenes())
        {
            if (c.ContainsKey(i.Key))
            {
                //Choose a random number between 0 and 1
                int n = Random.Range(0, 2);
                // If it is 0 select the gene from the first parent -> do nothing
                // If it is 1 select the gene from the second parent -> overwrite the current value in the dict
                if (n == 1)
                {
                    c[i.Key] = i.Value;
                }
            }
        }
        foreach (ConnectionGene i in parentB.GetConnectionGenes().Values)
        {
            child.AddConnection(i.GetFromGene(), i.GetToGene(), i.GetWeight());
        }

        return(child);
    }
Exemplo n.º 11
0
        public void CheckLinkResurrection()
        {
            // Arrange
            var parameters = new NetworkParameters(3, 1)
            {
                InitialConnectionDensity = 1f
            };
            var tracker =
                new InnovationTracker(NetworkParameters.BiasCount + parameters.SensorCount + parameters.EffectorCount);

            var genome = new Genome(parameters, tracker, false);

            // Act
            Assert.Equal(0, genome.VacantConnectionCount);
            var oldId = genome.NeatChromosome[1].Id;

            genome.RemoveConnection(1);
            Assert.Equal(1, genome.VacantConnectionCount);
            genome.AddConnection(0);

            // Assert
            Assert.Single(genome.NeatChromosome, gene => gene.Id == oldId);
        }
Exemplo n.º 12
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);
        }
Exemplo n.º 13
0
 private static Connection AddConnection(Genome genome, int from, int to)
 {
     return(genome.AddConnection(genome.Nodes.Single(n => n.Number == from), genome.Nodes.Single(n => n.Number == to)));
 }
Exemplo n.º 14
0
        public void LayeredNetworkCheck()
        {
            var parameters = new NetworkParameters(3, 1)
            {
                InitialConnectionDensity = 1f
            };
            var tracker =
                new InnovationTracker(NetworkParameters.BiasCount + parameters.SensorCount + parameters.EffectorCount);

            var genome = new Genome(parameters, tracker, false);

            genome.SplitConnection(0);
            genome.SplitConnection(2);
            genome.SplitConnection(genome.NeatChromosome.Single(g => g.SourceId == 5).Id);
            while (genome.VacantConnectionCount > 0)
            {
                genome.AddConnection(Neat.Utils.RandomSource.Next(genome.VacantConnectionCount));
            }

            var network = new RecurrentNetwork();

            void AddConnectionAndCopyWeight(int sourceId, int targetId)
            {
                var weight = genome.NeatChromosome.Single(g => g.SourceId == sourceId && g.TargetId == targetId).Weight;

                network.AddConnection(sourceId, targetId, weight);
            }

            #region Network initialization

            network.AddNeuron(0, 0);
            network.AddNeuron(1, 0);
            network.AddNeuron(2, 0);
            network.AddNeuron(3, 0);
            network.AddNeuron(5, 1);
            network.AddNeuron(6, 1);
            network.AddNeuron(7, 2);
            network.AddNeuron(4, 3);
            for (var i = 0; i < 4; i++)
            {
                for (var j = 4; j < 8; j++)
                {
                    AddConnectionAndCopyWeight(i, j);
                }
            }
            for (var i = 4; i < 8; i++)
            {
                for (var j = 4; j < 8; j++)
                {
                    AddConnectionAndCopyWeight(i, j);
                }
            }

            Assert.Equal(network.ConnectionsCount, genome.NeatChromosome.Count);
            #endregion

            float[] result = null;
            for (var i = 0; i < 5; i++)
            {
                var values = new[]
                {
                    1f,
                    Neat.Utils.RandomSource.Next(),
                    Neat.Utils.RandomSource.Next(),
                    Neat.Utils.RandomSource.Next()
                };

                for (var j = 0; j < 3; j++)
                {
                    genome.Network.Sensors[j] = values[j + 1];
                }

                genome.Network.Activate();

                result = network.Activate(values);
            }

            Assert.Equal(result[0], genome.Network.Effectors[0]);
        }