edu.stanford.rsl.jpop
Class SimpleFunctionOptimizer

java.lang.Object
  extended by edu.stanford.rsl.jpop.SimpleFunctionOptimizer

public class SimpleFunctionOptimizer
extends java.lang.Object

This class was is based on the Fortran-Java translation of Steve Verrill of fzero and fmin. The code was adopted and altered to fit into a hierarchical object-oriented program structure.

Version:
0.7 --- April 27, 2010
Author:
Andreas Maier

Constructor Summary
SimpleFunctionOptimizer()
           
 
Method Summary
 double findRoot(SimpleOptimizableFunction function)
          Method to find a root of a function between leftEndPoint and rightEndPoint.
May use an initial guess to speed to convergence.

Accuracy can be adjusted using relativeTolerance and absoluteTolerance.
static double fmin(double leftEndPoint, double rightEndPoint, SimpleOptimizableFunction function, double toleranceFactor)
          Deprecated. 
static void fzero(SimpleOptimizableFunction function, double[] b, double[] c, double r, double re, double ae, int[] iflag)
          Deprecated. 
 double getAbsoluteTolerance()
           
 double getInitialGuess()
           
 double getLeftEndPoint()
           
 double getRelativeTolerance()
           
 double getRightEndPoint()
           
 boolean isPrintWarnings()
           
 boolean isUseInitialGuess()
           
 double minimize(SimpleOptimizableFunction function)
          Minimizes a SimpleOptimizableFunction.
 void setAbsoluteTolerance(double absoluteTolerance)
          Sets the absolute tolerance of the search.
Defaults to 3.0E-08.
 void setInitialGuess(double initialGuess)
          An initial guess of a zero can be set.
 void setLeftEndPoint(double leftEndPoint)
          Sets the left end point of the search interval.
Default is Double.MIN_VALUE.
 void setPrintWarnings(boolean printWarnings)
          Turns warnings on or off.
Default is on.
 void setRelativeTolerance(double relativeTolerance)
          Sets the relative tolerance.
Defaults to 3.0E-04.
 void setRightEndPoint(double rightEndPoint)
          Sets the right end point of the search interval.
Defaults to Double.MAX_VALUE.
 void setUseInitialGuess(boolean useInitialGuess)
          Turns the use of an initial guess on or off.
Default is off.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleFunctionOptimizer

public SimpleFunctionOptimizer()
Method Detail

getLeftEndPoint

public double getLeftEndPoint()
Returns:
the leftEndPoint of the search interval.

setLeftEndPoint

public void setLeftEndPoint(double leftEndPoint)
Sets the left end point of the search interval.
Default is Double.MIN_VALUE.

Parameters:
leftEndPoint - the leftEndPoint to set

getRightEndPoint

public double getRightEndPoint()
Returns:
the rightEndPoint of the search interval.

setRightEndPoint

public void setRightEndPoint(double rightEndPoint)
Sets the right end point of the search interval.
Defaults to Double.MAX_VALUE.

Parameters:
rightEndPoint - the rightEndPoint to set

getAbsoluteTolerance

public double getAbsoluteTolerance()
Returns:
the absoluteTolerance

setAbsoluteTolerance

public void setAbsoluteTolerance(double absoluteTolerance)
Sets the absolute tolerance of the search.
Defaults to 3.0E-08. 1.1102e-16 is machine precision.

Parameters:
absoluteTolerance - the absoluteTolerance to set

getRelativeTolerance

public double getRelativeTolerance()
Returns:
the relativeTolerance

setRelativeTolerance

public void setRelativeTolerance(double relativeTolerance)
Sets the relative tolerance.
Defaults to 3.0E-04. 1.1102e-16 is machine precision.

Parameters:
relativeTolerance - the relativeTolerance to set

isPrintWarnings

public boolean isPrintWarnings()
Returns:
the printWarnings

setPrintWarnings

public void setPrintWarnings(boolean printWarnings)
Turns warnings on or off.
Default is on.

Parameters:
printWarnings - the printWarnings to set

isUseInitialGuess

public boolean isUseInitialGuess()
Returns:
the useInitialGuess

setUseInitialGuess

public void setUseInitialGuess(boolean useInitialGuess)
Turns the use of an initial guess on or off.
Default is off.

Parameters:
useInitialGuess - the useInitialGuess to set

getInitialGuess

public double getInitialGuess()
Returns:
the initialGuess

setInitialGuess

public void setInitialGuess(double initialGuess)
An initial guess of a zero can be set.
The value will only be used if useInitialGuess is true.

A guess of a zero of the function could help in speeding up convergence. If evaluate(leftEndPoint) and evaluate(initialGuess) have opposite signs, a root will be found in the interval (leftEndPoint,inititalGuess); if not, but evaluate(initialGuess) and evaluate(rightEndPoint) have opposite signs, a root will be found in the interval (initialGuess,rightEndPoint); otherwise, the interval (leftEndPoint,rightEndPoint) will be searched for a possible root.

Parameters:
initialGuess - the initialGuess to set

minimize

public double minimize(SimpleOptimizableFunction function)
Minimizes a SimpleOptimizableFunction. Searches between leftEndPoint and rightEndPoint with a tolerance of absoluteTolerance. Parameters must be set using the respective getters and setters.

Parameters:
function - the function to minimize.
Returns:
the position of the minimum.

fmin

@Deprecated
public static double fmin(double leftEndPoint,
                                     double rightEndPoint,
                                     SimpleOptimizableFunction function,
                                     double toleranceFactor)
Deprecated. 

This method performs a 1-dimensional minimization. It implements Brent's method which combines a golden-section search and parabolic interpolation. The introductory comments from the FORTRAN version are provided below. This method is a translation from FORTRAN to Java of the Netlib function fmin. In the Netlib listing no author is given. Translated by Steve Verrill, March 24, 1998.

Parameters:
leftEndPoint - Left endpoint of initial interval
rightEndPoint - Right endpoint of initial interval
function - A class that defines a method, evaluate(), to minimize. The class must implement the SimpleOptimizableFunction interface. evaluate must have one double valued argument.
toleranceFactor - Desired length of the interval in which the minimum will be determined to lie (This should be greater than, roughly, 3.0e-8.)
Returns:
the minimal value;

findRoot

public double findRoot(SimpleOptimizableFunction function)
Method to find a root of a function between leftEndPoint and rightEndPoint.
May use an initial guess to speed to convergence.

Accuracy can be adjusted using relativeTolerance and absoluteTolerance. Use setters of this class to adjust the parameters or to disable the printing of warnings.

Parameters:
function -
Returns:

fzero

@Deprecated
public static void fzero(SimpleOptimizableFunction function,
                                    double[] b,
                                    double[] c,
                                    double r,
                                    double re,
                                    double ae,
                                    int[] iflag)
Deprecated. 

This method searches for a zero of a function f(x) between the given values b and c until the width of the interval (b,c) has collapsed to within a tolerance specified by the stopping criterion, Math.abs(b-c) <= 2.0*(rw*Math.abs(b)+ae). The method used is an efficient combination of bisection and the secant rule. The introductory comments from the FORTRAN version are provided below. This method is a translation from FORTRAN to Java of the Netlib (actually it is in the SLATEC library which is available at Netlib) function dfzero. In the FORTRAN code, L.F. Shampine and H.A. Watts are listed as the authors. Translated by Steve Verrill, April 17, 2001.

Parameters:
function - A class that defines a method, evaluate, that returns a value that is to be zeroed. The class must implement the SimpleOptimizableFunction interface. evaluate must have one double valued argument.
b[1] - One end point of the initial interval. The value returned for b[1] is usually the better approximation to a zero of evaluate.
c[1] - The other end point of the initial interval.
r - Initial guess of a zero. A (better) guess of a zero of evaluate() which could help in speeding up convergence. If evaluate(b) and evaluate(r) have opposite signs, a root will be found in the interval (b,r); if not, but evaluate(r) and evaluate(c) have opposite signs, a root will be found in the interval (r,c); otherwise, the interval (b,c) will be searched for a possible root. When no better guess is known, it is recommended that r be set to b or c; because if r is not interior to the interval (b,c), it will be ignored.
re - Relative error used for rw in the stopping criterion. If the requested re is less than machine precision, then rw is set to approximately machine precision.
ae - Absolute error used in the stopping criterion. If the given interval (b,c) contains the origin, then a nonzero value should be chosen for AE.
iflag[1] - A status code. User must check iflag[1] after each call. Control returns to the user from dfzero in all cases. 1: b is within the requested tolerance of a zero. The interval (b,c) collapsed to the requested tolerance, the function changes sign in (b,c), and evaluate(x) decreased in magnitude as (b,c) collapsed. 2: evaluate(b) = 0. However, the interval (b,c) may not have collapsed to the requested tolerance. 3: b may be near a singular point of evaluate(x). The interval (b,c) collapsed to the requested tol- erance and the function changes sign in (b,c), but evaluate(x) increased in magnitude as (b,c) collapsed,i.e. Math.abs(evaluate(b out)) > Math.max(Math.abs(evaluate(b in)), Math.abs(evaluate(c in))) 4: No change in sign of evaluate(x) was found although the interval (b,c) collapsed to the requested tolerance. The user must examine this case and decide whether b is near a local minimum of evaluate(x), or b is near a zero of even multiplicity, or neither of these. 5: Too many (> 500) function evaluations used.