Example 2: Solve 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/
.
import com.imsl.io.*;
import com.imsl.math.*;
import java.io.*;
import java.text.*;
public class SparseLPEx2 {
public static void main(String args[]) throws Exception {
// Read from MPS file.
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.
reader.close();
stream.close();
}
}
Output
Problem Name: AFIRO
Objective: -464.75
Solution:
0 80.0
1 25.5
2 54.5
3 84.8
4 65.4
5 0.0
6 0.0
7 0.0
8 0.0
9 0.0
10 0.0
11 0.0
12 18.2
13 47.2
14 69.4
15 500.0
16 475.9
17 24.1
18 0.0
19 215.0
20 141.7
21 0.0
22 0.0
23 0.0
24 0.0
25 0.0
26 0.0
27 0.0
28 339.9
29 242.3
30 60.9
31 0.0
Link to Java source.