package com.imsl.test.example.math; import com.imsl.math.*; /**
Solves a linear programming problem with sparse representation.
* * $$\min{f(x) = 2x_1 - 8x_2 + 3x_3}$$ * *subject to
* * $$\begin{array}{rlr} * 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.
* * @see Code * @see Output */ 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()); } }