コード例 #1
0
    def test_solver_options(self):
        prog = mp.MathematicalProgram()

        prog.SetSolverOption(SolverType.kGurobi, "double_key", 1.0)
        prog.SetSolverOption(GurobiSolver().solver_id(), "int_key", 2)
        prog.SetSolverOption(SolverType.kGurobi, "string_key", "3")

        options = prog.GetSolverOptions(SolverType.kGurobi)
        self.assertDictEqual(options, {
            "double_key": 1.0,
            "int_key": 2,
            "string_key": "3"
        })
        options = prog.GetSolverOptions(GurobiSolver().solver_id())
        self.assertDictEqual(options, {
            "double_key": 1.0,
            "int_key": 2,
            "string_key": "3"
        })

        # For now, just make sure the constructor exists.  Once we bind more
        # accessors, we can test them here.
        options_object = SolverOptions()
        solver_id = SolverId("dummy")
        self.assertEqual(solver_id.name(), "dummy")
        options_object.SetOption(solver_id, "double_key", 1.0)
        options_object.SetOption(solver_id, "int_key", 2)
        options_object.SetOption(solver_id, "string_key", "3")
        options = options_object.GetOptions(solver_id)
        self.assertDictEqual(options, {
            "double_key": 1.0,
            "int_key": 2,
            "string_key": "3"
        })
コード例 #2
0
    def test_graph_of_convex_sets(self):
        spp = mut.GraphOfConvexSets()
        source = spp.AddVertex(set=mut.Point([0.1]), name="source")
        target = spp.AddVertex(set=mut.Point([0.2]), name="target")
        edge0 = spp.AddEdge(u=source, v=target, name="edge0")
        edge1 = spp.AddEdge(u_id=source.id(), v_id=target.id(), name="edge1")
        self.assertEqual(len(spp.Vertices()), 2)
        self.assertEqual(len(spp.Edges()), 2)
        result = spp.SolveShortestPath(source_id=source.id(),
                                       target_id=target.id(),
                                       convex_relaxation=True)
        self.assertIsInstance(result, MathematicalProgramResult)
        self.assertIsInstance(
            spp.SolveShortestPath(source_id=source.id(),
                                  target_id=target.id(),
                                  convex_relaxation=True,
                                  solver=ClpSolver()),
            MathematicalProgramResult)
        self.assertIsInstance(
            spp.SolveShortestPath(source_id=source.id(),
                                  target_id=target.id(),
                                  convex_relaxation=True,
                                  solver_options=SolverOptions()),
            MathematicalProgramResult)
        self.assertIsInstance(
            spp.SolveShortestPath(source=source,
                                  target=target,
                                  convex_relaxation=True),
            MathematicalProgramResult)
        self.assertIsInstance(
            spp.SolveShortestPath(source=source,
                                  target=target,
                                  convex_relaxation=True,
                                  solver=ClpSolver()),
            MathematicalProgramResult)
        self.assertIsInstance(
            spp.SolveShortestPath(source=source,
                                  target=target,
                                  convex_relaxation=True,
                                  solver_options=SolverOptions()),
            MathematicalProgramResult)
        self.assertIn(
            "source",
            spp.GetGraphvizString(result=result,
                                  show_slacks=True,
                                  precision=2,
                                  scientific=False))

        # Vertex
        self.assertIsInstance(source.id(), mut.GraphOfConvexSets.VertexId)
        self.assertEqual(source.ambient_dimension(), 1)
        self.assertEqual(source.name(), "source")
        self.assertIsInstance(source.x()[0], Variable)
        self.assertIsInstance(source.set(), mut.Point)
        np.testing.assert_array_almost_equal(source.GetSolution(result), [0.1],
                                             1e-6)

        # Edge
        self.assertAlmostEqual(edge0.GetSolutionCost(result=result), 0.0, 1e-6)
        np.testing.assert_array_almost_equal(
            edge0.GetSolutionPhiXu(result=result), [0.1], 1e-6)
        np.testing.assert_array_almost_equal(
            edge0.GetSolutionPhiXv(result=result), [0.2], 1e-6)
        self.assertIsInstance(edge0.id(), mut.GraphOfConvexSets.EdgeId)
        self.assertEqual(edge0.name(), "edge0")
        self.assertEqual(edge0.u(), source)
        self.assertEqual(edge0.v(), target)
        self.assertIsInstance(edge0.phi(), Variable)
        self.assertIsInstance(edge0.xu()[0], Variable)
        self.assertIsInstance(edge0.xv()[0], Variable)
        var, binding = edge0.AddCost(e=1.0 + edge0.xu()[0])
        self.assertIsInstance(var, Variable)
        self.assertIsInstance(binding, Binding[Cost])
        var, binding = edge0.AddCost(binding=binding)
        self.assertIsInstance(var, Variable)
        self.assertIsInstance(binding, Binding[Cost])
        self.assertEqual(len(edge0.GetCosts()), 2)
        binding = edge0.AddConstraint(f=(edge0.xu()[0] == edge0.xv()[0]))
        self.assertIsInstance(binding, Binding[Constraint])
        binding = edge0.AddConstraint(binding=binding)
        self.assertIsInstance(binding, Binding[Constraint])
        self.assertEqual(len(edge0.GetConstraints()), 2)
        edge0.AddPhiConstraint(phi_value=False)
        edge0.ClearPhiConstraints()

        # Remove Edges
        self.assertEqual(len(spp.Edges()), 2)
        spp.RemoveEdge(edge1.id())
        self.assertEqual(len(spp.Edges()), 1)
        spp.RemoveEdge(edge0)
        self.assertEqual(len(spp.Edges()), 0)

        # Remove Vertices
        self.assertEqual(len(spp.Vertices()), 2)
        spp.RemoveVertex(source.id())
        self.assertEqual(len(spp.Vertices()), 1)
        spp.RemoveVertex(target)
        self.assertEqual(len(spp.Vertices()), 0)