LSST Applications g042eb84c57+730a74494b,g04e9c324dd+8c5ae1fdc5,g134cb467dc+1f1e3e7524,g199a45376c+0ba108daf9,g1fd858c14a+fa7d31856b,g210f2d0738+f66ac109ec,g262e1987ae+83a3acc0e5,g29ae962dfc+d856a2cb1f,g2cef7863aa+aef1011c0b,g35bb328faa+8c5ae1fdc5,g3fd5ace14f+a1e0c9f713,g47891489e3+0d594cb711,g4d44eb3520+c57ec8f3ed,g4d7b6aa1c5+f66ac109ec,g53246c7159+8c5ae1fdc5,g56a1a4eaf3+fd7ad03fde,g64539dfbff+f66ac109ec,g67b6fd64d1+0d594cb711,g67fd3c3899+f66ac109ec,g6985122a63+0d594cb711,g74acd417e5+3098891321,g786e29fd12+668abc6043,g81db2e9a8d+98e2ab9f28,g87389fa792+8856018cbb,g89139ef638+0d594cb711,g8d7436a09f+80fda9ce03,g8ea07a8fe4+760ca7c3fc,g90f42f885a+033b1d468d,g97be763408+a8a29bda4b,g99822b682c+e3ec3c61f9,g9d5c6a246b+0d5dac0c3d,ga41d0fce20+9243b26dd2,gbf99507273+8c5ae1fdc5,gd7ef33dd92+0d594cb711,gdab6d2f7ff+3098891321,ge410e46f29+0d594cb711,geaed405ab2+c4bbc419c6,gf9a733ac38+8c5ae1fdc5,w.2025.38
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::classh<OptimizerObjective>;
46using PyOptimizerControl = py::classh<OptimizerControl>;
47using PyOptimizerHistoryRecorder =
48 py::classh<OptimizerHistoryRecorder>;
49using PyOptimizer = py::classh<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
pybind11::module module
The module object passed to the PYBIND11_MODULE block that contains this WrapperCollection.
Definition python.h:448
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