public void TestAddNode3()
        {
            var pop           = CreateNeatPopulation();
            var genomeBuilder = NeatGenomeBuilderFactory <double> .Create(pop.MetaNeatGenome);

            var genome = pop.GenomeList[0];

            var strategy = new AddNodeStrategy <double>(
                pop.MetaNeatGenome, genomeBuilder,
                pop.GenomeIdSeq, pop.InnovationIdSeq, pop.GenerationSeq,
                pop.AddedNodeBuffer);

            IRandomSource rng = RandomDefaults.CreateRandomSource();

            CircularBuffer <NeatGenome <double> > genomeRing = new CircularBuffer <NeatGenome <double> >(10);

            genomeRing.Enqueue(genome);

            for (int i = 0; i < 5000; i++)
            {
                NeatGenome <double> childGenome = CreateAndTestChildGenome(genome, strategy, rng);

                // Add the new child genome to the ring.
                genomeRing.Enqueue(childGenome);

                // Take the genome at the tail of the ring for the next parent.
                genome = genomeRing[0];
            }
        }
        private static NeatGenome <double> CreateAndTestChildGenome(
            NeatGenome <double> parentGenome,
            AddNodeStrategy <double> strategy,
            IRandomSource rng)
        {
            var nodeIdSet = GetNodeIdSet(parentGenome);
            var connSet   = GetDirectedConnectionSet(parentGenome);

            var childGenome = strategy.CreateChildGenome(parentGenome, rng);

            // The connection genes should be sorted.
            Assert.IsTrue(SortUtils.IsSortedAscending(childGenome.ConnectionGenes._connArr));

            // The child genome should have one more connection than parent.
            Assert.AreEqual(parentGenome.ConnectionGenes.Length + 1, childGenome.ConnectionGenes.Length);

            // The child genome should have one more node ID than the parent.
            var childNodeIdSet = GetNodeIdSet(childGenome);
            var newNodeIdList  = new List <int>(childNodeIdSet.Except(nodeIdSet));

            Assert.AreEqual(1, newNodeIdList.Count);
            int newNodeId = newNodeIdList[0];

            // The child genome's new connections should not be a duplicate of any of the existing/parent connections.
            var childConnSet = GetDirectedConnectionSet(childGenome);
            var newConnList  = new List <DirectedConnection>(childConnSet.Except(connSet));

            Assert.AreEqual(2, newConnList.Count);

            // The parent should have one connection that the child does not, i.e. the connection that was replaced.
            var removedConnList = new List <DirectedConnection>(connSet.Except(childConnSet));

            Assert.AreEqual(1, removedConnList.Count);

            // The two new connections should connect to the new node ID.
            var connRemoved = removedConnList[0];
            var connA       = newConnList[0];
            var connB       = newConnList[1];

            Assert.IsTrue(
                (connA.SourceId == connRemoved.SourceId && connA.TargetId == newNodeId && connB.SourceId == newNodeId && connB.TargetId == connRemoved.TargetId) ||
                (connB.SourceId == connRemoved.SourceId && connB.TargetId == newNodeId && connA.SourceId == newNodeId && connA.TargetId == connRemoved.TargetId));

            return(childGenome);
        }
Esempio n. 3
0
        public void TestAddNode1()
        {
            var pop           = CreateNeatPopulation();
            var genomeBuilder = NeatGenomeBuilderFactory <double> .Create(pop.MetaNeatGenome);

            var genome = pop.GenomeList[0];

            var strategy = new AddNodeStrategy <double>(
                pop.MetaNeatGenome, genomeBuilder,
                pop.GenomeIdSeq, pop.InnovationIdSeq, pop.GenerationSeq,
                pop.AddedNodeBuffer,
                RandomDefaults.CreateRandomSource());

            for (int i = 0; i < 10000; i++)
            {
                NeatGenome <double> childGenome = CreateAndTestChildGenome(genome, strategy);
            }
        }
Esempio n. 4
0
        public void TestAddNode2()
        {
            var pop           = CreateNeatPopulation();
            var genomeBuilder = NeatGenomeBuilderFactory <double> .Create(pop.MetaNeatGenome);

            var genome = pop.GenomeList[0];

            var strategy = new AddNodeStrategy <double>(
                pop.MetaNeatGenome, genomeBuilder,
                pop.GenomeIdSeq, pop.InnovationIdSeq, pop.GenerationSeq,
                pop.AddedNodeBuffer,
                RandomDefaults.CreateRandomSource());

            for (int i = 0; i < 2000; i++)
            {
                NeatGenome <double> childGenome = CreateAndTestChildGenome(genome, strategy);

                // Make the child genome the parent in the next iteration. I.e. accumulate add node mutations.
                genome = childGenome;
            }
        }