Ejemplo n.º 1
0
 def test_clp_solver(self):
     prog, x, x_expected = self._make_prog()
     solver = ClpSolver()
     self.assertEqual(solver.solver_id(), ClpSolver.id())
     self.assertTrue(solver.available())
     self.assertEqual(solver.solver_id().name(), "CLP")
     self.assertEqual(solver.SolverName(), "CLP")
     self.assertEqual(solver.solver_type(), mp.SolverType.kClp)
     result = solver.Solve(prog, None, None)
     self.assertTrue(result.is_success())
     self.assertTrue(np.allclose(result.GetSolution(x), x_expected))
     self.assertEqual(result.get_solver_details().status, 0)
     self.assertAlmostEqual(result.get_optimal_cost(), -244./3)
Ejemplo n.º 2
0
        def setup_and_test_prog(
                setup_aux, expected_xyw, expected_Bx, expected_By):
            '''
            1) Setup an optimization with 1D continuous variables x, y, and
               w.
            2) Constraint x*y=w using this piecewise McCormick envelope
               constraint.
            3) Call `setup_aux(prog, x, y, w)`, which may mutate the program
               to add additional costs and constraints for testing.
            4) Solve the program and assert that the solver finds the
               expected xyw values and the expected binary setting.
            '''
            prog = mp.MathematicalProgram()

            w, x, y = prog.NewContinuousVariables(3)
            N_x_divisions = len(expected_Bx)
            N_y_divisions = len(expected_By)
            Bx = prog.NewBinaryVariables(N_x_divisions)
            By = prog.NewBinaryVariables(N_y_divisions)
            # Divide range [0, 1] into appropriate number of bins.
            phi_x = np.linspace(0., 1., N_x_divisions+1)
            phi_y = np.linspace(0., 1., N_y_divisions+1)
            binning = mip_util.IntervalBinning.kLinear
            mip_util.AddBilinearProductMcCormickEnvelopeSos2(
                prog=prog, x=x, y=y, w=w, phi_x=phi_x, phi_y=phi_y,
                Bx=Bx, By=By, binning=binning
            )
            setup_aux(prog, x, y, w)
            solver = bnb.MixedIntegerBranchAndBound(
                prog, ClpSolver().solver_id())
            solution_result = solver.Solve()
            self.assertEqual(solution_result, mp.SolutionResult.kSolutionFound)

            xyw = solver.GetSolution([x, y, w])
            self.assertTrue(np.allclose(xyw, expected_xyw))
            self.assertTrue(np.allclose(solver.GetSolution(Bx), expected_Bx))
            self.assertTrue(np.allclose(solver.GetSolution(By), expected_By))
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
 def unavailable(self):
     """Per the BUILD file, this test is only run when CLP is disabled."""
     solver = ClpSolver()
     self.assertFalse(solver.available())