def test_one_edge(self):
     self.graph = Graph(self.nodes, self.edges[0:1])
     self.graph.nodes()[2].update_x(self.graph)
     self.graph.nodes()[2].update_x(self.graph)
     self.graph.nodes()[2].update_x(self.graph)
     problem = MultiAgentProblem(self.graph, 120)
     problem.solve()
     self.assertAlmostEqual(self.graph.nodes()[0].x[0], 1.5, 3)
     self.assertAlmostEqual(self.graph.nodes()[1].x[0], 1.5, 3)
     self.assertAlmostEqual(self.graph.nodes()[2].x[0], 3.0, 3)
    def test_full_graph(self):
        self.graph = Graph(self.nodes, self.edges)
        self.assertEqual(len(self.graph.edges()), 3)
        self.assertEqual(len(self.graph.nodes()), 3)
        self.assertListEqual([len(self.graph.edges(node)) for node in self.graph.nodes()], [2, 2, 2])
        self.assertAlmostEqual(AcceleratedGradient().solve(self.nodes[0].f, numpy.ones(1) * 10), 1.0, 3)
        self.assertAlmostEqual(AcceleratedGradient().solve(self.nodes[1].f, numpy.ones(1) * 10), 2.0, 3)
        self.assertAlmostEqual(AcceleratedGradient().solve(self.nodes[2].f, numpy.ones(1) * 10), 3.0, 3)

        problem = MultiAgentProblem(self.graph, 100)
        problem.solve()
        for node in problem.graph.nodes():
            self.assertAlmostEqual(node.x, 2.0 * numpy.ones(1), 3)
 def test_all(self):
     for i, node in enumerate(self.nodes):
         assert_array_almost_equal(AcceleratedGradient(50).solve(node.f, numpy.ones(2) * 10), [i] * 2)
     graph = Graph(self.nodes, self.edges)
     problem = MultiAgentProblem(graph, 100)
     problem.solve()
     for edge in self.edges:
         assert_array_almost_equal(edge.x_values(edge.l_node), edge.x_values(edge.r_node) * -1.0, 2)
     for i, node in enumerate(self.nodes):
         x = node.x
         if i is 0:
             self.assertAlmostEqual(x[0], 0.0, 2)
         else:
             self.assertAlmostEqual(x[0], i - 0.5, 2)
         if i is self.n - 1:
             self.assertAlmostEqual(x[1], i, 2)
         else:
             self.assertAlmostEqual(x[1], i + 0.5, 2)
 def test_stripped_one(self):
     self.graph = Graph(self.nodes, self.edges[:-1])
     problem = MultiAgentProblem(self.graph, 120)
     problem.solve()
     for node in problem.graph.nodes():
         self.assertAlmostEqual(node.x, 2.0 * numpy.ones(1), 4)