[TOC]
Imports System
Imports CenterSpace.NMath.Core
Imports CenterSpace.NMath.Analysis
Namespace CenterSpace.NMath.Analysis.Examples.VisualBasic
' A .NET example in VB.NET 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).
' Wrap the function to minimize in a delegate. MyFunction() that has three input variables and four
' output variables
Dim F As New NMathFunctions.DoubleVectorDoubleVectorFunction(AddressOf MyFunction)
Dim YDim As Integer = 4
' 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()
' Compute the solution and display the results.
Dim Solution As DoubleVector = Minimizer.Minimize(F, Start, YDim)
Console.WriteLine("The minimizer started at ")
Console.WriteLine(Start)
Console.WriteLine("with a residual of " & Minimizer.InitialResidual)
Console.WriteLine()
Console.WriteLine("After " & Minimizer.Iterations & " iterations, it reached a solution at")
Console.WriteLine(Solution)
Console.WriteLine("with a residual of " & Minimizer.FinalResidual)
Console.WriteLine()
Console.WriteLine("It stopped because " & Minimizer.StopCriterion.ToString() & ".")
' 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, YDim, LowerBounds, UpperBounds)
Console.WriteLine()
Console.WriteLine("Lower bounds = " & LowerBounds.ToString())
Console.WriteLine("Upper bounds = " & UpperBounds.ToString())
Console.WriteLine("The solution is at")
Console.WriteLine(Solution)
Console.WriteLine("with a residual of " & Minimizer.FinalResidual)
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
End Module
End Namespace
[TOC]