Data Analysis Mathematics Linear Algebra Statistics
New Version 8.1!

Supports .NET 6.0. Try it for free with our fully functional 60-day trial version. QuickStart Samples

# Linear Programming QuickStart Sample (IronPython)

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

```import numerics

# The linear programming classes reside in a namespace with
# other optimization-related classes.
from Extreme.Mathematics.Optimization import *
# Vectors and matrices are in the Extreme.Mathematics.LinearAlgebra
# namespace
from Extreme.Mathematics import *

#/ Illustrates solving linear programming problems
#/ using the classes in the Extreme.Mathematics.Optimization
#/ namespace of the Extreme Optimization Numerical Libraries for .NET.

# This QuickStart Sample 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:
c = Vector([ -1.0, -3.0, 0.0, 0.0, 0.0, 0.0 ])
# The coefficients of the constraints:
A = Matrix([[1,1,1,0,0,0], [1,1,0,-1,0,0], [1,0,0,0,1,0], [0,1,0,0,0,1 ]])
# The right-hand sides of the constraints:
b = Vector([ 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.
lp1 = LinearProgram(c, A, b, 4)

# Now we can call the Solve method to run the Revised
# Simplex algorithm:
x = lp1.Solve()
# The GetDualSolution method returns the dual solution:
y = lp1.GetDualSolution()
print "Primal: {0:.1f}".format(x)
print "Dual:   {0:.1f}".format(y)
# The optimal value is returned by the Extremum property:
print "Optimal value:   {0:.1f}".format(lp1.OptimalValue)

# The second way to create a Linear Program is by constructing
lp2 = LinearProgram()

# 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.
# 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()
print "Primal: {0:.1f}".format(x)
print "Dual:   {0:.1f}".format(y)
print "Optimal value:   {0:.1f}".format(lp2.OptimalValue)

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