Imports System Imports CenterSpace.NMath.Core Namespace CenterSpace.NMath.Examples.VisualBasic A .NET example in Visual Basic Module NonlinearProgrammingExample Sub Main() Dim Objective As DoubleFunctional = New ObjectiveFunction() Dim Problem As New NonlinearProgrammingProblem(Objective) Dimensionality of the objective and constraint function domains Dim xDimension As Integer = 2 Add constraint x0*x1 - x0 -x1 <= -1.5 Problem.AddUpperBoundConstraint(xDimension, AddressOf Constraint1, -1.5) Add constraint x0*x1 >= -10 Problem.AddLowerBoundConstraint(xDimension, AddressOf Constraint2, -10.0) Set some options on the solver by creating an Options object and setting some properties, then use it to construct the solver instance. Dim solveroptions As New ActiveSetLineSearchSQP.Options() solveroptions.StepDirectionTolerance = 0.00000001 solveroptions.FunctionChangeTolerance = 0.000001 Since our constraints are nearly linear (one quadratic term) well use the simpler constant step size. solveroptions.StepSizeCalculator = New ConstantSQPStepSize(1) Create the solver with the above options and use it to solve the problem. Dim Solver As New ActiveSetLineSearchSQP(solveroptions) Initial solution guess Dim x0 As New DoubleVector(-1.0, 1.0) Dim success As Boolean = Solver.Solve(Problem, x0) Console.WriteLine() Console.WriteLine(If(success, "Solver was successful", "Solver encountered a problem.")) Console.WriteLine("Termination status = " & Solver.SolverTerminationStatus.ToString()) Console.WriteLine("X = " & Solver.OptimalX.ToString()) Console.WriteLine("f(x) = " & Solver.OptimalObjectiveFunctionValue) Console.WriteLine("Iterations = " & Solver.Iterations) Console.WriteLine() Console.WriteLine("Press Enter Key") Console.Read() End Sub Function Constraint1(ByVal X As DoubleVector) As Double Return X(0) * X(1) - X(0) - X(1) End Function Function Constraint2(ByVal x As DoubleVector) As Double Return x(0) * x(1) End Function End Module <summary> f(x) = exp(x0)*(4*x0^2 + 2*x1^2 + 4*x0*x1 + 2*x1 + 1) </summary> Class ObjectiveFunction Inherits DoubleFunctional <summary> Constructor. Must initialize the base Module with dimension of the domain, 2 in this case. </summary> Public Sub New() MyBase.New(2) End Sub <summary> Evaluate the objective function at a point x. </summary> <param name="x">The point at which to evaluate the function.</param> <returns>The value of the objective function.</returns> Public Overrides Function Evaluate(ByVal X As DoubleVector) As Double Dim x0 As Double = X(0) Dim x1 As Double = X(1) Return Math.Exp(x0) * (4 * x0 * x0 + 2 * x1 * x1 + 4 * x0 * x1 + 2 * x1 + 1) End Function <summary> Returns the gradient of the objective function at a point x. </summary> <param name="x">The point at which to evaluate the gradient.</param> <param name="grad">Vector into which to place the gradient values.</param> Public Overrides Sub Gradient(ByVal X As DoubleVector, ByVal grad As DoubleVector) Dim x0 As Double = X(0) Dim x1 As Double = X(1) Dim ex0 As Double = Math.Exp(x0) grad(0) = ex0 * (4 * x0 * x0 + 2 * x1 * x1 + 4 * x0 * x1 + 2 * x1 + 1) + ex0 * (8 * x0 + 4 * x1) grad(1) = ex0 * (4 * x0 + 4 * x1 + 2) End Sub End Class End Namespace← All NMath Code Examples