public void Sort_ShortArray() { int[] keys = new int[] { 5, 8, 2, 16, 32, 12, 7 }; TimSort <int> .Sort(keys); Assert.True(SpanUtils.Equals <int>(new int[] { 2, 5, 7, 8, 12, 16, 32 }, keys)); }
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.Equals <int>(new int[] { 2, 5, 7, 8, 12, 16, 32 }, keys)); Assert.True(SpanUtils.Equals <int>(new int[] { 48, 45, 43, 42, 28, 24, 8 }, v)); Assert.True(SpanUtils.Equals <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.Equals <DirectedConnection>(expected._connArr, centroid._connArr)); Assert.True(ArrayTestUtils.ConponentwiseEqual(expected._weightArr, centroid._weightArr, 1e-6)); }
private static void Clamp_Inner(UniformDistributionSampler sampler, int len) { // Alloc array and fill with uniform random noise. double[] x = new double[len]; sampler.Sample(x); // Clip the elements of the array with the safe routine. double[] expected = (double[])x.Clone(); PointwiseClip(expected, -1.1, 18.8); // Clip the elements of the array. double[] actual = (double[])x.Clone(); MathSpanUtils.Clamp(actual, -1.1, 18.8); // Compare expected with actual array. Assert.True(SpanUtils.Equals <double>(expected, actual)); }
/// <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(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.Equals <int>(idArr, hiddenNodeIdArr)) { return(false); } return(true); }
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.Equals(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)); }