public class Minimizer
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static int |
ABORTED
Status returned: aborted by call to abort method.
|
static int |
INITIALIZATION_FAILURE
Status returned: Could not initialize the simplex because either the initialParams
resulted in the target function returning NaN or all attempts to find starting
parameters for the other simplex points resulted in the target function returning NaN.
|
static int |
MAX_ITERATIONS_EXCEEDED
Status returned: no convergence detected after maximum iteration count
|
static int |
MAX_RESTARTS_EXCEEDED
Status returned: not two equal solutions after maximum number of restarts
|
static int |
REINITIALIZATION_FAILURE
Status returned: Could not reinitialize the simplex because all attempts to find restarting
parameters resulted in the target function returning NaN.
|
static java.lang.String[] |
STATUS_STRING
Strings describing the status codes
|
static int |
SUCCESS
Status returned: successful completion
|
Constructor and Description |
---|
Minimizer() |
Modifier and Type | Method and Description |
---|---|
void |
abort()
Aborts minimization.
|
int |
getCompletedMinimizations()
Get number of minimizations completed (i.e.
|
double |
getFunctionValue()
Get the value of the minimum, i.e.
|
int |
getIterations()
Get number of iterations performed (includes all restarts).
|
int |
getMaxIterations()
Get maximum number of iterations allowed.
|
int |
getMaxRestarts()
Get maximum number of minimization restarts to do
|
double[] |
getParams()
Get the result, i.e., the set of parameter values (i.e., variable values)
from the best corner of the simplex.
|
int |
minimize(double[] initialParams,
double[] initialParamVariations)
Perform minimization with the gradient-enhanced simplex method once or a few
times, depending on the value of 'restarts'.
|
int |
minimizeOnce(double[] initialParams,
double[] initialParamVariations)
Perform minimization with the simplex method once, including re-initialization until
we have a stable solution.
|
void |
setExtraArrayElements(int numExtraArrayElements)
Add a given number of extra elements to array of parameters (independent vaiables)
for private use in the user function.
|
void |
setFunction(UserFunction userFunction,
int numParams)
Set the the target function, i.e.
|
void |
setMaxError(double maxRelError)
Sets the accuracy of convergence.
|
void |
setMaxError(double maxRelError,
double maxAbsError)
Sets the accuracy of convergence.
|
void |
setMaximumThreads(int numThreads)
Call setMaximuThreads(1) to avoid multi-threaded execution (in case the user-provided
target function does not allow moultithreading).
|
void |
setMaxIterations(int x)
Set maximum number of iterations allowed (including all restarts and all threads).
|
void |
setMaxRestarts(int n)
Set maximum number of minimization restarts to do.
|
void |
setParamResolutions(double[] paramResolutions)
Set the resolution of the parameters, for problems where the target function is not smooth
but suffers from numerical noise.
|
void |
setRandomSeed(int n)
Set a seed to initialize the random number generator, which is used for initialization
of the simplex.
|
void |
setStatusAndEsc(java.lang.String ijStatusString,
boolean checkEscape)
Create output on the number of iterations in the ImageJ Status line, e.g.
|
public static final int SUCCESS
public static final int INITIALIZATION_FAILURE
public static final int ABORTED
public static final int REINITIALIZATION_FAILURE
public static final int MAX_ITERATIONS_EXCEEDED
public static final int MAX_RESTARTS_EXCEEDED
public static final java.lang.String[] STATUS_STRING
public void setFunction(UserFunction userFunction, int numParams)
userFunction
- The class having a function to minimize (implementing
the UserFunction interface).
This function must allow simultaneous calls in multiple threads unless
setMaximumThreads(1); has been called.
Note that the function will be called with at least numParams+1 array
elements; the last one is needed to store the function value. Further
array elements for private use in the user function may be added by
calling setExtraArrayElements.numParams
- Number of independent variables (also called parameters)
of the function.public int minimize(double[] initialParams, double[] initialParamVariations)
initialParams
- Array with starting values of the parameters (variables).
When null, the starting values are assumed to be 0.
The target function must be defined (not returning NaN) for
the values specified as initialParams.initialParamVariations
- Parameters (variables) are initially varied by up to +/-
this value. If not given (null), initial variations are taken as
10% of initial parameter value or 0.01 for parameters that are zero.
When this array is given, all elements must be positive (nonzero).
If one or several initial parameters are zero, is advisable to set the initialParamVariations
array to useful values indicating the typical order of magnitude of the parameters.
For target functions with only one minimum, convergence is fastest with large values of
initialParamVariations, so that the expected value is within initialParam+/-initialParamVariations.
If local minima can occur, it is best to use a value close to the expected global minimum,
and rather small initialParamVariations, much lower than the distance to the nearest local
minimum.public int minimizeOnce(double[] initialParams, double[] initialParamVariations)
initialParams
- Array with starting values of the parameters (variables).
When null, the starting values are assumed to be 0.
The target function must be defined (not returning NaN) for
the values specified as initialParams.initialParamVariations
- Parameters (variables) are initially varied by up to +/-
this value. If not given (null), iniital variations are taken as
10% of inial parameter value or 0.01 for parameters that are zero.
When this array is given, all elements must be positive (nonzero).
If one or several initial parameters are zero, is advisable to set the initialParamVariations
array to useful values indicating the typical order of magnitude of the parameters.
For target functions with only one minimum, convergence is fastest with large values of
initialParamVariations, so that the expected value is within initialParam+/-initialParamVariations.
If local minima can occur, it is best to use a value close to the expected global minimum,
and rather small initialParamVariations, much lower than the distance to the nearest local
minimum.public double[] getParams()
public double getFunctionValue()
public int getIterations()
public void setMaxIterations(int x)
public int getMaxIterations()
public void setMaxRestarts(int n)
public int getMaxRestarts()
public int getCompletedMinimizations()
public void setRandomSeed(int n)
public void setMaxError(double maxRelError)
public void setMaxError(double maxRelError, double maxAbsError)
public void setParamResolutions(double[] paramResolutions)
public void setMaximumThreads(int numThreads)
public void abort()
public void setStatusAndEsc(java.lang.String ijStatusString, boolean checkEscape)
ijStatusString
- Displayed in the beginning of the status message. No display if null.
E.g. "Optimization: Iteration "checkEscape
- When true, the Minimizer stops if escape is pressed and the status
becomes ABORTED. Note that checking for ESC does not work in the Event Queue thread.public void setExtraArrayElements(int numExtraArrayElements)