Speeding up a slow running optimisaton problem
显示 更早的评论
I'm trying to workout how I can run an optimisation problem faster. I tried to vectorize it (converting my multidimentional arrays to vectors) rather than run for loops but I don't think that's possible when going into the optimisation solver.
It's a spectral unmixing algorithm where I've measured a mixed substance containing two components at two wavelengths (F) then used a non-negative regression and their known individual spectra (S) to approximate their amounts (A) through time (i).
The data measured is organised in a multidimentional arrays which are the dimentions of the square image (332x332, rows by columns, r, c) through time (i) at start (st) and stop (sp) times.
Despite putting display off - I still get a report every fit "optimisation completed because..." - can that be surpressed?
for r=1:332
for c=1:332
for i=st:sp
F=[double(imdye(r,c,i));double(imzero(r,c,i))];
A = optimvar('A',size(S,2),'LowerBound',0);
residual = S*A - F;
obj = sum(residual.^2);
nonneglsq = optimproblem('Objective',obj);
opts = optimoptions(nonneglsq,'Display','off');
[sol,fval,exitflag,output] = solve(nonneglsq);
dyeamount(r,c,i)=sol.A(1);
flatamount(r,c,i)=sol.A(2);
end
end
end
Any suggestions are very welcome
Thanks
2 个评论
Mike Croucher
2021-12-4
Is it possible to include all of your code so we can run it?
Sargondjani
2021-12-4
I think you mix up two things:
- Solve an equation analytically with symbolic toolbox
- Solve a numerical problem
Ad 1) You can evaluate the symbolic solution for different values of your parameters. You only need to solve the equation once, and then substitute the parameter values into that solution with a loop.
回答(1 个)
Don't use the problem-based framework when speed is critical and don't repeat unnecessary commands inside the loops.
N=size(S,2);
lb=zeros(N,1);
A=lb;
for r=1:332
for c=1:332
for i=st:sp
F=[double(imdye(r,c,i));double(imzero(r,c,i))];
[A,fval,exitflag,output] = lsqlin(S,F,[],[],[],[],lb,[],A,opts);
dyeamount(r,c,i)=A(1);
flatamount(r,c,i)=A(2);
end
end
end
类别
在 帮助中心 和 File Exchange 中查找有关 Mathematics and Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!