Mercurial > hg > octave-lyh
annotate scripts/optimization/optimset.m @ 10635:d1978e7364ad
Print name of function in error() string messages.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 16 May 2010 22:26:54 -0700 |
parents | eb63fbe60fab |
children | be55736a0783 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2007, 2008 John W. Eaton |
8647
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
2 ## Copyright (C) 2009 VZLU Prague |
6226 | 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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
6226 | 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 | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
6226 | 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. | |
8647
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
33 opts = __all_opts__ (); |
6226 | 34 |
35 if (nargs == 0) | |
36 if (nargout == 0) | |
37 ## Display possibilities. | |
8647
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
38 puts ("\nAll possible optimization options:\n\n"); |
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
39 printf (" %s\n", opts{:}); |
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
40 puts ("\n"); |
6226 | 41 else |
8304
eeaee297c0da
modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
7016
diff
changeset
|
42 ## Return empty structure. |
eeaee297c0da
modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
7016
diff
changeset
|
43 ## We're incompatible with Matlab at this point. |
eeaee297c0da
modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
7016
diff
changeset
|
44 retval = struct (); |
6226 | 45 endif |
46 elseif (nargs == 1 && ischar (varargin{1})) | |
47 ## Return defaults for named function. | |
48 fcn = varargin{1}; | |
8596
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8304
diff
changeset
|
49 try |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8304
diff
changeset
|
50 retval = feval (fcn, 'defaults'); |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8304
diff
changeset
|
51 catch |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
52 error ("optimset: no defaults for function `%s'", fcn); |
8598 | 53 end_try_catch |
6226 | 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}; | |
6227 | 59 fnames = fieldnames (old); |
8647
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
60 ## skip validation if we're in the internal query |
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
61 validation = ! isempty (opts); |
6226 | 62 for [val, key] = new |
8647
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
63 if (validation) |
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
64 ## Case insensitive lookup in all options. |
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
65 i = lookup (opts, key, "i"); |
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
66 ## Validate option. |
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
67 if (i > 0 && strcmpi (opts{i}, key)) |
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
68 ## Use correct case. |
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
69 key = opts{i}; |
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
70 else |
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
71 warning ("unrecognized option: %s", key); |
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
72 endif |
6226 | 73 endif |
6227 | 74 old.(key) = val; |
6226 | 75 endfor |
76 retval = old; | |
77 elseif (rem (nargs, 2) && isstruct (varargin{1})) | |
78 ## Set values in old from name/value pairs. | |
79 retval = optimset (varargin{1}, struct (varargin{2:end})); | |
80 elseif (rem (nargs, 2) == 0) | |
81 ## Create struct. Default values are replaced by those specified by | |
82 ## name/value pairs. | |
8304
eeaee297c0da
modify optimset & implement optimget
Jaroslav Hajek <highegg@gmail.com>
parents:
7016
diff
changeset
|
83 retval = optimset (struct (), struct (varargin{:})); |
6226 | 84 else |
85 print_usage (); | |
86 endif | |
87 | |
88 endfunction | |
8647
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
89 |
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
90 %!assert (optimget (optimset ('tolx', 1e-2), 'tOLx'), 1e-2) |
06f5dd901f30
implement registering of optimization options
Jaroslav Hajek <highegg@gmail.com>
parents:
8598
diff
changeset
|
91 %!assert (isfield (optimset ('tolFun', 1e-3), 'TolFun')) |