Home > Back-end >  Ceres solver - Set size of parameter block of CostFunction
Ceres solver - Set size of parameter block of CostFunction

Time:11-15

in this Ceres example, SizedCostFunction<1,1> is used. I would like to change it to CostFunction since I do not know the size of input parameters during compilation time. I found out that the number of residuals can be easily changed with set_num_residuals(int), however, I cannot find a way to set the number of inputs. Could you tell me how to set it?

class QuadraticCostFunction
    : public SizedCostFunction<1 /* number of residuals */,
                               1 /* size of first parameter */> {
 public:
  bool Evaluate(double const* const* parameters,
                double* residuals,
                double** jacobians) const override {
    double x = parameters[0][0];
    // f(x) = 10 - x.
    residuals[0] = 10 - x;
   
    if (jacobians != nullptr && jacobians[0] != nullptr) {
      jacobians[0][0] = -1;
    }
    return true;
  }
};

CodePudding user response:

You can call from QuadraticCostFunction these protected CostFunction member fuctions:

set_num_residuals(num);
*mutable_parameter_block_sizes() = std::vector<int32_t>{ /* size_1, ..., size_num */ };

You don't seem to need to inherit SizedCostFunction.

class QuadraticCostFunction
    : public CostFunction {
  • Related