private static ConnectionGenes <double> CreateCentroid(
            Dictionary <DirectedConnection, double> centroidElements,
            int pointCount)
        {
            int length    = centroidElements.Count;
            var connGenes = new ConnectionGenes <double>(length);

            var connArr   = connGenes._connArr;
            var weightArr = connGenes._weightArr;

            // Copy the unique coord elements from coordElemTotals into arrays, dividing each element's value
            // by the total number of coords as we go.
            double pointCountReciprocol = 1.0 / pointCount;

            int idx = 0;

            foreach (var elem in centroidElements)
            {
                connArr[idx]   = elem.Key;
                weightArr[idx] = elem.Value * pointCountReciprocol;
                idx++;
            }

            // Sort the connection genes.
            connGenes.Sort();

            return(connGenes);
        }
Ejemplo n.º 2
0
        private NeatGenome <double> CreateGenome1(MetaNeatGenome <double> metaNeatGenome)
        {
            var genomeBuilder = NeatGenomeBuilderFactory <double> .Create(metaNeatGenome);

            // Define a genome that matches the one defined in example1.genome.
            var connGenes = new ConnectionGenes <double>(12);

            connGenes[0]  = (0, 5, 0.5);
            connGenes[1]  = (0, 7, 0.7);
            connGenes[2]  = (0, 3, 0.3);
            connGenes[3]  = (1, 5, 1.5);
            connGenes[4]  = (1, 7, 1.7);
            connGenes[5]  = (1, 3, 1.3);
            connGenes[6]  = (1, 6, 1.6);
            connGenes[7]  = (1, 8, 1.8);
            connGenes[8]  = (1, 4, 1.4);
            connGenes[9]  = (2, 6, 2.6);
            connGenes[10] = (2, 8, 2.8);
            connGenes[11] = (2, 4, 2.4);

            // Ensure the connections are sorted correctly.
            connGenes.Sort();

            // Wrap in a genome.
            NeatGenome <double> genome = genomeBuilder.Create(0, 0, connGenes);

            return(genome);
        }
Ejemplo n.º 3
0
    public void DepthNodeReorderTest()
    {
        var metaNeatGenome = new MetaNeatGenome <double>(2, 2, true, new ReLU());
        var genomeBuilder  = NeatGenomeBuilderFactory <double> .Create(metaNeatGenome);

        // Define graph connections.
        var connGenes = new ConnectionGenes <double>(5);

        connGenes[0] = (0, 4, 0.0);
        connGenes[1] = (4, 5, 1.0);
        connGenes[2] = (5, 2, 2.0);
        connGenes[3] = (1, 2, 3.0);
        connGenes[4] = (2, 3, 4.0);
        connGenes.Sort();

        // Wrap in a genome.
        var genome = genomeBuilder.Create(0, 0, connGenes);

        // Note. The genome builder creates a digraph representation of the genome and attaches/caches it on the genome object.
        var acyclicDigraph = (DirectedGraphAcyclic)genome.DirectedGraph;

        Assert.NotNull(acyclicDigraph);

        // Simulate the actual weight array that would occur in e.g. a WeightedDirectedGraphAcyclic or NeuralNetAcyclic.
        double[] weightArrActual = new double[connGenes._weightArr.Length];
        for (int i = 0; i < weightArrActual.Length; i++)
        {
            weightArrActual[i] = connGenes._weightArr[genome.ConnectionIndexMap[i]];
        }

        // The nodes should have IDs allocated based on depth, i.e. the layer they are in.
        // And connections should be ordered by source node ID.
        var connArrExpected   = new DirectedConnection[5];
        var weightArrExpected = new double[5];

        connArrExpected[0] = new DirectedConnection(0, 2); weightArrExpected[0] = 0.0;
        connArrExpected[1] = new DirectedConnection(1, 4); weightArrExpected[1] = 3.0;
        connArrExpected[2] = new DirectedConnection(2, 3); weightArrExpected[2] = 1.0;
        connArrExpected[3] = new DirectedConnection(3, 4); weightArrExpected[3] = 2.0;
        connArrExpected[4] = new DirectedConnection(4, 5); weightArrExpected[4] = 4.0;

        // Compare actual and expected connections.
        CompareConnectionLists(connArrExpected, weightArrExpected, acyclicDigraph.ConnectionIds, weightArrActual);

        // Test layer info.
        LayerInfo[] layerArrExpected = new LayerInfo[5];
        layerArrExpected[0] = new LayerInfo(2, 2);
        layerArrExpected[1] = new LayerInfo(3, 3);
        layerArrExpected[2] = new LayerInfo(4, 4);
        layerArrExpected[3] = new LayerInfo(5, 5);
        layerArrExpected[4] = new LayerInfo(6, 5);
        Assert.Equal(5, acyclicDigraph.LayerArray.Length);

        // Check the node count.
        Assert.Equal(6, acyclicDigraph.TotalNodeCount);
    }