Hi all,
I want to pass the distance between current position and desired position as a term of the cost function for an nonlinear problem. I can just calculate the distance of function using an internal library. The input of the function should be double variables, while the X[k] is a MX variable. Do you have any solution or idea?
Please find below the relevant part of the code.
Best,
```
class MyCallback : public Callback {
private:
// Data members
mpc_obj mpc_obj1;
// Private constructor
MyCallback(mpc_obj mpc_obj) : mpc_obj1(mpc_obj) {}
public:
// Creator function, creates an owning reference
static Function create(const std::string& name, mpc_obj mpc_obj,
const Dict& opts=Dict()) {
return Callback::create(name, new MyCallback(mpc_obj), opts);
}
// Initialize the object
virtual void init() {
std::cout << "initializing object" << std::endl;
}
// Number of inputs and outputs
virtual int get_n_in() { return 2;}
virtual int get_n_out() { return 2;}
// Evaluate numerically
virtual std::vector<MX> eval(std::vector<MX>& arg) {
Position pos_car;
pos_car[0] = (double) x * boost::units::si::meter;
pos_car[1] = (double) y * boost::units::si::meter;
Length par=mpc_obj1.drive->findClosestParameter(pos_car);
pos_car=mpc_obj1.leftBoundary->interpolate(par);
MX f = {pos_car[0].value(),pos_car[1].value()};
return {f};
}
};
main
{
Function f = MyCallback::create("f", mpc_obj);
vector<MX> arg={X[k][0],X[k][1]};
std::vector<MX> res = f(arg);
//std::cout << res << std::endl;
MX driveSpline=0.01*(pow(X[k][0]-
res.at(0),2)+pow(X[k][1]-
res.at(0),2));
J += I_out.at("qf")+driveSpline;
}
// NLP
MXDict nlp = {{"x", V}, {"f", J}, {"g", vertcat(g)}};
// Set options
Dict opts;
opts["ipopt.tol"] = 1e-5;
opts["ipopt.max_iter"] = 200;
//opts["ipopt.linear_solver"] = "ma27";
// Create an NLP solver and buffers
Function solver = nlpsol("nlpsol", "ipopt", nlp, opts);
std::map<std::string, DM> arg, res;
}
```
Error
```
terminate called after throwing an instance of 'casadi::CasadiException'
what(): The assertion "hasDerivative()" on line 2427 of file "/home/mi/boroujeni/casadi/casadi/core/function/function_internal.cpp" failed.
Please notify the CasADi developers.
```