eharley0142

dogleg__.m

May 10th, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.46 KB | None | 0 0
  1. % Copyright (C) 2008, 2009 Jaroslav Hajek
  2. %
  3. % This file is part of Octave.
  4. %
  5. % Octave is free software; you can redistribute it and/or modify it
  6. % under the terms of the GNU General Public License as published by
  7. % the Free Software Foundation; either version 3 of the License, or (at
  8. % your option) any later version.
  9. %
  10. % Octave is distributed in the hope that it will be useful, but
  11. % WITHOUT ANY WARRANTY; without even the implied warranty of
  12. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. % General Public License for more details.
  14. %
  15. % You should have received a copy of the GNU General Public License
  16. % along with Octave; see the file COPYING.  If not, see
  17. % <http://www.gnu.org/licenses/>.
  18.  
  19. % -*- texinfo -*-
  20. % @deftypefn{Function File} {@var{x}} = __dogleg__ (@var{r}, @var{b}, @var{x}, @var{d}, @var{delta}, @var{ismin})
  21. % Solve the double dogleg trust-region problem:
  22. % Minimize
  23. % @example
  24. % norm(@var{r}*@var{x}-@var{b})
  25. % @end example
  26. % subject to the constraint
  27. % @example
  28. % norm(@var{d}.*@var{x}) <= @var{delta} ,
  29. % @end example
  30. % x being a convex combination of the gauss-newton and scaled gradient.
  31. % If @var{ismin} is true (default false), minimizes instead
  32. % @example
  33. % norm(@var{r}*@var{x})^2-2*@var{b}'*@var{x}
  34. % @end example
  35. % @end deftypefn
  36.  
  37.  
  38. % TODO: error checks
  39. % TODO: handle singularity, or leave it up to mldivide?
  40.  
  41. % function x = dogleg__ (r, b, d, delta, ismin = false
  42. function x = dogleg__ (r, b, d, delta, ismin)
  43.     % Get Gauss-Newton direction.
  44. if (ismin)
  45.     g = b;
  46.     b = r' \ g;
  47. end
  48. x = r \ b;
  49. xn = norm (d .* x);
  50. if (xn > delta)
  51.     % GN is too big, get scaled gradient.
  52.     if (ismin)
  53.         s = g ./ d;
  54.     else
  55.         s = (r' * b) ./ d;
  56.     end
  57.     sn = norm (s);
  58.     if (sn > 0)
  59.         % Normalize and rescale.
  60.         s = (s / sn) ./ d;
  61.         % Get the line minimizer in s direction.
  62.         tn = norm (r*s);
  63.         snm = (sn / tn) / tn;
  64.         if (snm < delta)
  65.             % Get the dogleg path minimizer.
  66.             bn = norm (b);
  67.             dxn = delta/xn; snmd = snm/delta;
  68.             t = (bn/sn) * (bn/xn) * snmd;
  69.             t = t - ( dxn * snmd^2 - sqrt ((t-dxn)^2 + (1-dxn^2)*(1-snmd^2)) );
  70.             alpha = dxn*(1-snmd^2) / t;
  71.         else
  72.             alpha = 0;
  73.         end
  74.     else
  75.         alpha = delta / xn;
  76.         snm = 0;
  77.     end
  78.     % Form the appropriate convex combination.
  79.     x = alpha * x + ((1-alpha) * min (snm, delta)) * s;
  80. end
  81.  
  82. end
Advertisement
Add Comment
Please, Sign In to add comment