Why does 40-variable optimization array exceed maximum array size preference?
Show older comments
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 Comments
Michael Van de Graaff
on 22 Jan 2022
Why does 15.8 GB seem unrealistic? That's just 45x7x6726720*8 bytes.
If that array size seems to large, I would track down where your code is producing those sizes. I see the 45 floating around in your code, but idk where the other two dimensions are coming from.
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);
Kelsey Chambers
on 23 Jan 2022
Edited: Kelsey Chambers
on 23 Jan 2022
Accepted Answer
More Answers (0)
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!