Inconsistent results produced by lsqcurvefit using the same setting and initial starting points

7 views (last 30 days)
Hi. I tried to estimate the optimal parameters using the lsqcurvefit function in the optimization toolbox but I found that I always got different results using exactly the same setting and the same initial starting points! As you may find when you run the code, the answers lsqcurvefit gives you are not the same! (The algorithm I used is the default trust region reflective) This seems pretty spooky to me because every time I run the script I might get different answers and I have completely no idea where I went wrong. Please help! Thank you guys so much in advance!!
% See attachment for yData,S,H and function Predict
%%Please load in yData,S,H and set path for Predict before running
XRadius=12.4; % x range
YRadius=11.4; % y range
[xX,yY] = meshgrid(-XRadius:0.2:XRadius,-YRadius:0.2:YRadius);
xData = zeros(horzcat(size(xX),2));
xData(:,:,1) = xX; % x-coordinate of the Gaussian grid
xData(:,:,2) = yY; % y-coordinate of the Gaussian grid
for times = 1:30
InitGuess = [1.0000 -4.2000 -11.4000 0.2000
1.0000 -4.6000 -11.4000 0.2000]; % Initial searching points
options = optimoptions(@lsqcurvefit, 'MaxFunEvals', 250000,'MaxIter', 15000,'TolX',0.01);
PHandle = @(PInit,xData) Predict(PInit,xData,S,H);
Problem = createOptimProblem ('lsqcurvefit','x0', InitGuess(1,:),'objective',PHandle,... % Set up the PROBLEM structure
'lb',[-Inf,-XRadius,-YRadius,0.2],'ub',[Inf,XRadius,YRadius,5],'xdata',xData,'ydata',yData,'options',options);
tpoints = CustomStartPointSet(InitGuess);
ms = MultiStart('UseParallel', true); % Use parallel processing
[Para,RSS]= run(ms,Problem,size(InitGuess,1));
disp([Para,RSS]); % Display results
end
As mentioned, I got two different answers:
Running the local solvers in parallel.
Run Local Local Local Local First-order
Index exitflag f(x) # iter F-count optimality
1 2 9.751 3 20 0.007618
2 2 9.751 20 105 7.013e-05
MultiStart completed the runs from all start points.
All 2 local solver runs converged with a positive local solver exit flag. 0.5601 -3.9837 -11.4000 0.2000 9.7510
Running the local solvers in parallel.
Run Local Local Local Local First-order
Index exitflag f(x) # iter F-count optimality
1 2 9.751 3 20 0.007618
2 2 5.545 18 95 0.001615
MultiStart completed the runs from all start points.
All 2 local solver runs converged with a positive local solver exit flag. 0.7281 -4.0394 1.4072 0.2116 5.5448

Accepted Answer

Alan Weiss
Alan Weiss on 10 Jun 2015
Thank you for providing complete data so that diagnosing your problem was easy.
You never passed in your custom start point set tpoints. You only passed in its size, 2, so MultiStart ran two independent randomly-started runs for each of your 30 requested calculations.
Instead, you should call it like this:
[Para,RSS]= run(ms,Problem,tpoints);
Alan Weiss
MATLAB mathematical toolbox documentation
  2 Comments
Ce Mo
Ce Mo on 11 Jun 2015
Thnak you so much for your response Alan. That seems to be exactly the problem. Now my problem is that the answer it gives is the worse one (larger RSS). Any chance you could perhaps give me some advice?
Alan Weiss
Alan Weiss on 11 Jun 2015
I do not know why you asked a parallel computation to compute exactly the same thing 30 times. Each computation did exactly the same thing, so gave no extra information.
Perhaps you were just trying to see how to get reproducible results or something. In that case, see Reproduce Results. The bottom of the page discusses reproducing results when computing in parallel.
In short, I suggest that you do not use a custom start point set, just use MultiStart, possibly with tighter-than-default bounds (perhaps use a RandomStartPointSet with nondefault ArtificialBound). Or, if you really want to use a custom start point, pick one that gives a better result.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!