24 #ifndef LSST_MEAS_MODELFIT_optimizer_h_INCLUDED 25 #define LSST_MEAS_MODELFIT_optimizer_h_INCLUDED 35 namespace lsst {
namespace meas {
namespace modelfit {
68 dataSize(dataSize_), parameterSize(parameterSize_)
91 ndarray::Array<Scalar const,2,1>
const & parameters,
92 ndarray::Array<Scalar,1,1>
const &
output 104 ndarray::Array<Scalar const,1,1>
const & parameters,
105 ndarray::Array<Scalar,1,1>
const & residuals
122 ndarray::Array<Scalar const,1,1>
const & parameters,
123 ndarray::Array<Scalar,2,-2>
const & derivatives
142 virtual Scalar computePrior(ndarray::Array<Scalar const,1,1>
const & parameters)
const {
return 1.0; }
159 ndarray::Array<Scalar const,1,1>
const & parameters,
160 ndarray::Array<Scalar,1,1>
const & gradient,
161 ndarray::Array<Scalar,2,1>
const & hessian
163 gradient.deep() = 0.0;
164 hessian.deep() = 0.0;
191 "If true, ignore the SR1 update term in the Hessian, resulting in a Levenberg-Marquardt-like method" 195 skipSR1UpdateThreshold,
double,
196 "Skip the SR1 update if |v||s| / (|v||s|) is less than this threshold" 200 minTrustRadiusThreshold,
double,
201 "If the trust radius falls below this threshold, consider the algorithm converged" 205 gradientThreshold,
double,
206 "If the maximum of the gradient falls below this threshold, consider the algorithm converged" 210 numDiffRelStep,
double,
211 "relative step size used for numerical derivatives (added to other steps)" 215 numDiffAbsStep,
double,
216 "absolute step size used for numerical derivatives (added to other steps)" 220 numDiffTrustRadiusStep,
double,
221 "step size (in units of trust radius) used for numerical derivatives (added to relative step)" 225 stepAcceptThreshold,
double,
226 "steps with reduction ratio greater than this are accepted" 230 trustRegionInitialSize,
double,
231 "the initial trust region will be set to this value" 235 trustRegionGrowReductionRatio,
double,
236 "steps with reduction radio greater than this may increase the trust radius" 240 trustRegionGrowStepFraction,
double,
241 "steps with length this fraction of the trust radius may increase the trust radius" 245 trustRegionGrowFactor,
double,
246 "when increase the trust region size, multiply the radius by this factor" 250 trustRegionShrinkReductionRatio,
double,
251 "steps with reduction radio less than this will decrease the trust radius" 255 trustRegionShrinkFactor,
double,
256 "when reducing the trust region size, multiply the radius by this factor" 260 trustRegionSolverTolerance,
double,
261 "value passed as the tolerance to solveTrustRegion" 265 maxInnerIterations,
int,
266 "maximum number of iterations (i.e. function evaluations and trust region subproblems) per step" 270 maxOuterIterations,
int,
271 "maximum number of steps" 275 doSaveIterations,
bool,
276 "whether to save all iterations for debugging purposes" 280 noSR1Term(false), skipSR1UpdateThreshold(1E-8),
281 minTrustRadiusThreshold(1E-5),
282 gradientThreshold(1E-5),
283 numDiffRelStep(0.0), numDiffAbsStep(0.0), numDiffTrustRadiusStep(0.1),
284 stepAcceptThreshold(0.0),
285 trustRegionInitialSize(1.0),
286 trustRegionGrowReductionRatio(0.75),
287 trustRegionGrowStepFraction(0.8),
288 trustRegionGrowFactor(2.0),
289 trustRegionShrinkReductionRatio(0.25),
290 trustRegionShrinkFactor(1.0/3.0),
291 trustRegionSolverTolerance(1E-8),
292 maxInnerIterations(20),
293 maxOuterIterations(500),
294 doSaveIterations(false)
304 bool doRecordDerivatives
316 void unpackDerivatives(
317 ndarray::Array<Scalar const,1,1>
const &
nested,
322 void unpackDerivatives(
328 void unpackDerivatives(
329 ndarray::Array<Scalar const,1,1>
const & nested,
330 ndarray::Array<Scalar,1,1>
const & gradient,
331 ndarray::Array<Scalar,2,2>
const & hessian
334 void unpackDerivatives(
336 ndarray::Array<Scalar,1,1>
const & gradient,
337 ndarray::Array<Scalar,2,2>
const & hessian
340 void fillObjectiveModelGrid(
342 ndarray::Array<Scalar const,2,1>
const & parameters,
343 ndarray::Array<Scalar,1,1>
const &
output 407 CONVERGED_GRADZERO = 0x0001,
408 CONVERGED_TR_SMALL = 0x0002,
409 CONVERGED = CONVERGED_GRADZERO | CONVERGED_TR_SMALL,
410 FAILED_MAX_INNER_ITERATIONS = 0x0010,
411 FAILED_MAX_OUTER_ITERATIONS = 0x0020,
412 FAILED_MAX_ITERATIONS = 0x0030,
413 FAILED_EXCEPTION = 0x0040,
415 FAILED = FAILED_MAX_INNER_ITERATIONS | FAILED_MAX_OUTER_ITERATIONS | FAILED_EXCEPTION | FAILED_NAN,
416 STATUS_STEP_REJECTED = 0x0100,
417 STATUS_STEP_ACCEPTED = 0x0200,
418 STATUS_STEP = STATUS_STEP_REJECTED | STATUS_STEP_ACCEPTED,
419 STATUS_TR_UNCHANGED = 0x1000,
420 STATUS_TR_DECREASED = 0x2000,
421 STATUS_TR_INCREASED = 0x4000,
422 STATUS_TR = STATUS_TR_UNCHANGED | STATUS_TR_DECREASED | STATUS_TR_INCREASED,
423 STATUS = STATUS_STEP | STATUS_TR,
427 PTR(Objective
const) objective,
428 ndarray::Array<Scalar const,1,1>
const & parameters,
436 bool step() {
return _stepImpl(0); }
439 return _stepImpl(0, &recorder, &history);
442 int run() {
return _runImpl(); }
445 return _runImpl(&recorder, &history);
452 ndarray::Array<Scalar const,1,1>
getParameters()
const {
return _current.parameters; }
454 ndarray::Array<Scalar const,1,1>
getResiduals()
const {
return _current.residuals; }
456 ndarray::Array<Scalar const,1,1>
getGradient()
const {
return _gradient; }
458 ndarray::Array<Scalar const,2,2>
getHessian()
const {
return _hessian; }
461 void removeSR1Term();
465 struct IterationData {
468 ndarray::Array<Scalar,1,1> parameters;
469 ndarray::Array<Scalar,1,1> residuals;
471 IterationData(
int dataSize,
int parameterSize);
480 HistoryRecorder
const * recorder=NULL,
486 void _computeDerivatives();
489 PTR(Objective
const) _objective;
492 IterationData _current;
494 ndarray::Array<Scalar,1,1> _step;
495 ndarray::Array<Scalar,1,1> _gradient;
496 ndarray::Array<Scalar,2,2> _hessian;
497 ndarray::Array<
Scalar,2,-2> _residualDerivative;
519 ndarray::Array<Scalar,1,1>
const &
x,
520 ndarray::Array<Scalar const,2,1>
const & F, ndarray::Array<Scalar const,1,1>
const & g,
521 double r,
double tolerance
526 #endif // !LSST_MEAS_MODELFIT_optimizer_h_INCLUDED Defines the fields and offsets for a table.
int run(HistoryRecorder const &recorder, afw::table::BaseCatalog &history)
void fillObjectiveValueGrid(ndarray::Array< Scalar const, 2, 1 > const ¶meters, ndarray::Array< Scalar, 1, 1 > const &output) const
Evaluate the Objective on a 1-d grid.
OptimizerObjective Objective
A numerical optimizer customized for least-squares problems with Bayesian priors. ...
double Scalar
Typedefs to be used for probability and parameter values.
virtual bool hasPrior() const
Return true if the Objective has a Bayesian prior as well as a likelihood.
virtual Scalar computePrior(ndarray::Array< Scalar const, 1, 1 > const ¶meters) const
Compute the value of the Bayesian prior for the given parameter vector.
#define LSST_CONTROL_FIELD(NAME, TYPE, DOC)
A preprocessor macro used to define fields in C++ "control object" structs.
OptimizerHistoryRecorder HistoryRecorder
Scalar getObjectiveValue() const
Abstract base class and concrete factories that define multi-shapelet galaxy models.
virtual bool differentiateResiduals(ndarray::Array< Scalar const, 1, 1 > const ¶meters, ndarray::Array< Scalar, 2,-2 > const &derivatives) const
Evaluate analytic derivatives of the model or signal that they are not available. ...
ndarray::Array< Scalar const, 1, 1 > getParameters() const
A base class for image defects.
ndarray::Array< Scalar const, 1, 1 > getGradient() const
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > Matrix
Typedefs to be used for probability and parameter values.
boost::shared_ptr< Objective const > getObjective() const
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Typedefs to be used for probability and parameter values.
virtual void differentiatePrior(ndarray::Array< Scalar const, 1, 1 > const ¶meters, ndarray::Array< Scalar, 1, 1 > const &gradient, ndarray::Array< Scalar, 2, 1 > const &hessian) const
Compute the first and second derivatives of the Bayesian prior with respect to the parameters...
afw::table::Key< int > inner
Configuration object for Optimizer.
virtual ~OptimizerObjective()
Base class for Bayesian priors.
bool step(HistoryRecorder const &recorder, afw::table::BaseCatalog &history)
ndarray::Array< Scalar const, 1, 1 > getResiduals() const
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.
afw::table::Key< int > outer
void swap(Image< PixelT > &a, Image< PixelT > &b)
ndarray::Array< Scalar const, 2, 2 > getHessian() const
Base class for all records.
static boost::shared_ptr< OptimizerObjective > makeFromLikelihood(boost::shared_ptr< Likelihood > likelihood, boost::shared_ptr< Prior > prior=boost::shared_ptr< Prior >())
Return a concrete Objective object built from a Likelihood and Prior.
Base class for optimizer/sampler likelihood functions that compute likelihood at a point...
afw::table::Key< int > state
ItemVariant const * other
Base class for objective functions for Optimizer.
Control const & getControl() const
OptimizerObjective(int dataSize_, int parameterSize_)
Base class constructor; must be called by all subclasses.
virtual void computeResiduals(ndarray::Array< Scalar const, 1, 1 > const ¶meters, ndarray::Array< Scalar, 1, 1 > const &residuals) const =0
Evaluate the residuals of the model for a given parameter vector.