LSST Applications 27.0.0,g0265f82a02+469cd937ee,g02d81e74bb+21ad69e7e1,g1470d8bcf6+cbe83ee85a,g2079a07aa2+e67c6346a6,g212a7c68fe+04a9158687,g2305ad1205+94392ce272,g295015adf3+81dd352a9d,g2bbee38e9b+469cd937ee,g337abbeb29+469cd937ee,g3939d97d7f+72a9f7b576,g487adcacf7+71499e7cba,g50ff169b8f+5929b3527e,g52b1c1532d+a6fc98d2e7,g591dd9f2cf+df404f777f,g5a732f18d5+be83d3ecdb,g64a986408d+21ad69e7e1,g858d7b2824+21ad69e7e1,g8a8a8dda67+a6fc98d2e7,g99cad8db69+f62e5b0af5,g9ddcbc5298+d4bad12328,ga1e77700b3+9c366c4306,ga8c6da7877+71e4819109,gb0e22166c9+25ba2f69a1,gb6a65358fc+469cd937ee,gbb8dafda3b+69d3c0e320,gc07e1c2157+a98bf949bb,gc120e1dc64+615ec43309,gc28159a63d+469cd937ee,gcf0d15dbbd+72a9f7b576,gdaeeff99f8+a38ce5ea23,ge6526c86ff+3a7c1ac5f1,ge79ae78c31+469cd937ee,gee10cc3b42+a6fc98d2e7,gf1cff7945b+21ad69e7e1,gfbcc870c63+9a11dc8c8f
LSST Data Management Base Package
Loading...
Searching...
No Matches
optimizer.cc
Go to the documentation of this file.
1// -*- lsst-c++ -*-
2/*
3 * LSST Data Management System
4 * Copyright 2008-2013 LSST Corporation.
5 *
6 * This product includes software developed by the
7 * LSST Project (http://www.lsst.org/).
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the LSST License Statement and
20 * the GNU General Public License along with this program. If not,
21 * see <http://www.lsstcorp.org/LegalNotices/>.
22 */
23
24#include "pybind11/pybind11.h"
26
27#include "ndarray/pybind11.h"
28
30
36
37namespace py = pybind11;
38using namespace pybind11::literals;
39
40namespace lsst {
41namespace meas {
42namespace modelfit {
43namespace {
44
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>>;
50
51PyOptimizerObjective declareOptimizerObjective(lsst::cpputils::python::WrapperCollection &wrappers) {
52 return wrappers.wrapType(PyOptimizerObjective(wrappers.module, "OptimizerObjective"), [](auto &mod, auto &cls) {
53 // Class is abstract, so no constructor.
54 cls.def_readonly("dataSize", &OptimizerObjective::dataSize);
55 cls.def_readonly("parameterSize", &OptimizerObjective::parameterSize);
56 cls.def_static("makeFromLikelihood", &OptimizerObjective::makeFromLikelihood, "likelihood"_a,
57 "prior"_a = nullptr);
58 // class is abstract and not subclassable in Python, so we don't wrap the ctor
59 cls.def("fillObjectiveValueGrid", &OptimizerObjective::fillObjectiveValueGrid, "parameters"_a,
60 "output"_a);
61 cls.def("computeResiduals", &OptimizerObjective::computeResiduals, "parameters"_a, "residuals"_a);
62 cls.def("differentiateResiduals", &OptimizerObjective::differentiateResiduals, "parameters"_a,
63 "derivatives"_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,
67 "hessian"_a);
68 });
69}
70
71PyOptimizerControl declareOptimizerControl(lsst::cpputils::python::WrapperCollection &wrappers) {
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<>());
92 });
93}
94
95PyOptimizerHistoryRecorder declareOptimizerHistoryRecorder(lsst::cpputils::python::WrapperCollection &wrappers) {
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,
101 "optimizer"_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);
113 // Other unpackDerivatives overloads do the same thing but with Eigen types,
114 // which makes them redundant in Python where it's all just NumPy.
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);
125 });
126}
127
128PyOptimizer declareOptimizer(lsst::cpputils::python::WrapperCollection &wrappers) {
129 return wrappers.wrapType(PyOptimizer(wrappers.module, "Optimizer"), [](auto &mod, auto &cls) {
130 // StateFlags enum is used as bitflag, so we wrap values as int class attributes.
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 &)) &
155 Optimizer::step,
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 &)) &
159 Optimizer::run,
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);
168 });
169}
170}
171
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;
180
181 wrappers.module.def("solveTrustRegion", &solveTrustRegion, "x"_a, "F"_a, "g"_a, "r"_a, "tolerance"_a);
182}
183
184} // namespace modelfit
185} // namespace meas
186} // namespace lsst
A helper class for subdividing pybind11 module across multiple translation units (i....
Definition python.h:242
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...
Definition python.h:391
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)
Definition optimizer.cc:172