1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 //   this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 //   notice, this list of conditions and the following disclaimer in the
13 //   documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #include "optimization_algorithm_levenberg.h"
28 
29 #include <iostream>
30 
31 #include "g2o/stuff/timeutil.h"
32 
33 #include "sparse_optimizer.h"
34 #include "solver.h"
35 #include "batch_stats.h"
36 using namespace std;
37 
38 namespace g2o {
39 
OptimizationAlgorithmLevenberg(std::unique_ptr<Solver> solver)40   OptimizationAlgorithmLevenberg::OptimizationAlgorithmLevenberg(std::unique_ptr<Solver> solver)
41       : OptimizationAlgorithmWithHessian(*solver.get()),
42         _currentLambda(cst(-1.)),
43         _tau(cst(1e-5)),
44         _goodStepLowerScale(cst(1. / 3.)),
45         _goodStepUpperScale(cst(2. / 3.)),
46         _ni(cst(2.)),
47         _levenbergIterations(0),
48         m_solver{std::move(solver)} {
49     _userLambdaInit = _properties.makeProperty<Property<number_t> >("initialLambda", 0.);
50     _maxTrialsAfterFailure = _properties.makeProperty<Property<int> >("maxTrialsAfterFailure", 10);
51   }
52 
~OptimizationAlgorithmLevenberg()53   OptimizationAlgorithmLevenberg::~OptimizationAlgorithmLevenberg()
54   {
55   }
56 
solve(int iteration,bool online)57   OptimizationAlgorithm::SolverResult OptimizationAlgorithmLevenberg::solve(int iteration, bool online)
58   {
59     assert(_optimizer && "_optimizer not set");
60     assert(_solver.optimizer() == _optimizer && "underlying linear solver operates on different graph");
61 
62     if (iteration == 0 && !online) { // built up the CCS structure, here due to easy time measure
63       bool ok = _solver.buildStructure();
64       if (! ok) {
65         cerr << __PRETTY_FUNCTION__ << ": Failure while building CCS structure" << endl;
66         return OptimizationAlgorithm::Fail;
67       }
68     }
69 
70     number_t t=get_monotonic_time();
71     _optimizer->computeActiveErrors();
72     G2OBatchStatistics* globalStats = G2OBatchStatistics::globalStats();
73     if (globalStats) {
74       globalStats->timeResiduals = get_monotonic_time()-t;
75       t=get_monotonic_time();
76     }
77 
78     number_t currentChi = _optimizer->activeRobustChi2();
79 
80     _solver.buildSystem();
81     if (globalStats) {
82       globalStats->timeQuadraticForm = get_monotonic_time()-t;
83     }
84 
85     // core part of the Levenbarg algorithm
86     if (iteration == 0) {
87       _currentLambda = computeLambdaInit();
88       _ni = 2;
89     }
90 
91     number_t rho=0;
92     int& qmax = _levenbergIterations;
93     qmax = 0;
94     do {
95       _optimizer->push();
96       if (globalStats) {
97         globalStats->levenbergIterations++;
98         t=get_monotonic_time();
99       }
100       // update the diagonal of the system matrix
101       _solver.setLambda(_currentLambda, true);
102       bool ok2 = _solver.solve();
103       if (globalStats) {
104         globalStats->timeLinearSolution+=get_monotonic_time()-t;
105         t=get_monotonic_time();
106       }
107       _optimizer->update(_solver.x());
108       if (globalStats) {
109         globalStats->timeUpdate = get_monotonic_time()-t;
110       }
111 
112       // restore the diagonal
113       _solver.restoreDiagonal();
114 
115       _optimizer->computeActiveErrors();
116       number_t tempChi = _optimizer->activeRobustChi2();
117 
118       if (! ok2)
119         tempChi=std::numeric_limits<number_t>::max();
120 
121       rho = (currentChi-tempChi);
122       number_t scale = computeScale();
123       scale += cst(1e-3); // make sure it's non-zero :)
124       rho /=  scale;
125 
126       if (rho>0 && g2o_isfinite(tempChi)){ // last step was good
127         number_t alpha = 1.-pow((2*rho-1),3);
128         // crop lambda between minimum and maximum factors
129         alpha = (std::min)(alpha, _goodStepUpperScale);
130         number_t scaleFactor = (std::max)(_goodStepLowerScale, alpha);
131         _currentLambda *= scaleFactor;
132         _ni = 2;
133         currentChi=tempChi;
134         _optimizer->discardTop();
135       } else {
136         _currentLambda*=_ni;
137         _ni*=2;
138         _optimizer->pop(); // restore the last state before trying to optimize
139         if (!g2o_isfinite(_currentLambda))
140           break;
141       }
142       qmax++;
143     } while (rho<0 && qmax < _maxTrialsAfterFailure->value() && ! _optimizer->terminate());
144 
145     if (qmax == _maxTrialsAfterFailure->value() || rho==0 || !g2o_isfinite(_currentLambda))
146       return Terminate;
147     return OK;
148   }
149 
computeLambdaInit() const150   number_t OptimizationAlgorithmLevenberg::computeLambdaInit() const
151   {
152     if (_userLambdaInit->value() > 0)
153       return _userLambdaInit->value();
154     number_t maxDiagonal=0;
155     for (size_t k = 0; k < _optimizer->indexMapping().size(); k++) {
156       OptimizableGraph::Vertex* v = _optimizer->indexMapping()[k];
157       assert(v);
158       int dim = v->dimension();
159       for (int j = 0; j < dim; ++j){
160         maxDiagonal = std::max(fabs(v->hessian(j,j)),maxDiagonal);
161       }
162     }
163     return _tau*maxDiagonal;
164   }
165 
computeScale() const166   number_t OptimizationAlgorithmLevenberg::computeScale() const
167   {
168     number_t scale = 0;
169     for (size_t j=0; j < _solver.vectorSize(); j++){
170       scale += _solver.x()[j] * (_currentLambda * _solver.x()[j] + _solver.b()[j]);
171     }
172     return scale;
173   }
174 
setMaxTrialsAfterFailure(int max_trials)175   void OptimizationAlgorithmLevenberg::setMaxTrialsAfterFailure(int max_trials)
176   {
177     _maxTrialsAfterFailure->setValue(max_trials);
178   }
179 
setUserLambdaInit(number_t lambda)180   void OptimizationAlgorithmLevenberg::setUserLambdaInit(number_t lambda)
181   {
182     _userLambdaInit->setValue(lambda);
183   }
184 
printVerbose(std::ostream & os) const185   void OptimizationAlgorithmLevenberg::printVerbose(std::ostream& os) const
186   {
187     os
188       << "\t schur= " << _solver.schur()
189       << "\t lambda= " << FIXED(_currentLambda)
190       << "\t levenbergIter= " << _levenbergIterations;
191   }
192 
193 } // end namespace
194