Extreme Optimization > QuickStart Samples > Optimization In One Dimension QuickStart Sample (VB.NET)

Extreme Optimization QuickStart Samples

Optimization In One Dimension QuickStart Sample (VB.NET)

Illustrates the use of the Brent and Golden Section optimizer classes in the Extreme.Mathematics.Optimization namespace for one-dimensional optimization in Visual Basic.

C# code Back to QuickStart Samples

' The optimization classes resides in the 
' Extreme.Mathematics.EquationSolvers namespace.
Imports Extreme.Mathematics.Optimization
' Function delegates reside in the Extreme.Mathematics
' namespace.
Imports Extreme.Mathematics

Namespace Extreme.Mathematics.QuickStart.VB
    ' Illustrates the use of the Brent and Golden Section optimizers
    ' in the Extreme.Mathematics.Optimization namespace of the
    ' Extreme Optimization Mathematics Library for .NET.
    Module NewtonEquationSolver

        Sub Main()
            ' Several algorithms exist for optimizing functions
            ' in one variable. The most common one is
            ' Brent's algorithm.

            ' The function we are trying to minimize is called the
            ' objective function and must be provided as a RealFunction. 
            ' For more information about this delegate, see the
            ' FunctionDelegates QuickStart sample.
            Dim f1 As RealFunction = New RealFunction(AddressOf TestFunction1)
            Dim f2 As RealFunction = New RealFunction(AddressOf TestFunction2)

            '
            ' Brent's algorithm
            '

            ' Now let's create the BrentOptimizer object.
            Dim optimizer As BrentOptimizer = New BrentOptimizer

            ' Set the objective function:
            optimizer.ObjectiveFunction = f1
            ' Optimizers can find either a minimum or a maximum.
            ' Which of the two is specified by the ExtremumType
            ' property
            optimizer.ExtremumType = ExtremumType.Minimum

            ' The first phase is to find an interval that contains
            ' a local minimum. This is done by the FindBracket method.
            optimizer.FindBracket(0, 6)
            ' You can verify that an interval was found from the
            ' IsBracketValid property:
            If (Not optimizer.IsBracketValid) Then
                Throw New Exception _
                    ("An interval containing a minimum was not found.")
            End If

            ' Finally, we can run the optimizer by calling the FindExtremum method:
            optimizer.FindExtremum()

            Console.WriteLine("Function 1: x^3 - 2x - 5")
            ' The Status property indicates
            ' the result of running the algorithm.
            Console.WriteLine("  Result: {0}", optimizer.Status)
            ' The result is available through the
            ' Result property.
            Console.WriteLine("  Minimum: {0}", optimizer.Result)
            Dim exactResult As Double = Math.Sqrt(2 / 3.0)
            Dim result As Double = CType(optimizer.Result, Double)
            Console.WriteLine("  Exact minimum: {0}", exactResult)

            ' You can find out the estimated error of the result
            ' through the EstimatedError property:
            Console.WriteLine("  Estimated error: {0}", _
                optimizer.EstimatedError)
            Console.WriteLine("  Actual error: {0}", _
                Math.Abs(result - exactResult))
            Console.WriteLine("  # iterations: {0}", _
                optimizer.IterationsNeeded)

            Console.WriteLine("Function 2: 1/Exp(x*x - 0.7*x +0.2)")
            ' You can also perform these calculations more directly 
            ' using the FindMinimum or FindMaximum methods. This implicitly
            ' calls the FindBracket method.
            result = optimizer.FindMaximum(f2, 0)
            Console.WriteLine("  Maximum: {0}", result)
            Console.WriteLine("  Actual maximum: {0}", 0.35)
            Console.WriteLine("  Estimated error: {0}", _
                optimizer.EstimatedError)
            Console.WriteLine("  Actual error: {0}", _
                result - 0.35)
            Console.WriteLine("  # iterations: {0}", _
                optimizer.IterationsNeeded)

            '
            ' Golden section search
            '

            ' A slower but simpler algorithm for finding an extremum
            ' is the golden section search. It is implemented by the
            ' GoldenSectionMinimizer class:
            Dim optimizer2 As GoldenSectionOptimizer = _
                New GoldenSectionOptimizer

            Console.WriteLine("Using Golden Section optimizer:")
            result = optimizer2.FindMaximum(f2, 0)
            Console.WriteLine("  Maximum: {0}", result)
            Console.WriteLine("  Actual maximum: {0}", 0.35)
            Console.WriteLine("  Estimated error: {0}", _
                optimizer2.EstimatedError)
            Console.WriteLine("  Actual error: {0}", _
                result - 0.35)
            Console.WriteLine("  # iterations: {0}", _
                optimizer2.IterationsNeeded)

            Console.Write("Press Enter key to exit...")
            Console.ReadLine()
        End Sub

        ' Minimum at x = Sqrt(2/3) = 0.816496580927726
        Function TestFunction1(ByVal x As Double) As Double
            Return x * x * x - 2 * x - 5
        End Function

        ' Maximum at x = 0.35
        Function TestFunction2(ByVal x As Double) As Double
            Return 1 / Math.Exp(x * x - 0.7 * x + 0.2)
        End Function

    End Module

End Namespace
Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
Information
Resources
Contact Us
Search

"The Extreme Optimization Statistics Library for .NET is a major boon for those doing statistical work in .NET. I strongly recommend this product."
- Marc Brooks

"I have made it my mission to institutionalize the value of good API design.  I strongly believe that this is key to making developers more productive and happy on our platform. It is clear that you value good API design in your work, and take to heart developer productivity and synergy with the .NET framework."
- Brad Abrams,
Lead Program Manager, Microsoft.

This is a partial list of companies who are using our libraries:
ABB Robotics
Allstate
Applied Materials
Arcam
Astra Schedule
Babson College
Canadian Council on Learning
Canyon Associates
Caxton Associates
CECity
Constellation Energy
CreditSights
DeepOcean
Duke University
Dynamotive
Elecsoft
Engelhard Corporation
Epcor
Equipoise Software
Galileo International
GAM UK
Gammex
GlaxoSmithKline
Global Matrix
The Hartford
Infinera Corporation
Intel
JDS Uniphase
LaBranche & Co.
Learning & Skills Council
Jacobs Consultancy
Litman Gregory
Lucas Systems
Malvern Instruments
Medrio
Merck & Co.
Mintera.
Monitor Software
MorningStar
NanoString Technologies
Paletta Invent
Parametric Portfolio Associates
Prosanos
RATA Associates
RiskShield
Ramboll
Standard & Poor's
Strategic Analysis Corporation
Univ. of Alicante
Univ. of South Carolina
vielife
Xerox
US Army