Example 1: Sparse Linear Programming

The linear programming problem

\begin{array}{llr} \min{f(x) = 2x_1 - 8x_2 + 3x_3}&\text{subject to}& x_1 + 3x_2 \leq 3\\ && 2x_2 + 3x_3 \leq 6 \\ && x_1 + x_2 +x_3 \geq 2 \\ && -1 \leq x_1 \leq 5 \\ && 0 \leq x_2 \leq 7 \\ && 0 \leq x_3 \leq 9 \end{array}

is solved.


import com.imsl.math.*;

public class SparseLPEx1 {

    public static void main(String args[]) throws Exception {
        double[] b = {3.0, 6.0, 2.0};
        double[] c = {2.0, -8.0, 3.0};
        double[] xlb = {-1.0, 0.0, 0.0};
        double[] xub = {5.0, 7.0, 9.0};
        int[] irtype = {1, 1, 2};

        SparseMatrix a = new SparseMatrix(3, 3);
        a.set(0, 0, 1.0);
        a.set(0, 1, 3.0);
        a.set(1, 1, 2.0);
        a.set(1, 2, 3.0);
        a.set(2, 0, 1.0);
        a.set(2, 1, 1.0);
        a.set(2, 2, 1.0);

        // Solve problem.
        SparseLP lp = new SparseLP(a, b, c);
        lp.setLowerBound(xlb);
        lp.setUpperBound(xub);
        lp.setConstraintType(irtype);
        lp.solve();

        // Print solution.
        PrintMatrixFormat mf = new PrintMatrixFormat();
        mf.setNoColumnLabels();
        new PrintMatrix("Solution").print(mf, lp.getSolution());

        System.out.printf("Objective:  %.2f\n", lp.getOptimalValue());
    }
}

Output

 Solution
           
0  -0.375  
1   1.125  
2   1.25   

Objective:  -6.00
Link to Java source.