1 ## Copyright (C) 2002 Etienne Grossmann <etienne@egdn.net>
2 ## Copyright (C) 2004 Michael Creel <michael.creel@uab.es>
4 ## This program is free software; you can redistribute it and/or modify it under
5 ## the terms of the GNU General Public License as published by the Free Software
6 ## Foundation; either version 3 of the License, or (at your option) any later
9 ## This program is distributed in the hope that it will be useful, but WITHOUT
10 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 ## You should have received a copy of the GNU General Public License along with
15 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## Test an optimization function with the same synopsis as bfgs.m
19 if ! exist ("optim_func"), optim_func = "bfgsmin"; end
23 if ! exist ("verbose"), verbose = 0; end
26 printf ("\n Testing '%s' on a quadratic programming problem\n\n",\
28 printf ([" Set 'optim_func' to the name of the optimization\n",\
29 " function you want to test (must have same synopsis\n",\
35 N = 1+floor(30*rand(1)) ;
37 truemin = randn(N,1) ;
40 offset = 10*randn(1) ;
43 metric = randn(2*N,N) ;
44 metric = metric'*metric ;
47 [u,d,v] = svd(metric);
48 d = (0.1+[0:(1/(N-1)):1]).^2 ;
49 metric = u*diag(d)*u' ;
52 function v = testfunc(x)
56 v = sum((x-truemin)'*metric*(x-truemin))+offset ;
59 function df = dtestf(x)
62 df = 2*(x-truemin)'*metric ;
65 xinit = 10*randn(N,1) ;
68 printf ([" Dimension is %i\n",\
69 " Condition is %f\n"],\
74 ## [x,v,niter] = feval (optim_func, "testfunc","dtestf", xinit);
76 if strcmp(optim_func,"bfgsmin")
81 [x,v,niter] = feval (optim_func, "testfunc", xinit2, ctl);
84 printf ("nev=%d N=%d errx=%8.3g errv=%8.3g\n",\
85 niter(1),N,max(abs( x-truemin )),v-offset);
88 if any (abs (x-truemin) > 1e-4)
90 if verbose, printf ("not ok 1 (best argument is wrong)\n"); end
91 elseif verbose, printf ("ok 1\n");
96 if verbose, printf ("not ok 2 (best function value is wrong)\n"); end
97 elseif verbose, printf ("ok 2\n");
101 if ok, printf ("All tests ok\n");
102 else printf ("Whoa!! Some test(s) failed\n");