File: optimset.m

package info (click to toggle)
octave 3.8.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 84,396 kB
  • ctags: 45,547
  • sloc: cpp: 293,356; ansic: 42,041; fortran: 23,669; sh: 13,629; objc: 7,890; yacc: 7,093; lex: 3,442; java: 2,125; makefile: 1,589; perl: 1,009; awk: 974; xml: 34
file content (175 lines) | stat: -rw-r--r-- 5,923 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
## Copyright (C) 2007-2013 John W. Eaton
## Copyright (C) 2009 VZLU Prague
##
## This file is part of Octave.
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or (at
## your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn  {Function File} {} optimset ()
## @deftypefnx {Function File} {} optimset (@var{par}, @var{val}, @dots{})
## @deftypefnx {Function File} {} optimset (@var{old}, @var{par}, @var{val}, @dots{})
## @deftypefnx {Function File} {} optimset (@var{old}, @var{new})
## Create options struct for optimization functions.
##
## Valid parameters are:
##
## @table @asis
## @item AutoScaling
##
## @item ComplexEqn
##
## @item Display
## Request verbose display of results from optimizations.  Values are:
##
## @table @asis
## @item @qcode{"off"} [default]
## No display.
##
## @item @qcode{"iter"}
## Display intermediate results for every loop iteration.
##
## @item @qcode{"final"}
## Display the result of the final loop iteration.
##
## @item @qcode{"notify"}
## Display the result of the final loop iteration if the function has
## failed to converge.
## @end table
##
## @item FinDiffType
##
## @item FunValCheck
## When enabled, display an error if the objective function returns an invalid
## value (a complex number, NaN, or Inf).  Must be set to @qcode{"on"} or
## @qcode{"off"} [default].  Note: the functions @code{fzero} and
## @code{fminbnd} correctly handle Inf values and only complex values or NaN
## will cause an error in this case. 
##
## @item GradObj
## When set to @qcode{"on"}, the function to be minimized must return a
## second argument which is the gradient, or first derivative, of the
## function at the point @var{x}.  If set to @qcode{"off"} [default], the
## gradient is computed via finite differences.
##
## @item Jacobian
## When set to @qcode{"on"}, the function to be minimized must return a
## second argument which is the Jacobian, or first derivative, of the
## function at the point @var{x}.  If set to @qcode{"off"} [default], the
## Jacobian is computed via finite differences.
##
## @item MaxFunEvals
## Maximum number of function evaluations before optimization stops.
## Must be a positive integer.
##
## @item MaxIter
## Maximum number of algorithm iterations before optimization stops.
## Must be a positive integer.
##
## @item OutputFcn
## A user-defined function executed once per algorithm iteration.
##
## @item TolFun
## Termination criterion for the function output.  If the difference in the
## calculated objective function between one algorithm iteration and the next
## is less than @code{TolFun} the optimization stops.  Must be a positive
## scalar.
##
## @item TolX
## Termination criterion for the function input.  If the difference in @var{x},
## the current search point, between one algorithm iteration and the next is
## less than @code{TolX} the optimization stops.  Must be a positive scalar.
##
## @item TypicalX
##
## @item Updating
## @end table
## @end deftypefn

function retval = optimset (varargin)

  nargs = nargin ();

  ## Add more as needed.
  opts = __all_opts__ ();

  if (nargs == 0)
    if (nargout == 0)
      ## Display possibilities.
      puts ("\nAll possible optimization options:\n\n");
      printf ("  %s\n", opts{:});
      puts ("\n");
    else
      ## Return struct with all options initialized to []
      retval = cell2struct (repmat ({[]}, size (opts)), opts, 2);
    endif
  elseif (nargs == 1 && ischar (varargin{1}))
    ## Return defaults for named function.
    fcn = varargin{1};
    try
      retval = feval (fcn, "defaults");
    catch
      error ("optimset: no defaults for function '%s'", fcn);
    end_try_catch
  elseif (nargs == 2 && isstruct (varargin{1}) && isstruct (varargin{2}))
    ## Set slots in old from nonempties in new.  Should we be checking
    ## to ensure that the field names are expected?
    old = varargin{1};
    new = varargin{2};
    fnames = fieldnames (old);
    ## skip validation if we're in the internal query
    validation = ! isempty (opts);
    for [val, key] = new
      if (validation)
        ## Case insensitive lookup in all options.
        i = strncmpi (opts, key, length (key));
        nmatch = sum (i);
        ## Validate option.
        if (nmatch == 1)
          key = opts{find (i)};
        elseif (nmatch == 0)
          warning ("unrecognized option: %s", key);
        else
          fmt = sprintf ("ambiguous option: %%s (%s%%s)",
                         repmat ("%s, ", 1, nmatch-1));
          warning (fmt, key, opts{i});
        endif
      endif
      old.(key) = val;
    endfor
    retval = old;
  elseif (rem (nargs, 2) && isstruct (varargin{1}))
    ## Set values in old from name/value pairs.
    pairs = reshape (varargin(2:end), 2, []);
    retval = optimset (varargin{1}, cell2struct (pairs(2, :), pairs(1, :), 2));
  elseif (rem (nargs, 2) == 0)
    ## Create struct.  Default values are replaced by those specified by
    ## name/value pairs.
    pairs = reshape (varargin, 2, []);
    retval = optimset (struct (), cell2struct (pairs(2, :), pairs(1, :), 2));
  else
    print_usage ();
  endif

endfunction


%!assert (optimget (optimset ("tolx", 1e-2), "tOLx"), 1e-2)
%!assert (isfield (optimset ("tolFun", 1e-3), "TolFun"))
%!warning (optimset ("Max", 10));
%!warning (optimset ("foobar", 13));

%!error (optimset ("%NOT_A_REAL_FUNCTION_NAME%"))