# HG changeset patch # User Jaroslav Hajek # Date 1225436774 -3600 # Node ID ae6cb5e2d988810b062d65a68d6055e5b3054501 # Parent e81bb4c503b52790915fd70c8427e929759d0474 modify optimset & implement optimget diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -0,0 +1,8 @@ +2008-10-31 Jaroslav Hajek + + * optimization/optimset.m: Don't include empty options in option + structure. + * optimization/optimget.m: New function file. + * optimization/Makefile.in: Add it. + * optimization/lsqnonneg.m: Query options using optimget. + diff --git a/scripts/optimization/Makefile.in b/scripts/optimization/Makefile.in --- a/scripts/optimization/Makefile.in +++ b/scripts/optimization/Makefile.in @@ -38,6 +38,7 @@ glpkmex.m \ lsqnonneg.m \ optimset.m \ + optimget.m \ qp.m \ sqp.m diff --git a/scripts/optimization/lsqnonneg.m b/scripts/optimization/lsqnonneg.m --- a/scripts/optimization/lsqnonneg.m +++ b/scripts/optimization/lsqnonneg.m @@ -67,10 +67,7 @@ x = zeros (columns (c), 1); endif - if (isempty (options)) - ## FIXME: what are the correct defaults? - options = optimset ("maxiter", 1e5, "tolx", 1e-8); - endif + MaxIter = optimget (options, "MaxIter", 1e5); ## Initialize the values. p = []; @@ -80,7 +77,7 @@ iter = 0; ## LH3: test for completion. - while (! isempty (z) && any (w(z) > 0) && iter < options.MaxIter) + while (! isempty (z) && any (w(z) > 0) && iter < MaxIter) ## LH4: find the maximum gradient. idx = find (w == max (w)); if (numel (idx) > 1) @@ -93,7 +90,7 @@ p(end+1) = idx; newx = false; - while (! newx && iter < options.MaxIter) + while (! newx && iter < MaxIter) iter++; ## LH6: compute the positive matrix and find the min norm solution @@ -132,7 +129,7 @@ residual = d-C*x; endif exitflag = iter; - if (nargout > 3 && iter >= options.MaxIter) + if (nargout > 3 && iter >= MaxIter) exitflag = 0; endif if (nargout > 4) diff --git a/scripts/optimization/optimget.m b/scripts/optimization/optimget.m new file mode 100644 --- /dev/null +++ b/scripts/optimization/optimget.m @@ -0,0 +1,39 @@ +## Copyright (C) 2008 Jaroslav Hajek +## +## 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 +## . + +## -*- texinfo -*- +## @deftypefn {Function File} {} optimget (@var{options}, @var{parname}) +## @deftypefn {Function File} {} optimget (@var{options}, @var{parname}, @var{default}) +## Used to retrieve a specific option from a structure created by +## @code{optimset}. If @var{parname} is not a field of the @var{options} +## structure, return @var{default} if supplied, otherwise return an +## empty matrix. +## @end deftypefn + +function retval = optimget (options, parname, default) + + if (isfield (options, parname)) + retval = options.(parname); + elseif (nargin > 2) + retval = default; + else + retval = []; + endif + +endfunction + diff --git a/scripts/optimization/optimset.m b/scripts/optimization/optimset.m --- a/scripts/optimization/optimset.m +++ b/scripts/optimization/optimset.m @@ -45,18 +45,16 @@ tmp = opts'; disp (struct (tmp{:})); else - ## Return structure with empty values. - t1 = opts(:,1)'; - t2 = cell (size (t1)); - tmp = [t1; t2]; - retval = struct (tmp{:}); + ## Return empty structure. + ## We're incompatible with Matlab at this point. + retval = struct (); endif elseif (nargs == 1 && ischar (varargin{1})) ## Return defaults for named function. fcn = varargin{1}; optfcn = sprintf ("__%s_defopts__", fcn); if (exist (optfcn)) - retval = optimset (optimset (), feval (optfcn)); + retval = optimset (struct (), feval (optfcn)); else error ("no defaults for function `%s'", fcn); endif @@ -80,7 +78,7 @@ elseif (rem (nargs, 2) == 0) ## Create struct. Default values are replaced by those specified by ## name/value pairs. - retval = optimset (optimset (), struct (varargin{:})); + retval = optimset (struct (), struct (varargin{:})); else print_usage (); endif