G2O 通过工厂函数类 OptimizationAlgorithmFactory 来生成固定搭配的优化算法

ops/2024/10/9 4:58:15/

         OptimizationAlgorithmFactory 类位于 optimization_algorithm_factory.h

//***g2o源码 g2o/g2o/core/optimization_algorithm_factory.h ***//
/*** \brief create solvers based on their short name** Factory to allocate solvers based on their short name.* The Factory is implemented as a sigleton and the single* instance can be accessed via the instance() function.*/
/*** \brief 工厂类用于根据优化算法的名称创建不同的优化算法(OptimizationAlgorithm)** 工厂类是单例模式(singleton)的实现,可以通过instance()方法获取唯一的实例,* 并通过destroy()方法释放实例。*/class G2O_CORE_API OptimizationAlgorithmFactory{public:typedef std::list<AbstractOptimizationAlgorithmCreator*>      CreatorList;//! return the instance//! 获取工厂类的唯一实例static OptimizationAlgorithmFactory* instance();//! free the instance//! 释放唯一实例static void destroy();/*** register a specific creator for allocating a solver* 注册特定的创建者 creator 以分配求解器 solver*/void registerSolver(AbstractOptimizationAlgorithmCreator* c);/*** unregister a specific creator for allocating a solver*/void unregisterSolver(AbstractOptimizationAlgorithmCreator* c);/*** construct a solver based on its name, e.g., var, fix3_2_cholmod* 根据名称 tag 来创建求解器 solver*/OptimizationAlgorithm* construct(const std::string& tag, OptimizationAlgorithmProperty& solverProperty) const;//! list the known solvers into a stream//! 将已知的求解器 solver 列到流中void listSolvers(std::ostream& os) const;//! return the underlying list of creators//! 返回创建者 creators 的基本列表const CreatorList& creatorList() const { return _creator;}protected:OptimizationAlgorithmFactory();~OptimizationAlgorithmFactory();CreatorList _creator;CreatorList::const_iterator findSolver(const std::string& name) const;CreatorList::iterator findSolver(const std::string& name);private:static OptimizationAlgorithmFactory* factoryInstance;};

        还有 RobustKernelFactory

//***g2o源码 g2o/g2o/core/robust_kernel_factory.h ***//
/*** \brief create robust kernels based on their human readable name*/class G2O_CORE_API RobustKernelFactory{public://! return the instancestatic RobustKernelFactory* instance();//! free the instancestatic void destroy();/*** register a tag for a specific creator*/void registerRobustKernel(const std::string& tag, AbstractRobustKernelCreator* c);/*** unregister a tag for a specific creator*/void unregisterType(const std::string& tag);/*** construct a robust kernel based on its tag*/RobustKernel* construct(const std::string& tag) const;/*** return the creator for a specific tag*/AbstractRobustKernelCreator* creator(const std::string& tag) const;/*** get a list of all known robust kernels*/void fillKnownKernels(std::vector<std::string>& types) const;protected:typedef std::map<std::string, AbstractRobustKernelCreator*>              CreatorMap;RobustKernelFactory();~RobustKernelFactory();CreatorMap _creator;     ///< look-up map for the existing creatorsprivate:static RobustKernelFactory* factoryInstance;};

        示例代码: 

// 这里使用一个工厂函数同时初始化迭代方式和线性求解方式,后续可以将迭代方式(总求解器 solver)和线性求解方式(线性求解器 LinearSolver)分开// 创建一个指向 g2o::OptimizationAlgorithmFactory 的实例指针g2o::OptimizationAlgorithmFactory *solver_factory = g2o::OptimizationAlgorithmFactory::instance();// 定义一个 g2o::OptimizationAlgorithmProperty 类型的对象用于存储与优化算法相关的元信息g2o::OptimizationAlgorithmProperty solver_property;// 根据名称 tag 创建求解器 solver,调用 construct() 时,函数会将所构建的求解器相关的元信息存储到这个对象中,// 比如该求解器的名称、是否需要舒尔补、位姿自由度、地图路标自由度等。g2o::OptimizationAlgorithm *solver = solver_factory->construct("lm_var", solver_property);std::unique_ptr<g2o::SparseOptimizer> graph_ptr_;graph_ptr_.reset(new g2o::SparseOptimizer());graph_ptr_->setAlgorithm(solver);if (!graph_ptr_->solver()) {LOG(ERROR) << "G2O 优化器创建失败!";}// 创建一个指向 g2o::RobustKernelFactory 的实例指针g2o::RobustKernelFactory *robust_kernel_factory_ = g2o::RobustKernelFactory::instance();// 根据给定的标签 tag 返回对应的鲁棒核函数实例g2o::RobustKernel *kernel = robust_kernel_factory_->construct(tag);// 设置鲁棒核函数的阈值为 delta_thkernel->setDelta(delta_th);

        其中,OptimizationAlgorithm* construct(const std::string& tag, OptimizationAlgorithmProperty& solverProperty) const; 函数中 tag 的所有可用算法与其对应描述有: 

//*** g2o源码 g2o/g2o/solvers/cholmod/solver_cholmod.cpp ***//
G2O_REGISTER_OPTIMIZATION_LIBRARY(cholmod);G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_var_cholmod,new CholmodSolverCreator(OptimizationAlgorithmProperty("gn_var_cholmod","Gauss-Newton: Cholesky solver using CHOLMOD (variable blocksize)","CHOLMOD", false, Eigen::Dynamic, Eigen::Dynamic)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_fix3_2_cholmod,new CholmodSolverCreator(OptimizationAlgorithmProperty("gn_fix3_2_cholmod","Gauss-Newton: Cholesky solver using CHOLMOD (fixed blocksize)","CHOLMOD", true, 3, 2)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_fix6_3_cholmod,new CholmodSolverCreator(OptimizationAlgorithmProperty("gn_fix6_3_cholmod","Gauss-Newton: Cholesky solver using CHOLMOD (fixed blocksize)","CHOLMOD", true, 6, 3)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_fix7_3_cholmod,new CholmodSolverCreator(OptimizationAlgorithmProperty("gn_fix7_3_cholmod","Gauss-Newton: Cholesky solver using CHOLMOD (fixed blocksize)","CHOLMOD", true, 7, 3)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_var_cholmod,new CholmodSolverCreator(OptimizationAlgorithmProperty("lm_var_cholmod","Levenberg: Cholesky solver using CHOLMOD (variable blocksize)","CHOLMOD", false, Eigen::Dynamic, Eigen::Dynamic)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_fix3_2_cholmod,new CholmodSolverCreator(OptimizationAlgorithmProperty("lm_fix3_2_cholmod","Levenberg: Cholesky solver using CHOLMOD (fixed blocksize)", "CHOLMOD",true, 3, 2)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_fix6_3_cholmod,new CholmodSolverCreator(OptimizationAlgorithmProperty("lm_fix6_3_cholmod","Levenberg: Cholesky solver using CHOLMOD (fixed blocksize)", "CHOLMOD",true, 6, 3)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_fix7_3_cholmod,new CholmodSolverCreator(OptimizationAlgorithmProperty("lm_fix7_3_cholmod","Levenberg: Cholesky solver using CHOLMOD (fixed blocksize)", "CHOLMOD",true, 7, 3)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(dl_var_cholmod,new CholmodSolverCreator(OptimizationAlgorithmProperty("dl_var_cholmod","Dogleg: Cholesky solver using CHOLMOD (variable blocksize)", "CHOLMOD",false, Eigen::Dynamic, Eigen::Dynamic)));
//*** g2o源码 g2o/g2o/solvers/csparse/solver_csparse.cpp ***//G2O_REGISTER_OPTIMIZATION_LIBRARY(csparse);G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_var_csparse, new CSparseSolverCreator(OptimizationAlgorithmProperty("gn_var_csparse", "Gauss-Newton: Cholesky solver using CSparse (variable blocksize)", "CSparse", false, Eigen::Dynamic, Eigen::Dynamic)));G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_fix3_2_csparse, new CSparseSolverCreator(OptimizationAlgorithmProperty("gn_fix3_2_csparse", "Gauss-Newton: Cholesky solver using CSparse (fixed blocksize)", "CSparse", true, 3, 2)));G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_fix6_3_csparse, new CSparseSolverCreator(OptimizationAlgorithmProperty("gn_fix6_3_csparse", "Gauss-Newton: Cholesky solver using CSparse (fixed blocksize)", "CSparse", true, 6, 3)));G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_fix7_3_csparse, new CSparseSolverCreator(OptimizationAlgorithmProperty("gn_fix7_3_csparse", "Gauss-Newton: Cholesky solver using CSparse (fixed blocksize)", "CSparse", true, 7, 3)));G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_var_csparse, new CSparseSolverCreator(OptimizationAlgorithmProperty("lm_var_csparse", "Levenberg: Cholesky solver using CSparse (variable blocksize)", "CSparse", false, Eigen::Dynamic, Eigen::Dynamic)));G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_fix3_2_csparse, new CSparseSolverCreator(OptimizationAlgorithmProperty("lm_fix3_2_csparse", "Levenberg: Cholesky solver using CSparse (fixed blocksize)", "CSparse", true, 3, 2)));G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_fix6_3_csparse, new CSparseSolverCreator(OptimizationAlgorithmProperty("lm_fix6_3_csparse", "Levenberg: Cholesky solver using CSparse (fixed blocksize)", "CSparse", true, 6, 3)));G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_fix7_3_csparse, new CSparseSolverCreator(OptimizationAlgorithmProperty("lm_fix7_3_csparse", "Levenberg: Cholesky solver using CSparse (fixed blocksize)", "CSparse", true, 7, 3)));G2O_REGISTER_OPTIMIZATION_ALGORITHM(dl_var_csparse, new CSparseSolverCreator(OptimizationAlgorithmProperty("dl_var_csparse", "Dogleg: Cholesky solver using CSparse (variable blocksize)", "CSparse", false, Eigen::Dynamic, Eigen::Dynamic)));
//*** g2o源码 g2o/g2o/solvers/eigen/solver_eigen.cpp ***//G2O_REGISTER_OPTIMIZATION_LIBRARY(eigen);G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_var, new EigenSolverCreator(OptimizationAlgorithmProperty("gn_var", "Gauss-Newton: Cholesky solver using Eigen's Sparse Cholesky methods (variable blocksize)", "Eigen", false, Eigen::Dynamic, Eigen::Dynamic)));G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_fix3_2, new EigenSolverCreator(OptimizationAlgorithmProperty("gn_fix3_2", "Gauss-Newton: Cholesky solver using  Eigen's Sparse Cholesky (fixed blocksize)", "Eigen", true, 3, 2)));G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_fix6_3, new EigenSolverCreator(OptimizationAlgorithmProperty("gn_fix6_3", "Gauss-Newton: Cholesky solver using  Eigen's Sparse Cholesky (fixed blocksize)", "Eigen", true, 6, 3)));G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_fix7_3, new EigenSolverCreator(OptimizationAlgorithmProperty("gn_fix7_3", "Gauss-Newton: Cholesky solver using  Eigen's Sparse Cholesky (fixed blocksize)", "Eigen", true, 7, 3)));G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_var, new EigenSolverCreator(OptimizationAlgorithmProperty("lm_var", "Levenberg: Cholesky solver using Eigen's Sparse Cholesky methods (variable blocksize)", "Eigen", false, Eigen::Dynamic, Eigen::Dynamic)));G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_fix3_2, new EigenSolverCreator(OptimizationAlgorithmProperty("lm_fix3_2", "Levenberg: Cholesky solver using  Eigen's Sparse Cholesky (fixed blocksize)", "Eigen", true, 3, 2)));G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_fix6_3, new EigenSolverCreator(OptimizationAlgorithmProperty("lm_fix6_3", "Levenberg: Cholesky solver using  Eigen's Sparse Cholesky (fixed blocksize)", "Eigen", true, 6, 3)));G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_fix7_3, new EigenSolverCreator(OptimizationAlgorithmProperty("lm_fix7_3", "Levenberg: Cholesky solver using  Eigen's Sparse Cholesky (fixed blocksize)", "Eigen", true, 7, 3)));G2O_REGISTER_OPTIMIZATION_ALGORITHM(dl_var, new EigenSolverCreator(OptimizationAlgorithmProperty("dl_var", "Dogleg: Cholesky solver using Eigen's Sparse Cholesky methods (variable blocksize)", "Eigen", false, Eigen::Dynamic, Eigen::Dynamic)));
//*** g2o源码 g2o/g2o/solvers/dense/solver_dense.cpp ***//
G2O_REGISTER_OPTIMIZATION_LIBRARY(dense);G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_dense, new DenseSolverCreator(OptimizationAlgorithmProperty("gn_dense", "Gauss-Newton: Dense solver (variable blocksize)","Dense", false, Eigen::Dynamic, Eigen::Dynamic)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_dense3_2,new DenseSolverCreator(OptimizationAlgorithmProperty("gn_dense3_2", "Gauss-Newton: Dense solver (fixed blocksize)", "Dense",true, 3, 2)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_dense6_3,new DenseSolverCreator(OptimizationAlgorithmProperty("gn_dense6_3", "Gauss-Newton: Dense solver (fixed blocksize)", "Dense",true, 6, 3)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_dense7_3,new DenseSolverCreator(OptimizationAlgorithmProperty("gn_dense7_3", "Gauss-Newton: Dense solver (fixed blocksize)", "Dense",true, 7, 3)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_dense, new DenseSolverCreator(OptimizationAlgorithmProperty("lm_dense", "Levenberg: Dense solver (variable blocksize)","Dense", false, -1, -1)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_dense3_2, new DenseSolverCreator(OptimizationAlgorithmProperty("lm_dense3_2", "Levenberg: Dense solver (fixed blocksize)","Dense", true, 3, 2)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_dense6_3, new DenseSolverCreator(OptimizationAlgorithmProperty("lm_dense6_3", "Levenberg: Dense solver (fixed blocksize)","Dense", true, 6, 3)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_dense7_3, new DenseSolverCreator(OptimizationAlgorithmProperty("lm_dense7_3", "Levenberg: Dense solver (fixed blocksize)","Dense", true, 7, 3)));
//*** g2o源码 g2o/g2o/solvers/pcg/solver_pcg.cpp ***//
G2O_REGISTER_OPTIMIZATION_LIBRARY(pcg);G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_pcg, new PCGSolverCreator(OptimizationAlgorithmProperty("gn_pcg","Gauss-Newton: PCG solver using block-Jacobi pre-conditioner ""(variable blocksize)","PCG", false, Eigen::Dynamic, Eigen::Dynamic)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_pcg3_2, new PCGSolverCreator(OptimizationAlgorithmProperty("gn_pcg3_2","Gauss-Newton: PCG solver using block-Jacobi ""pre-conditioner (fixed blocksize)","PCG", true, 3, 2)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_pcg6_3, new PCGSolverCreator(OptimizationAlgorithmProperty("gn_pcg6_3","Gauss-Newton: PCG solver using block-Jacobi ""pre-conditioner (fixed blocksize)","PCG", true, 6, 3)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(gn_pcg7_3, new PCGSolverCreator(OptimizationAlgorithmProperty("gn_pcg7_3","Gauss-Newton: PCG solver using block-Jacobi ""pre-conditioner (fixed blocksize)","PCG", true, 7, 3)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_pcg, new PCGSolverCreator(OptimizationAlgorithmProperty("lm_pcg","Levenberg: PCG solver using block-Jacobi pre-conditioner ""(variable blocksize)","PCG", false, Eigen::Dynamic, Eigen::Dynamic)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_pcg3_2, new PCGSolverCreator(OptimizationAlgorithmProperty("lm_pcg3_2","Levenberg: PCG solver using block-Jacobi pre-conditioner ""(fixed blocksize)","PCG", true, 3, 2)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_pcg6_3, new PCGSolverCreator(OptimizationAlgorithmProperty("lm_pcg6_3","Levenberg: PCG solver using block-Jacobi pre-conditioner ""(fixed blocksize)","PCG", true, 6, 3)));
G2O_REGISTER_OPTIMIZATION_ALGORITHM(lm_pcg7_3, new PCGSolverCreator(OptimizationAlgorithmProperty("lm_pcg7_3","Levenberg: PCG solver using block-Jacobi pre-conditioner ""(fixed blocksize)","PCG", true, 7, 3)));

        其中 RobustKernel* construct(const std::string& tag) const; 函数中 tag 的所有可用鲁棒核函数有: 

//*** g2o源码 g2o/g2o/core/robust_kernel_impl.cpp ***//
// register the kernel to their factory
G2O_REGISTER_ROBUST_KERNEL(Huber, RobustKernelHuber)
G2O_REGISTER_ROBUST_KERNEL(PseudoHuber, RobustKernelPseudoHuber)
G2O_REGISTER_ROBUST_KERNEL(Cauchy, RobustKernelCauchy)
G2O_REGISTER_ROBUST_KERNEL(GemanMcClure, RobustKernelGemanMcClure)
G2O_REGISTER_ROBUST_KERNEL(Welsch, RobustKernelWelsch)
G2O_REGISTER_ROBUST_KERNEL(Fair, RobustKernelFair)
G2O_REGISTER_ROBUST_KERNEL(Tukey, RobustKernelTukey)
G2O_REGISTER_ROBUST_KERNEL(Saturated, RobustKernelSaturated)
G2O_REGISTER_ROBUST_KERNEL(DCS, RobustKernelDCS)

参考

        [代码实践] G2O 学习记录(一):2D 位姿图优化

        [代码实践] G2O 学习记录(二):3D 位姿图优化


http://www.ppmy.cn/ops/123000.html

相关文章

使用CANFD路由实现CAN与CANFD互通

随着科技的发展&#xff0c;汽车电子和工业领域中CAN通信需要承载数据量也越来越大&#xff0c;传统CAN通信有了向CANFD通信过渡的倾向。在实现过渡的过程中可能会出现自己设备是CAN通信&#xff0c;客户设备是CANFD通信的情况&#xff0c;或者自己设备是CANFD通信&#xff0c;…

Street Gaussians 学习笔记

目录 3D Gaussian Splatting 依赖项&#xff1a; diff-gaussian-rasterization 浙大和理想汽车提出Street Gaussians&#xff1a;用于动态城市场景建模 3D Gaussian Splatting 最近的一项工作3D Gaussian Splatting (3D GS)&#xff0c;在3D世界中定义了一组各向异性的高斯…

从0开始下载安装并使用unity

首先我们要在浏览器上找到unity的官网 这一个就是了&#xff0c;我们点进去后是这个界面&#xff1a; 然后我们点击上面这张图的左下角的“下载Unity Hub”&#xff0c;推荐后续安装都装在D盘&#xff1a; 这里他会让我们注册一个账号&#xff0c;如果之前有的话登录就行了&am…

【TypeScript】抽象类 interface type的异同

#抽象类 interface type相似点很多&#xff0c;容易混淆使用# 抽象类&#xff08;abstract&#xff09; 定义类的格式&#xff0c;既可以包含抽象方法&#xff0c;也可以包含具体方法 一个类只能继承(extends)一个抽象类 abstract class Person {constructor(public name: s…

MySQL运维

MySQL运维 创建健壮的MySQL健康检查Python类 在本文中&#xff0c;我们将介绍如何创建一个强大而灵活的Python类&#xff0c;用于封装MySQL运维命令并提供易用的接口。这个类不仅支持后续扩展&#xff0c;还提供完备的响应和错误信息&#xff0c;同时要求必要的登录信息以确保…

2024年下半年软考准考证什么时候打印?

2024年下半年软考准考证打印入口网址如下&#xff1a; https://bm.ruankao.org.cn/sign/welcome 广东的同学特别注意&#xff1a;准考证打印截止时间是11月8号&#xff0c;也就是考试前一天。一定要提前打印准考证&#xff0c;考试当天是无法打印的。 2024年下半年软考准考证…

.locked勒索病毒:数据安全的新威胁

导言 在数字时代&#xff0c;数据已成为企业和个人的核心资产&#xff0c;其价值无可估量。然而&#xff0c;随着网络技术的飞速发展&#xff0c;一种名为“.locked勒索病毒”的恶意软件悄然兴起&#xff0c;对全球范围内的数据安全构成了严重威胁。这种病毒以其独特的加密方式…

Pikachu-Cross-Site Scripting-反射型xss(get)

存储型XSS 存储型XSS是指恶意脚本被存储在目标服务器上&#xff0c;当用户访问包含该脚本的页面时&#xff0c;脚本会被执行。攻击者通常通过输入框、留言板等用户可输入的地方进行注入。例如&#xff0c;攻击者可以在留言板中输入恶意脚本&#xff0c;当其他用户查看留言时&a…