예제 #1
0
        public Connection Copy(IInnovationPointGenerator innovationGenerator)
        {
            var copy = Copy();

            copy.Id = innovationGenerator.GetNextInnovation();
            return(copy);
        }
예제 #2
0
        public void AddNodeMutation_Mutate_AddsNodeAndConnection()
        {
            //ARRANGE
            var genome = CreateGenome(1, 1);

            genome.Connections.Add(1,
                                   new Connection {
                Id = 1, InNodeId = 1, OutNodeId = 2, IsEnabled = true, Weight = 2.0
            });

            _nodeInnovationGenerator.GetNextInnovation().Returns(3);
            _connectionInnovationGenerator.GetNextInnovation().Returns(2, 3);
            _randomizer.Next(1).Returns(call => call.Arg <int>() - 1);

            var mutator = new AddNodeMutation(_nodeInnovationGenerator, _connectionInnovationGenerator, _randomizer);
            //ACT
            var result = mutator.Mutate(ref genome);

            //ASSERT
            Assert.IsTrue(result);
            Assert.AreEqual(3, genome.Connections.Count);
            Assert.AreEqual(3, genome.Nodes.Count);
            Assert.AreEqual(NodeType.Hidden, genome.Nodes[3].NodeType);

            AssertConnection(genome, 1, false, 1, 2, 2.0);
            AssertConnection(genome, 2, true, 1, 3, 1.0);
            AssertConnection(genome, 3, true, 3, 2, 2.0);
        }
예제 #3
0
        public void AddConnectionMutation_Mutate_AddedConnection()
        {
            //ARRANGE
            var i = 0;

            _random.Next(Arg.Is <int>(2)).Returns(call =>
            {
                var ret = i;
                i       = i + 1;
                return(ret);
            });
            _random.NextDouble().ReturnsForAnyArgs(1.0);

            _innovationPointGenerator.GetNextInnovation().Returns(23);

            var genome = CreateGenome();

            var addConnectionMutator = new AddConnectionMutation(_innovationPointGenerator, _random, _config);

            //ACT
            var result = addConnectionMutator.Mutate(ref genome);

            //ASSERT
            Assert.IsTrue(result);

            Assert.AreEqual(1, genome.Connections.Count());

            Assert.AreEqual(1.0, genome.Connections.First().Value.Weight);
            Assert.AreEqual(23, genome.Connections.First().Value.Id);
        }
예제 #4
0
파일: Tests2.cs 프로젝트: mrBoie/NeatPlay
            private Genome GenerateTemplateGenome(IInnovationPointGenerator nodeGenerator, IInnovationPointGenerator connectionGenerator)
            {
                var genome = new Genome();

                genome.Nodes =
                    Enumerable
                    .Repeat(1, 3)
                    .Select(n => new Node
                {
                    Id       = nodeGenerator.GetNextInnovation(),
                    NodeType = NodeType.Sensor
                })
                    .ToDictionary(n => n.Id, n => n);

                var outputId = nodeGenerator.GetNextInnovation();

                genome.Nodes.Add(outputId, new Node {
                    Id = outputId, NodeType = NodeType.Output
                });

                var c1 = new Connection
                {
                    Id        = connectionGenerator.GetNextInnovation(),
                    InNodeId  = genome.Nodes.First(n => n.Value.NodeType == NodeType.Sensor).Key,
                    OutNodeId = genome.Nodes.First(n => n.Value.NodeType == NodeType.Output).Key,
                    IsEnabled = true,
                    Weight    = 0.5
                };
                var c2 = new Connection
                {
                    Id        = connectionGenerator.GetNextInnovation(),
                    InNodeId  = genome.Nodes.Last(n => n.Value.NodeType == NodeType.Sensor).Key,
                    OutNodeId = genome.Nodes.First(n => n.Value.NodeType == NodeType.Output).Key,
                    IsEnabled = true,
                    Weight    = 0.5
                };

                genome.Connections = new Dictionary <int, Connection>();
                genome.Connections.Add(c1.Id, c1);
                genome.Connections.Add(c2.Id, c2);

                return(genome);
            }
예제 #5
0
 private Connection CreateConnection(int inNode, int outNode)
 {
     return(new Connection
     {
         InNodeId = inNode,
         OutNodeId = outNode,
         IsEnabled = false,
         Weight = (_random.NextDouble() * 2.0) - 1.0, //RANDOM WEIGHT
         Id = _connectionInnovationGenerator.GetNextInnovation()
     });
 }
예제 #6
0
        private (Node newNode, Connection inConnection, Connection outConnection) GenerateNewNodeAndConnections(Connection connection)
        {
            var node = new Node()
            {
                Id       = nodeInnovationGenerator.GetNextInnovation(),
                NodeType = NodeType.Hidden
            };

            var inConnection = connection.Copy(connectionInnovationGenerator);

            inConnection.OutNodeId = node.Id;
            inConnection.Weight    = 1;

            var outConnection = connection.Copy(connectionInnovationGenerator);

            outConnection.InNodeId = node.Id;

            return(node, inConnection, outConnection);
        }