outputfcnsanitycheck.m 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. function stop = outputfcnsanitycheck(params,optimValues,state,tol,numsteps)
  2. % function stop = outputfcnsanitycheck(params,optimValues,state,tol,numsteps)
  3. %
  4. % <params> is the current optimization parameter
  5. % <optimValues>,<state> are optimization state stuff
  6. % <tol> (optional) is a positive number. default: 1e-6.
  7. % <numsteps> (optional) is the positive number of iterations in the
  8. % past to compare to. default: 10.
  9. %
  10. % we look back <numsteps> iterations and check whether the resnorm field
  11. % of <optimValues> has changed by less than <tol>. if so, we stop the
  12. % optimization. the only reason for this is to patch up some weird cases
  13. % where the regular optimizer doesn't stop when it should (in these cases,
  14. % the optimizer goes on forever without anything actually changing).
  15. %
  16. % example:
  17. % x = -1:.1:20;
  18. % y = evaldoublegamma([1 1 1 1 2 .2 0 0],x);
  19. % yn = y + 0.1*randn(size(y));
  20. % [params,d,d,exitflag,output] = lsqcurvefit(@(a,b) evaldoublegamma([a 0 0],b),ones(1,6),x,yn,[],[],defaultoptimset({'OutputFcn' @outputfcnplot}));
  21. % input
  22. if ~exist('tol','var') || isempty(tol)
  23. tol = 1e-6;
  24. end
  25. if ~exist('numsteps','var') || isempty(numsteps)
  26. numsteps = 10;
  27. end
  28. % global stuff
  29. global OFSC_RES;
  30. % do it
  31. switch state
  32. case 'init'
  33. OFSC_RES = [];
  34. case {'iter' 'done'}
  35. OFSC_RES = [OFSC_RES optimValues.resnorm];
  36. if length(OFSC_RES) >= numsteps+1
  37. if abs(OFSC_RES(end)-OFSC_RES(end-numsteps)) < tol
  38. stop = 1;
  39. return;
  40. end
  41. end
  42. end
  43. % return
  44. stop = 0;
  45. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  46. % % for sanity checking, we also check whether optimValues.ratio
  47. % % is NaN; if it is, we stop the optimization.
  48. % %
  49. % % if isfield(optimValues,'ratio') && isnan(optimValues.ratio) && optimValues.iteration > 5
  50. % % stop = 1;
  51. % % return;
  52. % % end