1 ## Copyright (C) 2004 Michael Creel <michael.creel@uab.es>
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
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
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/>.
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.
20 # usage: samin_example
22 1; # this is a script file
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
33 function f = obj(theta, curvature);
35 a = sum(exp(-cos(theta)));
37 c = sum(curvature*theta .^ 2);
41 k = 5; # dimensionality
42 theta = rand(k,1)*10 - 5; # random start value
44 # if you set "curvature" very small,
45 # you will need to increase nt, ns, and rt
46 # to minimize sucessfully
51 ub = 10*ones(rows(theta),1);
53 # setting ub and lb to same value restricts that parameter, and the algorithm does not search
56 theta(1,:) = 0; # must satisfy restriction
60 rt = 0.5; # careful - this is too low for many problems
65 verbosity = 1; # only final results. Inc
67 control = { lb, ub, nt, ns, rt, maxevals, neps, functol, paramtol, verbosity, 1};
72 [theta, obj_value, convergence] = samin("obj", {theta, curvature}, control);
74 printf("Elapsed time = %f\n\n\n",t);