c-----------------------------------------------------------------------
c
c.ORIGINAL VERSION:
c (c) Victor Luana Cabal, Sept. 11, 1991.
c Departamento de Quimica Fisica y Analitica.
c Universidad de Oviedo. 33007-Oviedo, Spain.
c
c.optim - General optimization tool adapted to a UNIX environment.
c Minimizes a general function of several variables using one of
c several optimization methods.
c Optim uses system calls to run an external procedure at every
c trial point. The variables that define this point are passed to
c the procedure as shell parameters. The procedure returns the
c function value printing its value in an arbitrary file.
c This mechanism, some involved from a programmers point of view,
c is very flexible and well adapted to have a numerical experiment
c running fast. The system calls add some time to the optimization,
c but this is irrelevant when the time to compute a single point of
c the function is significant.
c
c-----------------------------------------------------------------------
c
c.VERSIONS:
c
c VLC, Sept. 11, 1991:
c Powell's quadratically convergent method.
c
c VLC, march 20, 1992:
c Nelder and Mead downhill simplex and simulated annealing
c simplex methods added.
c
c VLC, july 8, 1992:
c Added -I and -O options to allow entering points previously
c computed and allowing a record of the points already determined.
c
c VLC, Sept. 3, 1992:
c Added dater routine to document the start/end of run.
c
c VLC, Feb. 23, 1998:
c Version that calls the external function with no arguments and
c uses a file for sending parameters instead.
c
c VLC, Sep. 25, 2001:
c Bestint discrete optimization of a collection of integers added.
c
c-----------------------------------------------------------------------
c
c.....How to run optim:
c USE: optim [-n] [-i include] [-o record] [input [output]]
c
c Input is read from stdin when a file name is not given as the
c first parameter. Output is send to the stdout when a file name is
c not given as the second parameter.
c When the include file is provided, the points stored there will
c be read in before any optimization is started. This option is
c provided as a mechanism for restarting optimizations interrupted
c due to hardware fails. See below the appropriate format for this
c file.
c When record is provided, optim will print to this file every new
c point computed. This information will be appended to the previous
c content of the record file, if any.
c
c Options:
c
c -n All communication of optim with the external script will be
c done using an intermediate file. Notice that in the original
c method (still being the default) optim sends as parameters
c the coordinates of the point to be calculated and reads the
c function value from a file. This produces very long lines
c when calling to the external procedure and may fail in some
c problems with many dimensions.
c
c-----------------------------------------------------------------------
c
c.....Input data:
c
c Rec #1 (*) : Function_name
c Name of the external procedure that evaluates the function to
c minimize.
c
c Rec #2 (*) : File_name
c File where the external procedure prints the function value.
c If the [-n] option was given this file also serves to pass
c the coordinates of the point to be evaluated to the external
c procedure.
c
c Rec #3 (*) : nvar
c Total number of variables.
c
c do i = 1, nvar
c Rec #4.i (*) : var_name(i), var_value(i), var_min(i), var_max(i)
c Name, initial value, and minimal and maximal values allowed
c for the variables.
c enddo
c
c Rec #5 (a) : method
c Optimization method requested. Presently implemented methods are:
c 1) powell -----> quadratically convergent Powell's method.
c 2) amoeba -----> Nelder and Mead's downhill simplex method.
c 3) annealing --> simulated anneling simplex method.
c 4) bestint ----> exhaustive optimization of an integer vector.
c
c IF (method .eq. 'powell) then
c
c Rec #6a (*) : noptim, (ioptim(i), i = 1, noptim)
c Number and order of the variables that will be optimized.
c
c Rec #7a (*) : ftol
c fractional tolerance in the function value such that failure
c to decrease by more than this amount on one iteration signals
c doneness to the powell's routine.
c
c ELSE IF (method .eq. 'amoeba') then
c
c Rec #6b (*) : noptim, (ioptim(i), i = 1, noptim)
c Number and order of the variables that will be optimized.
c
c Rec #7b (*) : ftol, fscale, maxit
c ftol .... fractional tolerance to stop iteration.
c fscale .. the initial simplex will be formed as:
c V0 = X
c Vi = X + Ui (Xmax(i) - Xmin(i)) / fscale
c where i=1..noptim, X is the initial point, and
c Ui is a unit vector in the direction i. Try 5 as
c a reasonable value.
c maxit ... max number of iterations allowed.
c
c ELSE IF (method .eq. 'annealing') then
c
c Rec #6c (*) : noptim, (ioptim(i), i = 1, noptim)
c Number and order of the variables that will be optimized.
c
c Rec #7c (*) : ftol, fscale, maxit
c ftol .... fractional tolerance to stop the simplex search at
c each temperature.
c fscale .. size scale for the building of the initial simplex
c (see above).
c maxit ... max number of iterations allowed at each temperature
c
c Rec #8c (*) : tini, tfin, nsteps
c The temperature will be reduced from tini to tfin in nsteps
c steps. A linear scale freezing curve will be used.
c
c ELSE IF (method .eq. 'bestint') then
c
c Rec #6d (*) : noptim, (ioptim(i), i = 1, noptim)
c Number and order of the variables that will be optimized.
c
c ENDIF
c
c (If additional data from Rec #5 on is given, further minimization
c will be done beginning since the optimum already found).
c
c-----------------------------------------------------------------------
c
c.....Format of the include and record files:
c
c Rec #1 (*) : (var_value(i), i = 1, nvar), func_value
c Values of the independent variables and the external function.
c Enter every point in a new record.
c The number and order of variables should coincide with the data
c provided in the input file. No check is done by optim.
c
c-----------------------------------------------------------------------
c
c.....Requirements of the external procedure:
c
c The function to be minimized is supossed to be evaluated by an
c external procedure, which will be run in a subshell by means of
c the order:
c
c Function_name var_value(1) var_value(2) ... var_value(nvar)
c
c This procedure should print the function value (and only that) in
c a file called File_name, with arbitrary precision.
c
c-----------------------------------------------------------------------