Extreme Optimization > QuickStart Samples > Linear Programming QuickStart Sample (C#)

Extreme Optimization QuickStart Samples

Linear Programming QuickStart Sample (VB.NET)

Illustrates solving linear programming (LP) problems using classes in the Extreme.Mathematics.Optimization.LinearProgramming namespace in Visual Basic.

C# code Back to QuickStart Samples

' The linear programming classes reside in their own namespace.
Imports Extreme.Mathematics.Optimization.LinearProgramming
' Vectors and matrices are in the Extreme.Mathematics.LinearAlgebra
' namespace
Imports Extreme.Mathematics.LinearAlgebra

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

        Sub Main()
            ' This QuickStart illustrates the three ways to create a Linear Program.

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

            ' The cost vector:
            Dim c As Vector = New GeneralVector(-1.0, -3.0, 0.0, 0.0, 0.0, 0.0)
            ' The coefficients of the constraints:
            Dim A As Matrix = New GeneralMatrix(4, 6, New Double() _
            { _
                1, 1, 1, 0, 0, 0, _
                1, 1, 0, -1, 0, 0, _
                1, 0, 0, 0, 1, 0, _
                0, 1, 0, 0, 0, 1 _
            }, MatrixElementOrder.RowMajor)
            ' The right-hand sides of the constraints:
            Dim b As Vector = New GeneralVector(1.5, 0.5, 1.0, 1.0)

            ' We're now ready to call the constructor.
            ' The last parameter specifies the number of equality
            ' constraints.
            Dim lp1 As LinearProgram = New LinearProgram(c, A, b, 4)

            ' Now we can call the Solve method to run the Revised
            ' Simplex algorithm:
            Dim x As Vector = lp1.Solve()
            ' The GetDualSolution method returns the dual solution:
            Dim y As Vector = lp1.GetDualSolution()
            Console.WriteLine("Primal: {0:F1}", x)
            Console.WriteLine("Dual:   {0:F1}", y)
            ' The optimal value is returned by the Extremum property:
            Console.WriteLine("Optimal value:   {0:F1}", lp1.Extremum)

            ' The second way to create a Linear Program is by constructing
            ' it by hand. We start with an 'empty' linear program.
            Dim lp2 As LinearProgram = New LinearProgram()

            ' Next, we add two variables: we specify the name, the cost,
            ' and optionally the lower and upper bound.
            lp2.AddVariable("X1", -1.0, 0, 1)
            lp2.AddVariable("X2", -3.0, 0, 1)

            ' Next, we add constraints. Constraints also have a name.
            ' We also specify the coefficients of the variables,
            ' the lower bound and the upper bound.
            lp2.AddConstraint("C1", New GeneralVector(1.0, 1.0), 0.5, 1.5)
            ' If a constraint is a simple equality or inequality constraint,
            ' you can supply a LinearProgramConstraintType value and the
            ' right-hand side of the constraint.

            ' We can now solve the linear program:
            x = lp2.Solve()
            y = lp2.GetDualSolution()
            Console.WriteLine("Primal: {0:F1}", x)
            Console.WriteLine("Dual:   {0:F1}", y)
            Console.WriteLine("Optimal value:   {0:F1}", lp2.Extremum)

            ' Finally, we can create a linear program from an MPS file.
            ' The MPS format is a standard format.
            Dim lp3 As LinearProgram = MpsReader.Read("p:\numerics\sample.mps")
            ' We can go straight to solving the linear program:
            x = lp3.Solve()
            y = lp3.GetDualSolution()
            Console.WriteLine("Primal: {0:F1}", x)
            Console.WriteLine("Dual:   {0:F1}", y)
            Console.WriteLine("Optimal value:   {0:F1}", lp3.Extremum)

            Console.Write("Press Enter key to exit...")
            Console.ReadLine()
        End Sub

    End Module

End Namespace
Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
Information
Resources
Contact Us
Search

"The Extreme Optimization Statistics Library for .NET is a major boon for those doing statistical work in .NET. I strongly recommend this product."
- Marc Brooks

"I have made it my mission to institutionalize the value of good API design.  I strongly believe that this is key to making developers more productive and happy on our platform. It is clear that you value good API design in your work, and take to heart developer productivity and synergy with the .NET framework."
- Brad Abrams,
Lead Program Manager, Microsoft.

This is a partial list of companies who are using our libraries:
ABB Robotics
Allstate
Applied Materials
Arcam
Astra Schedule
Babson College
Canadian Council on Learning
Canyon Associates
Caxton Associates
CECity
Constellation Energy
CreditSights
DeepOcean
Duke University
Dynamotive
Elecsoft
Engelhard Corporation
Epcor
Equipoise Software
Galileo International
GAM UK
Gammex
GlaxoSmithKline
Global Matrix
The Hartford
Infinera Corporation
Intel
JDS Uniphase
LaBranche & Co.
Learning & Skills Council
Jacobs Consultancy
Litman Gregory
Lucas Systems
Malvern Instruments
Medrio
Merck & Co.
Mintera.
Monitor Software
MorningStar
NanoString Technologies
Paletta Invent
Parametric Portfolio Associates
Prosanos
RATA Associates
RiskShield
Ramboll
Standard & Poor's
Strategic Analysis Corporation
Univ. of Alicante
Univ. of South Carolina
vielife
Xerox
US Army