# VB Simple Nonlinear Programming Example

← All NMath Code Examples

```ï»¿Imports System

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

Namespace CenterSpace.NMath.Analysis.Examples.VisualBasic

''' A .NET example in Visual Basic
Module NonlinearProgrammingExample

' Example illustrating using the Module ActiveSetLineSearchSQP to solve a
' NonLinear Programming (NLP) problem with linear constraints and variable
' bounds.
Sub Main()

' min -x0*x1*x2
' 0 <= x0 + 2*x1 + 2*x2 <= 72,
' 0 <= x0, x1, x2 <= 42

' Dimensionality of the domain of the objective function.
Dim XDim As Integer = 3
Dim Objective As Func(Of DoubleVector, Double) = AddressOf Temp
Dim Problem As New NonlinearProgrammingProblem(XDim, Objective)

For I = 0 To XDim - 1
' Add a lower bound of 0 and upper bound of 42 for the ith variable.
Next

' Add the constraint 0 <= x0 + 2*x1 + 2*x2 <= 72. Note that this
' is a linear constraint.
Problem.AddLinearConstraint(New DoubleVector(1.0, 2.0, 2.0), 0.0, 72)

' Pick the point (1, 1, 1) as initial solution guess.
Dim x0 As New DoubleVector(3, 1.0)

' Pick a tolerance for convergence. The iteration will stop when
' either the magnitude of the predicted function value change or the
' magnitude of  the step direction is less than the specified tolerance.
Dim tolerance As Double = 0.0001
Dim solver As New ActiveSetLineSearchSQP(tolerance)
Dim Success As Boolean = solver.Solve(Problem, x0)

Console.WriteLine()

Console.WriteLine("Termination status = " & solver.SolverTerminationStatus.ToString())
Console.WriteLine("X = " & solver.OptimalX.ToString())
Console.WriteLine("f(x) = " & solver.OptimalObjectiveFunctionValue)
Console.WriteLine("Iterations = " & solver.Iterations)

' Seems like it took quite a few iterations to converge. Note that the
' solver computes a step direction and a step size at each iteration.
' The step direction is computed by forming a quadratic programming
' problem with linear constraints at each iteration whose solution
' yields the step direction. The size of the step taken in this
' direction is then chosen to decrease the function value and not
' violate constraints. Since our constraints for this problem are linear
' to begin with, we may try taking a larger step in order to converge
' faster. We'll use the ConstantSQPStepSize Module with a constant step
' size of one and see what happens.
solver.SolverOptions.StepSizeCalculator = New ConstantSQPStepSize(1)
Success = solver.Solve(Problem, x0)
Console.WriteLine()
Console.WriteLine("Using a constant step size of 1:")
Console.WriteLine("Termination status = " & solver.SolverTerminationStatus.ToString())
Console.WriteLine("X = " & solver.OptimalX.ToString())
Console.WriteLine("f(x) = " & solver.OptimalObjectiveFunctionValue)
Console.WriteLine("Iterations = " & solver.Iterations)

' As you can see the algorithm converges must faster with the constant
' step size of one. In general if your constraints are all linear, or
' very nearly linear, the constant step size calculator may yield
' faster convergence.

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

End Sub

Function Temp(ByVal x As DoubleVector) As Double
Return -x(0) * x(1) * x(2)
End Function

End Module
End Namespace

```
← All NMath Code Examples
Top