Skip to content

Commit 60f880e

Browse files
authored
Merge pull request #1671 from borglab/optimizer-params
Optimizer Improvements
2 parents 508b5fd + c59f458 commit 60f880e

5 files changed

+41
-20
lines changed

gtsam/linear/IterativeSolver.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ void IterativeOptimizationParameters::print(ostream &os) const {
4646
<< verbosityTranslator(verbosity_) << endl;
4747
}
4848

49+
/*****************************************************************************/
50+
bool IterativeOptimizationParameters::equals(
51+
const IterativeOptimizationParameters &other, double tol) const {
52+
return verbosity_ == other.verbosity();
53+
}
54+
4955
/*****************************************************************************/
5056
ostream& operator<<(ostream &os, const IterativeOptimizationParameters &p) {
5157
p.print(os);

gtsam/linear/IterativeSolver.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,14 @@ class VectorValues;
4141
* parameters for iterative linear solvers
4242
*/
4343
class IterativeOptimizationParameters {
44-
45-
public:
46-
44+
public:
4745
typedef std::shared_ptr<IterativeOptimizationParameters> shared_ptr;
48-
enum Verbosity {
49-
SILENT = 0, COMPLEXITY, ERROR
50-
} verbosity_;
46+
enum Verbosity { SILENT = 0, COMPLEXITY, ERROR };
5147

52-
public:
48+
protected:
49+
Verbosity verbosity_;
50+
51+
public:
5352

5453
IterativeOptimizationParameters(Verbosity v = SILENT) :
5554
verbosity_(v) {
@@ -71,6 +70,9 @@ class IterativeOptimizationParameters {
7170
/* virtual print function */
7271
GTSAM_EXPORT virtual void print(std::ostream &os) const;
7372

73+
GTSAM_EXPORT virtual bool equals(const IterativeOptimizationParameters &other,
74+
double tol = 1e-9) const;
75+
7476
/* for serialization */
7577
GTSAM_EXPORT friend std::ostream &operator<<(
7678
std::ostream &os, const IterativeOptimizationParameters &p);

gtsam/nonlinear/LevenbergMarquardtOptimizer.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,9 @@ bool LevenbergMarquardtOptimizer::tryLambda(const GaussianFactorGraph& linear,
236236
if (currentState->iterations == 0) {
237237
cout << "iter cost cost_change lambda success iter_time" << endl;
238238
}
239-
cout << setw(4) << currentState->iterations << " " << setw(8) << newError << " " << setw(3) << setprecision(2)
240-
<< costChange << " " << setw(3) << setprecision(2) << currentState->lambda << " " << setw(4)
241-
<< systemSolvedSuccessfully << " " << setw(3) << setprecision(2) << iterationTime << endl;
239+
cout << setw(4) << currentState->iterations << " " << setw(12) << newError << " " << setw(12) << setprecision(2)
240+
<< costChange << " " << setw(10) << setprecision(2) << currentState->lambda << " " << setw(6)
241+
<< systemSolvedSuccessfully << " " << setw(10) << setprecision(2) << iterationTime << endl;
242242
}
243243
if (step_is_successful) {
244244
// we have successfully decreased the cost and we have good modelFidelity

gtsam/nonlinear/NonlinearOptimizerParams.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,28 @@ void NonlinearOptimizerParams::print(const std::string& str) const {
123123
std::cout.flush();
124124
}
125125

126+
/* ************************************************************************* */
127+
bool NonlinearOptimizerParams::equals(const NonlinearOptimizerParams& other,
128+
double tol) const {
129+
// Check for equality of shared ptrs
130+
bool iterative_params_equal = iterativeParams == other.iterativeParams;
131+
// Check equality of components
132+
if (iterativeParams && other.iterativeParams) {
133+
iterative_params_equal = iterativeParams->equals(*other.iterativeParams);
134+
} else {
135+
// Check if either is null. If both are null, then true
136+
iterative_params_equal = !iterativeParams && !other.iterativeParams;
137+
}
138+
139+
return maxIterations == other.getMaxIterations() &&
140+
std::abs(relativeErrorTol - other.getRelativeErrorTol()) <= tol &&
141+
std::abs(absoluteErrorTol - other.getAbsoluteErrorTol()) <= tol &&
142+
std::abs(errorTol - other.getErrorTol()) <= tol &&
143+
verbosityTranslator(verbosity) == other.getVerbosity() &&
144+
orderingType == other.orderingType && ordering == other.ordering &&
145+
linearSolverType == other.linearSolverType && iterative_params_equal;
146+
}
147+
126148
/* ************************************************************************* */
127149
std::string NonlinearOptimizerParams::linearSolverTranslator(
128150
LinearSolverType linearSolverType) const {

gtsam/nonlinear/NonlinearOptimizerParams.h

+1-10
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,7 @@ class GTSAM_EXPORT NonlinearOptimizerParams {
114114

115115
virtual void print(const std::string& str = "") const;
116116

117-
bool equals(const NonlinearOptimizerParams& other, double tol = 1e-9) const {
118-
return maxIterations == other.getMaxIterations()
119-
&& std::abs(relativeErrorTol - other.getRelativeErrorTol()) <= tol
120-
&& std::abs(absoluteErrorTol - other.getAbsoluteErrorTol()) <= tol
121-
&& std::abs(errorTol - other.getErrorTol()) <= tol
122-
&& verbosityTranslator(verbosity) == other.getVerbosity();
123-
// && orderingType.equals(other.getOrderingType()_;
124-
// && linearSolverType == other.getLinearSolverType();
125-
// TODO: check ordering, iterativeParams, and iterationsHook
126-
}
117+
bool equals(const NonlinearOptimizerParams& other, double tol = 1e-9) const;
127118

128119
inline bool isMultifrontal() const {
129120
return (linearSolverType == MULTIFRONTAL_CHOLESKY)

0 commit comments

Comments
 (0)