1 ## Copyright (C) 2002 Etienne Grossmann <etienne@egdn.net>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
16 ## test_bfgs - Test that bfgs works
18 ## Check that bfgs treats struct options correctly
20 ## Sets 'ok' to 1 if success, 0 otherwise
22 ## The name of the optimizing function
23 if ! exist ("optim_func"), optim_func = "bfgsmin"; end
28 if ! exist ("verbose"), verbose = 0; end
32 ## Make test reproducible
38 function v = ff(x,y,t)
39 A = [1 -1;1 1]; M = A'*diag([100,1])*A;
40 v = (x(1:2) - y(1:2))'*M*(x(1:2)-y(1:2)) + 1;
44 function dv = dff(x,y,t)
45 if nargin < 3, t = 1; end
46 if t == 1, N = length (x); else N = length (y); end
47 A = [1 -1;1 1]; M = A'*diag([100,1])*A;
48 dv = 2*(x(1:2)-y(1:2))'*M;
49 if N>2, dv = [dv, zeros(1,N-2)]; end
50 if t == 2, dv = -dv; end
55 printf ("\n Testing that %s accepts struct control variable\n\n",\
58 printf ([" Set 'optim_func' to the name of the optimization\n",\
59 " function you want to test (must have same synopsis\n",\
60 " as 'bfgsmin')\n\n"]);
62 printf (" Nparams = N = %i\n",N);
66 ## Plain run, just to make sure ######################################
67 ## Minimum wrt 'x' is y0
68 ## [xlev,vlev,nlev] = feval (optim_func, "ff", "dff", {x0,y0,1});
70 [xlev,vlev,nlev] = feval (optim_func, "ff", {x0,y0,1});
73 if max (abs (xlev-y0)) > 100*sqrt (eps)
75 printf ("Error is too big : %8.3g\n", max (abs (xlev-y0)));
78 elseif verbose, printf ("ok %i\n",cnt);
81 ## Minimize wrt 2nd arg ##############################################
82 ## Minimum wrt 'y' is x0
83 ## ctl = struct ("narg", 2,"df","dff");
85 ## [xlev,vlev,nlev] = feval (optim_func, "ff", list (x0,y0,2),ctl);
86 [xlev,vlev,nlev] = feval (optim_func, "ff", {x0,y0,2},{inf,0,1,2});
89 if max (abs (xlev-x0)) > 100*sqrt (eps)
91 printf ("Error is too big : %8.3g\n", max (abs (xlev-x0)));
94 elseif verbose, printf ("ok %i\n",cnt);
97 ## Set the verbose option ############################################
98 ## Minimum wrt 'x' is y0
99 ## ctl = struct ("narg", 1,"verbose",verbose, "df", "dff");
100 ## ctl = [nan,nan,2];
101 ## [xlev,vlev,nlev] = feval (optim_func, "ff", "dff", {x0,y0,1},ctl);
102 [xlev,vlev,nlev] = feval (optim_func, "ff", {x0,y0,1},{inf,1,1,1});
105 if max (abs (xlev-y0)) > 100*sqrt (eps)
107 printf ("Error is too big : %8.3g\n", max (abs (xlev-y0)));
110 elseif verbose, printf ("ok %i\n",cnt);
117 printf ( "All tests ok\n");