Exemplo n.º 1
0
        public void SerializeDeserializeBinaryTest()
        {
            Network net = new Network();

            Node inNode1    = new Node(Node.ENodeType.SENSOR, 1);
            Node inNode2    = new Node(Node.ENodeType.SENSOR, 2);
            Node hiddenNode = new Node(Node.ENodeType.HIDDEN, 3);
            Node outNode1   = new Node(Node.ENodeType.OUTPUT, 4);
            Node outNode2   = new Node(Node.ENodeType.OUTPUT, 5);

            net.AddNodes(new Node[] { inNode1, inNode2, hiddenNode, outNode1, outNode2 });

            net.AddLink(new Link(inNode1, hiddenNode, false, 1.0f));
            net.AddLink(new Link(inNode2, hiddenNode, false, 2.0f));
            net.AddLink(new Link(hiddenNode, outNode1, false, 3.0f));
            net.AddLink(new Link(hiddenNode, outNode2, false, 4.0f));

            net.ActivationFunction = new TestActivationFunction();

            net.SaveNetworkToFile("./test");

            net = Network.LoadNetworkFromFile("./test");

            // Check nodes
            Assert.AreEqual(5, net.Nodes.Count);
            Assert.AreEqual(2, net.Input.Count);
            Assert.AreEqual(2, net.Output.Count);
            for (int i = 0; i < 5; ++i)
            {
                Assert.AreEqual(i + 1, net.Nodes[i].ID);
            }

            // Check links
            Assert.AreEqual(4, net.Links.Count);
            for (int i = 0; i < 4; ++i)
            {
                Assert.AreEqual(i + 1, net.Links[i].Weight);
                Assert.AreEqual(false, net.Links[i].IsRecurrent);
            }

            Assert.AreEqual(1, net.Links[0].InNode.ID);
            Assert.AreEqual(3, net.Links[0].OutNode.ID);

            Assert.AreEqual(2, net.Links[1].InNode.ID);
            Assert.AreEqual(3, net.Links[1].OutNode.ID);

            Assert.AreEqual(3, net.Links[2].InNode.ID);
            Assert.AreEqual(4, net.Links[2].OutNode.ID);

            Assert.AreEqual(3, net.Links[3].InNode.ID);
            Assert.AreEqual(5, net.Links[3].OutNode.ID);

            Assert.IsTrue(net.ActivationFunction is TestActivationFunction);
        }
Exemplo n.º 2
0
    static void Main(string[] args)
    {
        string scientist = Console.ReadLine();

        if (scientist.Equals(Erdos))
        {
            Console.WriteLine(0);
            return;
        }
        //
        int N = int.Parse(Console.ReadLine());

        for (int i = 0; i < N; i++)
        {
            string title = Console.ReadLine();
            Titles.Add(title);
        }
        for (int i = 0; i < N; i++)
        {
            string authors = Console.ReadLine();
            Authors.Add(authors);
        }

        //
        Network network = new Network();

        //For each title
        for (int i = 0; i < N; i++)
        {
            //Console.Error.WriteLine(Titles[i]);
            string[] names      = Authors[i].Split(' ');
            int      numAuthors = names.Length;
            for (int j = 0; j < numAuthors; j++)
            {
                for (int k = j + 1; k < numAuthors; k++)
                {
                    //Console.Error.WriteLine(j + " " + k);
                    network.AddLink(
                        new Link(names[j], names[k], Titles[i]));
                }
            }
        }

        //
        List <string> works = network.FindPath(Erdos, scientist);

        if (works == null)
        {
            Console.WriteLine("infinite");
            return;
        }
        Console.WriteLine(works.Count());
        foreach (string work in works)
        {
            Console.WriteLine(work);
        }
    }
Exemplo n.º 3
0
        public void ActivateTest_HiddenNotConnected_Expected_True()
        {
            // Create network and leave hidden layer not connected
            Network net = new Network();

            Node inNode1    = new Node(Node.ENodeType.SENSOR, 1);
            Node inNode2    = new Node(Node.ENodeType.SENSOR, 2);
            Node hiddenNode = new Node(Node.ENodeType.HIDDEN, 3);
            Node outNode1   = new Node(Node.ENodeType.OUTPUT, 4);
            Node outNode2   = new Node(Node.ENodeType.OUTPUT, 5);

            net.AddNodes(new Node[] { inNode1, inNode2, hiddenNode, outNode1, outNode2 });

            net.AddLink(new Link(inNode1, outNode1, false, 1.0f));
            net.AddLink(new Link(inNode2, outNode2, false, 1.0f));

            Assert.AreEqual(true, net.Activate());
        }
Exemplo n.º 4
0
        public void SetInputTest_NullInput_Expected_Exception()
        {
            Network net = new Network();

            Node inNode1    = new Node(Node.ENodeType.SENSOR, 1);
            Node inNode2    = new Node(Node.ENodeType.SENSOR, 2);
            Node hiddenNode = new Node(Node.ENodeType.HIDDEN, 3);
            Node outNode1   = new Node(Node.ENodeType.OUTPUT, 4);
            Node outNode2   = new Node(Node.ENodeType.OUTPUT, 5);

            net.AddNodes(new Node[] { inNode1, inNode2, hiddenNode, outNode1, outNode2 });

            net.AddLink(new Link(inNode1, hiddenNode, false, 1.0f));
            net.AddLink(new Link(inNode2, hiddenNode, false, 1.0f));
            net.AddLink(new Link(hiddenNode, outNode1, false, 1.0f));

            net.SetInput(null);
        }
Exemplo n.º 5
0
        public void ActivateTest_OneOutputNotConnected_Expected_False()
        {
            // Create network and leave one of the output nodes unconnected
            Network net = new Network();

            Node inNode1    = new Node(Node.ENodeType.SENSOR, 1);
            Node inNode2    = new Node(Node.ENodeType.SENSOR, 2);
            Node hiddenNode = new Node(Node.ENodeType.HIDDEN, 3);
            Node outNode1   = new Node(Node.ENodeType.OUTPUT, 4);
            Node outNode2   = new Node(Node.ENodeType.OUTPUT, 5);

            net.AddNodes(new Node[] { inNode1, inNode2, hiddenNode, outNode1, outNode2 });

            net.AddLink(new Link(inNode1, hiddenNode, false, 1.0f));
            net.AddLink(new Link(inNode2, hiddenNode, false, 1.0f));
            net.AddLink(new Link(hiddenNode, outNode1, false, 1.0f));

            Assert.AreEqual(false, net.Activate());
        }
Exemplo n.º 6
0
        public void ActivateTest_AllOutputsConnected_Expected_True()
        {
            // Create network and make all outputs connected to the network
            Network net = new Network();

            Node inNode1    = new Node(Node.ENodeType.SENSOR, 1);
            Node inNode2    = new Node(Node.ENodeType.SENSOR, 2);
            Node hiddenNode = new Node(Node.ENodeType.HIDDEN, 3);
            Node outNode1   = new Node(Node.ENodeType.OUTPUT, 4);
            Node outNode2   = new Node(Node.ENodeType.OUTPUT, 5);

            net.AddNodes(new Node[] { inNode1, inNode2, hiddenNode, outNode1, outNode2 });

            net.AddLink(new Link(inNode1, hiddenNode, false, 1.0f));
            net.AddLink(new Link(inNode2, hiddenNode, false, 1.0f));
            net.AddLink(new Link(hiddenNode, outNode1, false, 1.0f));
            net.AddLink(new Link(hiddenNode, outNode2, false, 1.0f));

            Assert.AreEqual(true, net.Activate());
        }
Exemplo n.º 7
0
        public void SetInputTest_Correct()
        {
            Network net = new Network();

            Node inNode1    = new Node(Node.ENodeType.SENSOR, 1);
            Node inNode2    = new Node(Node.ENodeType.SENSOR, 2);
            Node hiddenNode = new Node(Node.ENodeType.HIDDEN, 3);
            Node outNode1   = new Node(Node.ENodeType.OUTPUT, 4);
            Node outNode2   = new Node(Node.ENodeType.OUTPUT, 5);

            net.AddNodes(new Node[] { inNode1, inNode2, hiddenNode, outNode1, outNode2 });

            net.AddLink(new Link(inNode1, hiddenNode, false, 1.0f));
            net.AddLink(new Link(inNode2, hiddenNode, false, 1.0f));
            net.AddLink(new Link(hiddenNode, outNode1, false, 1.0f));

            net.SetInput(new float[] { 1.0f, 2.0f });
            Assert.AreEqual(1.0f, inNode1.Activation);
            Assert.AreEqual(2.0f, inNode2.Activation);
        }
Exemplo n.º 8
0
        public void ComputeNodesActivationSumTest(float expectedOutput, float weight1, float weight2, float weight3)
        {
            Network net = new Network();

            Node inNode1    = new Node(Node.ENodeType.SENSOR, 1);
            Node inNode2    = new Node(Node.ENodeType.SENSOR, 2);
            Node inNode3    = new Node(Node.ENodeType.OUTPUT, 3);
            Node hiddenNode = new Node(Node.ENodeType.HIDDEN, 4);
            Node outNode1   = new Node(Node.ENodeType.OUTPUT, 5);

            net.AddNodes(new Node[] { inNode1, inNode2, inNode3, hiddenNode, outNode1 });

            inNode1.Activation = 1.0f; inNode1.ActivationCount = 1;
            inNode2.Activation = 2.0f; inNode2.ActivationCount = 1;
            inNode3.Activation = 4.0f; inNode3.ActivationCount = 1;

            net.AddLink(new Link(inNode1, hiddenNode, false, weight1));
            net.AddLink(new Link(inNode2, hiddenNode, false, weight2));
            net.AddLink(new Link(hiddenNode, outNode1, false, weight3));

            net.ComputeNodesActivationSum();

            Assert.AreEqual(expectedOutput, hiddenNode.ActivationSum);
        }
Exemplo n.º 9
0
    static void Main(string[] args)
    {
        //
        Network network = new Network();

        string[] inputs;
        inputs = Console.ReadLine().Split(' ');
        int N = int.Parse(inputs[0]); // the total number of nodes in the level, including the gateways
        int L = int.Parse(inputs[1]); // the number of links
        int E = int.Parse(inputs[2]); // the number of exit gateways

        for (int i = 0; i < L; i++)
        {
            inputs = Console.ReadLine().Split(' ');
            int  N1   = int.Parse(inputs[0]); // N1 and N2 defines a link between these nodes
            int  N2   = int.Parse(inputs[1]);
            Link link = new Link(N1, N2);
            network.AddLink(link);
        }
        network.Gateways = new int[E];
        for (int i = 0; i < E; i++)
        {
            int EI = int.Parse(Console.ReadLine()); // the index of a gateway node
            network.Gateways[i] = EI;
        }

        // game loop
        while (true)
        {
            int SI = int.Parse(Console.ReadLine()); // The index of the node on which the Skynet agent is positioned this turn

            //Cut the link if Skynet is next to a gateway
            Link link = network.IsAdjGateway(SI);
            //If not next to a gateway, get the most risky node
            if (link == null)
            {
                link = network.GetLink(SI);
            }
            //
            link.Cut();
            //
            Console.WriteLine(link.node1.ToString() + " " + link.node2.ToString());
        }
    }
Exemplo n.º 10
0
        public static Network GenerateNodes(Network net)
        {
            for (int i = 1; i <= 6; i++)
            {
                net.AddNode("N" + i.ToString());
            }

            net.AddLink("N1", "N2", 5);
            net.AddLink("N1", "N4", 4);
            net.AddLink("N2", "N3", 8);
            net.AddLink("N2", "N4", 1);
            net.AddLink("N3", "N5", 6);
            net.AddLink("N5", "N6", 11);
            net.AddLink("N4", "N6", 4);

            return(net);
        }