1

I am a beginner to Ceres (have been looking into it for little under a week) and am trying to implement to use the ceres solver to solve a cost function that I have within a class. Below is the sample version of my code and I have trouble getting the cost function set up for ceres.

struct myStruct {
    myStruct() {

    }

    template <typename T>
    bool operator()(const T* params, T* residuals) const {
        Myclass mClass;
        //I need to set the double[14] params to my class
        //mClass.SetParams(params); //params is the array of double[14]
        //call the cost function defined within the class
        //residuals[0] = T(mClass.evaluate());
        //get the double[14] params and residual
        //mclass.GetParams(params)
        return true;
    }

    static ceres::CostFunction* Create() {
        return (new ceres::AutoDiffCostFunction<myStruct, 1,14>(new myStruct()));
    }
]
};

int main(int argc, char** argv)
{   
    google::InitGoogleLogging(argv[0]);
    ceres::Problem problem;
    double[14] initParams;
        ceres::CostFunction* cost_function = myStruct::Create();
        problem.AddResidualBlock(cost_function,
            NULL, *initParams);
    ceres::Solver::Options options;
    options.linear_solver_type = ceres::DENSE_QR;
    options.minimizer_progress_to_stdout = true;

    ceres::Solver::Summary summary;
    ceres::Solve(options, &problem, &summary);
    std::cout << summary.FullReport() << "\n";

    std::cout << "Press any key to continue....";
    getchar();
    return 0;
}

The process should be as follows: I have an array of 14 parameters initparam[14] which has to be input to my class within the operator() in myStruct.

When I try accessing the initparam within the operator, each element is not a double, but a ceres Jet.

From what I could see from some other blog posts, I might have to implement something of this sort mentioned in the ceres documentation(http://ceres-solver.org/interfacing_with_autodiff.html). Unfortunately I have a limited knowledge of ceres and am not sure if this is actually the right way to go about it. Could anyone please help me out? I'd be happy to give out more information if needed

MSK
  • 448
  • 2
  • 5
  • 18

1 Answers1

0

The cost function or evaluation method inside your class needs to be able to work with dual numbers (the ceres::Jet type). Otherwise you can't get automatic differentiation.

Since the Jet type implements all the regular scalar operations, you could just template your class cost function on T (double or Jet): mClass.evaluate<T>().

Bogdan B
  • 485
  • 5
  • 15