Data Analysis Mathematics Linear Algebra Statistics
New Version 8.0! QuickStart Samples

# Quadratic Programming QuickStart Sample (Visual Basic)

Illustrates how to solve optimization problems a quadratic objective function and linear constraints using classes in the Extreme.Mathematics.Optimization namespace in Visual Basic.

```Option Infer On

' The quadratic programming classes reside in their own namespace.
Imports Extreme.Mathematics.Optimization
Imports Extreme.Mathematics

Namespace Extreme.Numerics.QuickStart.VB
' Illustrates solving quadratic programming problems
' using the classes in the Extreme.Mathematics.Optimization
' namespace of the Extreme Optimization Numerical Libraries for .NET.

Sub Main()
' This QuickStart illustrates the quadratic programming
' functionality by solving a portfolio optimization problem.

' Portfolio optimization is a common application of QP.
' For a collection of assets, the goal is to minimize
' the risk (variance of the return) while achieving
' a minimal return for a set maximum amount invested.

' The variables are the amounts invested in each asset.
' The quadratic term is the covariance matrix of the assets.
' THere is no linear term in this case.

' There are three ways to create a Quadratic Program.

' The first is in terms of matrices. The coefficients
' of the constraints and the quadratic terms are supplied
' as matrices. The cost vector, right-hand side and
' constraints on the variables are supplied as vectors.

' The linear term in the objective function:
Dim c = Vector.CreateConstant(4, 0.0)
' The quaratic term in the objective function:
Dim R = Matrix.CreateSymmetric(4,
New Double() _
{
0.08, -0.05, -0.05, -0.05,
-0.05, 0.16, -0.02, -0.02,
-0.05, -0.02, 0.35, 0.06,
-0.05, -0.02, 0.06, 0.35
}, MatrixTriangle.Upper, MatrixElementOrder.ColumnMajor)
' The coefficients of the constraints:
Dim A = Matrix.Create(2, 4, New Double() _
{ _
1, 1, 1, 1, _
-0.05, 0.2, -0.15, -0.3 _
}, MatrixElementOrder.RowMajor)
' The right-hand sides of the constraints:
Dim b = Vector.Create(10000.0, -1000.0)

' We're now ready to call the constructor.
' The last parameter specifies the number of equality
' constraints.
Dim qp1 As New QuadraticProgram(c, R, A, b, 0)

' Now we can call the Solve method to run the Revised
' Simplex algorithm:
Dim x = qp1.Solve()
Console.WriteLine("Solution: {0:F1}", x)
' The optimal value is returned by the Extremum property:
Console.WriteLine("Optimal value:   {0:F1}", qp1.OptimalValue)

' The second way to create a Quadratic Program is by constructing

' Next, we add two variables: we specify the name, the cost,
' and optionally the lower and upper bound.

' Next, we add constraints. Constraints also have a name.
' We also specify the coefficients of the variables,
' the lower bound and the upper bound.
Dim c1Values = Vector.Create(1.0, 1.0, 1.0, 1.0)
Dim c2Values = Vector.Create(0.05, -0.2, 0.15, 0.3)
' If a constraint is a simple equality or inequality constraint,
' you can supply a QuadraticProgramConstraintType value and the
' right-hand side of the constraint.

' Quadratic terms must be set individually.
' Each combination appears at most once.

' We can now solve the quadratic program:
x = qp2.Solve()
Console.WriteLine("Solution: {0:F1}", x)
Console.WriteLine("Optimal value:   {0:F1}", qp2.OptimalValue)

' Finally, we can create a quadratic program from an MPS file.
' The MPS format is a standard format.