10. Nonlinear optimization of multivariate function
Example of solving a nonlinear optimization of multivariate function, i.e. finding a minimum point of general multidimensional nonlinear function f(x1, x2, …, xn) by XLPack.
Let’s obtain the minimum point of the following function.
f(x1, x2) = 100(x2 - x12)2 + (1 - x1)2
The plot of this function is as follows (vertical axis is expressed in logarithmic value).
This function is called as Rosenbrock function which is frequently used as the test problem finding minimum at (1, 1) starting from (-1.2, 1). It is said that this problem is difficult to reach the minimum point since it is in the deep bent valley.
Solving problem with VBA program (1)
The following is an example code using VBA library subroutine Optif0.
Sub F(N As Long, X() As Double, Fval As Double) Fval = 100 * (X(1) - X(0) ^ 2) ^ 2 + (1 - X(0)) ^ 2 End Sub Sub Start() Const NMax = 10, NData = 5 Dim N As Long, X(NMax) As Double, Xpls(NMax) As Double, Fpls As Double Dim Info As Long, I As Long N = 2 For I = 0 To NData - 1 '--- Input data X(0) = Cells(5 + I, 1): X(1) = Cells(5 + I, 2) '--- Find min point of equation Call Optif0(N, X(), AddressOf F, Xpls(), Fpls, Info) '--- Output result Cells(5 + I, 3) = Xpls(0): Cells(5 + I, 4) = Xpls(1): Cells(5 + I, 5) = Info Next End Sub
Users should define the object function as the external subroutine and call Optif0 with specifying the initial value. Optif0 starts the iterative calculation from the given initial approximation and find the solution located near.
We try five initial approximations (-1.2, 1), (-1, 1), (-1, -1), (0, 1) and (0, 0).
The following result was obtained by running this program.
It did not converge for the initial approximation (-1, 1). In the above example, the difficult initial values are used since it is the test problem. But the best approximation value should be used for the real problem. There may be a problem which has multiple local minimums. In that case, the routine may converge to undesired local minimum if the best initial value is not given.
Solving problem with VBA program (2)
The following is an example code using the reverse communication version VBA library subroutine Optif0_r.
Sub Start() Const NMax = 10, NData = 5 Dim N As Long, X(NMax) As Double, Xpls(NMax) As Double, Fpls As Double Dim Info As Long, I As Long Dim XX(NMax - 1) As Double, YY As Double, IRev As Long N = 2 For I = 0 To NData - 1 '--- Input data X(0) = Cells(5 + I, 1): X(1) = Cells(5 + I, 2) '--- Find min point of equation IRev = 0 Do Call Optif0_r(N, X(), Xpls(), Fpls, Info, XX(), YY, IRev) If IRev <> 1 Then YY = 100 * (XX(1) - XX(0) ^ 2) ^ 2 + (1 - XX(0)) ^ 2 Loop While IRev <> 0 '--- Output result Cells(5 + I, 3) = Xpls(0): Cells(5 + I, 4) = Xpls(1): Cells(5 + I, 5) = Info Next End Sub
Instead of defining the object function as the external function, when returned with IRev = 1, users need to compute the object function value at XX(), enter it in YY and call Optif0_r again. For further details of reverse communication version, see here.
The same result with above is obtained by this program.