24#include "pybind11/pybind11.h"
27#include "ndarray/pybind11.h"
38using namespace pybind11::literals;
45using PyOptimizerObjective = py::class_<OptimizerObjective, std::shared_ptr<OptimizerObjective>>;
46using PyOptimizerControl = py::class_<OptimizerControl, std::shared_ptr<OptimizerControl>>;
47using PyOptimizerHistoryRecorder =
48 py::class_<OptimizerHistoryRecorder, std::shared_ptr<OptimizerHistoryRecorder>>;
49using PyOptimizer = py::class_<Optimizer, std::shared_ptr<Optimizer>>;
52 return wrappers.
wrapType(PyOptimizerObjective(wrappers.module,
"OptimizerObjective"), [](
auto &mod,
auto &cls) {
54 cls.def_readonly(
"dataSize", &OptimizerObjective::dataSize);
55 cls.def_readonly(
"parameterSize", &OptimizerObjective::parameterSize);
56 cls.def_static(
"makeFromLikelihood", &OptimizerObjective::makeFromLikelihood,
"likelihood"_a,
59 cls.def(
"fillObjectiveValueGrid", &OptimizerObjective::fillObjectiveValueGrid,
"parameters"_a,
61 cls.def(
"computeResiduals", &OptimizerObjective::computeResiduals,
"parameters"_a,
"residuals"_a);
62 cls.def(
"differentiateResiduals", &OptimizerObjective::differentiateResiduals,
"parameters"_a,
64 cls.def(
"hasPrior", &OptimizerObjective::hasPrior);
65 cls.def(
"computePrior", &OptimizerObjective::computePrior,
"parameters"_a);
66 cls.def(
"differentiatePrior", &OptimizerObjective::differentiatePrior,
"parameters"_a,
"gradient"_a,
72 return wrappers.
wrapType(PyOptimizerControl(wrappers.module,
"OptimizerControl"), [](
auto &mod,
auto &cls) {
73 LSST_DECLARE_CONTROL_FIELD(cls, OptimizerControl, noSR1Term);
74 LSST_DECLARE_CONTROL_FIELD(cls, OptimizerControl, skipSR1UpdateThreshold);
75 LSST_DECLARE_CONTROL_FIELD(cls, OptimizerControl, minTrustRadiusThreshold);
76 LSST_DECLARE_CONTROL_FIELD(cls, OptimizerControl, gradientThreshold);
77 LSST_DECLARE_CONTROL_FIELD(cls, OptimizerControl, numDiffRelStep);
78 LSST_DECLARE_CONTROL_FIELD(cls, OptimizerControl, numDiffAbsStep);
79 LSST_DECLARE_CONTROL_FIELD(cls, OptimizerControl, numDiffTrustRadiusStep);
80 LSST_DECLARE_CONTROL_FIELD(cls, OptimizerControl, stepAcceptThreshold);
81 LSST_DECLARE_CONTROL_FIELD(cls, OptimizerControl, trustRegionInitialSize);
82 LSST_DECLARE_CONTROL_FIELD(cls, OptimizerControl, trustRegionGrowReductionRatio);
83 LSST_DECLARE_CONTROL_FIELD(cls, OptimizerControl, trustRegionGrowStepFraction);
84 LSST_DECLARE_CONTROL_FIELD(cls, OptimizerControl, trustRegionGrowFactor);
85 LSST_DECLARE_CONTROL_FIELD(cls, OptimizerControl, trustRegionShrinkReductionRatio);
86 LSST_DECLARE_CONTROL_FIELD(cls, OptimizerControl, trustRegionShrinkFactor);
87 LSST_DECLARE_CONTROL_FIELD(cls, OptimizerControl, trustRegionSolverTolerance);
88 LSST_DECLARE_CONTROL_FIELD(cls, OptimizerControl, maxInnerIterations);
89 LSST_DECLARE_CONTROL_FIELD(cls, OptimizerControl, maxOuterIterations);
90 LSST_DECLARE_CONTROL_FIELD(cls, OptimizerControl, doSaveIterations);
91 cls.def(py::init<>());
96 return wrappers.
wrapType(PyOptimizerHistoryRecorder(wrappers.module,
"OptimizerHistoryRecorder"), [](
auto &mod,
auto &cls) {
97 cls.def(py::init<afw::table::Schema &, std::shared_ptr<Model>, bool>(),
"schema"_a,
"model"_a,
98 "doRecordDerivatives"_a);
99 cls.def(py::init<afw::table::Schema const &>(),
"schema"_a);
100 cls.def(
"apply", &OptimizerHistoryRecorder::apply,
"outerIterCount"_a,
"innerIterCount"_a,
"history"_a,
102 cls.def(
"unpackDerivatives",
103 (void (OptimizerHistoryRecorder::*)(ndarray::Array<Scalar const, 1, 1> const &,
104 ndarray::Array<Scalar, 1, 1> const &,
105 ndarray::Array<Scalar, 2, 2> const &) const) &
106 OptimizerHistoryRecorder::unpackDerivatives,
107 "nested"_a,
"gradient"_a,
"hessian"_a);
108 cls.def(
"unpackDerivatives", (void (OptimizerHistoryRecorder::*)(
109 afw::table::BaseRecord const &, ndarray::Array<Scalar, 1, 1> const &,
110 ndarray::Array<Scalar, 2, 2> const &) const) &
111 OptimizerHistoryRecorder::unpackDerivatives,
112 "record"_a,
"gradient"_a,
"hessian"_a);
115 cls.def(
"fillObjectiveModelGrid", &OptimizerHistoryRecorder::fillObjectiveModelGrid,
"record"_a,
116 "parameters"_a,
"output"_a);
117 cls.def_readonly(
"outer", &OptimizerHistoryRecorder::outer);
118 cls.def_readonly(
"inner", &OptimizerHistoryRecorder::inner);
119 cls.def_readonly(
"state", &OptimizerHistoryRecorder::state);
120 cls.def_readonly(
"objective", &OptimizerHistoryRecorder::objective);
121 cls.def_readonly(
"prior", &OptimizerHistoryRecorder::prior);
122 cls.def_readonly(
"trust", &OptimizerHistoryRecorder::trust);
123 cls.def_readonly(
"parameters", &OptimizerHistoryRecorder::parameters);
124 cls.def_readonly(
"derivatives", &OptimizerHistoryRecorder::derivatives);
129 return wrappers.
wrapType(PyOptimizer(wrappers.module,
"Optimizer"), [](
auto &mod,
auto &cls) {
131 cls.attr(
"CONVERGED_GRADZERO") = py::cast(int(Optimizer::CONVERGED_GRADZERO));
132 cls.attr(
"CONVERGED_TR_SMALL") = py::cast(int(Optimizer::CONVERGED_TR_SMALL));
133 cls.attr(
"CONVERGED") = py::cast(int(Optimizer::CONVERGED));
134 cls.attr(
"FAILED_MAX_INNER_ITERATIONS") = py::cast(int(Optimizer::FAILED_MAX_INNER_ITERATIONS));
135 cls.attr(
"FAILED_MAX_OUTER_ITERATIONS") = py::cast(int(Optimizer::FAILED_MAX_OUTER_ITERATIONS));
136 cls.attr(
"FAILED_MAX_ITERATIONS") = py::cast(int(Optimizer::FAILED_MAX_ITERATIONS));
137 cls.attr(
"FAILED_EXCEPTION") = py::cast(int(Optimizer::FAILED_EXCEPTION));
138 cls.attr(
"FAILED_NAN") = py::cast(int(Optimizer::FAILED_NAN));
139 cls.attr(
"FAILED") = py::cast(int(Optimizer::FAILED));
140 cls.attr(
"STATUS_STEP_REJECTED") = py::cast(int(Optimizer::STATUS_STEP_REJECTED));
141 cls.attr(
"STATUS_STEP_ACCEPTED") = py::cast(int(Optimizer::STATUS_STEP_ACCEPTED));
142 cls.attr(
"STATUS_STEP") = py::cast(int(Optimizer::STATUS_STEP));
143 cls.attr(
"STATUS_TR_UNCHANGED") = py::cast(int(Optimizer::STATUS_TR_UNCHANGED));
144 cls.attr(
"STATUS_TR_DECREASED") = py::cast(int(Optimizer::STATUS_TR_DECREASED));
145 cls.attr(
"STATUS_TR_INCREASED") = py::cast(int(Optimizer::STATUS_TR_INCREASED));
146 cls.attr(
"STATUS_TR") = py::cast(int(Optimizer::STATUS_TR));
147 cls.attr(
"STATUS") = py::cast(int(Optimizer::STATUS));
148 cls.def(py::init<std::shared_ptr<Optimizer::Objective const>, ndarray::Array<Scalar const, 1, 1> const &,
149 Optimizer::Control>(),
150 "objective"_a,
"parameters"_a,
"ctrl"_a);
151 cls.def(
"getObjective", &Optimizer::getObjective);
152 cls.def(
"getControl", &Optimizer::getControl, py::return_value_policy::copy);
153 cls.def(
"step", (bool (Optimizer::*)()) &Optimizer::step);
154 cls.def(
"step", (bool (Optimizer::*)(Optimizer::HistoryRecorder const &, afw::table::BaseCatalog &)) &
156 "recorder"_a,
"history"_a);
157 cls.def(
"run", (int (Optimizer::*)()) &Optimizer::run);
158 cls.def(
"run", (int (Optimizer::*)(Optimizer::HistoryRecorder const &, afw::table::BaseCatalog &)) &
160 "recorder"_a,
"history"_a);
161 cls.def(
"getState", &Optimizer::getState);
162 cls.def(
"getObjectiveValue", &Optimizer::getObjectiveValue);
163 cls.def(
"getParameters", &Optimizer::getParameters);
164 cls.def(
"getResiduals", &Optimizer::getResiduals);
165 cls.def(
"getGradient", &Optimizer::getGradient);
166 cls.def(
"getHessian", &Optimizer::getHessian);
167 cls.def(
"removeSR1Term", &Optimizer::removeSR1Term);
173 auto clsObjective = declareOptimizerObjective(wrappers);
174 auto clsControl = declareOptimizerControl(wrappers);
175 auto clsHistoryRecorder = declareOptimizerHistoryRecorder(wrappers);
176 auto cls = declareOptimizer(wrappers);
177 cls.attr(
"Objective") = clsObjective;
178 cls.attr(
"Control") = clsControl;
179 cls.attr(
"HistoryRecorder") = clsHistoryRecorder;
181 wrappers.module.def(
"solveTrustRegion", &
solveTrustRegion,
"x"_a,
"F"_a,
"g"_a,
"r"_a,
"tolerance"_a);
A helper class for subdividing pybind11 module across multiple translation units (i....
PyType wrapType(PyType cls, ClassWrapperCallback function, bool setModuleName=true)
Add a type (class or enum) wrapper, deferring method and other attribute definitions until finish() i...
void solveTrustRegion(ndarray::Array< Scalar, 1, 1 > const &x, ndarray::Array< Scalar const, 2, 1 > const &F, ndarray::Array< Scalar const, 1, 1 > const &g, double r, double tolerance)
Solve a symmetric quadratic matrix equation with a ball constraint.
void wrapOptimizer(lsst::cpputils::python::WrapperCollection &wrappers)