]> Creatis software - CreaPhase.git/blob - octave_packages/optim-1.2.0/test_min_1.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / optim-1.2.0 / test_min_1.m
1 ## Copyright (C) 2002 Etienne Grossmann <etienne@egdn.net>
2 ## Copyright (C) 2004 Michael Creel <michael.creel@uab.es>
3 ##
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
7 ## version.
8 ##
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
12 ## details.
13 ##
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/>.
16
17 ## Test an optimization function with the same synopsis as bfgs.m 
18
19 if ! exist ("optim_func"), optim_func = "bfgsmin"; end
20
21 ok = 1;
22
23 if ! exist ("verbose"), verbose = 0; end
24
25 if verbose
26   printf ("\n   Testing '%s' on a quadratic programming problem\n\n",\
27           optim_func);
28   printf (["     Set 'optim_func' to the name of the optimization\n",\
29            "     function you want to test (must have same synopsis\n",\
30            "     as 'bfgs')\n\n"]);
31 end
32
33
34
35 N = 1+floor(30*rand(1)) ;
36 global truemin ;
37 truemin = randn(N,1) ;
38
39 global offset ;
40 offset  = 10*randn(1) ;
41
42 global metric ;
43 metric = randn(2*N,N) ; 
44 metric = metric'*metric ;
45
46 if N>1,
47   [u,d,v] = svd(metric);
48   d = (0.1+[0:(1/(N-1)):1]).^2 ;
49   metric = u*diag(d)*u' ;
50 end
51
52 function v = testfunc(x)
53   global offset ;
54   global truemin ;
55   global metric ;
56   v = sum((x-truemin)'*metric*(x-truemin))+offset ;
57 end
58
59 function df = dtestf(x)
60   global truemin ;
61   global metric ;
62   df = 2*(x-truemin)'*metric ;
63 end
64
65 xinit = 10*randn(N,1) ;
66
67 if verbose,
68   printf (["   Dimension is %i\n",\
69            "   Condition is %f\n"],\
70           N, cond (metric));
71   fflush (stdout);
72 end
73
74 ## [x,v,niter] = feval (optim_func, "testfunc","dtestf", xinit);
75 ctl.df = "dtestf";
76 if strcmp(optim_func,"bfgsmin")
77         ctl = {-1,2,1,1};
78         xinit2 = {xinit};
79 else xinit2 = xinit;    
80 endif
81 [x,v,niter] = feval (optim_func, "testfunc", xinit2, ctl);
82
83 if verbose 
84   printf ("nev=%d  N=%d  errx=%8.3g   errv=%8.3g\n",\
85           niter(1),N,max(abs( x-truemin )),v-offset);
86 end
87
88 if any (abs (x-truemin) > 1e-4)
89   ok = 0;
90   if verbose, printf ("not ok 1 (best argument is wrong)\n"); end
91 elseif verbose, printf ("ok 1\n");
92 end
93
94 if  v-offset  > 1e-8
95   ok = 0;
96   if verbose, printf ("not ok 2 (best function value is wrong)\n"); end
97 elseif verbose, printf ("ok 2\n");
98 end
99
100 if verbose
101   if ok, printf ("All tests ok\n");
102   else   printf ("Whoa!! Some test(s) failed\n");
103   end
104 end