changeset 8304:eeaee297c0da

modify optimset & implement optimget
author Jaroslav Hajek <highegg@gmail.com>
date Fri, 31 Oct 2008 08:06:14 +0100
parents b11c31849b44
children 368b504777a8
files scripts/ChangeLog scripts/optimization/Makefile.in scripts/optimization/lsqnonneg.m scripts/optimization/optimget.m scripts/optimization/optimset.m
diffstat 5 files changed, 57 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Fri Oct 31 08:05:32 2008 +0100
+++ b/scripts/ChangeLog	Fri Oct 31 08:06:14 2008 +0100
@@ -1,3 +1,11 @@
+2008-10-31  Jaroslav Hajek  <highegg@gmail.com>
+
+	* 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.
+
 2008-10-31  Jaroslav Hajek <highegg@gmail.com>
 
 	* linear-algebra/__norm__.m: Remove.
--- a/scripts/optimization/Makefile.in	Fri Oct 31 08:05:32 2008 +0100
+++ b/scripts/optimization/Makefile.in	Fri Oct 31 08:06:14 2008 +0100
@@ -38,6 +38,7 @@
   glpkmex.m \
   lsqnonneg.m \
   optimset.m \
+  optimget.m \
   qp.m \
   sqp.m
 
--- a/scripts/optimization/lsqnonneg.m	Fri Oct 31 08:05:32 2008 +0100
+++ b/scripts/optimization/lsqnonneg.m	Fri Oct 31 08:06:14 2008 +0100
@@ -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)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/optimization/optimget.m	Fri Oct 31 08:06:14 2008 +0100
@@ -0,0 +1,39 @@
+## Copyright (C) 2008 Jaroslav Hajek <highegg@gmail.com>
+##
+## 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} {} 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
+
--- a/scripts/optimization/optimset.m	Fri Oct 31 08:05:32 2008 +0100
+++ b/scripts/optimization/optimset.m	Fri Oct 31 08:06:14 2008 +0100
@@ -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