Error using casadiMEXThe assertion "fcallback_.size_in(i)==size_out(i)" on line 245 of file "/Users/travis/build/casadi/binaries/casadi/casadi/core/function/nlpsol.cpp" failed.Callback function input size mismatchclassdef IterationCallback < casadi.Callback
properties
nx
ng
np
end
methods
function self = IterationCallback(name, nx, ng, np)
se...@casadi.Callback();
self.nx = nx;
self.ng = ng;
self.np = np;
opts.input_scheme = casadi.nlpsol_out();
opts.output_scheme = 'ret';
self.construct(name,opts);
disp('Callback instance created')
end
function v=get_n_in(self)
v=casadi.nlpsol_n_out;
end
function v = get_n_out(self)
v = 1;
end
function v = get_sparsity_in(self, i)
n = casadi.nlpsol_out(i);
if n=='f'
v = casadi.Sparsity.scalar();
elseif strcmp(n,'x') || strcmp(n,'lam_x')
v = casadi.Sparsity.dense(self.nx);
elseif strcmp(n,'g') || strcmp(n,'lam_g')
v = casadi.Sparsity.dense(self.ng);
elseif strcmp(n,'lam_p')
v = casadi.Sparsity.dense(self.np);
else
v = casadi.Sparsity(0,0);
end
end
function v = eval(self, arg)
disp('Solver iteration')
v = {0};
end
end
end
In get_sparsity_in, make it print 'i' and 'n'.
Also, give a printout of solver.print_dimensions()/solver.printDimensions() when you disable the callback.
We are fully aware that the Callbacks are not userfriendly yet.
Number of inputs: 8 Input 0, a.k.a. "x0", (243, 1) Input 1, a.k.a. "p", (0, 0) Input 2, a.k.a. "lbx", (243, 1) Input 3, a.k.a. "ubx", (243, 1) Input 4, a.k.a. "lbg", (180, 1) Input 5, a.k.a. "ubg", (180, 1) Input 6, a.k.a. "lam_x0", (243, 1) Input 7, a.k.a. "lam_g0", (180, 1) Number of outputs: 6 Output 0, a.k.a. "x", (243, 1) Output 1, a.k.a. "f", (1, 1) Output 2, a.k.a. "g", (180, 1) Output 3, a.k.a. "lam_x", (243, 1) Output 4, a.k.a. "lam_g", (180, 1) Output 5, a.k.a. "lam_p", (0, 0)get_n_inv = 6get_n_outCallback instance createdx = SX.sym('x');
y = SX.sym('y');
f = (1-x)^2+100*(y-x^2)^2;
nlp = struct('x',vertcat(x,y), 'f', f, 'g', x+y);
fcn = Function('f', {x, y}, {f});
mycallback = IterationCallback('mycallback', 2, 1, 0);
opts = struct;
opts.iteration_callback = mycallback;
opts.ipopt.tol = 1e-8;
opts.ipopt.max_iter = 50;
solver = nlpsol('solver', 'ipopt', nlp, opts);
args = struct;
args.lbx = -10;
args.ubx = 10;
args.lbg = -10;
args.ubg = 10;
sol = solver(args)