← All NMath Code Examples
 
Imports System
Imports CenterSpace.NMath.Core
Namespace CenterSpace.NMath.Examples.VisualBasic
  A .NET example in Visual Basic showing how to find find the minimization of a function
  using the trust-region method.
  Module TrustRegionMinimizationExample
    Sub Main()
      Console.WriteLine()
      Minimization in this context means finding the solution that evaluates to the vector with the smallest
      two norm (distance from origin).
      TrustRegionMimizer provides the Minimize() method for minimizing a given function 
      encapsulated as a DoubleMultiVariableFunction, an abstract class for representing a multivariable function. 
      Dim F As New MyFunction
      Choose a starting point
      Dim Start As New DoubleVector(50.0, 30.0, -1000.0)
      Create a minimizer with default tolerance and maximum iterations.
      Dim Minimizer As New TrustRegionMinimizer()
      Console.WriteLine()
      Compute the solution and display the results.
      Console.WriteLine("Minimize by implementing DoubleMultiVariableFunction..." & Environment.NewLine)
      Dim Solution As DoubleVector = Minimizer.Minimize(F, Start)
      Print(Minimizer, Start, Solution)
      You can also wrap a function to minimize in a delegate, rather than extending DoubleMultiVariableFunction
      Dim F2 As Func(Of DoubleVector, DoubleVector) = Function(x As DoubleVector)
                                                        Dim Y As New DoubleVector(4)
                                                        Y(0) = 5 * x(1) * x(1) + x(2) * x(2)
                                                        Y(1) = 4 * x(0) * x(0) - x(2) * x(2) + 45
                                                        Y(2) = x(0) * 3 * x(0) - x(1) * x(1) + 9
                                                        Y(3) = Y(0) + Y(1) + Y(2) * Y(2) - 43
                                                        Return Y
                                                      End Function
      Console.WriteLine("Minimize using delegate..." & Environment.NewLine)
      Dim yDim As Integer = 4
      Solution = Minimizer.Minimize(F2, Start, yDim)
      Print(Minimizer, Start, Solution)
      Console.WriteLine("Minimize with bounds..." & Environment.NewLine)
      Now add some constraints. Set a lower bound to (0, 0, 0) and an upper bound to (10, 10, 10)
      Dim LowerBounds As New DoubleVector(3)
      Dim UpperBounds As New DoubleVector(3, 10.0)
      Minimize again, and display the result.
      Solution = Minimizer.Minimize(F, Start, LowerBounds, UpperBounds)
      Console.WriteLine("Lower bounds = " & LowerBounds.ToString())
      Console.WriteLine("Upper bounds = " & UpperBounds.ToString())
      Print(Minimizer, Start, Solution)
      Console.WriteLine()
      Console.WriteLine("Press Enter Key")
      Console.Read()
    End Sub
    The function to minimize
    Private Function MyFunction(ByVal X As DoubleVector) As DoubleVector
      Dim y As New DoubleVector(4)
      y(0) = 5 * X(1) * X(1) + X(2) * X(2)
      y(1) = 4 * X(0) * X(0) - X(2) * X(2) + 45
      y(2) = X(0) * 3 * X(0) - X(1) * X(1) + 9
      y(3) = y(0) + y(1) + y(2) * y(2) - 43
      Return y
    End Function
    Private Sub Print(ByVal Minimizer As TrustRegionMinimizer, ByVal Start As DoubleVector, ByVal solution As DoubleVector)
      Console.WriteLine("Start: {0}", Start)
      Console.WriteLine("Initial Residual: {0}", Minimizer.InitialResidual)
      Console.WriteLine("Solution: {0}", solution)
      Console.WriteLine("Final Residual: {0}", Minimizer.FinalResidual)
      Console.WriteLine("Iterations: {0}", Minimizer.Iterations)
      Console.WriteLine("Stopping Criterion: {0}", Minimizer.StopCriterion)
      Console.WriteLine()
    End Sub
  End Module
  Encapsulate a function that has three input variables and four output variables
  Class MyFunction : Inherits DoubleMultiVariableFunction
    Public Sub New()
      MyBase.New(3, 4)
    End Sub
    Public Overrides Sub Evaluate(ByVal X As DoubleVector, ByRef y As DoubleVector)
      If (X.Length <> 3 Or y.Length <> 4) Then
        Throw New InvalidArgumentException("bad length")
      End If
      y(0) = 5 * X(1) * X(1) + X(2) * X(2)
      y(1) = 4 * X(0) * X(0) - X(2) * X(2) + 45
      y(2) = X(0) * 3 * X(0) - X(1) * X(1) + 9
      y(3) = y(0) + y(1) + y(2) * y(2) - 43
    End Sub
  End Class
End Namespace
← All NMath Code Examples