Mercurial > forge
annotate main/optim/inst/test_min_2.m @ 9930:d30cfca46e8a octave-forge
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
author | carandraug |
---|---|
date | Fri, 30 Mar 2012 15:14:48 +0000 |
parents | 97f77d4bd426 |
children | 28b882879c6e |
rev | line source |
---|---|
9930
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
9389
diff
changeset
|
1 ## Copyright (C) 2002 Etienne Grossmann <etienne@cs.uky.edu> |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
9389
diff
changeset
|
2 ## Copyright (C) 2004 Michael Creel <michael.creel@uab.es> |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
9389
diff
changeset
|
3 ## |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
9389
diff
changeset
|
4 ## This program is free software; you can redistribute it and/or modify it under |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
9389
diff
changeset
|
5 ## the terms of the GNU General Public License as published by the Free Software |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
9389
diff
changeset
|
6 ## Foundation; either version 3 of the License, or (at your option) any later |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
9389
diff
changeset
|
7 ## version. |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
8 ## |
9930
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
9389
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, but WITHOUT |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
9389
diff
changeset
|
10 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
9389
diff
changeset
|
11 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
9389
diff
changeset
|
12 ## details. |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
13 ## |
9930
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
9389
diff
changeset
|
14 ## You should have received a copy of the GNU General Public License along with |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
9389
diff
changeset
|
15 ## this program; if not, see <http://www.gnu.org/licenses/>. |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
16 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
17 ## test_min_2 - Test that bfgs works |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
18 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
19 ## Defines some simple functions and verifies that calling |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
20 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
21 ## bfgs on them returns the correct minimum. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
22 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
23 ## Sets 'ok' to 1 if success, 0 otherwise |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
24 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
25 if ! exist ("optim_func"), optim_func = "bfgsmin"; end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
26 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
27 ok = 1; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
28 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
29 if ! exist ("verbose"), verbose = 0; end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
30 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
31 P = 15; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
32 R = 20; # must have R >= P |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
33 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
34 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
35 global obsmat ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
36 ## Make test_min_2 reproducible by using fixed obsmat |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
37 ## obsmat = randn(R,P) ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
38 obsmat = zeros (R,P); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
39 obsmat(sub2ind([R,P],1:R,1+rem(0:R-1,P))) = 1:R; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
40 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
41 global truep ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
42 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
43 ## Make test_min_2 reproducible by using fixed starting point |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
44 ## truep = randn(P,1) ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
45 ## xinit = randn(P,1) ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
46 truep = rem (1:P, P/4)'; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
47 xinit = truep + 2*(1:P)'/(P); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
48 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
49 global obses ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
50 obses = obsmat*truep ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
51 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
52 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
53 function v = ff(x) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
54 global obsmat; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
55 global obses; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
56 v = mean ((obses - obsmat*x).^2) + 1 ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
57 endfunction |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
58 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
59 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
60 function dv = dff(x) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
61 global obsmat; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
62 global obses; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
63 er = -obses + obsmat*x ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
64 dv = 2*er'*obsmat / rows(obses) ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
65 ## dv = 2*er'*obsmat ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
66 endfunction |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
67 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
68 ## dt = mytic() |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
69 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
70 ## Returns the cputime since last call to 'mytic'. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
71 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
72 function dt = mytic() |
9389
97f77d4bd426
replacing instances of `static' by `persistent' since it has been deprecated
carandraug
parents:
2370
diff
changeset
|
73 persistent last_mytic = 0 ; |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
74 [t,u,s] = cputime() ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
75 dt = t - last_mytic ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
76 last_mytic = t ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
77 endfunction |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
78 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
79 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
80 if verbose |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
81 printf ("\n Testing %s on a quadratic problem\n\n", optim_func); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
82 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
83 printf ([" Set 'optim_func' to the name of the optimization\n",\ |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
84 " function you want to test (must have same synopsis\n",\ |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
85 " as 'bfgs')\n\n"]); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
86 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
87 printf (" Nparams = P = %i, Nobses = R = %i\n",P,R); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
88 fflush (stdout); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
89 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
90 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
91 ctl.df = "dff"; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
92 ctl.ftol = eps; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
93 ctl.dtol = 1e-7; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
94 mytic() ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
95 if strcmp(optim_func,"bfgsmin") |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
96 ctl = {-1,2,1,1}; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
97 xinit2 = {xinit}; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
98 else xinit2 = xinit; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
99 endif |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
100 ## [xlev,vlev,nlev] = feval(optim_func, "ff", "dff", xinit) ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
101 [xlev,vlev,nlev] = feval(optim_func, "ff", xinit2, ctl) ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
102 tlev = mytic() ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
103 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
104 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
105 if max (abs(xlev-truep)) > 1e-4, |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
106 if verbose |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
107 printf ("Error is too big : %8.3g\n", max (abs (xlev-truep))); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
108 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
109 ok = 0; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
110 elseif verbose, printf ("ok 1\n"); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
111 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
112 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
113 if verbose, |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
114 printf (" Costs : init=%8.3g, final=%8.3g, best=%8.3g\n",\ |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
115 ff(xinit), vlev, ff(truep)); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
116 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
117 if verbose |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
118 printf ( " time : %8.3g\n",tlev); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
119 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
120 if verbose && ok |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
121 printf ( "All tests ok (there's just one test)\n"); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
122 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
123 |