]> Creatis software - CreaPhase.git/blob - octave_packages/optim-1.2.0/test_fminunc_1.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / optim-1.2.0 / test_fminunc_1.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_fminunc_compat_1              - Test that fminunc_compat and optimset_compat work
17 ##
18 ## A quadratic function is fminunc_compatd. Various options are tested. Options
19 ## are passed incomplete (to see if properly completed) and
20 ## case-insensitive.
21
22 ok = 1;                         # Remains set if all ok. Set to 0 otherwise
23 cnt = 0;                        # Test counter
24 more off;
25 page_screen_output (0);
26 page_output_immediately (1);
27
28 if ! exist ("verbose"), verbose = 0; end
29
30 N = 2;
31
32 x0 = randn(N,1) ;
33 y0 = randn(N,1) ;
34
35 ## Return value
36 function v = ff(x,y,t)
37   A = [1 -1;1 1]; M = A'*diag([100,1])*A;
38   v = ((x - y)(1:2))'*M*((x-y)(1:2)) + 1;
39 endfunction
40
41
42 ## Return value, diff and 2nd diff
43 function [v,dv,d2v] = d2ff(x,y,t)
44   if nargin < 3, t = 1; end
45   if t == 1, N = length (x); else N = length (y); end
46   A = [1 -1;1 1]; M = A'*diag([100,1])*A;
47   v = ((x - y)(1:2))'*M*((x-y)(1:2)) + 1;
48   dv = 2*((x-y)(1:2))'*M;
49   d2v = zeros (N); d2v(1:2,1:2) = 2*M;
50   if N>2, dv = [dv, zeros(1,N-2)]; end
51   if t == 2, dv = -dv; end
52 endfunction
53
54
55 ## PRint Now
56 function prn (varargin), printf (varargin{:}); fflush (stdout); end
57
58
59 if verbose
60   prn ("\n   Testing that fminunc_compat() works as it should\n\n");
61   prn ("  Nparams = N = %i\n",N);
62   fflush (stdout);
63 end
64
65 ## Plain run, just to make sure ######################################
66 ## Minimum wrt 'x' is y0
67 opt = optimset_compat ();
68 [xlev,vlev] = fminunc_compat ("ff",x0,opt,y0,1);
69
70 cnt++;
71 if max (abs (xlev-y0)) > 100*sqrt (eps)
72   if verbose
73     prn ("Error is too big : %8.3g\n", max (abs (xlev-y0)));
74   end
75   ok = 0;
76 elseif verbose,  prn ("ok %i\n",cnt);
77 end
78
79 ## See what 'backend' gives in that last case ########################
80 opt = optimset_compat ("backend","on");
81 [method,ctl] = fminunc_compat ("ff",x0, opt, y0,1);
82
83 cnt++;
84 if ! ischar (method) || ! strcmp (method,"nelder_mead_min")
85   if verbose
86     if ischar (method)
87       prn ("Wrong method '%s' != 'nelder_mead_min' was chosen\n", method);
88     else
89       prn ("fminunc_compat pretends to use a method that isn't a string\n");
90     end
91     return
92   end
93   ok = 0;
94 elseif verbose,  prn ("ok %i\n",cnt);
95 end
96
97 [xle2,vle2,nle2] = feval (method, "ff",{x0,y0,1}, ctl);
98 cnt++;
99                                 # nelder_mead_min is not very repeatable
100                                 # because of restarts from random positions
101 if max (abs (xlev-xle2)) > 100*sqrt (eps)
102   if verbose
103     prn ("Error is too big : %8.3g\n", max (abs (xlev-xle2)));
104   end
105   ok = 0;
106 elseif verbose,  prn ("ok %i\n",cnt);
107 end
108
109
110 ## Run, w/ differential returned by function ('jac' option) ##########
111 ## Minimum wrt 'x' is y0
112
113 opt = optimset_compat ("GradO","on");
114 [xlev,vlev,nlev] = fminunc_compat ("d2ff",x0,opt,y0,1);
115
116 cnt++;
117 if max (abs (xlev-y0)) > 100*sqrt (eps)
118   if verbose
119     prn ("Error is too big : %8.3g\n", max (abs (xlev-y0)));
120   end
121   ok = 0;
122 elseif verbose,  prn ("ok %i\n",cnt);
123 end
124
125
126 ## Use the 'hess' option, when f can return 2nd differential #########
127 ## Minimum wrt 'x' is y0
128 opt = optimset_compat ("hessian","on");
129 [xlev,vlev,nlev] = fminunc_compat ("d2ff",x0,opt,y0,1);
130
131 cnt++;
132 if max (abs (xlev-y0)) > 100*sqrt (eps)
133   if verbose
134     prn ("Error is too big : %8.3g\n", max (abs (xlev-y0)));
135   end
136   ok = 0;
137 elseif verbose,  prn ("ok %i\n",cnt);
138 end
139
140
141 if verbose && ok
142   prn ( "All tests ok\n");
143 end
144