Why does 40-variable optimization array exceed maximum array size preference?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Kelsey Chambers
le 21 Jan 2022
Commenté : Kelsey Chambers
le 23 Jan 2022
I am estimating parameters for a system of ODEs. The goal of my model is to match the results Ftrue of an experiment with input values Fo. The model uses 40 parameters Ak(1) to Ak(40) in its calculations. I used a MATLAB tutorial code to make this optimization problem:
%openExample('optim/FitODEProblemBasedExample')
Ftrue = [0 ... 0]; %experimental end-values
Fo = [0 ... 0.014533]; %experimental start-values
Wspan = [0 6];
%soltrue = ode45(@(W,F)diffun(W,F,Ak),Wspan,Fo, Aktrue);
Ak = optimvar('Ak',40,"LowerBound",0,"UpperBound",200);
myfcn = fcn2optimexpr(@RtoODE,Ak,Wspan,Fo,'OutputSize',[1,45]);
obj = sum(sum((myfcn - Ftrue).^2));
prob = optimproblem("Objective",obj);
Ak0.Ak = [50 ... 50];
[Aksol,sumsq] = solve(prob,Ak0);
This code calls on function RtoODE, which calls function diffun.
function solpts = RtoODE(Ak,Wspan,Fo)
sol = ode45(@(W,F)diffun(W,F,Ak),Wspan,Fo);
solpts = deval(sol,Wspan);
end
function dFdW = diffun(~,F,Ak) %simplified sketch of function diffun
%shows relationships between inputs Ak (parameter) and F (material flows)
Flow1 = F(1,:);
...
Flow45 = F(45,:);
dFdW(1,:) = Ak(1)*Ak(40)*Flow1; %all dFdW outputs are functions of various Ak and F values
...
dFdW(45,:) = Ak(20)*Ak(30)*Flow30 + Ak(1)*Ak(40)*Flow1;
end
When I run the optimization problem, it times out and gives me the following error:
>> FinalOptimization
Error using cat
Requested 45x7x6726720 (15.8GB) array exceeds maximum array size preference. Creation of arrays greater than
this limit may take a long time and cause MATLAB to become unresponsive. See array size limit or preference
panel for more information.
Error in ode45 (line 434)
f3d = cat(3,f3d,zeros(neq,7,chunk,dataType));
Error in RtoODE (line 2)
sol = ode45(@(W,F)diffun(W,F,Ak),Wspan,Fo);
Error in generatedObjective (line 35)
Error in optim.problemdef.OptimizationProblem/compileObjective>@(x)objhandle(x,extraParams)
Error in fmincon (line 552)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in optim.problemdef.OptimizationProblem/solve
Error in FinalOptimization (line 16)
[Aksol,sumsq] = solve(prob,Ak0);
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
The 15.8 GB array seems unrealistic for the size of the optimization problem. I know MATLAB can be used to estimate 30+ parameters in an ODE function, and the function runs without bugs until solve(prob,Ak0). Have I created an infinite loop? Do I need to narrow the Ak bounds further?
3 commentaires
Torsten
le 22 Jan 2022
Modifié(e) : Torsten
le 23 Jan 2022
What is the total number of measurement data you want to reproduce by your model ?
One more observation:
In Wspan, you must specify the vector of times when measurement data are present.
Just saying Wspan = [0 6] is wrong because it allows the solver to return the solution at times of its own choice.
And you return this arbitrary vector to the optimizer by setting
solpts = deval(sol,Wspan);
Réponse acceptée
Torsten
le 23 Jan 2022
Modifié(e) : Torsten
le 23 Jan 2022
It's hard to see what MATLAB does when using the problem-based approach.
I prefer the solver-based approach because it is much more transparent.
Here is the code for your problem - easy to understand and easy to debug:
function main
Fo = [0 ... 0.014533]; %experimental start-values
Ftrue = [0 ... 0]; %experimental end-values
Wspan = [0 6];
Ak0 = [50 ... 50];
lbAk = [0 ... 0];
ubAk = [200 ... 200];
AK = lsqnonlin(@(x)fun_optim(x,Fo,Ftrue,Wspan),Ak0,lbAk,ubAk)
end
function res = fun_optim(Ak,Fo,Ftrue,Wspan)
[t,sol] = ode15s(@(t,y)fun_ode(y,Ak),Wspan,Fo);
res = sol(end,:) - Ftrue;
end
function dFdW = fun_ode(F,Ak)
Flow1 = F(1);
..
Flow45 = F(45);
dFdW = zeros(45,1);
dFdW(1) = Ak(1)*Ak(40)*Flow1;
...
dFdW(45) = Ak(20)*Ak(30)*Flow30 + Ak(1)*Ak(40)*Flow1;
end
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Solver Outputs and Iterative Display dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!