Skip to content

Commit

Permalink
[solvers] SolverOptions is serializable
Browse files Browse the repository at this point in the history
TBD things are deprecated
  • Loading branch information
jwnimmer-tri committed Oct 24, 2024
1 parent 95e41e0 commit e5682a1
Show file tree
Hide file tree
Showing 12 changed files with 582 additions and 470 deletions.
1 change: 1 addition & 0 deletions bindings/pydrake/solvers/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ drake_pybind_library(
"solvers_py_mobylcp.cc",
"solvers_py_mosek.cc",
"solvers_py_nlopt.cc",
"solvers_py_options.cc",
"solvers_py_osqp.cc",
"solvers_py_scs.cc",
"solvers_py_sdpa_free_format.cc",
Expand Down
3 changes: 3 additions & 0 deletions bindings/pydrake/solvers/solvers_py.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ void DefineSolversMosek(py::module m);
/* Defines the NLOPT bindings. See solvers_py_nlopt.cc. */
void DefineSolversNlopt(py::module m);

/* Defines the SolverOptions bindings. See solvers_py_options.cc. */
void DefineSolversOptions(py::module m);

/* Defines the OSQP bindings. See solvers_py_osqp.cc. */
void DefineSolversOsqp(py::module m);

Expand Down
69 changes: 69 additions & 0 deletions bindings/pydrake/solvers/solvers_py_options.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "drake/bindings/pydrake/documentation_pybind.h"
#include "drake/bindings/pydrake/pydrake_pybind.h"
#include "drake/bindings/pydrake/solvers/solvers_py.h"
#include "drake/solvers/common_solver_option.h"
#include "drake/solvers/solver_options.h"

namespace drake {
namespace pydrake {
namespace internal {

void DefineSolversOptions(py::module m) {
// NOLINTNEXTLINE(build/namespaces): Emulate placement in namespace.
using namespace drake::solvers;
constexpr auto& doc = pydrake_doc.drake.solvers;

py::enum_<CommonSolverOption>(
m, "CommonSolverOption", doc.CommonSolverOption.doc)
.value("kPrintFileName", CommonSolverOption::kPrintFileName,
doc.CommonSolverOption.kPrintFileName.doc)
.value("kPrintToConsole", CommonSolverOption::kPrintToConsole,
doc.CommonSolverOption.kPrintToConsole.doc);

py::class_<SolverOptions>(m, "SolverOptions", doc.SolverOptions.doc)
.def(py::init<>())
.def("SetOption",
py::overload_cast<const SolverId&, std::string,
SolverOptions::OptionValue>(&SolverOptions::SetOption),
py::arg("solver_id"), py::arg("key"), py::arg("value"),
doc.SolverOptions.SetOption.doc_3args)
.def("SetOption",
py::overload_cast<CommonSolverOption, SolverOptions::OptionValue>(
&SolverOptions::SetOption),
py::arg("key"), py::arg("value"),
doc.SolverOptions.SetOption.doc_2args)
.def("get_print_file_name", &SolverOptions::get_print_file_name,
doc.SolverOptions.get_print_file_name.doc)
.def("get_print_to_console", &SolverOptions::get_print_to_console,
doc.SolverOptions.get_print_to_console.doc)
.def("__repr__",
[](const SolverOptions& self) { return fmt::to_string(self); });

#if 0
// XXX dep kwarg name
.def("SetOption",
py::overload_cast<const SolverId&, std::string, SolverOptions::OptionValue>(
&SolverOptions::SetOption),
py::arg("solver_id"), py::arg("solver_option"),
py::arg("option_value"),
doc.SolverOptions.SetOption.doc_double_option)
.def(
"GetOptions",
[](const SolverOptions& solver_options, SolverId solver_id) {
py::dict out;
py::object update = out.attr("update");
update(solver_options.GetOptionsDouble(solver_id));
update(solver_options.GetOptionsInt(solver_id));
update(solver_options.GetOptionsStr(solver_id));
return out;
},
py::arg("solver_id"), doc.SolverOptions.GetOptionsDouble.doc)
// XXX
.def("common_solver_options", &SolverOptions::common_solver_options,
doc.SolverOptions.common_solver_options.doc)
#endif
}

} // namespace internal
} // namespace pydrake
} // namespace drake
4 changes: 4 additions & 0 deletions solvers/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ drake_cc_library(
],
deps = [
":solver_id",
"//common:string_container",
],
implementation_deps = [
"//common:overloaded",
],
)

Expand Down
19 changes: 16 additions & 3 deletions solvers/mathematical_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "drake/common/autodiff.h"
#include "drake/common/drake_assert.h"
#include "drake/common/drake_copyable.h"
#include "drake/common/drake_deprecated.h"
#include "drake/common/eigen_types.h"
#include "drake/common/fmt.h"
#include "drake/common/polynomial.h"
Expand Down Expand Up @@ -3225,19 +3226,31 @@ class MathematicalProgram {
*/
const SolverOptions& solver_options() const { return solver_options_; }

const std::unordered_map<std::string, double>& GetSolverOptionsDouble(
DRAKE_DEPRECATED("2025-05-01", "Use solver_options(), instead")
std::unordered_map<std::string, double> GetSolverOptionsDouble(
const SolverId& solver_id) const {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
return solver_options_.GetOptionsDouble(solver_id);
#pragma GCC diagnostic pop
}

const std::unordered_map<std::string, int>& GetSolverOptionsInt(
DRAKE_DEPRECATED("2025-05-01", "Use solver_options(), instead")
std::unordered_map<std::string, int> GetSolverOptionsInt(
const SolverId& solver_id) const {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
return solver_options_.GetOptionsInt(solver_id);
#pragma GCC diagnostic pop
}

const std::unordered_map<std::string, std::string>& GetSolverOptionsStr(
DRAKE_DEPRECATED("2025-05-01", "Use solver_options(), instead")
std::unordered_map<std::string, std::string> GetSolverOptionsStr(
const SolverId& solver_id) const {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
return solver_options_.GetOptionsStr(solver_id);
#pragma GCC diagnostic pop
}

/**
Expand Down
Loading

0 comments on commit e5682a1

Please sign in to comment.