41 regularization_ = derivedOptions->regularization;
42 doLineSearch_ = derivedOptions->doLineSearch;
43 useBlockJtJ_ = derivedOptions->useBlockJtJ;
44 targetRowsPerChunk_ = derivedOptions->targetRowsPerChunk;
53 updateEnabledParameters();
57void GaussNewtonSolverT<T>::updateEnabledParameters() {
58 enabledParameters_.clear();
59 enabledParameters_.reserve(this->numParameters_);
61 for (
int i = 0; i < gsl::narrow_cast<int>(this->numParameters_); ++i) {
62 if (this->activeParameters_.test(i)) {
63 enabledParameters_.push_back(i);
69void GaussNewtonSolverT<T>::computeJtJFromBlockJtJ() {
75 this->error_ = this->solverFunction_->getJtJR(this->parameters_, hessianApprox_, JtR_);
78 const int nSubsetParams = gsl::narrow_cast<int>(enabledParameters_.size());
79 for (
int iSubset = 0; iSubset < nSubsetParams; ++iSubset) {
80 const int iFull = enabledParameters_[iSubset];
81 if (iFull > iSubset) {
82 JtR_(iSubset) = JtR_(iFull);
88 for (
int iSubset = 0; iSubset < nSubsetParams; ++iSubset) {
89 const int iFull = enabledParameters_[iSubset];
92 if (iFull > iSubset) {
93 for (
int jSubset = 0; jSubset <= iSubset; ++jSubset) {
94 const int jFull = enabledParameters_[jSubset];
96 hessianApprox_(jSubset, iSubset) = hessianApprox_(jFull, iFull);
100 for (
int jSubset = 0; jSubset < iSubset; ++jSubset) {
101 const int jFull = enabledParameters_[jSubset];
103 hessianApprox_(iSubset, jSubset) = hessianApprox_(iFull, jFull);
110void GaussNewtonSolverT<T>::computeJtJFromJacobianBlocks() {
111 const int nSubsetParams = gsl::narrow_cast<int>(enabledParameters_.size());
114 hessianApprox_.setZero(nSubsetParams, nSubsetParams);
115 JtR_.setZero(nSubsetParams);
117 this->solverFunction_->initializeJacobianComputation(this->parameters_);
119 const size_t numBlocks = this->solverFunction_->getJacobianBlockCount();
122 std::vector<size_t> blockSizes(numBlocks);
123 size_t maxBlockSize = 0;
124 size_t totalRows = 0;
125 for (
size_t i = 0; i < numBlocks; ++i) {
126 blockSizes[i] = this->solverFunction_->getJacobianBlockSize(i);
127 maxBlockSize = std::max(maxBlockSize, blockSizes[i]);
128 totalRows += blockSizes[i];
132 const size_t effectiveTarget =
133 (targetRowsPerChunk_ == SIZE_MAX) ? totalRows : std::max(targetRowsPerChunk_, maxBlockSize);
139 while (blockIdx < numBlocks) {
141 size_t chunkRows = 0;
142 size_t chunkStartBlock = blockIdx;
145 while (blockIdx < numBlocks) {
146 const size_t blockSize = blockSizes[blockIdx];
147 if (blockSize == 0) {
153 if (chunkRows == 0 || chunkRows + blockSize <= effectiveTarget) {
154 chunkRows += blockSize;
161 if (chunkRows == 0) {
166 jacobian_.resizeAndSetZero(chunkRows, this->numParameters_);
167 residual_.resizeAndSetZero(chunkRows);
171 for (
size_t i = chunkStartBlock; i < blockIdx; ++i) {
172 const size_t blockSize = blockSizes[i];
173 if (blockSize == 0) {
177 auto jacBlock = jacobian_.mat().block(rowPos, 0, blockSize, this->numParameters_);
178 auto resBlock = residual_.vec().segment(rowPos, blockSize);
180 size_t actualRows = 0;
181 error += this->solverFunction_->computeJacobianBlock(
182 this->parameters_, i, jacBlock, resBlock, actualRows);
184 actualRows <= blockSize,
185 "Jacobian block {} used {} rows but only {} rows were allocated",
190 rowPos += actualRows;
197 auto activeJacobianRows = jacobian_.mat().topRows(rowPos);
204 for (
int iSubsetParam = 0; iSubsetParam < nSubsetParams; ++iSubsetParam) {
205 if (enabledParameters_[iSubsetParam] > iSubsetParam) {
206 activeJacobianRows.col(iSubsetParam) =
207 activeJacobianRows.col(enabledParameters_[iSubsetParam]);
212 const auto J_chunk = activeJacobianRows.leftCols(nSubsetParams);
213 const auto r_chunk = residual_.vec().head(rowPos);
215 hessianApprox_.template triangularView<Eigen::Lower>() += J_chunk.transpose() * J_chunk;
216 JtR_.noalias() += J_chunk.transpose() * r_chunk;
219 this->solverFunction_->finalizeJacobianComputation();
220 this->error_ = error;
227 const int nSubsetParams = gsl::narrow_cast<int>(enabledParameters_.size());
232 computeJtJFromBlockJtJ();
234 computeJtJFromJacobianBlocks();
244 auto jtjBlock = hessianApprox_.topLeftCorner(nSubsetParams, nSubsetParams);
245 auto jtrBlock = JtR_.head(nSubsetParams);
248 jtjBlock.diagonal().array() += regularization_;
251 Eigen::VectorX<T> subsetDelta = llt_.compute(jtjBlock).solve(jtrBlock);
254 Eigen::VectorX<T> delta = Eigen::VectorX<T>::Zero(this->
numParameters_);
255 for (
int iSubsetParam = 0; iSubsetParam < nSubsetParams; ++iSubsetParam) {
256 delta(enabledParameters_[iSubsetParam]) = subsetDelta(iSubsetParam);
259 updateParameters(delta);
267 if (jtjHist.rows() !=
268 gsl::narrow_cast<Eigen::Index>(this->actualParameters_ * this->getMaxIterations()) ||
269 jtjHist.cols() != gsl::narrow_cast<Eigen::Index>(this->actualParameters_)) {
275 this->actualParameters_,
276 this->actualParameters_) =
277 hessianApprox_.topLeftCorner(this->actualParameters_, this->actualParameters_);
283void GaussNewtonSolverT<T>::updateParameters(Eigen::VectorX<T>& delta) {
284 if (!doLineSearch_) {
286 this->solverFunction_->updateParameters(this->parameters_, delta);
292 static constexpr T kC1 = 1e-3;
293 static constexpr T kTau = 0.5;
294 static constexpr size_t kMaxLineSearchSteps = 10;
296 const T scaledError = kC1 * this->error_;
297 const Eigen::VectorX<T> parametersOrig = this->parameters_;
298 T lineSearchScale = 1.0;
300 for (
size_t i = 0; i < kMaxLineSearchSteps && std::isnormal(lineSearchScale); ++i) {
301 this->parameters_ = parametersOrig;
303 this->solverFunction_->updateParameters(this->parameters_, lineSearchScale * delta);
305 const double errorNew = this->solverFunction_->getError(this->parameters_);
306 if ((this->error_ - errorNew) >= lineSearchScale * scaledError) {
311 lineSearchScale *= kTau;
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Dense implementation of the Gauss-Newton optimization algorithm.
GaussNewtonSolverT(const SolverOptions &options, SolverFunctionT< T > *solver)
Creates a solver with the specified options and function to optimize.
void setOptions(const SolverOptions &options) final
Updates solver configuration, handling both base and Gauss-Newton specific options.
void initializeSolver() final
Initializes solver state before optimization begins.
void doIteration() final
Performs one iteration of the Gauss-Newton algorithm.
std::string_view getName() const override
Returns "GaussNewton" as the solver name.
Abstract base class for optimization objective functions.
virtual void setOptions(const SolverOptions &options)
Updates solver configuration with new options.
std::unordered_map< std::string, Eigen::MatrixX< T > > iterationHistory_
Recorded data from optimization process.
size_t getMaxIterations() const
Returns the maximum number of iterations before terminating.
size_t iteration_
Current iteration count.
bool storeHistory
Whether to record optimization progress for analysis.
size_t numParameters_
Total number of parameters in the optimization problem.
SolverT(const SolverOptions &options, SolverFunctionT< T > *solver)
Constructor with solver options and function to optimize.
int actualParameters_
Number of parameters currently enabled for optimization.
Profiling macros that dispatch to one of three backends, selected at compile time:
#define MT_PROFILE_EVENT(NAME)
#define MT_PROFILE_FUNCTION()
float regularization
Damping parameter added to Hessian diagonal for numerical stability; see https://en....
bool doLineSearch
Enables backtracking line search to ensure error reduction at each step.
Extended options specific to the Gauss-Newton optimization algorithm.
bool useBlockJtJ
Uses pre-computed JᵀJ and JᵀR from the solver function.
size_t targetRowsPerChunk
Target number of Jacobian rows to accumulate before computing JᵀJ.
Common options for numerical optimization solvers.