Skip to content

Commit

Permalink
Add python binding for IpoptSolverDetails. (RobotLocomotion#11613)
Browse files Browse the repository at this point in the history
  • Loading branch information
hongkai-dai authored Jun 11, 2019
1 parent 757f89a commit 4e2ed3e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
12 changes: 10 additions & 2 deletions bindings/pydrake/solvers/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,18 @@ drake_pybind_library(

drake_pybind_library(
name = "ipopt_py",
cc_deps = ["//bindings/pydrake:documentation_pybind"],
cc_deps = [
"//bindings/pydrake:documentation_pybind",
"//bindings/pydrake/common:value_pybind",
],
cc_srcs = ["ipopt_py.cc"],
package_info = PACKAGE_INFO,
py_deps = [":mathematicalprogram_py"],
py_deps = [
":mathematicalprogram_py",
# TODO(jwnimmer-tri) Only for AbstractValue; we should switch this to
# depend on on pydrake/common once the Value bindings have moved there.
"//bindings/pydrake/systems:framework_py",
],
)

drake_pybind_library(
Expand Down
17 changes: 17 additions & 0 deletions bindings/pydrake/solvers/ipopt_py.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#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/ipopt_solver.h"
Expand All @@ -15,10 +16,26 @@ PYBIND11_MODULE(ipopt, m) {
m.doc() = "Ipopt solver bindings for MathematicalProgram";

py::module::import("pydrake.solvers.mathematicalprogram");
py::module::import("pydrake.systems.framework");

py::class_<IpoptSolver, SolverInterface>(
m, "IpoptSolver", doc.IpoptSolver.doc)
.def(py::init<>(), doc.IpoptSolver.ctor.doc);

py::class_<IpoptSolverDetails>(
m, "IpoptSolverDetails", doc.IpoptSolverDetails.doc)
.def_readwrite("status", &IpoptSolverDetails::status,
doc.IpoptSolverDetails.status.doc)
.def_readwrite(
"z_L", &IpoptSolverDetails::z_L, doc.IpoptSolverDetails.z_L.doc)
.def_readwrite(
"z_U", &IpoptSolverDetails::z_U, doc.IpoptSolverDetails.z_U.doc)
.def_readwrite("g", &IpoptSolverDetails::g, doc.IpoptSolverDetails.g.doc)
.def_readwrite("lambda", &IpoptSolverDetails::lambda,
doc.IpoptSolverDetails.lambda.doc)
.def("ConvertStatusToString", &IpoptSolverDetails::ConvertStatusToString,
doc.IpoptSolverDetails.ConvertStatusToString.doc);
AddValueInstantiation<IpoptSolverDetails>(m);
}

} // namespace pydrake
Expand Down
3 changes: 3 additions & 0 deletions bindings/pydrake/solvers/test/ipopt_solver_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def test_ipopt_solver(self):
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.assertEqual(result.get_solver_details().ConvertStatusToString(),
"Success")

def unavailable(self):
"""Per the BUILD file, this test is only run when IPOPT is disabled."""
Expand Down

0 comments on commit 4e2ed3e

Please sign in to comment.