VB Interior Point QP Example

← All NMath Code Examples

 

Imports System

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

Namespace CenterSpace.NMath.Analysis.Examples.VisualBasic

  ''' <summary>
  ''' A .NET example in Visual Basic showing how to use the interior point quadratic
  ''' programming solver <c>InteriorPointQPSolver</c> to solve a quadratic
  ''' programming problem.
  ''' </summary>
  Module InteriorPointQPExample

    Sub Main()

      ' Use the InteriorPointQPSolver class to solve a quadratic programming 
      ' problem of the form
      ' 0.5*x'Hx + x'c 
      ' Our problem will have 4 variables and eight constraints. In addition
      ' the variables will be required to be nonnegative.

      ' Construct the 4x4 H matrix and c vector for the problem.
      Dim HMatrixData As Double(,) = New Double(,) {{3.0625, 0.0, 0.0, 0.0},
                                        {0.0, 0.0405, 0.0, 0.0},
                                        {0.0, 0.0, 0.0271, -0.0031},
                                        {0.0, 0.0, -0.0031, 0.0054}}
      Dim H As New DoubleMatrix(HMatrixData)
      Dim C As New DoubleVector(-2671.0, -135.0, -103.0, -19.0)

      ' Construct the quadratic programming problem from H and C.
      Dim Problem As New QuadraticProgrammingProblem(H, C)

      ' Our problem will have 8 linear constraints and will be of the form
      ' Cx <= b
      ' Where C is an 8x4 matrix and b is a vector of length 8.

      ' Construct the C matrix and b vector.
      Dim CMatrixData As Double(,) = New Double(,) {
        {-0.0401, -0.0162, -0.0039, 0.0002},
        {-0.1326, -0.0004, -0.0034, 0.0006},
        {1.5413, 0.0, 0.0, 0.0},
        {0.0, 0.0203, 0.0, 0.0},
        {0.0, 0.0, 0.0136, -0.0075},
        {0.0, 0.0, -0.0016, 0.0027},
        {0.016, 0.0004, 0.0005, 0.0002}
      }
      Dim CMatrix As New DoubleMatrix(CMatrixData)
      Dim B As New DoubleVector(-92.6, -29.0, 2671.0, 135.0, 103.0, 19.0, 10.0)

      ' Now, add the constraints to the problem.
      Dim I As Integer
      For I = 0 To CMatrix.Rows - 1
        Problem.AddUpperBoundConstraint(CMatrix.Row(I), B(I))
      Next

      ' Add variable bounds. Variable values are required to be >= 0.
      For I = 0 To 3
        Problem.AddLowerBound(I, 0.0)
      Next

      ' Construct the solver.
      Dim Solver As New InteriorPointQPSolver()

      ' Set parameters for the solve.
      Dim SolverParams As New InteriorPointQPSolverParams
      SolverParams.KktForm = InteriorPointQPSolverParams.KktFormOption.Blended
      SolverParams.Tolerance = 0.000001
      SolverParams.MaxDenseColumnRatio = 0.9
      SolverParams.PresolveLevel = InteriorPointQPSolverParams.PresolveLevelOption.Full
      SolverParams.SymbolicOrdering = InteriorPointQPSolverParams.SymbolicOrderingOption.ApproximateMinDegree

      Solver.Solve(Problem, SolverParams)
      Console.WriteLine("\nSolver Parameters:")
      Console.WriteLine(SolverParams.ToString())
      Console.WriteLine("\nResult = " & Solver.Result)
      Console.WriteLine("Optimal x = " & NMathFunctions.Round(Solver.OptimalX, 0).ToString())
      Console.WriteLine("Optimal Function value = " & Solver.OptimalObjectiveFunctionValue.ToString("F3"))
      Console.WriteLine("iterations = " & Solver.IterationCount)

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

    End Sub
  End Module
End Namespace


← All NMath Code Examples
Top