]> Creatis software - CreaPhase.git/blob - octave_packages/optim-1.2.0/test_min_4.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / optim-1.2.0 / test_min_4.m
1 ## Copyright (C) 2002 Etienne Grossmann <etienne@egdn.net>
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 ## test_bfgs              - Test that bfgs works
17 ##
18 ## Check that bfgs treats struct options correctly
19 ##
20 ## Sets 'ok' to 1 if success, 0 otherwise
21
22 ## The name of the optimizing function
23 if ! exist ("optim_func"), optim_func = "bfgsmin"; end
24
25 ok = 1;
26 cnt = 0;
27
28 if ! exist ("verbose"), verbose = 0; end
29
30 N = 2;
31
32 ## Make test reproducible
33 ## x0 = randn(N,1) ;
34 ## y0 = randn(N,1) ;
35 x0 = (1:N)'/N;
36 y0 = (N:-1:1)'/N;
37
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;
41 endfunction
42
43
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
51 endfunction
52
53
54 if verbose
55   printf ("\n   Testing that %s accepts struct control variable\n\n",\
56           optim_func);
57
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"]);
61
62   printf ("  Nparams = N = %i\n",N);
63   fflush (stdout);
64 end
65
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});
69 ## ctl.df = "dff";
70 [xlev,vlev,nlev] = feval (optim_func, "ff", {x0,y0,1});
71
72 cnt++;
73 if max (abs (xlev-y0)) > 100*sqrt (eps)
74   if verbose
75     printf ("Error is too big : %8.3g\n", max (abs (xlev-y0)));
76   end
77   ok = 0;
78 elseif verbose,  printf ("ok %i\n",cnt);
79 end
80
81 ## Minimize wrt 2nd arg ##############################################
82 ## Minimum wrt 'y' is x0
83 ## ctl = struct ("narg", 2,"df","dff");
84 ## ctl = [nan,nan,2];
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});
87
88 cnt++;
89 if max (abs (xlev-x0)) > 100*sqrt (eps)
90   if verbose
91     printf ("Error is too big : %8.3g\n", max (abs (xlev-x0)));
92   end
93   ok = 0;
94 elseif verbose,  printf ("ok %i\n",cnt);
95 end
96
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});
103
104 cnt++;
105 if max (abs (xlev-y0)) > 100*sqrt (eps)
106   if verbose
107     printf ("Error is too big : %8.3g\n", max (abs (xlev-y0)));
108   end
109   ok = 0;
110 elseif verbose,  printf ("ok %i\n",cnt);
111 end
112
113
114
115
116 if verbose && ok
117   printf ( "All tests ok\n");
118 end
119