eharley0142

optimset.m

May 10th, 2012
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.20 KB | None | 0 0
  1. % Copyright (C) 2007, 2008 John W. Eaton
  2. % Copyright (C) 2009 VZLU Prague
  3. %
  4. % This file is part of Octave.
  5. %
  6. % Octave is free software; you can redistribute it and/or modify it
  7. % under the terms of the GNU General Public License as published by
  8. % the Free Software Foundation; either version 3 of the License, or (at
  9. % your option) any later version.
  10. %
  11. % Octave is distributed in the hope that it will be useful, but
  12. % WITHOUT ANY WARRANTY; without even the implied warranty of
  13. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14. % General Public License for more details.
  15. %
  16. % You should have received a copy of the GNU General Public License
  17. % along with Octave; see the file COPYING.  If not, see
  18. % <http://www.gnu.org/licenses/>.
  19.  
  20. % -*- texinfo -*-
  21. % @deftypefn {Function File} {} optimset ()
  22. % @deftypefnx {Function File} {} optimset (@var{par}, @var{val}, @dots{})
  23. % @deftypefnx {Function File} {} optimset (@var{old}, @var{par}, @var{val}, @dots{})
  24. % @deftypefnx {Function File} {} optimset (@var{old}, @var{new})
  25. % Create options struct for optimization functions.
  26. % @end deftypefn
  27.  
  28. function retval = optimset (varargin)
  29.  
  30. nargs = nargin ();
  31.  
  32. % Add more as needed.
  33. opts = all_opts__ ();
  34.  
  35. if (nargs == 0)
  36.     if (nargout == 0)
  37.         % Display possibilities.
  38.         puts ('\nAll possible optimization options:\n\n');
  39.         printf ('  %s\n', opts{:});
  40.         puts ('\n');
  41.     else
  42.         % Return empty structure.
  43.         % We're incompatible with Matlab at this point.
  44.         retval = struct ();
  45.     end
  46. elseif (nargs == 1 && ischar (varargin{1}))
  47.     % Return defaults for named function.
  48.     fcn = varargin{1};
  49.     try
  50.         retval = feval (fcn, 'defaults');
  51.     catch
  52.         error ('no defaults for function "%s"', fcn);
  53.     end
  54. elseif (nargs == 2 && isstruct (varargin{1}) && isstruct (varargin{2}))
  55.     % Set slots in old from nonempties in new.  Should we be checking
  56.     % to ensure that the field names are expected?
  57.     old = varargin{1};
  58.     new = varargin{2};
  59.     fnames = fieldnames (old);
  60.     % skip validation if we're in the internal query
  61.     validation = ~ isempty (opts);
  62.     %    for [val, key] = new
  63.     fn = fieldnames(new);
  64.     for ii = 1:length(fn)    %% FIXME
  65.         key = fn{ii};
  66.         val = new.(fn{ii});
  67.         if (validation)
  68.             % Case insensitive lookup in all options.
  69.             i = lookup (opts, key, 'i');
  70.             % Validate option.
  71.             if (i > 0 && strcmpi (opts{i}, key))
  72.                 % Use correct case.
  73.                 key = opts{i};
  74.             else
  75.                 warning ('unrecognized option: %s', key);
  76.             end
  77.         end
  78.         old.(key) = val;
  79.     end
  80.     retval = old;
  81. elseif (rem (nargs, 2) && isstruct (varargin{1}))
  82.     % Set values in old from name/value pairs.
  83.     retval = optimset (varargin{1}, struct (varargin{2:end}));
  84. elseif (rem (nargs, 2) == 0)
  85.     % Create struct.  Default values are replaced by those specified by
  86.     % name/value pairs.
  87.     retval = optimset (struct (), struct (varargin{:}));
  88. else
  89.     print_usage ();
  90. end
  91.  
  92. end
  93.  
  94. %!assert (optimget (optimset ('tolx', 1e-2), 'tOLx'), 1e-2)
  95. %!assert (isfield (optimset ('tolFun', 1e-3), 'TolFun'))
Advertisement
Add Comment
Please, Sign In to add comment