Imports System Imports CenterSpace.NMath.Core Namespace CenterSpace.NMath.Examples.VisualBasic <summary> A .NET example in Visual Basic showing how to use ConstrainedLeastSquares class to solve the constrained least squares problem Cx = d, subject to the constraints Ax < b </summary> Public Module ConstrainedLeastSquaresExample Sub Main() Solve, in the least squares sense, Cx = d, subject to Ax <= b and -0.1 <= x[i] <= 2.0 Dim C As New DoubleMatrix("5x4 [0.9501 0.7620 0.6153 0.4057 " & "0.2311 0.4564 0.7919 0.9354 " & "0.6068 0.0185 0.9218 0.9169 " & "0.4859 0.8214 0.7382 0.4102 " & "0.8912 0.4447 0.1762 0.8936]") Dim D As New DoubleVector(0.0578, 0.3528, 0.8131, 0.0098, 0.1388) Constraint coefficient matrix Dim A As New DoubleMatrix("3x4[0.2027 0.2721 0.7467 0.4659 " & "0.1987 0.1988 0.4450 0.4186 " & "0.6037 0.0152 0.9318 0.8462]") Constraints right hand sides. Dim B As New DoubleVector(0.5251, 0.2026, 0.6721) Create the constrained least squares problem for minimizing || Cx - d||^2 subject to Ax <= b and -0.1 <= x[i] <= 2.0 We first construct the problem object from the matrix C and the vector d, we then add the constraints. Dim Problem = New ConstrainedLeastSquaresProblem(C, D) Add the inequality constraints Ax <= b using a constraint tolerance of 0.00001. This allows for small violations of the constraints. Specifically the constraints will be considered satisfied for a vector x if Ax <= b + 0.00001 Dim ConstraintTolerance As Double = 0.00001 Dim I As Integer For I = 0 To A.Rows - 1 Problem.AddUpperBoundConstraint(A.Row(I), B(I), ConstraintTolerance) Next All variable values for the solution must satisfy the bounds -0.1 <= x[i] <= 2.0 Dim LB As New DoubleVector(Problem.NumVariables, -0.1) Dim UB As New DoubleVector(Problem.NumVariables, 2.0) For I = 0 To Problem.NumVariables - 1 Problem.AddBounds(I, LB(I), UB(I), 0.00001) Next Create the solver instance. Dim Solver As New ConstrainedLeastSquares() The ConstrainedLeastSquares solver uses a QP (Quadratic Programming) solver to solve the constrained least squares problem. The current default QP solver is the NMath active set quadratic programming solver with default options. Dim Success As Boolean = Solver.Solve(Problem) Console.WriteLine("Default solver success = " & Success) Console.WriteLine("Default solver solution x = " & Solver.X.ToString("0.0000")) Console.WriteLine("Default solver residual norm = " & Solver.ResidualNorm.ToString("0.0000")) Console.WriteLine("Default solver performed {0} iterations", solver.Iterations) You can pass in an instance of a quadratic programming solver for the constrained least squares class to use. This allows you to set option on the QP solver and inspect results of the QP solver. Dim InteriorPointQP As New InteriorPointQPSolver() Dim SolverParams As New InteriorPointQPSolverParams() SolverParams.MaxIterations = 10000 SolverParams.PresolveLevel = InteriorPointQPSolverParams.PresolveLevelOption.None Solver.Solve(Problem, InteriorPointQP, SolverParams) Console.WriteLine() Console.WriteLine("Interior point solver success = " & Success) Console.WriteLine("Interior point QP result = " & InteriorPointQP.Result) Console.WriteLine("Interior point QP solver iteration count = " & Solver.Iterations) Console.WriteLine("Interior point solver solution x = " & Solver.X.ToString("0.0000")) Console.WriteLine("Interior point solver residual norm = " & Solver.ResidualNorm.ToString("0.0000")) If you use the active set QP solver you can determine which constraints are active in the solution by accessing the active set QP solvers Lagrange multiplier property. A constraint is active if its corresponding Lagrange multiplier is nonzero. Dim ActiveSetQP As New ActiveSetQPSolver() Success = Solver.Solve(Problem, ActiveSetQP) Console.WriteLine() Console.WriteLine("Active set solver success = " & Success) Console.WriteLine("Active set solver solution x = " & Solver.X.ToString("0.0000")) Console.WriteLine("Active set solver residual norm = " & Solver.ResidualNorm.ToString("0.0000")) Print out the active constraints. For I = 0 To ActiveSetQP.LagrangeMultiplier.Length - 1 If (ActiveSetQP.LagrangeMultiplier(I) <> 0.0) Then Console.WriteLine("Constraint {0} = {1} is active", I, Problem.Constraints(I).ToString()) End If Next Console.WriteLine() Console.WriteLine("Press Enter Key") Console.Read() End Sub End Module End Namespace← All NMath Code Examples