]> Creatis software - CreaPhase.git/blob - octave_packages/optim-1.2.0/test_min_3.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / optim-1.2.0 / test_min_3.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 ## ok                     - Test that bfgs works with extra
18 ##                          arguments 
19 ##
20 ## Defines some simple functions and verifies that calling
21 ## bfgs on them returns the correct minimum.
22 ##
23 ## Sets 'ok' to 1 if success, 0 otherwise
24
25 if ! exist ("optim_func"), optim_func = "bfgsmin"; end
26
27 ok = 1;
28
29 if ! exist ("verbose"), verbose = 0; end
30
31 P = 2;
32 R = 3;
33
34 ## Make tests reproducible
35 ## obsmat = randn(R,P) ;
36 obsmat = zeros (R,P);
37 obsmat(sub2ind([R,P],1:R,1+rem(0:R-1,P))) = 1:R;
38
39 ## Make test_min_2 repeatable by using fixed starting point
40 ## truep = randn(P,1) ;
41 ## xinit = randn(P,1) ;
42 truep = rem (1:P, P/4)';
43 xinit = truep + 2*(1:P)'/(P);
44
45
46 ## global obses ;
47 obses = obsmat*truep ;
48
49 extra = {obsmat, obses};
50
51
52 function v = ff(x, obsmat, obses)
53   v = mean ( (obses - obsmat*x)(:).^2 ) + 1 ;
54 endfunction
55
56
57 function dv = dff(x, obsmat, obses)
58   er = -obses + obsmat*x ;
59   dv = 2*er'*obsmat / rows(obses) ;
60   ## dv = 2*er'*obsmat ;
61 endfunction
62
63
64
65 if verbose
66   printf ("   Checking that extra arguments are accepted\n\n");
67
68   printf (["     Set 'optim_func' to the name of the optimization\n",\
69            "     function you want to test (must have same synopsis\n",\
70            "     as 'bfgs')\n\n"]);
71
72   printf ("   Tested function : %s\n",optim_func);
73   printf ("   Nparams = P = %i,  Nobses = R = %i\n",P,R);
74   fflush (stdout);
75 end
76 function dt = mytic()
77    persistent last_mytic = 0 ;
78    [t,u,s] = cputime() ;
79    dt = t - last_mytic ;
80    last_mytic = t ;
81 endfunction
82
83 ctl.df = "dff";
84 mytic() ;
85 ## [xlev,vlev,nlev] = feval (optim_func, "ff", "dff", xinit, "extra", extra) ;
86 ## [xlev,vlev,nlev] = feval \
87 ##     (optim_func, "ff", "dff", list (xinit, obsmat, obses));
88 if strcmp(optim_func,"bfgsmin")
89         ctl = {-1,2,1,1};
90 endif
91 [xlev,vlev,nlev] = feval \
92     (optim_func, "ff", {xinit, obsmat, obses}, ctl);
93 tlev = mytic() ;
94
95
96 if max (abs(xlev-truep)) > 1e-4,
97   if verbose, 
98     printf ("Error is too big : %8.3g\n", max (abs (xlev-truep)));
99   end
100   ok = 0;
101 end
102 if verbose,
103   printf ("  Costs :     init=%8.3g, final=%8.3g, best=%8.3g\n",\
104           ff(xinit,obsmat,obses), vlev, ff(truep,obsmat,obses));    
105 end
106 if verbose
107     printf ( "   time : %8.3g\n",tlev);
108 end
109 if verbose && ok
110     printf ( "All tests ok\n");
111 end
112