XLPack 7.0
XLPack Numerical Library (Excel VBA) Reference Manual
Loading...
Searching...
No Matches

◆ Optif9_r()

Sub Optif9_r ( N As  Long,
X() As  Double,
Typsiz() As  Double,
Fscale As  Double,
Xpls() As  Double,
Fpls As  Double,
Gpls() As  Double,
Info As  Long,
XX() As  Double,
YY As  Double,
YYp() As  Double,
YYp2() As  Double,
IRev As  Long,
Optional Info2 As  Long,
Optional Iter As  Long,
Optional Iagflg As  Long = 0,
Optional Iahflg As  Long = 0,
Optional Iresult As  Long = 0,
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) (reverse communication version)

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 the analytic gradient and/or Hessian values. If the analytic gradient is not supplied, the first order finite difference is used to compute the gradients. If the analytic Hessian 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 is not supplied and the default parameters are used, the problem is solved by the quasi-Newton method (BFGS method).

Optif9_r is the reverse communication version of Optif9.
Parameters
[in]NThe order or dimension of the problem. (N > 1)
[in]X()Array X(LX - 1) (LX >= N)
Initial approximation of the solution vector.
[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]FscaleEstimate 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]FplsFunction 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.
= -3: The argument Typsiz() is invalid.
= -5: The argument Xpls() is invalid.
= -7: The argument Gpls() is invalid.
= -9: The argument XX() is invalid.
= -11: The argument YYp() is invalid.
= -12: The argument YYp2() 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]XX()Array XX(LXX - 1) (LXX >= N)
IRev = 1, 2 or 3: XX() contains the abscissa where the function value, derivatives or Hessian matrix should be evaluated and given in the next call.
IRev = 4 to 6: XX() contains the current solution vector x for printing.
[in,out]YY[in] IRev = 1: The function value f(XX) should be given in YY in the next call.
[out] IRev = 4 to 6: YY contains the function value at current x for printing.
[in,out]YYp()Array YYp(LYYp - 1) (LYYp >= N)
When returned with IRev=2, the derivatives should be given in YYp() in the next call. [in] IRev = 2: The derivatives at XX() should be given in YYp() in the next call.
[out] IRev= 4 to 6: YYp() contains the derivatives at current x for printing.
[in,out]YYp2()Array YYp2(LYYp21 - 1, LYYp22 - 1) (LYYp21 >= N, LYYp22 >= N)
[in] IRev = 3: The Hessian matrix at XX() should be given in YYp2() in the next call.
[out] IRev = 4 to 6: A() contains the Hessian at current x for printing. YYp2() is also used for work area for Hessian calculation.
[in,out]IRevControl variable for reverse communication.
[in] Before first call, IRev should be initialized to zero. On succeeding calls, IRev should not be altered.
[out] If IRev is not zero, set the required values to the specified variables or display the intermediate result as follows and call the routine again.
= 0: Computation finished. See return code in Info.
= 1: User should set the function values at XX() in YY. Do not alter any variables other than YY.
= 2: User should set the derivatives at XX() in YYp(). Do not alter any variables other than YYp(). (When Iagflg = 1)
= 3: User should set the Hessian matrix at XX() in YYp2(). Do not alter any variables other than YYp2(). (When Iahflg = 1)
= 4, 5, 6: Print the intermediate result (XX(), YY, YYp(), YYp2(), Iter). Do not alter any variables. (When Iresult = 1)
  IRev = 4: First return with initial values.
  IRev = 5: Returns during iteration with intermediate results.
  IRev = 6: Last return with final result.
[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]Iagflg(Optional)
Flag for analytic gradient. (default = 0)
= 0: To compute internally by finite difference approximation.
= 1: To return with IRev = 2 for analytic computation (analytic gradient will be validated by comparing with the finite difference before starting calculation).
= 2: To return with IRev = 2 for analytic computation (analytic gradient will not be checked).
[in]Iahflg(Optional)
Flag for analytic Hessian. (default = 0)
= 0: To compute internally by secant method or finite difference approximation. (see Iexp)
= 1: To return with IRev = 3 for analytic computation (analytic Hessian will be validated by comparing with the finite difference before starting calculation).
= 2: To return with IRev = 3 for analytic computation (analytic Hessian will not be checked).
[in]Iresult(Optional)
Flag for printing the intermediate and final information of iterations. (default = 0)
= 0: Do not print (will not return with Irev = 4, 5 or 6).
= 1: Print information (will return with Irev = 4, 5 or 6 on each iteration).
(If Iresult <> 0 and Iresult <> 1, the default value will be used)
[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 Ex_Optif9_r()
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
Dim Info As Long, Iagflg As Long
Dim XX(N - 1) As Double, YY As Double, YYp(N - 1) As Double
Dim YYp2(N - 1, N - 1) As Double, IRev As Long
Iagflg = 1
X(0) = -1.2: X(1) = 1
Typsiz(0) = 1: Typsiz(1) = 1: Fscale = 0.000000000000001 '1.0e-15
IRev = 0
Do
Call Optif9_r(N, X(), Typsiz(), Fscale, Xp(), Fp, Gp(), Info, XX(), YY, YYp(), YYp2(), IRev, , , Iagflg)
If IRev = 1 Then
YY = 100 * (XX(1) - XX(0) ^ 2) ^ 2 + (1 - XX(0)) ^ 2
ElseIf IRev = 2 Then
YYp(0) = -400 * XX(0) * (XX(1) - XX(0) ^ 2) + 2 * XX(0) - 2
YYp(1) = 200 * XX(1) - 200 * XX(0) ^ 2
End If
Loop While IRev <> 0
Debug.Print "X1, X2 =", Xp(0), Xp(1)
Debug.Print "Info =", Info
End Sub
Sub Optif9_r(N As Long, X() As Double, Typsiz() As Double, Fscale As Double, Xpls() As Double, Fpls As Double, Gpls() As Double, Info As Long, XX() As Double, YY As Double, YYp() As Double, YYp2() As Double, IRev As Long, Optional Info2 As Long, Optional Iter As Long, Optional Iagflg As Long=0, Optional Iahflg As Long=0, Optional Iresult As Long=0, 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) (reverse c...
Example Results
X1, X2 = 1.0000000582881 1.00000013384608
Info = 0