]> Creatis software - CreaPhase.git/blob - octave_packages/optim-1.2.0/battery.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / optim-1.2.0 / battery.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 ## battery.m: repeatedly call bfgs using a battery of 
17 ## start values, to attempt to find global min
18 ## of a nonconvex function
19 ##
20 ## INPUTS:
21 ## func: function to mimimize
22 ## args: args of function
23 ## minarg: argument to minimize w.r.t. (usually = 1)
24 ## startvals: kxp matrix of values to try for sure (don't include all zeros, that's automatic)
25 ## max iters per start value
26 ## number of additional random start values to try
27 ##
28 # OUTPUT: theta - the best value found - NOT iterated to convergence
29
30 function theta = battery(func, args, minarg, startvals, maxiters)
31
32 # setup
33 [k,trials] = size(startvals);
34 bestobj = inf;
35 besttheta = zeros(k,1);
36 bfgscontrol = {maxiters,0,0,1};
37 # now try the supplied start values, and optionally the random start values
38 for i = 1:trials
39         args{minarg} = startvals(:,i);
40         [theta, obj_value, convergence] = bfgsmin (func, args, bfgscontrol);
41         
42         if obj_value < bestobj
43                 besttheta = theta;
44                 bestobj = obj_value;
45         endif
46 endfor
47         
48 theta = besttheta;
49 endfunction