예제 #1
0
        public void Sort_ShortArray()
        {
            int[] keys = new int[] { 5, 8, 2, 16, 32, 12, 7 };
            TimSort <int> .Sort(keys);

            Assert.True(SpanUtils.Equal <int>(new int[] { 2, 5, 7, 8, 12, 16, 32 }, keys));
        }
예제 #2
0
        public void Sort_ShortArray()
        {
            int[] keys = new int[] { 5, 8, 2, 16, 32, 12, 7 };
            int[] v    = new int[]    { 45, 42, 48, 24, 8, 28, 43 };
            int[] w    = new int[]    { 0, 1, 2, 3, 4, 5, 6 };
            IntroSort <int, int, int> .Sort(keys, v, w);

            Assert.True(SpanUtils.Equal <int>(new int[] { 2, 5, 7, 8, 12, 16, 32 }, keys));
            Assert.True(SpanUtils.Equal <int>(new int[] { 48, 45, 43, 42, 28, 24, 8 }, v));
            Assert.True(SpanUtils.Equal <int>(new int[] { 2, 0, 6, 1, 5, 3, 4 }, w));
        }
    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));
    }
예제 #4
0
        private static void Clip_Inner(UniformDistributionSampler sampler, int len)
        {
            // Alloc array and fill with uniform random noise.
            float[] x = new float[len];
            sampler.Sample(x);

            // Clip the elements of the array with the safe routine.
            float[] expected = (float[])x.Clone();
            PointwiseClip(expected, -1.1f, 18.8f);

            // Clip the elements of the array.
            float[] actual = (float[])x.Clone();
            MathSpan.Clip(actual, -1.1f, 18.8f);

            // Compare expected with actual array.
            Assert.True(SpanUtils.Equal <float>(expected, actual));
        }
예제 #5
0
        private static void Clip_Inner(ISampler <int> sampler, int len)
        {
            // Alloc array and fill with uniform random noise.
            int[] x = new int[len];
            sampler.Sample(x);

            // Clip the elements of the array with the safe routine.
            int[] expected = (int[])x.Clone();
            PointwiseClip(expected, -1, 18);

            // Clip the elements of the array.
            int[] actual = (int[])x.Clone();
            MathSpan.Clip(actual, -1, 18);

            // Compare expected with actual array.
            Assert.True(SpanUtils.Equal <int>(expected, actual));
        }
예제 #6
0
        /// <summary>
        /// Validation tests on an array of hidden node IDs and an associated array of connections.
        /// </summary>
        /// <param name="hiddenNodeIdArr">Array of hidden node IDs.</param>
        /// <param name="connArr">Array of connections.</param>
        /// <param name="inputOutputCount">The total number of input and output nodes.</param>
        /// <returns>true if the provided data is valid; otherwise false.</returns>
        public static bool ValidateHiddenNodeIds(
            int[] hiddenNodeIdArr,
            DirectedConnection[] connArr,
            int inputOutputCount)
        {
            // Test that the IDs are sorted (required to allow for efficient searching of IDs using a binary search).
            if (!SortUtils.IsSortedAscending <int>(hiddenNodeIdArr))
            {
                return(false);
            }

            // Get the set of hidden node IDs described by the connections, and test that they match the supplied hiddenNodeIdArr.
            int[] idArr = CreateHiddenNodeIdArray(connArr, inputOutputCount, new HashSet <int>());
            if (!SpanUtils.Equal <int>(idArr, hiddenNodeIdArr))
            {
                return(false);
            }
            return(true);
        }
예제 #7
0
        private void PerformMutationOp()
        {
            int outcome = DiscreteDistribution.Sample(_rng, _opDistribution);

            switch (outcome)
            {
            case 0:     // Write.
            {
                PerformMutationOp_Write();
                break;
            }

            case 1:     // Write byte.
            {
                byte b = (byte)_rng.Next();
                _strmA.WriteByte(b);
                _strmB.WriteByte(b);
                Debug.WriteLine("WriteByte");
                break;
            }

            case 2:     // Change read/write head position.
            {
                PerformMutationOp_Position();
                break;
            }

            case 3:     // SetLength
            {
                PerformMutationOp_SetLength();
                break;
            }

            case 4:     // Seek
            {
                PerformMutationOp_Seek();
                break;
            }

            case 5:     // Trim
            {
                _strmB.Trim();
                Debug.WriteLine("Trim");
                break;
            }

            case 6:     // Read byte.
            {
                int a = _strmA.ReadByte();
                int b = _strmB.ReadByte();
                if (a != b)
                {
                    throw new Exception("ReadByte mismatch");
                }
                Debug.WriteLine("ReadByte");
                break;
            }

            case 7:     // Read
            {
                int len = _rng.Next(20_000);

                byte[] abuf = new byte[len];
                byte[] bbuf = new byte[len];

                int alen = _strmA.Read(abuf);
                int blen = _strmB.Read(bbuf);

                if (alen != blen)
                {
                    throw new Exception("Read mismatch");
                }

                if (!SpanUtils.Equal <byte>(abuf, bbuf))
                {
                    throw new Exception("Read mismatch");
                }
                Debug.WriteLine("Read");
                break;
            }
            }
        }
        /// <summary>
        /// Creates a new directed acyclic graph instance from the provided graph structure, accompanying graph
        /// layer information, and node ID mappings.
        /// </summary>
        /// <param name="digraph">A directed graph structure.</param>
        /// <param name="depthInfo">Depth/layer information, describing what layer each node of the graph is within.</param>
        /// <param name="newIdByOldId">Returns a set of node ID mappings. These describe a mapping from the
        /// non-contiguous node ID space of <paramref name="digraph"/>, to the contiguous node ID space of the
        /// returned <see cref="DirectedGraphAcyclic"/>
        /// contiguous space.</param>
        /// <param name="connectionIndexMap">Returns a set of connection index mappings. The connections of
        /// <paramref name="digraph"/> are re-ordered based on the layer/depth of each connection's source node;
        /// this structure conveys the new index of each connection given its original/old index.</param>
        /// <param name="timsortWorkArr">A re-usable working array for use as temporary storage by the timsort algorithm.</param>
        /// <param name="timsortWorkVArr">A secondary re-usable working array for use as temporary storage by the timsort algorithm.</param>
        /// <returns>A new instance of <see cref="DirectedGraphAcyclic"/>.</returns>
        public static DirectedGraphAcyclic CreateDirectedGraphAcyclic(
            DirectedGraph digraph,
            GraphDepthInfo depthInfo,
            out int[] newIdByOldId,
            out int[] connectionIndexMap,
            ref int[]?timsortWorkArr,
            ref int[]?timsortWorkVArr)
        {
            int inputCount  = digraph.InputCount;
            int outputCount = digraph.OutputCount;

            // Assert that all input nodes are at depth zero.
            // Any input node with a non-zero depth must have an input connection, and this is not supported.
            Debug.Assert(SpanUtils.Equal(depthInfo._nodeDepthArr.AsSpan(0, inputCount), 0));

            // Compile a mapping from current node IDs to new IDs (based on node depth in the graph).
            newIdByOldId = CompileNodeIdMap(depthInfo, digraph.TotalNodeCount, inputCount, ref timsortWorkArr, ref timsortWorkVArr);

            // Map the connection node IDs.
            ConnectionIdArrays connIdArrays = digraph.ConnectionIdArrays;

            MapIds(connIdArrays, newIdByOldId);

            // Init connection index map.
            int connCount = connIdArrays.Length;

            connectionIndexMap = new int[connCount];
            for (int i = 0; i < connCount; i++)
            {
                connectionIndexMap[i] = i;
            }

            // Sort the connections based on sourceID, targetId; this will arrange the connections based on the depth
            // of the source nodes.
            // Note. This sort routine will also sort a secondary array, i.e. keep the items in both arrays aligned;
            // here we use this to create connectionIndexMap.
            ConnectionSorter <int> .Sort(connIdArrays, connectionIndexMap);

            // Make a copy of the sub-range of newIdMap that represents the output nodes.
            // This is required later to be able to locate the output nodes now that they have been sorted by depth.
            int[] outputNodeIdxArr = new int[outputCount];
            Array.Copy(newIdByOldId, inputCount, outputNodeIdxArr, 0, outputCount);

            // Create an array of LayerInfo(s).
            // Each LayerInfo contains the index + 1 of both the last node and last connection in that layer.
            //
            // The array is in order of depth, from layer zero (inputs nodes) to the last layer (usually output nodes,
            // but not necessarily if there is a dead end pathway with a high number of hops).
            //
            // Note. There is guaranteed to be at least one connection with a source at a given depth level, this is
            // because for there to be a layer N there must necessarily be a connection from a node in layer N-1
            // to a node in layer N.
            int graphDepth = depthInfo._graphDepth;

            LayerInfo[] layerInfoArr = new LayerInfo[graphDepth];

            // Note. Scanning over nodes can start at inputCount instead of zero, because all nodes prior to that index
            // are input nodes and are therefore at depth zero. (input nodes are never the target of a connection,
            // therefore are always guaranteed to be at the start of a connectivity graph, and thus at depth zero).
            int nodeCount = digraph.TotalNodeCount;
            int nodeIdx   = inputCount;
            int connIdx   = 0;

            int[] nodeDepthArr = depthInfo._nodeDepthArr;
            int[] srcIdArr     = connIdArrays._sourceIdArr;

            for (int currDepth = 0; currDepth < graphDepth; currDepth++)
            {
                // Scan for last node at the current depth.
                for (; nodeIdx < nodeCount && nodeDepthArr[nodeIdx] == currDepth; nodeIdx++)
                {
                    ;
                }

                // Scan for last connection at the current depth.
                for (; connIdx < srcIdArr.Length && nodeDepthArr[srcIdArr[connIdx]] == currDepth; connIdx++)
                {
                    ;
                }

                // Store node and connection end indexes for the layer.
                layerInfoArr[currDepth] = new LayerInfo(nodeIdx, connIdx);
            }

            // Construct and return.
            return(new DirectedGraphAcyclic(
                       inputCount, outputCount, nodeCount,
                       connIdArrays,
                       layerInfoArr,
                       outputNodeIdxArr));
        }