Exemplo n.º 1
0
    /// <summary>
    /// Create a NeatGenome with the given meta data and connection genes.
    /// </summary>
    /// <param name="id">Genome ID.</param>
    /// <param name="birthGeneration">Birth generation.</param>
    /// <param name="connGenes">Connection genes.</param>
    /// <returns>A new NeatGenome instance.</returns>
    public NeatGenome <T> Create(
        int id,
        int birthGeneration,
        ConnectionGenes <T> connGenes)
    {
        // Determine the set of node IDs, and create a mapping from node IDs to node indexes.
        int[] hiddenNodeIdArr = ConnectionGenesUtils.CreateHiddenNodeIdArray(connGenes._connArr, _metaNeatGenome.InputOutputNodeCount, _workingIdSet);

        return(Create(id, birthGeneration, connGenes, hiddenNodeIdArr));
    }
        public void TestDeleteConnection()
        {
            var pop           = CreateNeatPopulation();
            var generationSeq = new Int32Sequence();
            var genomeBuilder = NeatGenomeBuilderFactory <double> .Create(pop.MetaNeatGenome);

            var genome = pop.GenomeList[0];

            var strategy = new DeleteConnectionStrategy <double>(
                pop.MetaNeatGenome, genomeBuilder,
                pop.GenomeIdSeq, generationSeq);

            IRandomSource rng = RandomDefaults.CreateRandomSource();

            var nodeIdSet = GetNodeIdSet(genome);
            var connSet   = GetDirectedConnectionSet(genome);

            for (int i = 0; i < 1000; i++)
            {
                var childGenome = strategy.CreateChildGenome(genome, rng);

                // The child genome should have one less connection than the parent.
                Assert.AreEqual(genome.ConnectionGenes.Length - 1, childGenome.ConnectionGenes.Length);

                // The child genome's connections should be a proper subset of the parent genome's.
                var childConnSet = GetDirectedConnectionSet(childGenome);
                Assert.IsTrue(childConnSet.IsProperSubsetOf(connSet));

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

                // Test that the array of hidden node IDs is correct, i.e. corresponds with the hidden node IDs described by the connections.
                Assert.IsTrue(ConnectionGenesUtils.ValidateHiddenNodeIds(
                                  childGenome.HiddenNodeIdArray,
                                  childGenome.ConnectionGenes._connArr,
                                  childGenome.MetaNeatGenome.InputOutputNodeCount));
            }
        }
    /// <summary>
    /// Validate/verify the data associated with a NEAT genome, applying a series of debug asserts.
    /// </summary>
    /// <param name="metaNeatGenome">Genome metadata.</param>
    /// <param name="id">Genome ID.</param>
    /// <param name="birthGeneration">Genome birth generation.</param>
    /// <param name="connGenes">Connection genes data structure.</param>
    /// <param name="hiddenNodeIdArr">An array of the genome's hidden node IDs.</param>
    /// <param name="nodeIndexByIdMap">Mapping function for obtaining a node index for a given node ID.</param>
    /// <param name="digraph">The directed graph that the current genome represents.</param>
    /// <param name="connectionIndexMap">A set of connection index mappings.</param>
    public static void AssertIsValid(
        MetaNeatGenome <T> metaNeatGenome,
        int id,
        int birthGeneration,
        ConnectionGenes <T> connGenes,
        int[] hiddenNodeIdArr,
        INodeIdMap nodeIndexByIdMap,
        DirectedGraph digraph,
        int[]?connectionIndexMap)
    {
        // Check for mandatory object references.
        Debug.Assert(metaNeatGenome is not null);
        Debug.Assert(connGenes is not null);
        Debug.Assert(hiddenNodeIdArr is not null);
        Debug.Assert(nodeIndexByIdMap is not null);
        Debug.Assert(digraph is not null);

        // Basic check on ID and birth generation.
        Debug.Assert(id >= 0);
        Debug.Assert(birthGeneration >= 0);

        // Acyclic graph checks.
        if (metaNeatGenome.IsAcyclic)
        {
            AssertAcyclicGraph(metaNeatGenome, digraph, connectionIndexMap);
        }

        // Node counts.
        AssertNodeCounts(metaNeatGenome, hiddenNodeIdArr, nodeIndexByIdMap, digraph);

        // Hidden node IDs.
        Debug.Assert(ConnectionGenesUtils.ValidateHiddenNodeIds(hiddenNodeIdArr, connGenes._connArr, metaNeatGenome.InputOutputNodeCount));

        // Connections.
        AssertConnections(connGenes, digraph, nodeIndexByIdMap, connectionIndexMap);
    }