Esempio n. 1
0
        public void SingleInput_WeightOne()
        {
            var connList = new List <WeightedDirectedConnection <double> >();

            connList.Add(new WeightedDirectedConnection <double>(0, 1, 1.0));

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

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

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

            // Activate and test.
            net.InputVector[0] = 1.0;
            net.Activate();
            Assert.AreEqual(actFn.Fn(1), net.OutputVector[0]);

            // Activate and test.
            net.InputVector[0] = 10.0;
            net.Activate();
            Assert.AreEqual(actFn.Fn(10.0), net.OutputVector[0]);
        }
Esempio n. 2
0
        public void TwoInputs_WeightHalf()
        {
            var connList = new List <WeightedDirectedConnection <double> >
            {
                new WeightedDirectedConnection <double>(0, 2, 0.5),
                new WeightedDirectedConnection <double>(1, 2, 0.5)
            };

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

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

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

            // Activate and test.
            net.InputVector[0] = 1.0;
            net.InputVector[1] = 2.0;
            net.Activate();
            Assert.AreEqual(actFn.Fn(1.5), net.OutputVector[0]);

            // Activate and test.
            net.InputVector[0] = 10.0;
            net.InputVector[1] = 20.0;
            net.Activate();
            Assert.AreEqual(actFn.Fn(15.0), net.OutputVector[0]);
        }
Esempio n. 3
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 = WeightedAcyclicDirectedGraphBuilder <double> .Create(connList, 3, 3);

            // Create neural net
            var actFn = new LogisticFunction();
            var net   = new AcyclicNeuralNet(digraph, actFn.Fn, 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]);
        }
Esempio n. 4
0
        public void SingleInput_WeightZero()
        {
            var connList = new List <WeightedDirectedConnection <double> >();

            connList.Add(new WeightedDirectedConnection <double>(0, 1, 0.0));

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

            // Create neural net
            var actFn = new LogisticFunction();
            var net   = new AcyclicNeuralNet(digraph, actFn.Fn, 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]);
        }
Esempio n. 5
0
        public void Complex_WeightOne()
        {
            var connList = new List <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)
            };

            // Create graph.
            var digraph = WeightedAcyclicDirectedGraphBuilder <double> .Create(connList, 2, 2);

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

            // Activate and test.
            net.InputVector[0] = 0.5;
            net.InputVector[1] = 0.25;
            net.Activate();

            double output1 = actFn.Fn(actFn.Fn(0.25));

            Assert.AreEqual(output1, net.OutputVector[1]);

            double output0 = actFn.Fn(actFn.Fn(output1 + 0.5 + 0.25) * 0.9);

            Assert.AreEqual(output0, net.OutputVector[0]);
        }
Esempio n. 6
0
        public void HiddenNode()
        {
            var connList = new List <WeightedDirectedConnection <double> >
            {
                new WeightedDirectedConnection <double>(0, 3, 0.5),
                new WeightedDirectedConnection <double>(1, 3, 0.5),
                new WeightedDirectedConnection <double>(3, 2, 2.0)
            };

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

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

            // Activate and test.
            net.InputVector[0] = 0.0;
            net.InputVector[1] = 0.0;
            net.Activate();
            Assert.AreEqual(actFn.Fn(1.0), net.OutputVector[0]);

            // Activate and test.
            net.InputVector[0] = 0.5;
            net.InputVector[1] = 0.25;
            net.Activate();
            Assert.AreEqual(actFn.Fn(actFn.Fn(0.375) * 2.0), net.OutputVector[0]);
        }
        /// <summary>
        /// Decode a genome into a working neural network.
        /// </summary>
        /// <param name="genome">The genome to decode.</param>
        /// <param name="boundedOutput">Indicates whether the output nodes should be bounded to the interval [0,1]</param>
        public IPhenome <double> Decode(
            NeatGenome <double> genome)
        {
            Debug.Assert(genome?.MetaNeatGenome?.IsAcyclic == true);
            Debug.Assert(null != genome?.ConnectionGenes);
            Debug.Assert(genome.ConnectionGenes.Length == genome?.ConnectionIndexMap?.Length);
            Debug.Assert(genome.DirectedGraph is AcyclicDirectedGraph);

            // Create neural net weight array.
            // Note. We cannot use the genome's weight array directly here (as is done in NeatGenomeDecoder,
            // i.e. for cyclic graphs) because the genome connections and digraph connections have a
            // different order.
            double[] neuralNetWeightArr = CreateNeuralNetWeightArray(genome);

            // Create a working neural net.
            var neuralNet = new AcyclicNeuralNet(
                (AcyclicDirectedGraph)genome.DirectedGraph,
                neuralNetWeightArr,
                genome.MetaNeatGenome.ActivationFn.Fn,
                _boundedOutput);

            return(neuralNet);
        }