I've made a number of changes to line_min for compatibility with the
current Octave command set as well as for more efficient operation.
Please see the attached.

Best,

Nir
18c18
< ## args : list    : List of argument of f
---
> ## args : cell    : Cell array of arguments of f
42c42,53
< function [a,fx,nev] = line_min (f, dx, args, narg)
---
> ## 2007-11-20 Nir Krakauer
> ## * modified to limit the number of function evaluations
> ## * added a check to ensure that the function value returned was never more 
> than the initial value
> ## 2008-07-05 Nir Krakauer
> ## * randomized the step size h
> ## 2008-11-24 Nir Krakauer
> ## * modified to take args as a cell array rather than a list and to use 
> feval rather than leval, since lists are deprecated in Octave 3
> ## 2011-08-24 Nir Krakauer
> ## * modified to no longer use the function splice, which doesn't exist in 
> Octave 3.4
> ## 
>
> function [a,fx,nev] = line_min(f, dx, args, narg);
47a59
>   nev_max = 30;
49c61,62
<   h = 0.001;                  # Was 0.01 here
---
>   h = exp(rand() - 0.5)*0.001; %0.001;                        # Was 0.01 here
>   %%x = nth (args,narg);
53,56c66,78
<   while (abs (velocity) > 0.000001)
<     fx = feval (f,args{1:narg-1}, x+a*dx, args{narg+1:end});
<     fxph = feval (f,args{1:narg-1}, x+(a+h)*dx, args{narg+1:end});
<     fxmh = feval (f,args{1:narg-1}, x+(a-h)*dx, args{narg+1:end});
---
>   while (abs (velocity) > 0.000001) && (nev < nev_max)
>     %%fx = feval (f,splice(args, narg, 1, {x+a*dx}){:} );
>     args_new = args; args_new{narg} = x+a*dx;
>     fx = feval (f, args_new{:} );
>     if nev == 0
>       fx0 = fx;
>     end       
>     args_new{narg} = x+(a+h)*dx;
>     fxph = feval (f, args_new{:} );
>     args_new{narg} = x+(a-h)*dx;
>     fxmh = feval (f, args_new{:} );    
>     %%fxph = feval (f,splice(args, narg,1,{x+(a+h)*dx}){:} );
>     %%fxmh = feval (f,splice(args, narg,1,{x+(a-h)*dx}){:} );
61c83
<                               # Use abs(accel) to avoid problems due to
---
>                               # Use abs(acceleration) to avoid problems due to
65a88,101
>   
>   args_new = args; args_new{narg} = x+a*dx;
>   fx = feval (f, args_new{:} );  
>   %%fx = feval (f, splice(args, narg, 1,{x+a*dx}){:} )
>   nev++;
>   if fx >= fx0 %if no improvement, return the starting value
>       a = 0;
>       fx = fx0;
>   end
>   
>   if (nev >= nev_max)
>       disp('my_line_min: maximum number of function evaluations exceeded')
>   end
>   
69c105
< ## correspond to (nearly) optimal fx.
\ No newline at end of file
---
> ## correspond to (nearly) optimal fx.
## Copyright (C) 2000 Ben Sapp.  All rights reserved.
##
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by the
## Free Software Foundation; either version 2, or (at your option) any
## later version.
##
## This is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
## for more details.

## [a,fx,nev] = line_min (f, dx, args, narg) - Minimize f() along dx
##
## INPUT ----------
## f    : string  : Name of minimized function
## dx   : matrix  : Direction along which f() is minimized
## args : cell    : Cell array of arguments of f
## narg : integer : Position of minimized variable in args.  Default=1
##
## OUTPUT ---------
## a    : scalar  : Value for which f(x+a*dx) is a minimum (*)
## fx   : scalar  : Value of f(x+a*dx) at minimum (*)
## nev  : integer : Number of function evaluations
##
## (*) The notation f(x+a*dx) assumes that args == list (x).

## Author: Ben Sapp <bs...@lanl.gov>
## Reference: David G Luenberger's Linear and Nonlinear Programming
##
## Changelog : -----------
## 2002-01-28 Paul Kienzle
## * save two function evaluations by inlining the derivatives
## * pass through varargin{:} to the function
## 2002-03-13 Paul Kienzle
## * simplify update expression
## 2002-04-17 Etienne Grossmann <etie...@isr.ist.utl.pt>
## * Rename nrm.m to line_min.m (in order not to break dfp, which uses nrm)
## * Use list of args, suppress call to __pseudo_func__
## * Add nargs argument, assume args is a list
## * Change help text
## 2007-11-20 Nir Krakauer
## * modified to limit the number of function evaluations
## * added a check to ensure that the function value returned was never more than the initial value
## 2008-07-05 Nir Krakauer
## * randomized the step size h
## 2008-11-24 Nir Krakauer
## * modified to take args as a cell array rather than a list and to use feval rather than leval, since lists are deprecated in Octave 3
## 2011-08-24 Nir Krakauer
## * modified to no longer use the function splice, which doesn't exist in Octave 3.4
## 

function [a,fx,nev] = line_min(f, dx, args, narg);
  velocity = 1;
  acceleration = 1;

  if nargin < 4, narg = 1; end

  nev_max = 30;
  nev = 0;
  h = exp(rand() - 0.5)*0.001; %0.001;			# Was 0.01 here
  %%x = nth (args,narg);
  x = args{narg};
  a = 0;
				# was 1e-4
  while (abs (velocity) > 0.000001) && (nev < nev_max)
    %%fx = feval (f,splice(args, narg, 1, {x+a*dx}){:} );
    args_new = args; args_new{narg} = x+a*dx;
    fx = feval (f, args_new{:} );
    if nev == 0
    	fx0 = fx;
    end	
    args_new{narg} = x+(a+h)*dx;
    fxph = feval (f, args_new{:} );
    args_new{narg} = x+(a-h)*dx;
    fxmh = feval (f, args_new{:} );    
    %%fxph = feval (f,splice(args, narg,1,{x+(a+h)*dx}){:} );
    %%fxmh = feval (f,splice(args, narg,1,{x+(a-h)*dx}){:} );

    velocity = (fxph - fxmh)/(2*h);
    acceleration = (fxph - 2*fx + fxmh)/(h^2);
    if abs(acceleration) <= eps, acceleration = 1; end # Don't do div by zero
				# Use abs(acceleration) to avoid problems due to
				# concave function
    a = a - velocity/abs(acceleration);
    nev += 3;
  endwhile
  
  args_new = args; args_new{narg} = x+a*dx;
  fx = feval (f, args_new{:} );  
  %%fx = feval (f, splice(args, narg, 1,{x+a*dx}){:} )
  nev++;
  if fx >= fx0 %if no improvement, return the starting value
  	a = 0;
	fx = fx0;
  end
  
  if (nev >= nev_max)
  	disp('my_line_min: maximum number of function evaluations exceeded')
  end
  
endfunction

## Rem : Although not clear from the code, the returned a always seems to
## correspond to (nearly) optimal fx.
------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure 
contains a definitive record of customers, application performance, 
security threats, fraudulent activity, and more. Splunk takes this 
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
Octave-dev mailing list
Octave-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/octave-dev

Reply via email to