public void Complex_WeightOne()
    {
        var connList = new LightweightList <WeightedDirectedConnection <double> >
        {
            new WeightedDirectedConnection <double>(0, 4, 1.0),
            new WeightedDirectedConnection <double>(1, 4, 1.0),
            new WeightedDirectedConnection <double>(1, 5, 1.0),
            new WeightedDirectedConnection <double>(3, 4, 1.0),
            new WeightedDirectedConnection <double>(4, 2, 0.9),
            new WeightedDirectedConnection <double>(5, 3, 1.0)
        };
        var connSpan = connList.AsSpan();

        // Create graph.
        var digraph = WeightedDirectedGraphAcyclicBuilder <double> .Create(connSpan, 2, 2);

        // Create neural net and run tests.
        var actFn = new Logistic();
        var net   = new NeuralNetAcyclic(digraph, actFn.Fn);

        Complex_WeightOne_Inner(net, actFn);

        // Create vectorized neural net and run tests.
        var vnet = new NeuralNets.Double.Vectorized.NeuralNetAcyclic(digraph, actFn.Fn);

        Complex_WeightOne_Inner(vnet, actFn);
    }
Пример #2
0
        public void SimpleAcyclic_DefinedNodes_NodeIdGap()
        {
            // Simple acyclic graph.
            var connList = new LightweightList <DirectedConnection>
            {
                new DirectedConnection(100, 103),
                new DirectedConnection(101, 103),
                new DirectedConnection(102, 103),
                new DirectedConnection(102, 104)
            };

            // Create graph.
            var digraph = DirectedGraphBuilder.Create(connList.AsSpan(), 0, 10);

            // The gaps in the node IDs should be removed such that node IDs form a contiguous span starting from zero.
            var connListExpected = new LightweightList <DirectedConnection>
            {
                new DirectedConnection(10, 13),
                new DirectedConnection(11, 13),
                new DirectedConnection(12, 13),
                new DirectedConnection(12, 14)
            };

            CompareConnectionLists(connListExpected.AsSpan(), digraph.ConnectionIdArrays);

            // Check the node count.
            Assert.Equal(15, digraph.TotalNodeCount);
        }
    public void SimpleAcyclic_A1()
    {
        // Simple acyclic graph.
        var connList = new LightweightList <DirectedConnection>
        {
            new DirectedConnection(0, 3),
            new DirectedConnection(1, 3),
            new DirectedConnection(2, 3),
            new DirectedConnection(2, 4),
            new DirectedConnection(4, 3)
        };

        // Create graph.
        var connSpan = connList.AsSpan();

        connSpan.Sort();
        var digraph = DirectedGraphBuilder.Create(connSpan, 3, 2);

        // Perform depth analysis.
        GraphDepthInfo depthInfo = new CyclicGraphDepthAnalysis().CalculateNodeDepths(digraph);

        // Assertions.
        Assert.Equal(3, depthInfo._graphDepth);
        Assert.Equal(5, depthInfo._nodeDepthArr.Length);

        // Node depths.
        Assert.Equal(0, depthInfo._nodeDepthArr[0]);
        Assert.Equal(0, depthInfo._nodeDepthArr[1]);
        Assert.Equal(0, depthInfo._nodeDepthArr[2]);
        Assert.Equal(2, depthInfo._nodeDepthArr[3]);
        Assert.Equal(1, depthInfo._nodeDepthArr[4]);
    }
Пример #4
0
        private static DirectedGraphViewModel CreateGraphViewModel()
        {
            // Simple acyclic graph.
            var connList = new LightweightList <WeightedDirectedConnection <float> >
            {
                new WeightedDirectedConnection <float>(0, 4, 1f),
                new WeightedDirectedConnection <float>(4, 5, 2f),
                new WeightedDirectedConnection <float>(5, 2, 3f),
                new WeightedDirectedConnection <float>(1, 2, 4f),
                new WeightedDirectedConnection <float>(2, 2, 5f),
                new WeightedDirectedConnection <float>(2, 3, 5f),
                new WeightedDirectedConnection <float>(2, 4, 5f),
                new WeightedDirectedConnection <float>(2, 5, 5f)
            };
            var connSpan = connList.AsSpan();

            connSpan.Sort(WeightedDirectedConnectionComparer <float> .Default);

            // Create graph.
            var digraph = WeightedDirectedGraphBuilder <float> .Create(connSpan, 2, 2);

            // Create graph view model, and return.
            INodeIdMap             nodeIdByIdx    = CreateNodeIdByIdx(digraph.TotalNodeCount);
            DirectedGraphViewModel graphViewModel = new(digraph, digraph.WeightArray, nodeIdByIdx);

            return(graphViewModel);
        }
        public void SimpleAcyclic_DefinedNodes_NodeIdGap()
        {
            // Simple acyclic graph.
            var connList = new LightweightList <WeightedDirectedConnection <double> >
            {
                new WeightedDirectedConnection <double>(100, 103, 0.0),
                new WeightedDirectedConnection <double>(101, 103, 1.0),
                new WeightedDirectedConnection <double>(102, 103, 2.0),
                new WeightedDirectedConnection <double>(102, 104, 3.0)
            };
            var connSpan = connList.AsSpan();

            // Create graph.
            var digraph = WeightedDirectedGraphBuilder <double> .Create(connSpan, 0, 10);

            // The gaps in the node IDs should be removed such that node IDs form a contiguous span starting from zero.
            var connListExpected = new LightweightList <WeightedDirectedConnection <double> >
            {
                new WeightedDirectedConnection <double>(10, 13, 0.0),
                new WeightedDirectedConnection <double>(11, 13, 1.0),
                new WeightedDirectedConnection <double>(12, 13, 2.0),
                new WeightedDirectedConnection <double>(12, 14, 3.0)
            };
            var connSpanExpected = connListExpected.AsSpan();

            // The graph should be unchanged from the input connections.
            CompareConnectionLists(connSpanExpected, digraph.ConnectionIdArrays, digraph.WeightArray);

            // Check the node count.
            Assert.Equal(15, digraph.TotalNodeCount);
        }
        public void Regression1()
        {
            // Simple acyclic graph.
            var connList = new LightweightList <DirectedConnection>
            {
                new DirectedConnection(0, 2),
                new DirectedConnection(0, 3),
                new DirectedConnection(0, 4),
                new DirectedConnection(2, 5),
                new DirectedConnection(3, 6),
                new DirectedConnection(4, 7),
                new DirectedConnection(5, 8),
                new DirectedConnection(6, 4),
                new DirectedConnection(6, 9),
                new DirectedConnection(7, 10),
                new DirectedConnection(8, 1),
                new DirectedConnection(9, 1),
                new DirectedConnection(10, 1)
            };

            // Create graph.
            var connSpan = connList.AsSpan();

            connSpan.Sort();
            var digraph = DirectedGraphBuilder.Create(connSpan, 0, 0);

            // Test if cyclic.
            var  cyclicGraphCheck = new CyclicGraphCheck();
            bool isCyclic         = cyclicGraphCheck.IsCyclic(digraph);

            Assert.False(isCyclic);
        }
    public void ComplexCyclic()
    {
        var connList = new LightweightList <WeightedDirectedConnection <double> >
        {
            new WeightedDirectedConnection <double>(0, 1, -2.0),
            new WeightedDirectedConnection <double>(0, 2, 1.0),
            new WeightedDirectedConnection <double>(1, 2, 1.0),
            new WeightedDirectedConnection <double>(2, 1, 1.0)
        };
        var connSpan = connList.AsSpan();

        // Create graph.
        var digraph = WeightedDirectedGraphBuilder <double> .Create(connSpan, 1, 1);

        // Create neural net and run tests.
        var actFn = new Logistic();
        var net   = new NeuralNetCyclic(digraph, actFn.Fn, 1);

        ComplexCyclic_Inner(net, actFn);

        // Create vectorized neural net and run tests.
        var vnet = new NeuralNets.Double.Vectorized.NeuralNetCyclic(digraph, actFn.Fn, 1);

        ComplexCyclic_Inner(vnet, actFn);
    }
        public void SimpleCyclic_DefinedNodes_NodeIdGap()
        {
            // Simple acyclic graph.
            var connList = new LightweightList <DirectedConnection>
            {
                new DirectedConnection(100, 103),
                new DirectedConnection(101, 103),
                new DirectedConnection(102, 103),
                new DirectedConnection(102, 104),
                new DirectedConnection(104, 101),
                new DirectedConnection(101, 102)
            };

            // Create graph.
            var connSpan = connList.AsSpan();

            connSpan.Sort();
            var digraph = DirectedGraphBuilder.Create(connSpan, 0, 10);

            // Test if cyclic.
            var  cyclicGraphCheck = new CyclicGraphCheck();
            bool isCyclic         = cyclicGraphCheck.IsCyclic(digraph);

            Assert.True(isCyclic);
        }
        public void SimpleCyclic()
        {
            // Simple acyclic graph.
            var connList = new LightweightList <DirectedConnection>
            {
                new DirectedConnection(0, 3),
                new DirectedConnection(1, 3),
                new DirectedConnection(2, 3),
                new DirectedConnection(2, 4),
                new DirectedConnection(4, 1),
                new DirectedConnection(1, 2)
            };

            // Create graph.
            var connSpan = connList.AsSpan();

            connSpan.Sort();
            var digraph = DirectedGraphBuilder.Create(connSpan, 0, 0);

            // Test if cyclic.
            var  cyclicGraphCheck = new CyclicGraphCheck();
            bool isCyclic         = cyclicGraphCheck.IsCyclic(digraph);

            Assert.True(isCyclic);
        }
    public void Random1()
    {
        // Simple acyclic graph.
        var connList = new LightweightList <DirectedConnection>
        {
            new DirectedConnection(0, 2),
            new DirectedConnection(0, 3),
            new DirectedConnection(0, 4),
            new DirectedConnection(2, 5),
            new DirectedConnection(2, 10),
            new DirectedConnection(3, 1),
            new DirectedConnection(3, 4),
            new DirectedConnection(3, 6),
            new DirectedConnection(3, 7),
            new DirectedConnection(4, 1),
            new DirectedConnection(4, 6),
            new DirectedConnection(4, 7),
            new DirectedConnection(4, 10),
            new DirectedConnection(5, 4),
            new DirectedConnection(5, 8),
            new DirectedConnection(6, 9),
            new DirectedConnection(7, 9),
            new DirectedConnection(7, 10),
            new DirectedConnection(8, 1),
            new DirectedConnection(8, 3),
            new DirectedConnection(8, 9),
            new DirectedConnection(9, 1),
            new DirectedConnection(10, 1),
        };

        // Create graph.
        var connSpan = connList.AsSpan();

        connSpan.Sort();
        var digraph = DirectedGraphBuilder.Create(connSpan, 1, 1);

        // Depth analysis.
        GraphDepthInfo depthInfo = new AcyclicGraphDepthAnalysis().CalculateNodeDepths(digraph);

        // Assertions.
        Assert.Equal(9, depthInfo._graphDepth);
        Assert.Equal(11, depthInfo._nodeDepthArr.Length);

        // Node depths.
        Assert.Equal(0, depthInfo._nodeDepthArr[0]);
        Assert.Equal(8, depthInfo._nodeDepthArr[1]);
        Assert.Equal(1, depthInfo._nodeDepthArr[2]);
        Assert.Equal(4, depthInfo._nodeDepthArr[3]);
        Assert.Equal(5, depthInfo._nodeDepthArr[4]);
        Assert.Equal(2, depthInfo._nodeDepthArr[5]);
        Assert.Equal(6, depthInfo._nodeDepthArr[6]);
        Assert.Equal(6, depthInfo._nodeDepthArr[7]);
        Assert.Equal(3, depthInfo._nodeDepthArr[8]);
        Assert.Equal(7, depthInfo._nodeDepthArr[9]);
        Assert.Equal(7, depthInfo._nodeDepthArr[10]);
    }
    /// <summary>
    /// Construct with the given acyclic flag and initial capacity.
    /// </summary>
    /// <param name="isAcyclic">Indicates whether we are building acyclic networks or not.</param>
    /// <param name="capacity">Initial capacity.</param>
    public ConnectionGeneListBuilder(bool isAcyclic, int capacity)
    {
        _isAcyclic = isAcyclic;
        if (_isAcyclic)
        {
            _cyclicCheck = new CyclicConnectionCheck();
        }

        _connList   = new LightweightList <DirectedConnection>(capacity);
        _weightList = new LightweightList <T>(capacity);
    }
        public void DepthNodeReorderTest()
        {
            // Define graph connections.
            var connList = new LightweightList <WeightedDirectedConnection <double> >
            {
                new WeightedDirectedConnection <double>(0, 4, 0.0),
                new WeightedDirectedConnection <double>(4, 5, 1.0),
                new WeightedDirectedConnection <double>(5, 2, 2.0),
                new WeightedDirectedConnection <double>(1, 2, 3.0),
                new WeightedDirectedConnection <double>(2, 3, 4.0)
            };

            // Create graph.
            var connSpan = connList.AsSpan();

            connSpan.Sort(WeightedDirectedConnectionComparer <double> .Default);
            var digraph = WeightedDirectedGraphAcyclicBuilder <double> .Create(connSpan, 2, 2);

            // 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 connListExpected = new LightweightList <WeightedDirectedConnection <double> >
            {
                new WeightedDirectedConnection <double>(0, 2, 0.0),
                new WeightedDirectedConnection <double>(1, 4, 3.0),
                new WeightedDirectedConnection <double>(2, 3, 1.0),
                new WeightedDirectedConnection <double>(3, 4, 2.0),
                new WeightedDirectedConnection <double>(4, 5, 4.0)
            };

            // Compare actual and expected connections.
            var connSpanExpected = connListExpected.AsSpan();

            CompareConnectionLists(connSpanExpected, digraph.ConnectionIdArrays, digraph.WeightArray);

            // 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, digraph.LayerArray.Length);

            // Check the node count.
            Assert.Equal(6, digraph.TotalNodeCount);
        }
Пример #13
0
        public void SimpleAcyclic_DefinedNodes()
        {
            // Simple acyclic graph.
            var connList = new LightweightList <DirectedConnection>
            {
                new DirectedConnection(10, 13),
                new DirectedConnection(11, 13),
                new DirectedConnection(12, 13),
                new DirectedConnection(12, 14)
            };

            // Create graph.
            var digraph = DirectedGraphBuilder.Create(connList.AsSpan(), 0, 10);

            // The graph should be unchanged from the input connections.
            CompareConnectionLists(connList.AsSpan(), digraph.ConnectionIdArrays);

            // Check the node count.
            Assert.Equal(15, digraph.TotalNodeCount);
        }
Пример #14
0
    private static LightweightList <int>[] BuildNodesByLayer(DirectedGraph digraph)
    {
        // Build an array that gives the node layer for each node, keyed by node index.
        int[] nodeLayerByIdx = BuildNodeLayerByIdx(digraph);

        // Group nodes into layers.
        int layerCount   = MathSpan.Max(nodeLayerByIdx) + 1;
        var nodesByLayer = new LightweightList <int> [layerCount];

        for (int i = 0; i < layerCount; i++)
        {
            nodesByLayer[i] = new LightweightList <int>();
        }

        for (int nodeIdx = 0; nodeIdx < nodeLayerByIdx.Length; nodeIdx++)
        {
            int depth = nodeLayerByIdx[nodeIdx];
            nodesByLayer[depth].Add(nodeIdx);
        }
        return(nodesByLayer);
    }
        public void SimpleAcyclic_DefinedNodes()
        {
            // Simple acyclic graph.
            var connList = new LightweightList <WeightedDirectedConnection <double> >
            {
                new WeightedDirectedConnection <double>(10, 13, 0.0),
                new WeightedDirectedConnection <double>(11, 13, 1.0),
                new WeightedDirectedConnection <double>(12, 13, 2.0),
                new WeightedDirectedConnection <double>(12, 14, 3.0)
            };
            var connSpan = connList.AsSpan();

            // Create graph.
            var digraph = WeightedDirectedGraphBuilder <double> .Create(connSpan, 0, 10);

            // The graph should be unchanged from the input connections.
            CompareConnectionLists(connSpan, digraph.ConnectionIdArrays, digraph.WeightArray);

            // Check the node count.
            Assert.Equal(15, digraph.TotalNodeCount);
        }
    public void SingleInput_WeightOne()
    {
        var connList = new LightweightList <WeightedDirectedConnection <double> > {
            new WeightedDirectedConnection <double>(0, 1, 1.0)
        };
        var connSpan = connList.AsSpan();

        // Create graph.
        var digraph = WeightedDirectedGraphAcyclicBuilder <double> .Create(connSpan, 1, 1);

        // Create neural net and run tests.
        var actFn = new Logistic();
        var net   = new NeuralNetAcyclic(digraph, actFn.Fn);

        SingleInput_WeightOne_Inner(net, actFn);

        // Create vectorized neural net and run tests.
        var vnet = new NeuralNets.Double.Vectorized.NeuralNetAcyclic(digraph, actFn.Fn);

        SingleInput_WeightOne_Inner(vnet, actFn);
    }
    public void ShortAndLongPath()
    {
        // Simple acyclic graph.
        var connList = new LightweightList <DirectedConnection>
        {
            new DirectedConnection(0, 4),
            new DirectedConnection(4, 5),
            new DirectedConnection(5, 2),
            new DirectedConnection(1, 2),
            new DirectedConnection(2, 3)
        };

        // Create graph.
        var connSpan = connList.AsSpan();

        connSpan.Sort();
        var digraph = DirectedGraphBuilder.Create(connSpan, 2, 2);

        // Assert is acyclic.
        var cyclicGraphCheck = new CyclicGraphCheck();

        Assert.False(cyclicGraphCheck.IsCyclic(digraph));

        // Depth analysis.
        GraphDepthInfo depthInfo = new AcyclicGraphDepthAnalysis().CalculateNodeDepths(digraph);

        // Assertions.
        Assert.Equal(5, depthInfo._graphDepth);
        Assert.Equal(6, depthInfo._nodeDepthArr.Length);

        // Node depths.
        Assert.Equal(0, depthInfo._nodeDepthArr[0]);
        Assert.Equal(0, depthInfo._nodeDepthArr[1]);
        Assert.Equal(3, depthInfo._nodeDepthArr[2]);
        Assert.Equal(4, depthInfo._nodeDepthArr[3]);
        Assert.Equal(1, depthInfo._nodeDepthArr[4]);
        Assert.Equal(2, depthInfo._nodeDepthArr[5]);
    }
    public void TwoInputs_WeightHalf()
    {
        var connList = new LightweightList <WeightedDirectedConnection <double> >
        {
            new WeightedDirectedConnection <double>(0, 2, 0.5),
            new WeightedDirectedConnection <double>(1, 2, 0.5)
        };
        var connSpan = connList.AsSpan();

        // Create graph.
        var digraph = WeightedDirectedGraphAcyclicBuilder <double> .Create(connSpan, 2, 1);

        // Create neural net and run tests.
        var actFn = new Logistic();
        var net   = new NeuralNetAcyclic(digraph, actFn.Fn);

        TwoInputs_WeightHalf_Inner(net, actFn);

        // Create vectorized neural net and run tests.
        var vnet = new NeuralNets.Double.Vectorized.NeuralNetAcyclic(digraph, actFn.Fn);

        TwoInputs_WeightHalf_Inner(vnet, actFn);
    }
    public void MultipleInputsOutputs()
    {
        var connList = new LightweightList <WeightedDirectedConnection <double> >
        {
            new WeightedDirectedConnection <double>(0, 5, 1.0),
            new WeightedDirectedConnection <double>(1, 3, 1.0),
            new WeightedDirectedConnection <double>(2, 4, 1.0)
        };
        var connSpan = connList.AsSpan();

        // Create graph.
        var digraph = WeightedDirectedGraphBuilder <double> .Create(connSpan, 3, 3);

        // Create neural net and run tests.
        var actFn = new Logistic();
        var net   = new NeuralNetCyclic(digraph, actFn.Fn, 1);

        MultipleInputsOutputs_Inner(net, actFn);

        // Create neural net and run tests.
        var vnet = new NeuralNets.Double.Vectorized.NeuralNetCyclic(digraph, actFn.Fn, 1);

        MultipleInputsOutputs_Inner(vnet, actFn);
    }