|
|
◆ Optif9()
| Sub Optif9 |
( |
N As |
Long, |
|
|
X() As |
Double, |
|
|
F As |
LongPtr, |
|
|
Typsiz() As |
Double, |
|
|
Fscale As |
Double, |
|
|
Xpls() As |
Double, |
|
|
Fpls As |
Double, |
|
|
Gpls() As |
Double, |
|
|
Info As |
Long, |
|
|
Optional Info2 As |
Long, |
|
|
Optional Iter As |
Long, |
|
|
Optional D1fcn As |
LongPtr = NullPtr, |
|
|
Optional D2fcn As |
LongPtr = NullPtr, |
|
|
Optional DfcnChk As |
Long = 0, |
|
|
Optional Result As |
LongPtr = NullPtr, |
|
|
Optional Method As |
Long = 1, |
|
|
Optional Iexp As |
Long = 1, |
|
|
Optional Ndigit As |
Long = 0, |
|
|
Optional MaxIter As |
Long = 0, |
|
|
Optional Dlt As |
Double = -1, |
|
|
Optional Gradtl As |
Double = -1, |
|
|
Optional Stepmx As |
Double = 0, |
|
|
Optional Steptl As |
Double = -1 |
|
) |
| |
Minimum of a multivariable nonlinear function (quasi-Newton method or trust region method)
- Purpose
- This routine finds the local minimum point (xs1, xs2, ..., xsn) of general nonlinear function f(x1, x2, ..., xn) (a twice continuously differentiable real-valued function).
The user may supply analytic routines for the gradient and/or the Hessian. If the analytic gradient routine is not supplied, the first order finite difference is used to compute the gradients. If the analytic Hessian routine is not supplied, the secant method (BFGS update) or the second order finite difference is used to compute the Hessian.
Three global strategies, a line search, a dogleg trust region and a hookstep trust region methods have been implemented.
If the user's analytic Hessian routine is not supplied and the default parameters are used, the problem is solved by the quasi-Newton method (BFGS method).
- Parameters
-
| [in] | N | The order or dimension of the problem. (N > 1) |
| [in] | X() | Array X(LX - 1) (LX >= N)
Initial approximation of the solution vector. |
| [in] | F | The user-supplied subroutine which calculates the function f(x1, x2, ..., xn) defined as follows. Sub F(N As Long, X() As Double, Fval As Double)
Calculate the function value from given N and X() and return in Fval. The other variables should not be altered.
End Sub
|
| [in] | Typsiz() | Array Typsiz(LTypsiz - 1) (LTypsiz >= N) Typical size for each component of X() to be used for scaling. For example, if X(0) = -0.001, Typsiz(0) should be set to 0.001. (If 0 is specified, 1 will be used. if negative value is specified, the absolute value will be used) |
| [in] | Fscale | Estimate of scale of the function at minimum. For example, if f(Xpls) = 0.0001, Fscale should be set to 0.0001.
(If 0 is specified, 1 will be used. if negative value is specified, the absolute value will be used) |
| [out] | Xpls() | Array Xpls(LXpls - 1) (LXpls >= N)
Local minimum. |
| [out] | Fpls | Function value at local minimum Xpls. |
| [out] | Gpls() | Array Gpls(LGpls - 1) (LGpls >= N)
Gradient at local minimum Xpls. |
| [out] | Info | = 0: Successful exit. (See sub-code in Info2)
= -1: The argument N had an illegal value. (N <= 1)
= -2: The argument X() is invalid.
= -4: The argument Typsiz() is invalid.
= -6: The argument Xpls() is invalid.
= -8: The argument Gpls() is invalid.
= 1: Last global step failed to locate a point lower than Xpls. Either Xpls is an approximate local minimum of the function, the function is too non-linear for this algorithm, or Steptl is too large.
= 2: Iteration limit exceeded.
= 3: Maximum step size Stepmx exceeded five consecutive times. Either the function is unbounded below, becomes asymptotic to a finite value from above in some direction, or Stepmx is too small.
= 4: Probable coding error in the user's analytic gradient routine D1fcn.
= 5: Probable coding error in the user's analytic Hessian routine D2fcn. |
| [out] | Info2 | (Optional)
Sub-code for Info = 0.
= 1: Relative gradient is close to zero.
= 2: Successive iterates within tolerance. |
| [out] | Iter | (Optional)
Number of iterations performed to converge. |
| [in] | D1fcn | (Optional)
The user-supplied subroutine which calculates the derivatives of the function f(x1, x2, ..., xn) defined as follows. Sub D1fcn(N As Long, X() As Double, G() As Double)
Calculate the derivatives df/dX(i) from given N and X() and return in G(i) (i = 0 to N-1).
The other variables should not be altered.
End Sub
If this parameter is not supplied or NullPtr is specified, the derivatives will be calculated by the finite differences. |
| [in] | D2fcn | (Optional)
The user-supplied subroutine which calculates the Hessian matrix of the function f(x1, x2, ..., xn) defined as follows. Sub D2fcn(N As Long, X() As Double, H() As Double)
Calculate the Hessian matrix d2f/dX(i)dX(j) from given N and X() and return in H(i, j) (i = 0 to N-1, j = 0 to N-1).
The other variables should not be altered.
End Sub
If this parameter is not supplied or NullPtr is specified, the Hessian matrix will be calculated by the secant update (BFGS method) or the finite differences. |
| [in] | DfcnChk | (Optional)
Switch to check user analytic gradient routine D1fcn and Hessian routine D2fcn before starting calculation. (default = 0)
= 0: Check both D1fcn and D2fcn.
= 1: Do not check D1fcn.
= 2: Do not check D2fcn.
= 3: Do not check neither D1fcn nor D2fcn.
(If DfcnChk < 0 or DfcnChk > 3, the default value will be used) |
| [in] | Result | (Optional)
The user-supplied subroutine which prints the intermediate and final information of iterations defined as follows. Sub Result(N As Long, X() As Double, F As Double, G() As Double, H() As Double, P() As Double, Itncnt As Long, Iflag As Long)
Print necessary information from given arguments.
N: Number of variables.
X(0 to N-1): Current variable values.
F: Function value at X().
G(0 to N-1): Gradient values at X().
H(0 to N-1, 0 to N-1): Hessian values at X().
P(0 to N-1): Recent steps.
Itncnt: Iteration number.
Iflag: Control information.
= 1: First call to print initial values.
= 2: Calls during iteration.
= 3: Last call with final result.
All arguments are input variables and should not be altered.
End Sub
This subroutine will be called on each iteration. If this parameter is not supplied or NullPtr is specified, the subroutine call for printing will be skipped. |
| [in] | Method | (Optional)
Algorithm to use to solve minimization problem. (default = 1)
= 1: Line search.
= 2: Double dogleg trust region.
= 3: Hookstep trust region.
(If Method < 1 or Method > 3, the default value will be used) |
| [in] | Iexp | (Optional)
Evaluation time of the function. (default = 1)
= 0: Function evaluation is not expensive.
= 1: Function evaluation is expensive.
If D2fcn is available, Hessian matrix will be updated by D2fcn. If not, it will be updated by the secant method (BFGS method) in the case of Iexp = 1, and by the second order finite difference in the case of Iexp = 0. (If Iexp <> 0 and Iexp <> 1, the default value will be used) |
| [in] | Ndigit | (Optional)
Number of reliable decimal digits returned by F. (Ndigit > 0) (default = the full number of significant digits) (If Ndigit <= 0, the default value will be used) |
| [in] | MaxIter | (Optional)
Maximum number of iterations. (MaxIter > 0) (default = 200) (If MaxIter <= 0, the default value will be used) |
| [in] | Dlt | (Optional)
Initial trust region radius. If -1 is given, the length of the scaled gradient is used. (default = -1) (If Dlt <= 0, -1 is used. If Dlt > Stepmx, Stepmx is used) |
| [in] | Gradtl | (Optional)
Tolerance at which gradient considered close enough to zero to terminate algorithm. (Gradtl >= 0) (default = 1.0e-5) (If Gradtl < 0, the default value will be used) |
| [in] | Stepmx | (Optional)
Maximum allowable step size. (default = max(||X||2*1000, 1000)) (If Stepmx <= 0, the default value will be used) |
| [in] | Steptl | (Optional)
Relative step size at which successive iterates considered close enough to terminate algorithm. Values between 1.0e-3 and 1.0e-7 are usual. (Steptl >= 0) (default = 1.0e-5) (If Steptl < 0, the default value will be used) |
- Reference
- CMLIB
- Example Program
- Find the minimum point of the following function (Rosenbrock function).
f(x1, x2) = 100(x2 - x1^2)^2 + (1 - x1)^2
The initial approximation is (x1, x2) = (-1.2, 1). Sub FOptif(N As Long, X() As Double, F As Double)
F = 100 * (X(1) - X(0) ^ 2) ^ 2 + (1 - X(0)) ^ 2
End Sub
Sub GOptif(N As Long, X() As Double, G() As Double)
G(0) = -400 * X(0) * (X(1) - X(0) ^ 2) + 2 * X(0) - 2
G(1) = 200 * X(1) - 200 * X(0) ^ 2
End Sub
Sub Ex_Optif9()
Const N = 2
Dim X(N - 1) As Double, Typsiz(N - 1) As Double, Fscale As Double
Dim Xp(N - 1) As Double, Fp As Double, Gp(N - 1) As Double, Info As Long
X(0) = -1.2: X(1) = 1
Typsiz(0) = 1: Typsiz(1) = 1: Fscale = 0.000000000000001 '1.0e-15
Call Optif9(N, X(), AddressOf FOptif, Typsiz(), Fscale, Xp(), Fp, Gp(), Info, , , AddressOf GOptif)
Debug.Print "X1, X2 =", Xp(0), Xp(1)
Debug.Print "Info =", Info
End Sub
- Example Results
X1, X2 = 1.0000000582881 1.00000013384608
Info = 0
|