30 #include <boost/algorithm/string.hpp> 31 #include <boost/shared_ptr.hpp> 45 std::unique_ptr<internal::NonlinearOptimizerState> state)
46 : graph_(graph), state_(
std::
move(state)) {}
67 double currentError =
error();
70 if (currentError <= params.
errorTol) {
72 cout <<
"Exiting, as error = " << currentError <<
" < " << params.
errorTol << endl;
80 cout <<
"Initial error: " << currentError << endl;
91 double newError = currentError;
94 currentError = newError;
109 cout <<
"newError: " << newError << endl;
118 cout <<
"Terminating because reached maximum iterations" << endl;
158 throw std::runtime_error(
159 "NonlinearOptimizer::solve: cg parameter has to be assigned ...");
161 if (
auto pcg = boost::dynamic_pointer_cast<PCGSolverParameters>(
164 }
else if (
auto spcg =
165 boost::dynamic_pointer_cast<SubgraphSolverParameters>(
168 throw std::runtime_error(
"SubgraphSolver needs an ordering");
171 throw std::runtime_error(
172 "NonlinearOptimizer::solve: special cg parameter type is not handled in LM solver ...");
175 throw std::runtime_error(
"NonlinearOptimizer::solve: Optimization parameter is invalid");
184 double errorThreshold,
double currentError,
double newError,
187 if (newError <= errorThreshold)
188 cout <<
"errorThreshold: " << newError <<
" < " << errorThreshold << endl;
190 cout <<
"errorThreshold: " << newError <<
" > " << errorThreshold << endl;
193 if (newError <= errorThreshold)
197 double absoluteDecrease = currentError - newError;
199 if (absoluteDecrease <= absoluteErrorTreshold)
200 cout <<
"absoluteDecrease: " << setprecision(12) << absoluteDecrease <<
" < " 201 << absoluteErrorTreshold << endl;
203 cout <<
"absoluteDecrease: " << setprecision(12) << absoluteDecrease
204 <<
" >= " << absoluteErrorTreshold << endl;
208 double relativeDecrease = absoluteDecrease / currentError;
210 if (relativeDecrease <= relativeErrorTreshold)
211 cout <<
"relativeDecrease: " << setprecision(12) << relativeDecrease <<
" < " 212 << relativeErrorTreshold << endl;
214 cout <<
"relativeDecrease: " << setprecision(12) << relativeDecrease
215 <<
" >= " << relativeErrorTreshold << endl;
217 bool converged = (relativeErrorTreshold && (relativeDecrease <= relativeErrorTreshold)) ||
218 (absoluteDecrease <= absoluteErrorTreshold);
220 if (absoluteDecrease >= 0.0)
221 cout <<
"converged" << endl;
223 cout <<
"Warning: stopping nonlinear iterations because error increased" << endl;
225 cout <<
"errorThreshold: " << newError <<
" <? " << errorThreshold << endl;
226 cout <<
"absoluteDecrease: " << setprecision(12) << absoluteDecrease <<
" <? " 227 << absoluteErrorTreshold << endl;
228 cout <<
"relativeDecrease: " << setprecision(12) << relativeDecrease <<
" <? " 229 << relativeErrorTreshold << endl;
238 currentError, newError, params.
verbosity);
virtual const Values & optimize()
VectorValues optimize(const Eliminate &function=EliminationTraitsType::DefaultEliminate) const
IterationHook iterationHook
Subgraph Solver from IROS 2010.
virtual GaussianFactorGraph::shared_ptr iterate()=0
Verbosity verbosity
The printing verbosity during optimization (default SILENT)
NonlinearFactorGraph graph
double absoluteErrorTol
The maximum absolute error decrease to stop iterating (default 1e-5)
double relativeErrorTol
The maximum relative error decrease to stop iterating (default 1e-5)
std::unique_ptr< internal::NonlinearOptimizerState > state_
PIMPL'd state.
double errorTol
The maximum total error to stop iterating (default 0.0)
detail::enable_if_t<!detail::move_never< T >::value, T > move(object &&obj)
bool checkConvergence(double relativeErrorTreshold, double absoluteErrorTreshold, double errorThreshold, double currentError, double newError, NonlinearOptimizerParams::Verbosity verbosity)
const Values & values() const
return values in current optimizer state
GaussianFactorGraph::Eliminate getEliminationFunction() const
boost::optional< Ordering > ordering
The optional variable elimination ordering, or empty to use COLAMD (default: empty) ...
VectorValues optimize() const
Optimize from zero.
Linear Factor Graph where all factors are Gaussians.
IterativeOptimizationParameters::shared_ptr iterativeParams
The container for iterativeOptimization parameters. used in CG Solvers.
static SmartStereoProjectionParams params
virtual const NonlinearOptimizerParams & _params() const =0
double error() const
return error in current optimizer state
bool isSequential() const
void print(const std::string &str="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
const Values & optimizeSafely()
virtual ~NonlinearOptimizer()
virtual VectorValues solve(const GaussianFactorGraph &gfg, const NonlinearOptimizerParams ¶ms) const
Private class for NonlinearOptimizer state.
bool isMultifrontal() const
boost::shared_ptr< BayesNetType > eliminateSequential(OptionalOrderingType orderingType=boost::none, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex=boost::none) const
VectorValues optimize(const GaussianFactorGraph &gfg, const KeyInfo &keyInfo, const std::map< Key, Vector > &lambda, const VectorValues &initial) override
Ordering::OrderingType orderingType
The method of ordering use during variable elimination (default COLAMD)
Base class and parameters for nonlinear optimization algorithms.
#define tictoc_finishedIteration()
size_t maxIterations
The maximum iterations to stop iterating (default 100)
size_t iterations() const
return number of iterations in current optimizer state