]> Creatis software - CreaPhase.git/blob - octave_packages/optim-1.2.0/samin_example.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / optim-1.2.0 / samin_example.m
1 ## Copyright (C) 2004 Michael Creel <michael.creel@uab.es>
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 # samin_example: example script that contains examples of how to call
17 # samin for minimization using simulated annealing.
18 # Edit the script to see how samin may be used.
19 #
20 # usage: samin_example
21
22 1; # this is a script file
23
24 # Example objective function
25 # remember that cos(0)=1, so
26 # "a" has a local minimum at 0 (each dimension)
27 # "b" makes the function value 0 at min
28 # "c" adds some curvature to make the min
29 #       at (0,0,...,0) global.
30 # the closer is "curvature" to zero the more alike are
31 # the local mins, so the harder the global min is to find
32
33 function f = obj(theta, curvature);
34         dim = rows(theta);
35         a = sum(exp(-cos(theta)));
36         b =  - dim*exp(-1);
37         c = sum(curvature*theta .^ 2);
38         f = a + b + c;
39 endfunction
40
41 k = 5; # dimensionality
42 theta = rand(k,1)*10 - 5; # random start value
43
44 # if you set "curvature" very small,
45 # you will need to increase nt, ns, and rt
46 # to minimize sucessfully
47 curvature = 0.01;
48
49
50 # SA controls
51 ub = 10*ones(rows(theta),1);
52 lb = -ub;
53 # setting ub and lb to same value restricts that parameter, and the algorithm does not search
54 ub(1,:) = 0;
55 lb(1,:) = 0;
56 theta(1,:) = 0; # must satisfy restriction
57
58 nt = 20;
59 ns = 5;
60 rt = 0.5; # careful - this is too low for many problems
61 maxevals = 1e10;
62 neps = 5;
63 functol = 1e-10;
64 paramtol = 1e-3;
65 verbosity = 1; # only final results. Inc
66 minarg = 1;
67 control = { lb, ub, nt, ns, rt, maxevals, neps, functol, paramtol, verbosity, 1};
68
69
70 # do sa
71 t=cputime();
72 [theta, obj_value, convergence] = samin("obj", {theta, curvature}, control);
73 t = cputime() - t;
74 printf("Elapsed time = %f\n\n\n",t);
75