c++ - 修改平方和 [ceres-solver]

标签 c++ ceres-solver

我正在尝试修改 ceres 的默认行为,即计算残差的平方和作为成本函数。我希望它只计算总和(残差已经以只能为正的方式计算)

根据文档,我应该使用 ConditionedCostFunction

这是我所做的: 我定义了带有 1 个残差和 1 个参数的调节器

struct Conditioners : ceres::CostFunction
{
public:
    Conditioners()
    {
        set_num_residuals(1);
        mutable_parameter_block_sizes()->push_back(1);
    }

    ~Conditioners()
    {}

    template<typename T>
    T operator() (T x)
    {
        return T(x * x);
    }

    bool Evaluate(double const* const* parameters, double* residuals, double** jacobians) const
    {
        return true;
    }
};

我把调节器放在一个 vector 中

std::vector<ceres::CostFunction*> conditioners;

for(int i = 0; i < 1; i++)
    conditioners.push_back(new Conditioners());

ceres::ConditionedCostFunction* ccf =
              new ceres::ConditionedCostFunction(cost_function, conditioners, ceres::TAKE_OWNERSHIP);

problem.AddResidualBlock(ccf, NULL, &x);

它编译和一切。但这并不能解决问题。它甚至没有开始。它说:

Ceres Solver Report: Iterations: 0, Initial cost: 4.512500e+01, Final cost: 4.512500e+01, Termination: CONVERGENCE
x : 0.5 -> 0.5

代替:

iter      cost      cost_change  |gradient|   |step|    tr_ratio  tr_radius  ls_iter  iter_time  total_time
   0  4.512500e+01    0.00e+00    9.50e+00   0.00e+00   0.00e+00  1.00e+04       0    2.99e-04    1.04e-03
   1  4.511598e-07    4.51e+01    9.50e-04   9.50e+00   1.00e+00  3.00e+04       1    3.84e-04    9.72e-03
   2  5.012552e-16    4.51e-07    3.17e-08   9.50e-04   1.00e+00  9.00e+04       1    2.98e-05    9.92e-03
Ceres Solver Report: Iterations: 2, Initial cost: 4.512500e+01, Final cost: 5.012552e-16, Termination: CONVERGENCE
x : 0.5 -> 10

(如果你想自己试试,这个例子修改了helloword example) 您对出了什么问题有任何指示吗? (ceres 报告没有更具体)

最佳答案

我找到了解决方案,即:

struct Conditioners : ceres::CostFunction
{
public:
Conditioners()
{
    set_num_residuals(1);
    mutable_parameter_block_sizes()->push_back(1);
}

~Conditioners()
{}

template<typename T>
T operator() (T x)
{
    return T(x * x);
}

bool Evaluate(double const* const* parameters, double* residuals, double** jacobians) const
{
    residuals[0] = parameters[0][0] * parameters[0][0]

    if (jacobians)
         jacobians[0][0] = 2.0 * parameters[0][0]

    return true;
}
};

我的错误是认为我只需要重新实现 () 运算符,ceres 就会自动找到 jacobian。它没有。

关于c++ - 修改平方和 [ceres-solver],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25574010/

相关文章:

c++ - 如何从根文件夹及其所有子文件夹生成目录树?

c++ - 我们如何知道 True Type 字体的代码点是否高于 0xFFFF?

c++ - 使用 ceres 求解器时传递 double 参数但获取 Jet<double,6>

c++ - 使C++自动将int转换为double

c++ - 使用 Ceres 求解器进行 CMake-ing 项目时冲突的 Eigen 依赖项

c++ - 初始化对象时复制构造函数/赋值运算符混淆

c++ - 移动 SDL2 窗口的功能,同时鼠标向下闪烁窗口并且移动不够 'fast'

c++ - malloc导致内存泄漏

android - 如何修复 Ceres_USE_OPENMP、CERES_USE_CXX11_THREADS 或 CERES_NO_THREADS 中的一个错误必须在 Ceres Solver Android 中定义

android - 使用 Android 和 Eclipse 编译 Ceres Solver