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 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 SingleInput_WeightZero()
        {
            var connList = new List <WeightedDirectedConnection <double> >
            {
                new WeightedDirectedConnection <double>(0, 1, 0.0)
            };

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

            // Create neural net
            var actFn = new LogisticFunction();
            var net   = new CyclicNeuralNet(digraph, actFn.Fn, 2, false);

            // Note. The single connection weight is zero, so the input value has no affect.
            // Activate and test.
            net.InputVector[0] = 100.0;
            net.Activate();
            Assert.AreEqual(0.5, net.OutputVector[0]);

            // Activate and test.
            net.InputVector[0] = 0;
            net.Activate();
            Assert.AreEqual(0.5, net.OutputVector[0]);

            // Activate and test.
            net.InputVector[0] = -100;
            net.Activate();
            Assert.AreEqual(0.5, net.OutputVector[0]);
        }
        public void CyclicOutput()
        {
            var connList = new List <WeightedDirectedConnection <double> >
            {
                new WeightedDirectedConnection <double>(0, 1, 1.0),
                new WeightedDirectedConnection <double>(1, 1, 1.0)
            };

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

            // Create neural net
            var actFn = new LogisticFunction();
            var net   = new CyclicNeuralNet(digraph, actFn.Fn, 1, false);

            // Activate and test.
            const double input    = 0.1;
            double       inputVal = input;

            net.InputVector[0] = inputVal;

            for (int i = 0; i < 10; i++)
            {
                net.Activate();
                double outputExpected = actFn.Fn(inputVal);
                Assert.AreEqual(outputExpected, net.OutputVector[0]);
                inputVal = input + outputExpected;
            }
        }
        public void MultipleInputsOutputs()
        {
            var connList = new List <WeightedDirectedConnection <double> >
            {
                new WeightedDirectedConnection <double>(0, 5, 1.0),
                new WeightedDirectedConnection <double>(1, 3, 1.0),
                new WeightedDirectedConnection <double>(2, 4, 1.0)
            };

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

            // Create neural net
            var actFn = new LogisticFunction();
            var net   = new CyclicNeuralNet(digraph, actFn.Fn, 1, false);

            // Activate and test.
            net.InputVector[0] = 1.0;
            net.InputVector[1] = 2.0;
            net.InputVector[2] = 3.0;
            net.Activate();
            Assert.AreEqual(actFn.Fn(2.0), net.OutputVector[0]);
            Assert.AreEqual(actFn.Fn(3.0), net.OutputVector[1]);
            Assert.AreEqual(actFn.Fn(1.0), net.OutputVector[2]);
        }
Beispiel #6
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);
        }
        /// <summary>
        /// Create with the provided list of connections, and input/output node counts.
        /// </summary>
        /// <param name="connectionList">A list of weighted connections that describe the graph.</param>
        /// <param name="inputCount">Input node count.</param>
        /// <param name="outputCount">Output node count.</param>
        /// <returns>A new instance of <see cref="WeightedDirectedGraphAcyclic{T}"/>.</returns>
        public static WeightedDirectedGraphAcyclic <T> Create(
            IList <WeightedDirectedConnection <T> > connectionList,
            int inputCount, int outputCount)
        {
            // Convert the set of connections to a standardised graph representation.
            WeightedDirectedGraph <T> digraph = WeightedDirectedGraphBuilder <T> .Create(connectionList, inputCount, outputCount);

            // Invoke factory logic specific to acyclic graphs.
            return(Create(digraph));
        }
Beispiel #8
0
        public void SimpleAcyclic_DefinedNodes()
        {
            // Simple acyclic graph.
            var connList = new List <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)
            };

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

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

            // Check the node count.
            Assert.AreEqual(15, digraph.TotalNodeCount);
        }
        public void SimpleAcyclic()
        {
            // Simple acyclic graph.
            var connList = new LightweightList <WeightedDirectedConnection <double> >
            {
                new WeightedDirectedConnection <double>(0, 3, 0.0),
                new WeightedDirectedConnection <double>(1, 3, 1.0),
                new WeightedDirectedConnection <double>(2, 3, 2.0),
                new WeightedDirectedConnection <double>(2, 4, 3.0)
            };
            var connSpan = connList.AsSpan();

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

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

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

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

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

            SingleInput_WeightOne_Inner(net, actFn);

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

            SingleInput_WeightOne_Inner(vnet, actFn);
        }
        public void SingleInput_WeightOne()
        {
            var connList = new List <WeightedDirectedConnection <double> >
            {
                new WeightedDirectedConnection <double>(0, 1, 1.0)
            };

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

            // Create neural net
            var actFn = new LogisticFunction();
            var net   = new CyclicNeuralNet(digraph, actFn.Fn, 1, false);

            // Activate and test.
            net.InputVector[0] = 0.0;
            for (int i = 0; i < 10; i++)
            {
                net.Activate();
                Assert.AreEqual(0.5, net.OutputVector[0]);
            }

            // Activate and test.
            net.InputVector[0] = 1.0;
            for (int i = 0; i < 10; i++)
            {
                net.Activate();
                Assert.AreEqual(actFn.Fn(1), net.OutputVector[0]);
            }

            // Activate and test.
            net.InputVector[0] = 10.0;
            for (int i = 0; i < 10; i++)
            {
                net.Activate();
                Assert.AreEqual(actFn.Fn(10), net.OutputVector[0]);
            }
        }
        public void MultipleInputsOutputs()
        {
            var connList = new List <WeightedDirectedConnection <double> >
            {
                new WeightedDirectedConnection <double>(0, 5, 1.0),
                new WeightedDirectedConnection <double>(1, 3, 1.0),
                new WeightedDirectedConnection <double>(2, 4, 1.0)
            };

            // Create graph.
            var digraph = WeightedDirectedGraphBuilder <double> .Create(connList, 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);
        }
        public void ComplexCyclic()
        {
            var connList = new List <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)
            };

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

            // Create neural net
            var actFn = new LogisticFunction();
            var net   = new CyclicNeuralNet(digraph, actFn.Fn, 1, false);

            // Simulate network in C# and compare calculated outputs with actual network outputs.
            double[] preArr  = new double[3];
            double[] postArr = new double[3];

            postArr[0]         = 3.0;
            net.InputVector[0] = 3.0;

            for (int i = 0; i < 10; i++)
            {
                preArr[1] = postArr[0] * -2.0 + postArr[2];
                preArr[2] = postArr[0] + postArr[1];

                postArr[1] = actFn.Fn(preArr[1]);
                postArr[2] = actFn.Fn(preArr[2]);

                net.Activate();

                Assert.AreEqual(postArr[1], net.OutputVector[0]);
            }
        }