Example: Reading an MPS file
This example reads the data for a linear programming problem from an MPS file.
using System;
using System.IO;
using Imsl.Math;
public class MPSReaderEx1
{
public static void Main(String[] args)
{
FileStream aFile = File.OpenRead("testprob.mps");
StreamReader sr = new StreamReader(aFile);
MPSReader mps = new MPSReader();
mps.Read(sr);
Console.Out.WriteLine(mps.Name);
Console.Out.WriteLine(mps.NameRHS);
Console.Out.WriteLine(mps.NameBounds);
Console.Out.WriteLine(mps.NameRanges);
int nRows = mps.NumberOfRows;
System.Console.Out.WriteLine("NumberOfConstraints " + nRows);
for (int i = 0; i < nRows; i++)
{
System.Console.Out.WriteLine(" " + mps.GetLowerRange(i) +
" <= row[" + i + "] = " + mps.GetNameRow(i) +
" <= " + mps.GetUpperRange(i));
}
int nColumns = mps.NumberOfColumns;
System.Console.Out.WriteLine("NumberOfColumns " + nColumns);
for (int i = 0; i < nColumns; i++)
{
System.Console.Out.WriteLine(" " + mps.GetLowerBound(i) +
" <= var[" + i + "] = " + mps.GetNameColumn(i) +
" <= " + mps.GetUpperBound(i));
}
System.Console.Out.WriteLine("NumberOfNonZeros " + mps.NumberOfNonZeros);
for (int iRow = 0; iRow < nRows; iRow++)
{
System.Console.Out.WriteLine(" row " + mps.GetNameRow(iRow));
System.Collections.IEnumerator iter = mps.GetRow(iRow).Iterator();
while (iter.MoveNext())
{
MPSReader.Element elem = (MPSReader.Element) iter.Current;
int iColumn = elem.Column;
System.String nameColumn = mps.GetNameColumn(iColumn);
System.Console.Out.WriteLine(" " +
nameColumn + ": " + elem.Value);
}
}
}
}
Output
TESTPROB
RHS1
BND1
NumberOfConstraints 3
-Infinity <= row[0] = LIM1 <= 5
10 <= row[1] = LIM2 <= Infinity
7 <= row[2] = MYEQN <= 7
NumberOfColumns 3
0 <= var[0] = XONE <= 4
-1 <= var[1] = YTWO <= 1
0 <= var[2] = ZTHREE <= Infinity
NumberOfNonZeros 6
row LIM1
XONE: 1
YTWO: 1
row LIM2
XONE: 1
ZTHREE: 1
row MYEQN
YTWO: -1
ZTHREE: 1
Link to C# source.