Vectorization/increase efficiency of nonconvex optimization problem using systematic initializations
Show older comments
I am trying to speed up an optimization process. Right now, since I have a nonconvex optimization problem, I need to use systematic intializations of the start point parameters in order to find the global minimum. This is very computationally expensive and inefficient since I am using loops. I have two main questions:
1) Is there a way to vectorize the systematic initializations instead of using nested loops?
2) Can I fit mulitple optimization problems simultaneously (instead of looping through data channels)? i.e run one process for 100 different fits instead of using a loop 1:100?
Below is my code:
%%
%Parameter values for systematic evaluations
angularPosSpace = 0:360/16:360-360/16;
curvatureSpace = -1:2/11:1;
kSpace = [.5, 2];
sigmaSpace = [.2, 0.7];
%%
%Preallocates variables
storedParam2 = nan(6, length(curvatureSpace), length(angularPosSpace), length(sigmaSpace), length(kSpace), lenChannels);
determinedError = nan(length(curvatureSpace), length(angularPosSpace), length(sigmaSpace), length(kSpace), lenChannels);
usedParameters2 = nan(6, lenChannels);
%Loops through data channels
for ch = 1:lenChannels
display(ch)
%For curvature intialization
for curvVal = 1:length(curvatureSpace)
%For angular position initilization
for angVal = 1:length(angularPosSpace)
%For k spread initilization
for kVal = 1:length(kSpace)
%For sigma spread initialization
for sigmaVal = 1:length(sigmaSpace)
%Declares fitting function
gaussianModel2 = @(param) squeeze(max(param(1) + param(2) .* ((exp(-1 .* (allStimCurvAng(:, 1, :) - param(3)).^2 ./ (param(4).^2))) .* ...
(exp(1/param(5) .* cosd(allStimCurvAng(:, 2, :) - param(6))) ./ exp(1 ./ param(5))))));
%Declares objective function
resid2 = @(param) (gaussianModel2(param) - respSpikeSec(:, channels(ch)));
%Calculates values necessary for startPoint
minData = min(respSpikeSec(:, channels(ch)));
maxData = max(respSpikeSec(:, channels(ch)));
lb = [minData, -inf, -1.3, 0.05, 0, -inf];
ub = [maxData, maxData * 1.5, 1.3, 30, 100, inf];
%Initial Start Point - systmatic tiling of parameter
%space
startPoint = [minData, maxData, curvatureSpace(curvVal), sigmaSpace(sigmaVal), kSpace(kVal), angularPosSpace(angVal)];
%Finds parameters with lowest error for the given start
%point initialization
[parameters2, tempError2] = lsqnonlin(@(x) resid2(x), startPoint, lb, ub, options);
%Stores relevant fit information
storedParam2(:, curvVal, angVal, sigmaVal, kVal, ch) = parameters2;
determinedError(curvVal, angVal, sigmaVal, kVal, ch) = tempError2;
end
end
end
end
%%
%Determines parameters with lowest error
errorChanLsq = determinedError(:, :, :, :, ch);
linIdx = find(errorChanLsq == min(errorChanLsq, [], 'all'));
[curvLoc, angLoc, sigmaLoc, kLoc] = ind2sub(size(errorChanLsq), min(linIdx));
usedParameters2(:, ch) = storedParam2(:, curvLoc, angLoc, sigmaLoc, kLoc, ch);
end
Accepted Answer
More Answers (0)
Categories
Find more on Surrogate Optimization 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!