public void ZerosRearrange()
        {
            int[] inputArr    = { 1, 2, 0, 4, 3, 0, 5, 0 };
            int[] expectedArr = { 1, 2, 4, 3, 5, 0, 0, 0 };

            rearrangement.ZerosRearrange(inputArr);
            Assert.True(ArrayTestUtils.ArraysEqual(inputArr, expectedArr));
        }
예제 #2
0
        public void TestReverse()
        {
            int[] inputArr    = { 1, 2, 3, 4, 5 };
            int[] expectedArr = { 5, 4, 3, 2, 1 };

            ArrayUtils.Reverse(inputArr);
            Assert.True(ArrayTestUtils.ArraysEqual(inputArr, expectedArr));
        }
        public void PosNegInOrderRearrange()
        {
            int[] inputArr    = { -5, -2, 5, 2, 4, 7, 1, 8, 0, -8 };
            int[] expectedArr = { -5, 5, -2, 2, -8, 4, 7, 1, 8, 0 };

            rearrangement.PosNegInOrderRearrange(inputArr);
            Assert.True(ArrayTestUtils.ArraysEqual(inputArr, expectedArr));
        }
        public void Rearrange()
        {
            int[] inputArr    = { -1, -1, 6, 1, 9, 3, 2, -1, 4, -1 };
            int[] expectedArr = { -1, 1, 2, 3, 4, -1, 6, -1, -1, 9 };

            rearrangement.Rearrange(inputArr);
            Assert.True(ArrayTestUtils.ArraysEqual(inputArr, expectedArr));
        }
예제 #5
0
        public void RotationShouldWorkIfRotationCountIsDivisibleByArraySize()
        {
            var d = 14;

            int[] inputArr    = { 1, 2, 3, 4, 5, 6, 7 };
            int[] expectedArr = { 1, 2, 3, 4, 5, 6, 7 };

            rotation.Rotate(inputArr, d);
            Assert.True(ArrayTestUtils.ArraysEqual(inputArr, expectedArr));
        }
예제 #6
0
        public void RotationShouldWorkIfRotationCountIsGreaterThanArraySize()
        {
            var d = 24;

            int[] inputArr    = { 1, 2, 3, 4, 5, 6, 7 };
            int[] expectedArr = { 4, 5, 6, 7, 1, 2, 3 };

            rotation.Rotate(inputArr, d);
            Assert.True(ArrayTestUtils.ArraysEqual(inputArr, expectedArr));
        }
예제 #7
0
        public void RotationShouldWorkForArrayOfSizeTwo()
        {
            var d = 1;

            int[] inputArr    = { 1, 2 };
            int[] expectedArr = { 2, 1 };

            rotation.Rotate(inputArr, d);
            Assert.True(ArrayTestUtils.ArraysEqual(inputArr, expectedArr));
        }
예제 #8
0
        public void RotationShouldWork()
        {
            var d = 2;

            int[] inputArr    = { 1, 2, 3, 4, 5, 6, 7 };
            int[] expectedArr = { 3, 4, 5, 6, 7, 1, 2 };

            rotation.Rotate(inputArr, d);
            Assert.True(ArrayTestUtils.ArraysEqual(inputArr, expectedArr));
        }
        public void ReorderArrays()
        {
            int[] inputArr   = { 10, 11, 12 };
            int[] inputIndex = { 1, 0, 2 };

            int[] expectedArr   = { 11, 10, 12 };
            int[] expectedIndex = { 0, 1, 2 };

            rearrangement.ReorderArrays(inputArr, inputIndex);
            Assert.True(ArrayTestUtils.ArraysEqual(inputArr, expectedArr));
            Assert.True(ArrayTestUtils.ArraysEqual(inputIndex, expectedIndex));
        }
예제 #10
0
        public static void CompareGenomes(NeatGenome <double> x, NeatGenome <double> y)
        {
            // Compare connections.
            var xGenes = x.ConnectionGenes;
            var yGenes = y.ConnectionGenes;

            ArrayTestUtils.Compare(xGenes._connArr, yGenes._connArr);
            ArrayTestUtils.Compare(xGenes._weightArr, yGenes._weightArr);

            // Compare hidden node ID arrays.
            ArrayTestUtils.Compare(x.HiddenNodeIdArray, y.HiddenNodeIdArray);
        }
    public void CalculateEuclideanCentroid()
    {
        // Init input gene arrays.
        var connGenes1 = new ConnectionGenes <double>(6);

        connGenes1[0] = (0, 1, 1.0);
        connGenes1[1] = (0, 2, 2.0);
        connGenes1[2] = (2, 2, 3.0);
        connGenes1[3] = (2, 4, 4.0);
        connGenes1[4] = (2, 5, 5.0);
        connGenes1[5] = (3, 0, 6.0);

        var connGenes2 = new ConnectionGenes <double>(8);

        connGenes2[0] = (0, 1, 10.0);
        connGenes2[1] = (0, 3, 20.0);
        connGenes2[2] = (2, 2, 30.0);
        connGenes2[3] = (2, 3, 40.0);
        connGenes2[4] = (2, 5, 50.0);
        connGenes2[5] = (2, 6, 60.0);
        connGenes2[6] = (3, 0, 70.0);
        connGenes2[7] = (4, 5, 80.0);

        var connGenes3 = new ConnectionGenes <double>(2);

        connGenes3[0] = (2, 5, 100.0);
        connGenes3[1] = (10, 20, 200.0);

        var arr = new ConnectionGenes <double>[] { connGenes1, connGenes2, connGenes3 };

        // Calc centroid.
        ConnectionGenes <double> centroid = DistanceMetricUtils.CalculateEuclideanCentroid(arr);

        // Expected centroid.
        var expected = new ConnectionGenes <double>(11);

        expected[0]  = (0, 1, 11 / 3.0);
        expected[1]  = (0, 2, 2 / 3.0);
        expected[2]  = (0, 3, 20 / 3.0);
        expected[3]  = (2, 2, 33 / 3.0);
        expected[4]  = (2, 3, 40 / 3.0);
        expected[5]  = (2, 4, 4 / 3.0);
        expected[6]  = (2, 5, 155 / 3.0);
        expected[7]  = (2, 6, 60 / 3.0);
        expected[8]  = (3, 0, 76 / 3.0);
        expected[9]  = (4, 5, 80 / 3.0);
        expected[10] = (10, 20, 200 / 3.0);

        Assert.True(SpanUtils.Equal <DirectedConnection>(expected._connArr, centroid._connArr));
        Assert.True(ArrayTestUtils.ConponentwiseEqual(expected._weightArr, centroid._weightArr, 1e-6));
    }
예제 #12
0
        public void MinHeapify()
        {
            int[] inputArr    = { 7, 10, 4, 3, 20, 15 };
            int[] expectedArr = { 3, 4, 7, 10, 20, 15 };

            minHeap = new MinHeap(inputArr.Length);
            for (int i = 0; i < inputArr.Length; i++)
            {
                minHeap.insertKey(inputArr[i]);
            }

            minHeap.MinHeapify(0);
            Assert.True(ArrayTestUtils.ArraysEqual(minHeap.heapArr, expectedArr));
        }
        public void TestCalcGradients()
        {
            const int         sampleCount = 100;
            ParamSamplingInfo psi         = new ParamSamplingInfo(0, 2 * Math.PI, sampleCount);

            double[] yArr = new double[sampleCount];
            FuncRegressionUtils.Probe((x) => Math.Sin(x), psi, yArr);

            // Calc gradients.
            double[] gradientArr = new double[sampleCount];
            FuncRegressionUtils.CalcGradients(psi, yArr, gradientArr);

            // Calc expected gradients (using simple non-vectorized logic).
            double[] gradientArrExpected = new double[sampleCount];
            CalcGradients(psi, yArr, gradientArrExpected);

            // Compare results.
            ArrayTestUtils.Compare(gradientArrExpected, gradientArr);
        }
        public void TestAddAcyclicConnection_CumulativeAdditions()
        {
            var pop           = CreateNeatPopulation();
            var generationSeq = new Int32Sequence();
            var genomeBuilder = NeatGenomeBuilderFactory <double> .Create(pop.MetaNeatGenome);

            var rootGenome = pop.GenomeList[0];

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

            IRandomSource rng = RandomDefaults.CreateRandomSource();

            var nodeIdSet = GetNodeIdSet(rootGenome);

            CyclicGraphAnalysis cyclicGraphAnalysis = new CyclicGraphAnalysis();

            AcyclicGraphDepthAnalysis graphDepthAnalysis = new AcyclicGraphDepthAnalysis();

            // Run the inner loop test multiple times.
            // Note. The add-connection mutations are random, thus each loop accumulates a different set of mutations.
            for (int i = 0; i < 50; i++)
            {
                var parentGenome = rootGenome;

                // Accumulate random mutations for some number of loops.
                for (int j = 0; j < 20;)
                {
                    var childGenome = strategy.CreateChildGenome(rootGenome, rng);

                    // Note. the strategy will return a null if it cannot find an acyclic connection to add;
                    // test for this and try again. The test will be for N successful mutations rather than N attempts.
                    if (null == childGenome)
                    {
                        continue;
                    }

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

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

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

                    // The child genome should have the same set of node IDs as the parent.
                    var childNodeIdSet = GetNodeIdSet(childGenome);
                    Assert.IsTrue(nodeIdSet.SetEquals(childNodeIdSet));

                    // The child genome should describe an acyclic graph, i.e. the new connection should not have
                    // formed a cycle in the graph.
                    var digraph = childGenome.DirectedGraph;
                    Assert.IsFalse(cyclicGraphAnalysis.IsCyclic(digraph));

                    // Run the acyclic graph depth analysis algorithm.
                    GraphDepthInfo depthInfo = graphDepthAnalysis.CalculateNodeDepths(childGenome.DirectedGraph);

                    // Run again with the alternative algorithm (that uses function recursion).
                    GraphDepthInfo depthInfo2 = AcyclicGraphDepthAnalysisByRecursion.CalculateNodeDepths(childGenome.DirectedGraph);

                    Assert.AreEqual(nodeIdSet.Count, depthInfo._nodeDepthArr.Length);
                    Assert.AreEqual(nodeIdSet.Count, depthInfo2._nodeDepthArr.Length);
                    ArrayTestUtils.Compare(depthInfo2._nodeDepthArr, depthInfo._nodeDepthArr);

                    // Set the child genome to be the new parent, thus we accumulate random new connections over time.
                    parentGenome = childGenome;

                    // Increment for successful tests only.
                    j++;
                }
            }
        }