26 #include <boost/variant.hpp>
41 double _wildfireThreshold = 0.001
42 ) : wildfireThreshold(_wildfireThreshold) {}
44 void print(
const std::string str =
"")
const {
45 std::cout << str <<
"type: ISAM2GaussNewtonParams\n";
46 std::cout << str <<
"wildfireThreshold: " << wildfireThreshold <<
"\n";
50 double getWildfireThreshold()
const {
return wildfireThreshold; }
51 void setWildfireThreshold(
double wildfireThreshold) { this->wildfireThreshold = wildfireThreshold; }
68 double _initialDelta = 1.0,
69 double _wildfireThreshold = 1e-5,
72 ) : initialDelta(_initialDelta), wildfireThreshold(_wildfireThreshold),
73 adaptationMode(_adaptationMode), verbose(_verbose) {}
75 void print(
const std::string str =
"")
const {
76 std::cout << str <<
"type: ISAM2DoglegParams\n";
77 std::cout << str <<
"initialDelta: " << initialDelta <<
"\n";
78 std::cout << str <<
"wildfireThreshold: " << wildfireThreshold <<
"\n";
79 std::cout << str <<
"adaptationMode: " << adaptationModeTranslator(adaptationMode) <<
"\n";
83 double getInitialDelta()
const {
return initialDelta; }
84 double getWildfireThreshold()
const {
return wildfireThreshold; }
85 std::string getAdaptationMode()
const {
return adaptationModeTranslator(adaptationMode); };
86 bool isVerbose()
const {
return verbose; };
88 void setInitialDelta(
double initialDelta) { this->initialDelta = initialDelta; }
89 void setWildfireThreshold(
double wildfireThreshold) { this->wildfireThreshold = wildfireThreshold; }
90 void setAdaptationMode(
const std::string& adaptationMode) { this->adaptationMode = adaptationModeTranslator(adaptationMode); }
91 void setVerbose(
bool verbose) { this->verbose = verbose; };
101 typedef FastMap<char,Vector> ISAM2ThresholdMap;
102 typedef ISAM2ThresholdMap::value_type ISAM2ThresholdMapValue;
138 enum Factorization { CHOLESKY, QR };
176 int _relinearizeSkip = 10,
177 bool _enableRelinearization =
true,
178 bool _evaluateNonlinearError =
false,
179 Factorization _factorization = ISAM2Params::CHOLESKY,
180 bool _cacheLinearizedFactors =
true,
182 ) : optimizationParams(_optimizationParams), relinearizeThreshold(_relinearizeThreshold),
183 relinearizeSkip(_relinearizeSkip), enableRelinearization(_enableRelinearization),
184 evaluateNonlinearError(_evaluateNonlinearError), factorization(_factorization),
185 cacheLinearizedFactors(_cacheLinearizedFactors), keyFormatter(_keyFormatter),
186 enableDetailedResults(false), enablePartialRelinearizationCheck(false),
187 findUnusedFactorSlots(false) {}
189 void print(
const std::string& str =
"")
const {
190 std::cout << str <<
"\n";
192 boost::get<ISAM2GaussNewtonParams>(optimizationParams).
print(
"optimizationParams: ");
194 boost::get<ISAM2DoglegParams>(optimizationParams).
print(
"optimizationParams: ");
196 std::cout <<
"optimizationParams: " <<
"{unknown type}" <<
"\n";
197 if(relinearizeThreshold.type() ==
typeid(double))
198 std::cout <<
"relinearizeThreshold: " << boost::get<double>(relinearizeThreshold) <<
"\n";
201 std::cout <<
"relinearizeThreshold: " <<
"{mapped}" <<
"\n";
202 BOOST_FOREACH(
const ISAM2ThresholdMapValue& value, boost::get<ISAM2ThresholdMap>(relinearizeThreshold)) {
203 std::cout <<
" '" << value.first <<
"' -> [" << value.second.transpose() <<
" ]\n";
206 std::cout <<
"relinearizeSkip: " << relinearizeSkip <<
"\n";
207 std::cout <<
"enableRelinearization: " << enableRelinearization <<
"\n";
208 std::cout <<
"evaluateNonlinearError: " << evaluateNonlinearError <<
"\n";
209 std::cout <<
"factorization: " << factorizationTranslator(factorization) <<
"\n";
210 std::cout <<
"cacheLinearizedFactors: " << cacheLinearizedFactors <<
"\n";
211 std::cout <<
"enableDetailedResults: " << enableDetailedResults <<
"\n";
212 std::cout <<
"enablePartialRelinearizationCheck: " << enablePartialRelinearizationCheck <<
"\n";
213 std::cout <<
"findUnusedFactorSlots: " << findUnusedFactorSlots <<
"\n";
219 RelinearizationThreshold getRelinearizeThreshold()
const {
return relinearizeThreshold; }
220 int getRelinearizeSkip()
const {
return relinearizeSkip; }
221 bool isEnableRelinearization()
const {
return enableRelinearization; }
222 bool isEvaluateNonlinearError()
const {
return evaluateNonlinearError; }
223 std::string getFactorization()
const {
return factorizationTranslator(factorization); }
224 bool isCacheLinearizedFactors()
const {
return cacheLinearizedFactors; }
225 KeyFormatter getKeyFormatter()
const {
return keyFormatter; }
226 bool isEnableDetailedResults()
const {
return enableDetailedResults; }
227 bool isEnablePartialRelinearizationCheck()
const {
return enablePartialRelinearizationCheck; }
229 void setOptimizationParams(OptimizationParams optimizationParams) { this->optimizationParams = optimizationParams; }
230 void setRelinearizeThreshold(RelinearizationThreshold relinearizeThreshold) { this->relinearizeThreshold = relinearizeThreshold; }
231 void setRelinearizeSkip(
int relinearizeSkip) { this->relinearizeSkip = relinearizeSkip; }
232 void setEnableRelinearization(
bool enableRelinearization) { this->enableRelinearization = enableRelinearization; }
233 void setEvaluateNonlinearError(
bool evaluateNonlinearError) { this->evaluateNonlinearError = evaluateNonlinearError; }
234 void setFactorization(
const std::string& factorization) { this->factorization = factorizationTranslator(factorization); }
235 void setCacheLinearizedFactors(
bool cacheLinearizedFactors) { this->cacheLinearizedFactors = cacheLinearizedFactors; }
236 void setKeyFormatter(
KeyFormatter keyFormatter) { this->keyFormatter = keyFormatter; }
237 void setEnableDetailedResults(
bool enableDetailedResults) { this->enableDetailedResults = enableDetailedResults; }
238 void setEnablePartialRelinearizationCheck(
bool enablePartialRelinearizationCheck) { this->enablePartialRelinearizationCheck = enablePartialRelinearizationCheck; }
240 Factorization factorizationTranslator(
const std::string& str)
const;
241 std::string factorizationTranslator(
const Factorization& value)
const;
244 return factorization == CHOLESKY
330 VariableStatus(): isReeliminated(false), isAboveRelinThreshold(false), isRelinearizeInvolved(false),
331 isRelinearized(false), isObserved(false), isNew(false), inRootClique(false) {}
344 void print(
const std::string str =
"")
const {
345 std::cout << str <<
" Reelimintated: " << variablesReeliminated <<
" Relinearized: " << variablesRelinearized <<
" Cliques: " << cliques << std::endl;
350 size_t getVariablesReeliminated()
const {
return variablesReeliminated; };
351 size_t getCliques()
const {
return cliques; };
363 typedef boost::shared_ptr<This> shared_ptr;
364 typedef boost::weak_ptr<This> weak_ptr;
368 Base::FactorType::shared_ptr cachedFactor_;
369 Vector gradientContribution_;
377 Base(other), cachedFactor_(other.cachedFactor_), gradientContribution_(other.gradientContribution_) {}
382 Base::operator=(other);
383 cachedFactor_ = other.cachedFactor_;
384 gradientContribution_ = other.gradientContribution_;
389 void setEliminationResult(
const FactorGraphType::EliminationResult& eliminationResult);
397 bool equals(
const This& other,
double tol=1e-9)
const;
400 void print(
const std::string& s =
"",
const KeyFormatter& formatter = DefaultKeyFormatter)
const;
405 friend class boost::serialization::access;
406 template<
class ARCHIVE>
407 void serialize(ARCHIVE & ar,
const unsigned int version) {
408 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(
Base);
409 ar & BOOST_SERIALIZATION_NVP(cachedFactor_);
410 ar & BOOST_SERIALIZATION_NVP(gradientContribution_);
424 class GTSAM_EXPORT ISAM2:
public BayesTree<ISAM2Clique> {
491 virtual bool equals(
const ISAM2& other,
double tol = 1e-9)
const;
523 const std::vector<size_t>& removeFactorIndices = std::vector<size_t>(),
525 const boost::optional<
FastList<Key> >& noRelinKeys = boost::none,
526 const boost::optional<
FastList<Key> >& extraReelimKeys = boost::none,
527 bool force_relinearize =
false);
544 boost::optional<std::vector<size_t>&> marginalFactorsIndices = boost::none,
545 boost::optional<std::vector<size_t>&> deletedFactorsIndices = boost::none);
554 Values calculateEstimate()
const;
562 template<
class VALUE>
563 VALUE calculateEstimate(
Key key)
const;
572 const Value& calculateEstimate(
Key key)
const;
575 Matrix marginalCovariance(
Key key)
const;
585 Values calculateBestEstimate()
const;
602 size_t lastAffectedVariableCount;
603 size_t lastAffectedFactorCount;
604 size_t lastAffectedCliqueCount;
605 size_t lastAffectedMarkedCount;
606 mutable size_t lastBacksubVariableCount;
609 const ISAM2Params& params()
const {
return params_; }
612 void printStats()
const { getCliqueData().getStats().print(); }
632 void updateDelta(
bool forceFullSolve =
false)
const;
647 template<
class CLIQUE>
649 double threshold,
const FastSet<Key>& replaced, VectorValues&
delta);
651 template<
class CLIQUE>
652 size_t optimizeWildfireNonRecursive(
const boost::shared_ptr<CLIQUE>& root,
653 double threshold,
const FastSet<Key>& replaced, VectorValues&
delta);
656 template<
class CLIQUE>
Base::Cliques Cliques
List of Clique typedef from base class.
Definition: ISAM2.h:479
boost::optional< double > errorBefore
The nonlinear error of all of the factors, including new factors and variables added during the curre...
Definition: ISAM2.h:270
bool enableRelinearization
Controls whether ISAM2 will ever relinearize any variables (default: true)
Definition: ISAM2.h:134
double wildfireThreshold
Continue updating the linear delta only when changes are above this threshold (default: 0...
Definition: ISAM2.h:37
DoglegOptimizerImpl::TrustRegionAdaptationMode adaptationMode
See description in DoglegOptimizerImpl::TrustRegionAdaptationMode.
Definition: ISAM2.h:63
boost::shared_ptr< Clique > sharedClique
Shared pointer to a clique.
Definition: BayesTree.h:72
Factorization factorization
Specifies whether to use QR or CHOESKY numerical factorization (default: CHOLESKY).
Definition: ISAM2.h:147
Incremental update functionality (ISAM2) for BayesTree, with fluid relinearization.
boost::optional< double > errorAfter
The nonlinear error of all of the factors computed after the current update, meaning that variables a...
Definition: ISAM2.h:281
The status of a single variable, this struct is stored in DetailedResults::variableStatus.
Definition: ISAM2.h:319
bool inRootClique
Whether the variable is in the root clique.
Definition: ISAM2.h:329
const FastSet< Key > & getFixedVariables() const
Access the nonlinear variable index.
Definition: ISAM2.h:600
double wildfireThreshold
Continue updating the linear delta only when changes are above this threshold (default: 1e-5) ...
Definition: ISAM2.h:62
bool isAboveRelinThreshold
Whether the variable was just relinearized due to being above the relinearization threshold...
Definition: ISAM2.h:324
ISAM2 This
This class.
Definition: ISAM2.h:475
This is the interface class for any value that may be used as a variable assignment in a factor graph...
Definition: Value.h:81
bool verbose
Whether Dogleg prints iteration and convergence information.
Definition: ISAM2.h:64
boost::optional< DetailedResults > detail
Detailed results, if enabled by ISAM2Params::enableDetailedResults.
Definition: ISAM2.h:341
virtual ~ISAM2()
default virtual destructor
Definition: ISAM2.h:488
size_t variablesReeliminated
The number of variables that were reeliminated as parts of the Bayes' Tree were recalculated, due to new factors.
Definition: ISAM2.h:299
bool isReeliminated
Whether the variable was just reeliminated, due to being relinearized, observed, new, or on the path up to the root clique from another reeliminated variable.
Definition: ISAM2.h:323
FastMap< Key, VariableStatus > variableStatus
The status of each variable during this update, see VariableStatus.
Definition: ISAM2.h:336
const Vector & gradientContribution() const
Access the gradient contribution.
Definition: ISAM2.h:395
bool enableDetailedResults
Whether to compute and return ISAM2Result::detailedResults, this can increase running time (default: ...
Definition: ISAM2.h:158
Base::FactorType::shared_ptr & cachedFactor()
Access the cached factor.
Definition: ISAM2.h:392
Nonlinear factor graph optimizer using Powell's Dogleg algorithm (detail implementation) ...
VariableIndex variableIndex_
VariableIndex lets us look up factors by involved variable and keeps track of dimensions.
Definition: ISAM2.h:432
GaussianFactorGraph linearFactors_
The current linear factors, which are only updated as needed.
Definition: ISAM2.h:459
int calculate_nnz(const boost::shared_ptr< CLIQUE > &clique)
calculate the number of non-zero entries for the tree starting at clique (use root for complete matri...
Definition: ISAM2-inl.h:297
Gaussian Bayes Tree, the result of eliminating a GaussianJunctionTree.
bool findUnusedFactorSlots
When you will be removing many factors, e.g.
Definition: ISAM2.h:170
Factor Graph Constsiting of non-linear factors.
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:75
ISAM2DoglegParams(double _initialDelta=1.0, double _wildfireThreshold=1e-5, DoglegOptimizerImpl::TrustRegionAdaptationMode _adaptationMode=DoglegOptimizerImpl::SEARCH_EACH_ITERATION, bool _verbose=false)
Specify parameters as constructor arguments.
Definition: ISAM2.h:67
size_t getVariablesRelinearized() const
Getters and Setters.
Definition: ISAM2.h:349
bool isObserved
Whether the variable was relinearized, either by being above the relinearization threshold or by invo...
Definition: ISAM2.h:327
void print(const Matrix &A, const string &s, ostream &stream)
print a matrix
Definition: Matrix.cpp:183
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: GaussianConditional.h:42
OptimizationParams getOptimizationParams() const
Getters and Setters for all properties.
Definition: ISAM2.h:218
Base::sharedClique sharedClique
Shared pointer to a clique.
Definition: ISAM2.h:478
const VariableIndex & getVariableIndex() const
Access the nonlinear variable index.
Definition: ISAM2.h:597
A non-linear factor graph is a graph of non-Gaussian, i.e.
Definition: NonlinearFactorGraph.h:69
NonlinearFactorGraph nonlinearFactors_
All original nonlinear factors are stored here to use during relinearization.
Definition: ISAM2.h:456
bool cacheLinearizedFactors
Whether to cache linear factors (default: true).
Definition: ISAM2.h:154
FastSet< Key > deltaReplacedMask_
A cumulative mask for the variables that were replaced and have not yet been updated in the linear so...
Definition: ISAM2.h:453
RelinearizationThreshold relinearizeThreshold
Only relinearize variables whose linear delta magnitude is greater than this threshold (default: 0...
Definition: ISAM2.h:130
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: GaussianFactorGraph.h:74
bool evaluateNonlinearError
Whether to evaluate the nonlinear error before and after the update, to return in ISAM2Result from up...
Definition: ISAM2.h:136
void printStats() const
prints out clique statistics
Definition: ISAM2.h:612
Specialized Clique structure for ISAM2, incorporating caching and gradient contribution TODO: more do...
Definition: ISAM2.h:358
Base::Clique Clique
A clique.
Definition: ISAM2.h:477
Template to create a binary predicate.
Definition: Testable.h:102
double initialDelta
The initial trust region radius for Dogleg.
Definition: ISAM2.h:61
boost::function< EliminationResult(const FactorGraphType &, const Ordering &)> Eliminate
The function type that does a single dense elimination step on a subgraph.
Definition: EliminateableFactorGraph.h:89
size_t optimizeWildfire(const boost::shared_ptr< CLIQUE > &root, double threshold, const FastSet< Key > &keys, VectorValues &delta)
Optimize the BayesTree, starting from the root.
Definition: ISAM2-inl.h:248
The VariableIndex class computes and stores the block column structure of a factor graph...
Definition: VariableIndex.h:42
size_t Key
Integer nonlinear key type.
Definition: types.h:59
ISAM2Clique & operator=(const ISAM2Clique &other)
Assignment operator, does not copy solution pointers as these are invalid in different trees...
Definition: ISAM2.h:380
size_t factorsRecalculated
The number of factors that were included in reelimination of the Bayes' tree.
Definition: ISAM2.h:302
const Values & getLinearizationPoint() const
Access the current linearization point.
Definition: ISAM2.h:548
A struct holding detailed results, which must be enabled with ISAM2Params::enableDetailedResults.
Definition: ISAM2.h:316
boost::variant< ISAM2GaussNewtonParams, ISAM2DoglegParams > OptimizationParams
Either ISAM2GaussNewtonParams or ISAM2DoglegParams.
Definition: ISAM2.h:104
ISAM2Clique()
Default constructor.
Definition: ISAM2.h:373
This is the base class for BayesTree cliques.
Definition: BayesTreeCliqueBase.h:44
BayesTree< ISAM2Clique > Base
The BayesTree base class.
Definition: ISAM2.h:476
int relinearizeSkip
Only relinearize any variables every relinearizeSkip calls to ISAM2::update (default: 10) ...
Definition: ISAM2.h:132
This class represents a collection of vector-valued variables associated each with a unique integer i...
Definition: VectorValues.h:89
bool enablePartialRelinearizationCheck
Check variables for relinearization in tree-order, stopping the check once a variable does not need t...
Definition: ISAM2.h:165
boost::optional< double > doglegDelta_
The current Dogleg Delta (trust region radius)
Definition: ISAM2.h:465
bool isRelinearizeInvolved
Whether the variable was below the relinearization threshold but was relinearized by being involved i...
Definition: ISAM2.h:325
ISAM2GaussNewtonParams(double _wildfireThreshold=0.001)
Specify parameters as constructor arguments.
Definition: ISAM2.h:40
size_t variablesRelinearized
The number of variables that were relinearized because their linear deltas exceeded the reslinearizat...
Definition: ISAM2.h:291
const NonlinearFactorGraph & getFactorsUnsafe() const
Access the set of nonlinear factors.
Definition: ISAM2.h:594
A Linear Factor Graph is a factor graph where all factors are Gaussian, i.e.
Definition: GaussianFactorGraph.h:65
Definition: ISAM2-impl.h:25
bool isNew
Whether the variable itself was just added.
Definition: ISAM2.h:328
ISAM2Clique(const ISAM2Clique &other)
Copy constructor, does not copy solution pointers as these are invalid in different trees...
Definition: ISAM2.h:376
Incremental update functionality (ISAM2) for BayesTree, with fluid relinearization.
ISAM2Params params_
The current parameters.
Definition: ISAM2.h:462
TrustRegionAdaptationMode
Specifies how the trust region is adapted at each Dogleg iteration.
Definition: DoglegOptimizerImpl.h:53
Definition: FastList.h:38
ISAM2Params(OptimizationParams _optimizationParams=ISAM2GaussNewtonParams(), RelinearizationThreshold _relinearizeThreshold=0.1, int _relinearizeSkip=10, bool _enableRelinearization=true, bool _evaluateNonlinearError=false, Factorization _factorization=ISAM2Params::CHOLESKY, bool _cacheLinearizedFactors=true, const KeyFormatter &_keyFormatter=DefaultKeyFormatter)
Specify parameters as constructor arguments.
Definition: ISAM2.h:173
KeyFormatter keyFormatter
A KeyFormatter for when keys are printed during debugging (default: DefaultKeyFormatter) ...
Definition: ISAM2.h:156
Values theta_
The current linearization point.
Definition: ISAM2.h:429
VectorValues delta_
The linear delta from the last linear solution, an update to the estimate in theta.
Definition: ISAM2.h:440
FastVector< size_t > newFactorsIndices
The indices of the newly-added factors, in 1-to-1 correspondence with the factors passed as newFactor...
Definition: ISAM2.h:311
size_t cliques
The number of cliques in the Bayes' Tree.
Definition: ISAM2.h:305
Vector delta(size_t n, size_t i, double value)
Create basis vector of dimension n, with a constant in spot i.
Definition: Vector.cpp:53
A conditional Gaussian functions as the node in a Bayes network It has a set of parents y...
Definition: GaussianConditional.h:36
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: types.h:62
OptimizationParams optimizationParams
Optimization parameters, this both selects the nonlinear optimization method and specifies its parame...
Definition: ISAM2.h:113
boost::variant< double, FastMap< char, Vector > > RelinearizationThreshold
Either a constant relinearization threshold or a per-variable-type set of thresholds.
Definition: ISAM2.h:105
int update_count_
Counter incremented every update(), used to determine periodic relinearization.
Definition: ISAM2.h:471
FastSet< Key > fixedVariables_
Set of variables that are involved with linear factors from marginalized variables and thus cannot ha...
Definition: ISAM2.h:469