/// <summary> /// Constructs a AcyclicNeuralNet with the provided neural net definition parameters. /// </summary> /// <param name="digraph">Network structure definition</param> /// <param name="weightArr">Connection weights array.</param> /// <param name="activationFn">Node activation function.</param> public NeuralNetAcyclic( DirectedGraphAcyclic digraph, double[] weightArr, VecFnSegment <double> activationFn) { // Store refs to network structure data. _srcIdArr = digraph.ConnectionIdArrays._sourceIdArr; _tgtIdArr = digraph.ConnectionIdArrays._targetIdArr; _weightArr = weightArr; _layerInfoArr = digraph.LayerArray; // Store network activation function. _activationFn = activationFn; // Store input/output node counts. _inputCount = digraph.InputCount; _outputCount = digraph.OutputCount; // Create working array for node activation signals. _activationArr = new double[digraph.TotalNodeCount]; // Wrap a sub-range of the _activationArr that holds the activation values for the input nodes. _inputVector = new VectorSegment <double>(_activationArr, 0, _inputCount); // Wrap the output nodes. Nodes have been sorted by depth within the network therefore the output // nodes can no longer be guaranteed to be in a contiguous segment at a fixed location. As such their // positions are indicated by outputNodeIdxArr, and so we package up this array with the node signal // array to abstract away the indirection described by outputNodeIdxArr. _outputVector = new MappingVector <double>(_activationArr, digraph.OutputNodeIdxArr); }
/// <summary> /// Constructs a CyclicNetwork with the provided neural net definition. /// </summary> public NeuralNetCyclic( DirectedGraph digraph, double[] weightArr, VecFnSegment2 <double> activationFn, int cyclesPerActivation) { Debug.Assert(digraph.ConnectionIdArrays._sourceIdArr.Length == weightArr.Length); // Store refs to network structure data. _srcIdArr = digraph.ConnectionIdArrays._sourceIdArr; _tgtIdArr = digraph.ConnectionIdArrays._targetIdArr; _weightArr = weightArr; // Store network activation function and parameters. _activationFn = activationFn; _cyclesPerActivation = cyclesPerActivation; // Store input/output node counts. _inputCount = digraph.InputCount; _outputCount = digraph.OutputCount; // Create node pre- and post-activation signal arrays. int nodeCount = digraph.TotalNodeCount; _preActivationArr = new double[nodeCount]; _postActivationArr = new double[nodeCount]; // Wrap sub-ranges of the neuron signal arrays as input and output vectors. _inputVector = new VectorSegment <double>(_postActivationArr, 0, _inputCount); // Note. Output neurons follow input neurons in the arrays. _outputVector = new VectorSegment <double>(_postActivationArr, _inputCount, _outputCount); }
public void SimpleSegment2() { int[] innerArr = Enumerable.Range(0, 10).ToArray(); var vecSeg = new VectorSegment <int>(innerArr, 0, 10); // Test the segment yields the expected values. int[] expectedArr = Enumerable.Range(0, 10).ToArray(); Compare(expectedArr, vecSeg); }
public void SimpleSegment1() { int[] innerArr = Enumerable.Range(0, 10).ToArray(); var vecSeg = new VectorSegment <int>(innerArr, 5, 3); // Test the segment yields the expected values. int[] expectedArr = new int[] { 5, 6, 7 }; ConponentwiseEqual(expectedArr, vecSeg); }
public void CopyToTest3() { int[] innerArr = Enumerable.Range(0, 10).ToArray(); var vecSeg = new VectorSegment <int>(innerArr, 5, 3); int[] tgtArr = new int[12]; vecSeg.CopyTo(tgtArr, 2, 1, 2); // Test the segment yields the expected values. int[] expectedArr = new int[] { 0, 0, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0 }; Compare(expectedArr, tgtArr); }
public void CopyFromTest4() { int[] innerArr = Enumerable.Range(0, 10).ToArray(); var vecSeg = new VectorSegment <int>(innerArr, 5, 3); int[] srcArr = Enumerable.Range(100, 10).ToArray(); vecSeg.CopyFrom(srcArr, 5, 1, 2); // Test the segment yields the expected values. int[] expectedInnerArr = new int[] { 0, 1, 2, 3, 4, 5, 105, 106, 8, 9 }; Compare(expectedInnerArr, innerArr); }
public void CopyFromTest1() { int[] innerArr = Enumerable.Range(0, 10).ToArray(); var vecSeg = new VectorSegment <int>(innerArr, 5, 3); int[] srcArr = Enumerable.Range(100, 3).ToArray(); vecSeg.CopyFrom(srcArr, 0); // Test the segment yields the expected values. int[] expectedInnerArr = new int[] { 0, 1, 2, 3, 4, 100, 101, 102, 8, 9 }; Assert.Equal(expectedInnerArr, innerArr); }
public void CopyFromTest_InvalidCopyOperations() { int[] innerArr = Enumerable.Range(0, 10).ToArray(); var vecSeg = new VectorSegment <int>(innerArr, 5, 4); int[] srcArr = Enumerable.Range(100, 3).ToArray(); //--- Two param tests. // Copy beyond end of vecSeg. Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyFrom(srcArr, 2)); // Invalid target index. Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyFrom(srcArr, -1)); //--- Three param tests. // Copy length longer than srcArr. Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyFrom(srcArr, 0, 4)); // Invalid source index. Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyFrom(srcArr, -1, 1)); // Invalid length. Assert.ThrowsException <ArgumentOutOfRangeException>(() => vecSeg.CopyFrom(srcArr, 0, -1)); // Copy beyond the end of vecSeg. Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyFrom(srcArr, 2, 3)); Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyFrom(srcArr, 3, 2)); Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyFrom(srcArr, 4, 1)); // Four param tests. // Copy beyond end of srcArr. Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyFrom(srcArr, 0, 0, 4)); Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyFrom(srcArr, 1, 0, 3)); Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyFrom(srcArr, 2, 0, 2)); Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyFrom(srcArr, 3, 0, 1)); // Copy beyond the end of vecSeg. Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyFrom(srcArr, 0, 3, 2)); Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyFrom(srcArr, 0, 2, 3)); // Invalid source and target indexes. Assert.ThrowsException <ArgumentOutOfRangeException>(() => vecSeg.CopyFrom(srcArr, -1, 0, 1)); Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyFrom(srcArr, 0, -1, 1)); // Invalid length. Assert.ThrowsException <ArgumentOutOfRangeException>(() => vecSeg.CopyFrom(srcArr, 0, 0, -1)); }
public void CopyToTest_InvalidCopyOperations() { int[] innerArr = Enumerable.Range(0, 10).ToArray(); var vecSeg = new VectorSegment <int>(innerArr, 5, 3); int[] tgtArr = new int[12]; //--- Two param tests. // Copy beyond end of tgtArr. Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyTo(tgtArr, 10)); // Invalid target index. Assert.ThrowsException <ArgumentOutOfRangeException>(() => vecSeg.CopyTo(tgtArr, -1)); //--- Three param tests. // Copy length longer then vecSeg length. Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyTo(tgtArr, 0, 4)); // Invalid target index. Assert.ThrowsException <ArgumentOutOfRangeException>(() => vecSeg.CopyTo(tgtArr, -1, 1)); // Invalid length. Assert.ThrowsException <ArgumentOutOfRangeException>(() => vecSeg.CopyTo(tgtArr, 0, -1)); // Copy beyond end of tgtArr. Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyTo(tgtArr, 11, 2)); Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyTo(tgtArr, 12, 1)); //--- Four param tests. // Copy beyond end of vecSeg. Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyTo(tgtArr, 0, 1, 3)); // Copy beyond end of tgtArr. Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyTo(tgtArr, 11, 1, 2)); Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyTo(tgtArr, 12, 1, 1)); // Invalid source and target indexes. Assert.ThrowsException <ArgumentOutOfRangeException>(() => vecSeg.CopyTo(tgtArr, -1, 0, 1)); Assert.ThrowsException <ArgumentException>(() => vecSeg.CopyTo(tgtArr, 0, -1, 1)); // Invalid length. Assert.ThrowsException <ArgumentOutOfRangeException>(() => vecSeg.CopyTo(tgtArr, 0, 1, -1)); }
private Vector3 GetClosestPathPosition(Vector3 p, out int segIndex) { float minDSquared = -1; Vector3 closest = p; segIndex = -1; for (int i = 0; i < segs.Count; i++) { VectorSegment s = segs[i]; Vector3 candidate = s.ClosestPoint(p); float dSquared = (p - candidate).sqrMagnitude; if (minDSquared < 0 || dSquared < minDSquared) { segIndex = i; closest = candidate; minDSquared = dSquared; } } return(closest); }
/// <summary> /// Constructs a CyclicNetwork with the provided neural net definition parameters. /// </summary> public CyclicNeuralNet( WeightedDirectedGraph <double> digraph, VecFnSegment2 <double> activationFn, int activationCount, bool boundedOutput) { // Store refs to network structure data. _srcIdArr = digraph.ConnectionIdArrays._sourceIdArr; _tgtIdArr = digraph.ConnectionIdArrays._targetIdArr; _weightArr = digraph.WeightArray; // Store network activation function and parameters. _activationFn = activationFn; _activationCount = activationCount; // Store input/output node counts. _inputCount = digraph.InputCount; _outputCount = digraph.OutputCount; // Create node pre- and post-activation signal arrays. int nodeCount = digraph.TotalNodeCount; _preActivationArr = new double[nodeCount]; _postActivationArr = new double[nodeCount]; // Wrap sub-ranges of the neuron signal arrays as input and output vectors. _inputVector = new VectorSegment <double>(_postActivationArr, 0, _inputCount); // Note. Output neurons follow input neurons in the arrays. var outputVec = new VectorSegment <double>(_postActivationArr, _inputCount, _outputCount); if (boundedOutput) { _outputVector = new BoundedVector(outputVec); } else { _outputVector = outputVec; } }