package com.imsl.test.example.math;
import com.imsl.io.*;
import com.imsl.math.*;
import java.io.*;
import java.text.*;
/**
*
* SparseLP Example 2: Solves a linear programming problem defined in an MPS
* file.
*
*
* This example demonstrates how the class MPSReader
can be used
* with SparseLP
to solve a linear programming problem defined in
* an MPS file. The MPS file used in this example is an uncompressed version of
* the file 'afiro', available from
* http://www.netlib.org/lp/data/.
*
*
* @see Code
* @see Output
*/
public class SparseLPEx2 {
public static void main(String args[]) throws Exception {
//Read the problem from the file.
try (InputStream stream = SparseLPEx2.class.getResourceAsStream("afiro");
Reader reader = new InputStreamReader(stream)) {
MPSReader mps = new MPSReader();
mps.read(reader);
// Solve problem.
SparseLP lp = new SparseLP(mps);
lp.setPresolve(6);
lp.solve();
// Print solution.
System.out.println("Problem Name: " + mps.getName());
System.out.printf("Objective: %.2f\n", lp.getOptimalValue());
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(1);
nf.setMinimumFractionDigits(1);
PrintMatrixFormat mf = new PrintMatrixFormat();
mf.setNumberFormat(nf);
mf.setNoColumnLabels();
new PrintMatrix("Solution:").print(mf, lp.getSolution());
// Close all resources.
}
}
}