Hzine
Hzine

Reputation: 123

Can ceres solver and nlopt use the same model for least squares curve fitting?

I have a model setup that is working with NLopt, the setup consist of a baseModel (abstract base class for all derived models to take advantage of polymorphism), see below code example (the code is very long and these are the core functionalities):

class BaseModel
{
public:
    virtual double calculate(doubel x, std::vector<double>& params) const =0;
}

class Model1: public BaseModel
{
public:
    double calculate(doubel x, std::vector<double>& params) const override
    {
        //do calculation1
        return ans1;
    }
}

class Model2: public BaseModel
{
public:
    double calculate(doubel x, std::vector<double>& params) const override
    {
        //do calculation2
        return ans2;
    }
}

class ModelManager
{
    std::shared_ptr<BaseModel> createModel(std::string selectedModel)
    {
        if(selectedModel =="model1") return make_shared<Model1>();
        if(selectedModel =="model2") return make_shared<Model2>();
        .
        .
        .
        if(selectedModel =="modeln") return make_shared<Modeln>();
    }

    void setModel(std::string selectedModel)
    {
        Model_ptr = createModel(selectedModel);
    }

    double objectiveFunction(std::vector<double>& params, std::vector<double>& grad)
    {
        //return the sum of residuals squared using xdata and ydata;
        double res=0;
        for (int i=0;i<xdata.size();i++)
        {
            double modelValue = model_ptr->calculate(xdata[i], params)
            double resid = ydata[i]-modelValue
            res += resid*resid;
        }
        return res;
    }
    void nlopt_optimize()
    {
        // run the nlopt optimize and return the best parameters with fitdata;
        // this is working very fine for me right now
    }

    void ceres_optimize()
    {
        // run the ceres optimize and return the best parameters with fitdata;
        // I want to add this to solve the same models
    }
private:
std::shared_ptr<BaseModel> model_ptr;
std::vector<double> xdata,ydata,fitdata,bestParams;

}

As I mentioned, the code for nlopt is working very fine, I want to add the option to curve fit using ceres solver, so the user will set the model at runtime, by choosing the fitting model, the program and based on the model selection will fill all the fitting requirements like the number of parameters and their bounds, the problem arises when I attempted to add a CostFunctor that point to the model, it says that the model can not accept the template type sent by the functor, I tried to use templates in the baseModel but I could not due to pure virtual method 'calculate'.

Is there a way that I can preserve my working nlopt structure and add ceres solver functionalities to it so it solves the same model without breaking the working code.

As troubleshooting, I searched the documentation of ceres solver, I checked the common questions asked about the subject in stackoverflow especially those of Sameer Agrawal the developer of ceres, and tried the template appraoch without success. I confirm that I reviewed all the suggested question during the editing of this question. I can provide more information as needed.

Upvotes: 0

Views: 60

Answers (0)

Related Questions