Using Matlab coder with lsqcurvefit(), can not pass optimoptions as an argument to the target function

3 vues (au cours des 30 derniers jours)
I want to convert my function to mex using matlab coder. The function fits data to an exponential model.
function ab_fitted = my_fit_ab_1(x0, xdata, ydata)
simple_exp_func = @(x, xdata) x(1)*exp(-x(2)*xdata); % y=a*exp(-b)
Opt = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt','MaxIter', 100);
ab_fitted = lsqcurvefit(simple_exp_func, x0, xdata, ydata, [],[], Opt);
end
The script main_1.m that calls my_fit_ab_1() is as follows,
clear
a = rand();
b = rand();
xdata = rand(8,1);
ydata = a*exp(-b*xdata);
x0 = [rand, rand];
ab_fitted = my_fit_ab_1(x0, xdata, ydata);
With matlab coder App, I can easily convert my_fit_ab_1() to mex file my_fit_ab_1_mex.mexw64.
However, if I want to pass the variable Opt as an argument, i.e. my_fit_ab_2.m
function ab_fitted = my_fit_ab_2(x0, xdata, ydata, Opt)
simple_exp_func = @(x, xdata) x(1)*exp(-x(2)*xdata); % y=a*exp(-b)
ab_fitted = lsqcurvefit(simple_exp_func, x0, xdata, ydata, [],[], Opt);
end
and main_2.m
clear
a = rand();
b = rand();
xdata = rand(8,1);
ydata = a*exp(-b*xdata);
Opt = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt','MaxIter', 100);
x0 = [rand, rand];
ab_fitted = my_fit_ab_2(x0, xdata, ydata, Opt);
the coder says " The 'optim.options.Lsqcurvefit' class does not support code generation. "
Why optimoptions can be set inside the function while can not be passed as an argument?
I've attached the four files mentioned above.

Réponse acceptée

Steve Grikschat
Steve Grikschat le 17 Jan 2021
Hi Xingwang,
Despite support for the optimoptions function, the objects it generates are not supported for code generation. In the generated code, optimoptions generates structs of options.
As a workaround, I can suggest 2 alternatives, both of which require you to call optimoptions in the function that is being codegened:
  1. The most simple, which will work for this case but won't scale well is to pass the individual options you want set as inputs to your function. This won't be as easy to update as you add more options.
function ab_fitted = my_fit_ab_2(x0, xdata, ydata, maxiter)
Opt = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt','MaxIterations',maxiter);
simple_exp_func = @(x, xdata) x(1)*exp(-x(2)*xdata); % y=a*exp(-b)
ab_fitted = lsqcurvefit(simple_exp_func, x0, xdata, ydata, [],[], Opt);
end
2. If you anticipate wanting to toggle different options in the future, then try passing a struct of options that can be copied over into the optimoptions:
function ab_fitted = my_fit_ab_2(x0, xdata, ydata, optIn)
f = fieldnames(optIn);
Opt = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt');
for k = 1:numel(f)
thisOption = f{k};
Opt.(thisOption) = optIn.(thisOption);
end
simple_exp_func = @(x, xdata) x(1)*exp(-x(2)*xdata); % y=a*exp(-b)
ab_fitted = lsqcurvefit(simple_exp_func, x0, xdata, ydata, [],[], Opt);
end
To generate code for this, try this script:
dblMatType = coder.typeof(double(1), [8, 1], [1, 1]);
dblVecType = coder.typeof(double(1), [2, 1], [1, 1]);
dblScalarType = coder.typeof(double(1), [1, 1], [1, 1]);
optsType = struct('Algorithm', 'levenberg-marquardt', 'MaxIterations', dblScalarType);
cfg = coder.config('mex');
% x0 xdata ydata opts
cArgs = {dblVecType, dblMatType, dblMatType, optsType};
codegen -config cfg my_fit_ab_2 -args cArgs
Note that the 4th input is a struct of "typeof" objects that you can add to as needed.
  10 commentaires
Steve Grikschat
Steve Grikschat le 11 Mai 2021
Hi Xingwang,
It turns out that this may not be fixable in our code. The for-loop that sets options in a dynamic way breaks the codegen process for lsqcurvefit.
The for-loop that sets options in a dynamic way means that Coder cannot determine the value of SpecifyObjectiveGradient statically. This is a problem for generating the correct code since lsqcurvefit relies on this value to determine how to call the objective function (as we do in MATLAB).
Therefore, to deal with the uncertainty at codegen time, codegen wants to generate C code for a function with 2 output arguments (y and Jacobian) which is the maximum number of possible outputs.
Anyway, a workaround to add a Jacobian output with a dummy value. This works since Coder can safely make a 2 output syntax, and then the value of SpecifyObjectiveGradient = false will be checked at runtime to avoid ever using the dummy Jacobian value.
The modified simple_exp_func_1 looks like this
function [y,J] = simple_exp_func_1(x, xdata, constant)
y = x(1)*exp(-x(2)*xdata) + constant;
if nargout > 1
J = 0; % Dummy value, incorrect size, but doesn't matter since it never gets used at runtime
% Real Jacobian looks like this BTW
% J = [exp(-x(2)*xdata) -xdata*x(1).*exp(-x(2)*xdata)];
end
end % simple_exp_func_1
Xingwang Yong
Xingwang Yong le 12 Mai 2021
Modifié(e) : Xingwang Yong le 12 Mai 2021
Steve, thanks for your expertise. This dummy Jacobian solution works perfectly.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Input Specification dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by