forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bindings for CsdpSolver (RobotLocomotion#13544)
- Loading branch information
1 parent
36de86c
commit 93ef69e
Showing
12 changed files
with
140 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include "pybind11/eigen.h" | ||
#include "pybind11/pybind11.h" | ||
|
||
#include "drake/bindings/pydrake/common/value_pybind.h" | ||
#include "drake/bindings/pydrake/documentation_pybind.h" | ||
#include "drake/bindings/pydrake/pydrake_pybind.h" | ||
#include "drake/solvers/csdp_solver.h" | ||
|
||
namespace drake { | ||
namespace pydrake { | ||
|
||
PYBIND11_MODULE(csdp, m) { | ||
// NOLINTNEXTLINE(build/namespaces): Emulate placement in namespace. | ||
using namespace drake::solvers; | ||
constexpr auto& doc = pydrake_doc.drake.solvers; | ||
|
||
m.doc() = "Csdp solver bindings for MathematicalProgram"; | ||
|
||
py::module::import("pydrake.common.value"); | ||
py::module::import("pydrake.solvers.mathematicalprogram"); | ||
|
||
py::class_<CsdpSolver, SolverInterface>(m, "CsdpSolver", doc.CsdpSolver.doc) | ||
.def(py::init<>(), doc.CsdpSolver.ctor.doc); | ||
|
||
py::class_<CsdpSolverDetails>( | ||
m, "CsdpSolverDetails", doc.CsdpSolverDetails.doc) | ||
.def_readonly("return_code", &CsdpSolverDetails::return_code, | ||
doc.CsdpSolverDetails.return_code.doc) | ||
.def_readonly("primal_objective", &CsdpSolverDetails::primal_objective, | ||
doc.CsdpSolverDetails.primal_objective.doc) | ||
.def_readonly("dual_objective", &CsdpSolverDetails::dual_objective, | ||
doc.CsdpSolverDetails.dual_objective.doc) | ||
.def_readonly( | ||
"y_val", &CsdpSolverDetails::y_val, doc.CsdpSolverDetails.y_val.doc) | ||
.def_readonly( | ||
"Z_val", &CsdpSolverDetails::Z_val, doc.CsdpSolverDetails.Z_val.doc); | ||
AddValueInstantiation<CsdpSolverDetails>(m); | ||
} | ||
|
||
} // namespace pydrake | ||
} // namespace drake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import unittest | ||
import warnings | ||
|
||
import numpy as np | ||
|
||
from pydrake.solvers import mathematicalprogram as mp | ||
from pydrake.solvers.csdp import CsdpSolver | ||
|
||
|
||
class TestCsdpSolver(unittest.TestCase): | ||
def _make_prog(self): | ||
prog = mp.MathematicalProgram() | ||
x1 = prog.NewSymmetricContinuousVariables(2, "x1") | ||
x2 = prog.NewSymmetricContinuousVariables(3, "x2") | ||
y = prog.NewContinuousVariables(2, "y") | ||
prog.AddPositiveSemidefiniteConstraint(x1) | ||
prog.AddPositiveSemidefiniteConstraint(x2) | ||
prog.AddLinearConstraint(y[0] >= 0) | ||
prog.AddLinearConstraint(y[1] >= 0) | ||
prog.AddLinearEqualityConstraint(3*x1[0, 0] + 2*x1[0, 1] + 3*x1[1, 1] | ||
+ y[0] == 1) | ||
prog.AddLinearEqualityConstraint(3*x2[0, 0] + 4*x2[1, 1] + 2*x2[0, 2] | ||
+ 5*x2[2, 2] + y[1] == 2) | ||
prog.AddLinearCost(-(2*x1[0, 0] + 2*x1[0, 1] + 2*x1[1, 1] + 3*x2[0, 0] | ||
+ 2*x2[1, 1] + 2*x2[0, 2] + 3*x2[2, 2])) | ||
x1_expected = 0.125 * np.ones((2, 2)) | ||
return prog, x1, x1_expected | ||
|
||
def test_csdp_solver(self): | ||
prog, x1, x1_expected = self._make_prog() | ||
solver = CsdpSolver() | ||
self.assertTrue(solver.available()) | ||
self.assertEqual(solver.solver_id().name(), "CSDP") | ||
self.assertEqual(solver.SolverName(), "CSDP") | ||
self.assertEqual(solver.solver_type(), mp.SolverType.kCsdp) | ||
result = solver.Solve(prog, None, None) | ||
self.assertTrue(result.is_success()) | ||
self.assertTrue(np.allclose(result.GetSolution(x1), x1_expected)) | ||
self.assertEqual(result.get_solver_details().return_code, 0) | ||
np.testing.assert_allclose( | ||
result.get_solver_details().primal_objective, 2.75) | ||
np.testing.assert_allclose( | ||
result.get_solver_details().dual_objective, 2.75) | ||
np.testing.assert_allclose( | ||
result.get_solver_details().y_val, np.array([0.75, 1.])) | ||
z_expected = np.zeros((7, 7)) | ||
z_expected[0, :2] = [0.25, -0.25] | ||
z_expected[1, :2] = [-0.25, 0.25] | ||
z_expected[3:, 3:] = np.diag([2., 2., 0.75, 1.]) | ||
np.testing.assert_allclose( | ||
result.get_solver_details().Z_val.toarray(), z_expected, atol=1e-8) | ||
|
||
def unavailable(self): | ||
"""Per the BUILD file, this test is only run when CSDP is disabled.""" | ||
solver = CsdpSolver() | ||
self.assertFalse(solver.available()) |