public abstract class Optimization extends java.lang.Object implements TechnicalInformationHandler, RevisionHandler
class MyOpt extends Optimization {
// Provide the objective function
protected double objectiveFunction(double[] x) {
// How to calculate your objective function...
// ...
}
// Provide the first derivatives
protected double[] evaluateGradient(double[] x) {
// How to calculate the gradient of the objective function...
// ...
}
// If possible, provide the indexˆ{th} row of the Hessian matrix
protected double[] evaluateHessian(double[] x, int index) {
// How to calculate the indexˆth variable's second derivative
// ...
}
}
When it's the time to use it, in some routine(s) of other class...
MyOpt opt = new MyOpt();
// Set up initial variable values and bound constraints
double[] x = new double[numVariables];
// Lower and upper bounds: 1st row is lower bounds, 2nd is upper
double[] constraints = new double[2][numVariables];
...
// Find the minimum, 200 iterations as default
x = opt.findArgmin(x, constraints);
while(x == null){ // 200 iterations are not enough
x = opt.getVarbValues(); // Try another 200 iterations
x = opt.findArgmin(x, constraints);
}
// The minimal function value
double minFunction = opt.getMinFunction();
...
It is recommended that Hessian values be provided so that the second-order Lagrangian multiplier estimate can be calcluated. However, if it is not provided, there is no need to override the evaluateHessian() function.
REFERENCES (see also the getTechnicalInformation() method):getTechnicalInformation()| Modifier and Type | Class and Description |
|---|---|
protected class |
Optimization.DynamicIntArray
Implements a simple dynamic array for ints.
|
| Modifier and Type | Field and Description |
|---|---|
protected double |
m_ALF |
protected double |
m_BETA |
protected boolean |
m_Debug |
protected static double |
m_Epsilon
Compute machine precision
|
protected double |
m_f
function value
|
protected boolean |
m_IsZeroStep
Test if zero step in lnsrch
|
protected int |
m_MAXITS |
protected double |
m_STPMX |
protected double |
m_TOLX |
protected double[] |
m_X
Used when iteration overflow occurs
|
protected static double |
m_Zero
Compute machine precision
|
| Constructor and Description |
|---|
Optimization() |
| Modifier and Type | Method and Description |
|---|---|
protected abstract double[] |
evaluateGradient(double[] x)
Subclass should implement this procedure to evaluate gradient of the objective function
|
protected double[] |
evaluateHessian(double[] x,
int index)
Subclass is recommended to override this procedure to evaluate second-order gradient of the objective function.
|
double[] |
findArgmin(double[] initX,
double[][] constraints)
Main algorithm.
|
double |
getMinFunction()
Get the minimal function value
|
TechnicalInformation |
getTechnicalInformation()
Returns an instance of a TechnicalInformation object, containing detailed information about the technical background of this class, e.g., paper reference or book this class is based on.
|
double[] |
getVarbValues()
Get the variable values.
|
double[] |
lnsrch(double[] xold,
double[] gradient,
double[] direct,
double stpmax,
boolean[] isFixed,
double[][] nwsBounds,
Optimization.DynamicIntArray wsBdsIndx)
Find a new point x in the direction p from a point xold at which the value of the function has decreased sufficiently, the positive definiteness of B matrix (approximation of the inverse of the Hessian) is preserved and no bound
constraints are violated.
|
protected abstract double |
objectiveFunction(double[] x)
Subclass should implement this procedure to evaluate objective function to be minimized
|
void |
setDebug(boolean db)
Set whether in debug mode
|
void |
setMaxIteration(int it)
Set the maximal number of iterations in searching (Default 200)
|
static double[] |
solveTriangle(Matrix t,
double[] b,
boolean isLower,
boolean[] isZero)
Solve the linear equation of TX=B where T is a triangle matrix It can be solved using back/forward substitution, with O(N^2) complexity
|
protected void |
updateCholeskyFactor(Matrix L,
double[] D,
double[] v,
double coeff,
boolean[] isFixed)
One rank update of the Cholesky factorization of B matrix in BFGS updates, i.e.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetRevisionprotected double m_ALF
protected double m_BETA
protected double m_TOLX
protected double m_STPMX
protected int m_MAXITS
protected boolean m_Debug
protected double m_f
protected boolean m_IsZeroStep
protected double[] m_X
protected static double m_Epsilon
protected static double m_Zero
public TechnicalInformation getTechnicalInformation()
getTechnicalInformation in interface TechnicalInformationHandlerprotected abstract double objectiveFunction(double[] x)
throws java.lang.Exception
x - the variable valuesjava.lang.Exception - if something goes wrongprotected abstract double[] evaluateGradient(double[] x)
throws java.lang.Exception
x - the variable valuesjava.lang.Exception - if something goes wrongprotected double[] evaluateHessian(double[] x,
int index)
throws java.lang.Exception
x - the variablesindex - the row index in the Hessian matrixjava.lang.Exception - if something goes wrongpublic double getMinFunction()
public void setMaxIteration(int it)
it - the maximal number of iterationspublic void setDebug(boolean db)
db - use debug or notpublic double[] getVarbValues()
public double[] lnsrch(double[] xold,
double[] gradient,
double[] direct,
double stpmax,
boolean[] isFixed,
double[][] nwsBounds,
Optimization.DynamicIntArray wsBdsIndx)
throws java.lang.Exception
xold - old x valuegradient - gradient at that pointdirect - direction vectorstpmax - maximum step lengthisFixed - indicating whether a variable has been fixednwsBounds - non-working set bounds. Means these variables are free and subject to the bound constraints in this stepwsBdsIndx - index of variables that has working-set bounds. Means these variables are already fixed and no longer subject to the constraintsjava.lang.Exception - if an error occurspublic double[] findArgmin(double[] initX,
double[][] constraints)
throws java.lang.Exception
initX - initial point of x, assuming no value's on the bound!constraints - the bound constraints of each variable constraints[0] is the lower bounds and constraints[1] is the upper boundsjava.lang.Exception - if an error occurspublic static double[] solveTriangle(Matrix t, double[] b, boolean isLower, boolean[] isZero)
t - the matrix Tb - the vector BisLower - whether T is a lower or higher triangle matrixisZero - which row(s) of T are not used when solving the equation. If it's null or all 'false', then every row is used.protected void updateCholeskyFactor(Matrix L, double[] D, double[] v, double coeff, boolean[] isFixed) throws java.lang.Exception
L - the unit triangle matrix LD - the diagonal matrix Dv - the update vector vcoeff - the coeffcient of updateisFixed - which variables are not to be updatedjava.lang.Exception