Ceres 求解器:如何定义 bounds/constraints?
Ceres Solver: how to define bounds/constraints?
Ceres 求解器到处都说它可以
[...] solve robustified bounds constrained non-linear least squares problems
并且它支持参数块的上限和下限约束(例如在 http://ceres-solver.org/modeling_faqs.html 中它声明 Ceres Solver only supports upper and lower bounds constraints on the parameter blocks
),但不知何故我在文档中找不到任何地方我可以设置这些上限和下限。
那么,如何在 ceres 求解器中设置参数块的上限和下限?
具体来说,我该如何在 AutoDiffCostFunction
中做到这一点?如果我使用 if
语句来 return 一个非常大的残差越界,那么这个函数是不可微分的。
例如,这是ceres Hello World:
struct CostFunctor {
template <typename T>
bool operator()(const T* const x, T* residual) const {
residual[0] = 10.0 - x[0];
return true;
}
};
int main(int argc, char** argv) {
google::InitGoogleLogging(argv[0]);
// The variable to solve for with its initial value.
double initial_x = 5.0;
double x = initial_x;
// Build the problem.
Problem problem;
// Set up the only cost function (also known as residual). This uses
// auto-differentiation to obtain the derivative (jacobian).
CostFunction* cost_function =
new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
problem.AddResidualBlock(cost_function, nullptr, &x);
// Run the solver!
Solver::Options options;
options.linear_solver_type = ceres::DENSE_QR;
options.minimizer_progress_to_stdout = true;
Solver::Summary summary;
Solve(options, &problem, &summary);
std::cout << summary.BriefReport() << "\n";
std::cout << "x : " << initial_x
<< " -> " << x << "\n";
return 0;
}
在此示例中,我如何将 2.0 的下限和 20.0 的上限强加给参数 x?
您可以使用此处定义的方法 setParameterLowerBound
和 setParameterUpperBound
:
http://ceres-solver.org/nnls_modeling.html?highlight=setparameterlowerbound#_CPPv4N5ceres7Problem22SetParameterLowerBoundEPdid
在你的情况下,我猜是这样的:
problem.SetParameterLowerBound(&x, 0, lower_bound);
problem.SetParameterUpperBound(&x, 0, upper_bound);
Ceres 求解器到处都说它可以
[...] solve robustified bounds constrained non-linear least squares problems
并且它支持参数块的上限和下限约束(例如在 http://ceres-solver.org/modeling_faqs.html 中它声明 Ceres Solver only supports upper and lower bounds constraints on the parameter blocks
),但不知何故我在文档中找不到任何地方我可以设置这些上限和下限。
那么,如何在 ceres 求解器中设置参数块的上限和下限?
具体来说,我该如何在 AutoDiffCostFunction
中做到这一点?如果我使用 if
语句来 return 一个非常大的残差越界,那么这个函数是不可微分的。
例如,这是ceres Hello World:
struct CostFunctor {
template <typename T>
bool operator()(const T* const x, T* residual) const {
residual[0] = 10.0 - x[0];
return true;
}
};
int main(int argc, char** argv) {
google::InitGoogleLogging(argv[0]);
// The variable to solve for with its initial value.
double initial_x = 5.0;
double x = initial_x;
// Build the problem.
Problem problem;
// Set up the only cost function (also known as residual). This uses
// auto-differentiation to obtain the derivative (jacobian).
CostFunction* cost_function =
new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
problem.AddResidualBlock(cost_function, nullptr, &x);
// Run the solver!
Solver::Options options;
options.linear_solver_type = ceres::DENSE_QR;
options.minimizer_progress_to_stdout = true;
Solver::Summary summary;
Solve(options, &problem, &summary);
std::cout << summary.BriefReport() << "\n";
std::cout << "x : " << initial_x
<< " -> " << x << "\n";
return 0;
}
在此示例中,我如何将 2.0 的下限和 20.0 的上限强加给参数 x?
您可以使用此处定义的方法 setParameterLowerBound
和 setParameterUpperBound
:
http://ceres-solver.org/nnls_modeling.html?highlight=setparameterlowerbound#_CPPv4N5ceres7Problem22SetParameterLowerBoundEPdid
在你的情况下,我猜是这样的:
problem.SetParameterLowerBound(&x, 0, lower_bound);
problem.SetParameterUpperBound(&x, 0, upper_bound);