# VB Stochastic Hill Climbing Example

← All NMath Code Examples

```ï»¿Imports System
Imports System.Diagnostics

Imports CenterSpace.NMath.Core
Imports CenterSpace.NMath.Analysis
Imports CenterSpace.NMath.Matrix

Namespace CenterSpace.NMath.Analysis.Examples.VisualBasic

' A .NET example in Visual Basic showing how to use the stochastic hill
' climbing solver <c>StochasticHillClimbingSolver</c> to solve a
' nonlinear programming problem.
' W. Hock, K. Schittkowski, Test Examples for Nonlinear Programming Codes.
' This is problem number 86.
Public Module StochasticHillClimbingExample

Sub Main()

' Create the objective function as an instance of a class derived from
' DoubleFunctional.
Dim F As New ObjectiveFunction()

' Construct the nonlinear programming problem. There are
' five constraints and each variable is required to be >= 0.
Dim Problem As New NonlinearProgrammingProblem(F)
Dim NumVars As Integer = 5
Dim NumConstraints As Integer = 10
Dim I As Integer = 0
For I = 0 To NumConstraints - 1
' Value of the ith constraint function must be >= 0.
Next
For I = 0 To NumVars - 1
' Each variable must be >= 0.
Next

' Create the solver object.
Dim Solver As New StochasticHillClimbingSolver()
' The solver is stochastic. Setting a random seed before
' the solver will ensure consistent results between runs.
Solver.RandomSeed = &H248

' Create a solver parameter object and set a time limit for
' the solver. By default the solver will run until a solution
' is found. Since this could take forever, it is a good idea to
' set a reasonable time limit on the solve. Here we set the time
' limit for ten seconds. If an optimal solution is not found within
' the specified time limit the solver will exit and the solver's
' Result property will be equal to SolverResult.SolverInterrupted.
' We also specify that we want to presolve. By default there is no
' presolve step. For some problems presolve can reduce the size and
' complexity and result in fewer steps to reach a solution.
Dim SolverParams As New StochasticHillClimbingParameters
SolverParams.TimeLimitMilliSeconds = 10000
SolverParams.Presolve = True

' Attempt the solver and write out the results.
Solver.Solve(Problem, SolverParams)
Console.WriteLine("Solver Result = " & Solver.Result)
Console.WriteLine("Number of steps = " & Solver.Steps)
Console.WriteLine("Optimal x = " & Solver.OptimalX.ToString("F3"))
Console.WriteLine("Optimal function value = " & Solver.OptimalObjectiveFunctionValue.ToString("F3"))

Console.WriteLine()
Console.WriteLine("Press Enter Key")
End Sub

' Objective function. This is the function we want to minimize.
Private Class ObjectiveFunction
Inherits DoubleFunctional

' Constants used in evaluating the objective function.
Private C_ As DoubleSymmetricMatrix
Private D_ As DoubleVector
Private E_ As DoubleVector

' Constructs an instance of the objective function.
Sub New()
MyBase.New(5)
E_ = New DoubleVector(-15.0, -27.0, -36.0, -18.0, -12.0)
D_ = New DoubleVector(4.0, 8.0, 10.0, 6.0, 2.0)
C_ = New DoubleSymmetricMatrix(5)
C_(0, 0) = 30
C_(0, 1) = -20
C_(0, 2) = -10
C_(0, 3) = 32
C_(0, 4) = -10

C_(1, 1) = 39
C_(1, 2) = -6
C_(1, 3) = -31
C_(1, 4) = 32

C_(2, 2) = 10
C_(2, 3) = -6
C_(2, 4) = -10

C_(3, 3) = 39
C_(3, 4) = -20

C_(4, 4) = 30

End Sub

' Evaluates the objective function at the given point.
' <param name="x">Evaluate at this point.</param>
' <returns>The objective function value at the point.</returns>
Public Overrides Function Evaluate(X As DoubleVector) As Double

Dim S1 As Double = 0
Dim S2 As Double = 0
Dim S3 As Double = 0
Dim I As Integer
For I = 0 To 4
S1 = S1 + E_(I) * X(I)
Dim J As Integer
For J = 0 To 4
S2 = S2 + C_(I, J) * X(I) * X(J)
Next
S3 = S3 + D_(I) * Math.Pow(X(I), 3)
Next
Return S1 + S2 + S3
End Function
End Class

' <summary>
' Constraint functions for the nonlinear programming problem.
' Constraints contain five variables. Ten different constraint
' objects may be constructed.
' </summary>
Class ConstraintFunctions
Inherits DoubleFunctional

' <summary>
' Coefficients for each of the ten different constraints.
' Each row of the matrix contains coefficients for the five
' variables.
' </summary>

' <summary>
' Constant values used in evaluating each of the ten different
' constraints.
' </summary>
Private Shared BData As DoubleVector

' <summary>
' References the variable coefficients for this constraint.
' </summary>
Private ARow As DoubleVector

' <summary>
' The constant value for this constraint.
' </summary>
Private BValue As Double

Shared Sub New()

Dim ADataArray(,) As Double = New Double(,) {{-16.0, 2.0, 0.0, 1.0, 0.0}, {0.0, -2.0, 0.0, 0.4, 2.0},
{-3.5, 0, 2, 0, 0},
{0, -2, 0, -4, -1},
{0, -9, -2, 1, -2.8},
{2, 0, -4, 0, 0},
{-1, -1, -1, -1, -1},
{-1, -2, -3, -2, -1},
{1, 2, 3, 4, 5},
{1, 1, 1, 1, 1}
}
BData = New DoubleVector(-40.0, -2.0, -0.25, -4, -4, -1, -40, -60, 5, 1)
End Sub

' <summary>
' Constructs an instance of one of the ten constraint functions.
' </summary>
' <param name="i">Constraint function number. Must be between
' 0 and 9 inclusive.</param>
' <exception cref="InvalidArgumentException">Thrown if the constraint
' number is less than zero or greater than 9.</exception>
Public Sub New(I As Integer)
MyBase.New(5)
If (I < 0 Or I > AData.Rows) Then
Dim Msg = String.Format("Invalid constraint number {0}. Must be between 0 and 9", I)
Throw New InvalidArgumentException(Msg)
End If
BValue = BData(I)
End Sub

' <summary>
' Evaluates the constraint function at the given point.
' </summary>
' <param name="x">Point to evaluate at.</param>
' <returns>Constraint value at the given point.</returns>
Public Overrides Function Evaluate(X As DoubleVector) As Double
Return NMathFunctions.Dot(ARow, X) - BValue
End Function

End Class
End Module
End Namespace

```
← All NMath Code Examples
Top