Ejemplo n.º 1
0
        public static bool Test1()
        {
            var i1 = new InputNode();
            var i2 = new InputNode();
            var h1 = new Perceptron(new Linear());
            var h2 = new Perceptron(new Linear());
            var h3 = new Perceptron(new Linear());
            LinearWeight.Connect(i1, h1, 1.0);
            LinearWeight.Connect(i1, h2, 1.0);
            LinearWeight.Connect(i2, h2, 1.0);
            LinearWeight.Connect(i2, h3, 1.0);
            var ib = new BiasNode();
            LinearWeight.Connect(ib, h1, 1.0);
            LinearWeight.Connect(ib, h2, 2.0);
            LinearWeight.Connect(ib, h3, 1.0);
            var o = new Perceptron(new Linear());
            LinearWeight.Connect(h1, o, 1.0);
            LinearWeight.Connect(h2, o, -2.0);
            LinearWeight.Connect(h3, o, 1.0);
            var hb = new BiasNode();
            var whbo = new LinearWeight(hb, o);
            whbo[0] = 1.0;

            var network = new GeneralNetwork(new List<InputNode> {i1, i2}, new List<INode>{o});
            network.Forward(new[] { 0.0, 0.0 });
            network.Forward(new[] { 1.0, 0.0 });
            network.Forward(new[] { 0.0, 1.0 });
            network.Forward(new[] { 1.0, 1.0 });
            return true;
        }
    public void initializeNetwork()
    {
        // add two empty lists to the network (inputs and outputs)
        network.net.Add(new List <Node>());
        network.net.Add(new List <Node>());
        // bias node added to input nodes
        BiasNode bn = new BiasNode();

        bn.setBias(1);
        // TODO: remember to add a bias node whenever a hidden layer is created
        network.net[0].Add(bn);
    }
Ejemplo n.º 3
0
 /// <summary>
 ///     Overridden.Constructs network topology.
 /// </summary>
 /// <remarks>Creates nodes, links and connects nodes using created links.</remarks>
 protected override void CreateNetwork()
 {
     nodes = new NeuroNode[NodesCount];
     links = new NeuroLink[LinksCount];
     for (var i = 0; i < InputNodesCount; i++)
     {
         nodes[i] = new InputNode();
     }
     nodes[NodesCount - 2] = new BiasNode(1);
     nodes[NodesCount - 1] = new AdalineNode(LearningRate);
     for (var i = 0; i < LinksCount; i++)
     {
         links[i] = new AdalineLink();
     }
     for (var i = 0; i < LinksCount; i++)
     {
         nodes[i].LinkTo(nodes[NodesCount - 1], links[i]);
     }
 }
Ejemplo n.º 4
0
            public static IList <T> CreateGenericNode <T>(int numberOfNodes)
            {
                var       t            = typeof(T);
                int       index        = _registeredTypes.IndexOf(t);
                var       typeToCreate = _registeredTypes[index];
                IList <T> list         = new List <T>();

                if (typeToCreate == typeof(InputNode))
                {
                    for (int i = 0; i < numberOfNodes; i++)
                    {
                        InputNode node = new InputNode();
                        list.Add((T)(object)node);
                    }
                }
                else if (typeToCreate == typeof(HiddenNode))
                {
                    for (int i = 0; i < numberOfNodes; i++)
                    {
                        HiddenNode node = new HiddenNode();
                        list.Add((T)(object)node);
                    }
                }
                else if (typeToCreate == typeof(OutputNode))
                {
                    for (int i = 0; i < numberOfNodes; i++)
                    {
                        OutputNode node = new OutputNode();
                        list.Add((T)(object)node);
                    }
                }
                else if (typeToCreate == typeof(BiasNode))
                {
                    for (int i = 0; i < numberOfNodes; i++)
                    {
                        BiasNode node = new BiasNode();
                        list.Add((T)(object)node);
                    }
                }

                return(list);
            }
    protected void SetUp()
    {
        // Use the Assert class to test conditions.
        creat   = new Creature();
        network = new Network();
        network.net.Add(new List <Node>());
        network.net.Add(new List <Node>());
        BiasNode inNode1 = new BiasNode();

        inNode1.value = 2;
        BiasNode inNode2 = new BiasNode();

        inNode2.value = 3;

        network.net[0].Add(inNode1);
        network.net[0].Add(inNode2);

        outNode    = new OutputNode(network, creat, 1);
        activInput = 2 * outNode.weights[0] + 3 * outNode.weights[1];
    }
Ejemplo n.º 6
0
        public static bool Test1()
        {
            var i1 = new InputNode();
            var i2 = new InputNode();
            var h1 = new Perceptron(new Linear());
            var h2 = new Perceptron(new Linear());
            var h3 = new Perceptron(new Linear());

            LinearWeight.Connect(i1, h1, 1.0);
            LinearWeight.Connect(i1, h2, 1.0);
            LinearWeight.Connect(i2, h2, 1.0);
            LinearWeight.Connect(i2, h3, 1.0);
            var ib = new BiasNode();

            LinearWeight.Connect(ib, h1, 1.0);
            LinearWeight.Connect(ib, h2, 2.0);
            LinearWeight.Connect(ib, h3, 1.0);
            var o = new Perceptron(new Linear());

            LinearWeight.Connect(h1, o, 1.0);
            LinearWeight.Connect(h2, o, -2.0);
            LinearWeight.Connect(h3, o, 1.0);
            var hb   = new BiasNode();
            var whbo = new LinearWeight(hb, o);

            whbo[0] = 1.0;

            var network = new GeneralNetwork(new List <InputNode> {
                i1, i2
            }, new List <INode> {
                o
            });

            network.Forward(new[] { 0.0, 0.0 });
            network.Forward(new[] { 1.0, 0.0 });
            network.Forward(new[] { 0.0, 1.0 });
            network.Forward(new[] { 1.0, 1.0 });
            return(true);
        }
        public override void CreateNetwork()
        {
            Nodes = new NeuralNodeBase[NodeCount];
            Links = new NeuralLink[LinkCount];

            for (var i = 0; i < NodeCount - 2; i++)     // Create Input Nodes
            {
                Nodes[i] = new InputNode();
            }

            Nodes[NodeCount - 2] = new BiasNode();      // Create Bias Node
            Nodes[NodeCount - 1] = new AdalineNode(LearningRate);

            for (var i = 0; i < LinkCount; i++)
            {
                var l = new AdalineLink(Nodes[i], Nodes[NodeCount - 1]); // Create links
                Links[i] = l;
            }

            for (var i = 0; i < LinkCount; i++)           // Connect inputs to ADALINE
            {
                Nodes[i].OutLinks.Add(Links[i]);
                Nodes[NodeCount - 1].InLinks.Add(Links[i]);
                Links[i].InNode  = Nodes[i];
                Links[i].OutNode = Nodes[NodeCount - 1];

                var inNode  = Nodes[i];
                var outNode = Nodes[NodeCount - 1];
                var link    = Links[i];

                outNode.InLinks.Add(link);
                inNode.OutLinks.Add(link);

                link.InNode  = inNode;
                link.OutNode = outNode;
            }
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            CreateInputPatterns();
            //
            // TODO: Add code to start application here
            //

            Console.WriteLine("Network class test......");

            //NeuralNodeBase [] nodes = new NeuralNodeBase[5];

//			for( int i=0; i<5; i++ )
//			{
//				nodes[i] = new FeedbackNode();
//				nodes[i].Name = string.Format( "Node:{0}", i.ToString() );
//			}
//
//			Console.WriteLine("Creatiung a network, it should look like this:");
//			Console.WriteLine("(0)--\\");
//			Console.WriteLine("(1)===\\");
//			Console.WriteLine("		 (4)");
//			Console.WriteLine("(2)===/");
//			Console.WriteLine("(3)--/");


            NeuralNodeBase [] nodes = new NeuralNodeBase[4];
            NeuralLink []     Link  = new NeuralLink[3];

            nodes[0] = new InputNode();                      // Create Nodes for Network
            nodes[1] = new InputNode();

            nodes[0].Name = "Input1";
            nodes[1].Name = "Input2";

            nodes[2]      = new BiasNode();
            nodes[2].Name = "BiasNode";

            nodes[3]      = new AdalineNode(0.45);         // ADALINE node with learning rate of 0.45
            nodes[3].Name = "AdalineNode";

            Link[0] = new AdalineLink(nodes[0], nodes[3]);                      // Create Links for Network
            Link[1] = new AdalineLink(nodes[1], nodes[3]);
            Link[2] = new AdalineLink(nodes[2], nodes[3]);

            NeuralNodeBase.Link(nodes[0], nodes[3], Link[0]);               // Connect Network
            NeuralNodeBase.Link(nodes[1], nodes[3], Link[1]);
            NeuralNodeBase.Link(nodes[2], nodes[3], Link[2]);

            // add output links
//			nodes[0].Link( nodes[4], LinkDirection.OUTPUT );
//			nodes[1].Link( nodes[4], LinkDirection.OUTPUT );
//			nodes[2].Link( nodes[4], LinkDirection.OUTPUT );
//			nodes[3].Link( nodes[4], LinkDirection.OUTPUT );
//
//			// add input links
//			nodes[4].Link( nodes[0], LinkDirection.INPUT );
//			nodes[4].Link( nodes[1], LinkDirection.INPUT );
//			nodes[4].Link( nodes[2], LinkDirection.INPUT );
//			nodes[4].Link( nodes[3], LinkDirection.INPUT );

            // let's see
            Console.WriteLine("Examining Links.....");

            for (int i = 0; i < nodes.Length; i++)
            {
                Console.WriteLine("Node Index: {0}, Node Name: {1}", i, nodes[i].Name);
                Console.WriteLine("   Input Nodes ({0}):", nodes[i].InLinks.Count);
                foreach (NeuralLink link in nodes[i].InLinks)
                {
                    Console.WriteLine("      In Node:{0} Out Node:{1}", link.InNode.Name, link.OutNode.Name);
                }

                Console.WriteLine("   Output Nodes ({0}):", nodes[i].OutLinks.Count);
                foreach (NeuralLink link in nodes[i].OutLinks)
                {
                    Console.WriteLine("      In Node:{0} Out Node:{1}", link.InNode.Name, link.OutNode.Name);
                }
            }

            Console.WriteLine("Press enter to continue....");
            Console.Read();
        }
Ejemplo n.º 9
0
 /// <summary>
 ///     Overridden.Constructs network topology.
 /// </summary>
 /// <remarks>Creates nodes, links and connects nodes using created links.</remarks>
 protected override void CreateNetwork()
 {
     nodes = new NeuroNode[NodesCount];
     links = new NeuroLink[LinksCount];
     for (var i = 0; i < InputNodesCount; i++)
         nodes[i] = new InputNode();
     nodes[NodesCount - 2] = new BiasNode(1);
     nodes[NodesCount - 1] = new AdalineNode(LearningRate);
     for (var i = 0; i < LinksCount; i++)
         links[i] = new AdalineLink();
     for (var i = 0; i < LinksCount; i++)
         nodes[i].LinkTo(nodes[NodesCount - 1], links[i]);
 }
Ejemplo n.º 10
0
    public static Node getNewNode(Node oldNode, Creature creatureCopy, Network parentNet)
    {
        if (oldNode.GetType().Name == "SensoryInputNode")
        {
            SensoryInputNode newNode = (SensoryInputNode)oldNode.clone();
            newNode.creature = creatureCopy;
            return(newNode);
        }
        else if (oldNode.GetType().Name == "InternalResourceInputNode")
        {
            InternalResourceInputNode newNode = (InternalResourceInputNode)oldNode.clone();
            newNode.creature = creatureCopy;
            return(newNode);
        }
        else if (oldNode.GetType().Name == "OutputNode")
        {
            OutputNode oldNode2 = (OutputNode)oldNode;
            OutputNode newNode  = (OutputNode)oldNode.clone();
            newNode.resetRand();
            newNode.parentNet      = parentNet;
            newNode.parentCreature = creatureCopy;
            newNode.action         = getNewAction(newNode.action);
            //newNode.setActivBehavior(new LogisticActivBehavior());
            newNode.prevNodes = new List <Node>();
            newNode.assignPrevNodes();
            newNode.weights = new List <float>();
            for (int i = 0; i < oldNode2.weights.Count; i++)
            {
                newNode.weights.Add(oldNode2.weights[i]);
            }
            newNode.extraWeights = new List <float>();
            for (int i = 0; i < oldNode2.extraWeights.Count; i++)
            {
                newNode.extraWeights.Add(oldNode2.extraWeights[i]);
            }
            return(newNode);
        }
        else if (oldNode.GetType().Name == "NonInputNode")
        {
            NonInputNode oldNode2 = (NonInputNode)oldNode;
            NonInputNode newNode  = (NonInputNode)oldNode.clone();
            newNode.resetRand();
            newNode.parentNet      = parentNet;
            newNode.parentCreature = creatureCopy;
            //newNode.setActivBehavior(new LogisticActivBehavior());
            newNode.prevNodes = new List <Node>();
            newNode.assignPrevNodes();
            newNode.weights = new List <float>();
            for (int i = 0; i < oldNode2.weights.Count; i++)
            {
                newNode.weights.Add(oldNode2.weights[i]);
            }
            newNode.extraWeights = new List <float>();
            for (int i = 0; i < oldNode2.extraWeights.Count; i++)
            {
                newNode.extraWeights.Add(oldNode2.extraWeights[i]);
            }
            return(newNode);
        }
        else if (oldNode.GetType().Name == "BiasNode")
        {
            BiasNode oldNode2 = (BiasNode)oldNode;
            BiasNode newNode  = oldNode2.clone();
            return(newNode);
        }

        else if (oldNode.GetType().Name == "InnerInputNode")
        {
            InnerInputNode oldNode2 = (InnerInputNode)oldNode;
            InnerInputNode newNode  = oldNode2.clone();
            newNode.parentCreature = creatureCopy;

            /*
             * Debug.Log("linked network layer number: " + oldNode2.linkedNodeNetworkLayer);
             * Debug.Log("net name: " + oldNode2.linkedNetName);
             * Debug.Log("node index: " + newNode.linkedNodeIndex);
             */
            Network linkedNetwork = creatureCopy.networks[oldNode2.linkedNodeNetworkLayer][oldNode2.linkedNetName];
            newNode.linkedNode = linkedNetwork.net[linkedNetwork.net.Count - 1][newNode.linkedNodeIndex];
            return(newNode);
        }
        // called when template is being copied
        else if (oldNode.GetType().Name == "PhenotypeInputNode")
        {
            PhenotypeInputNode oldNode2 = (PhenotypeInputNode)oldNode;
            PhenotypeInputNode newNode  = (PhenotypeInputNode)oldNode.clone();
            // copy phenotype
            newNode.parentCreat = creatureCopy;
            int length = oldNode2.phenotype.Length;
            newNode.phenotype = new bool[length];
            for (int i = 0; i < newNode.phenotype.Length; i++)
            {
                newNode.phenotype[i] = oldNode2.phenotype[i];
            }
            return(newNode);
        }

        else if (oldNode.GetType().Name == "CommInputNode")
        {
            //TODO
            return(null);
        }
        else if (oldNode.GetType().Name == "MemoryInputNode")
        {
            //TODO
            return(null);
        }
        else
        {
            Debug.LogError("Didn't find correct type of node to add");
            return(null);
        }
    }