Example 2: Linear Programming

The linear programming problem

{\rm {min}} \,\, f(x) = -x_1 - 3x_2

subject to:

0.5 \le x_1 + x_2 \le 1.5
0 \le x_1 \le 1.0
0 \le x_2 \le 1.0

is solved.

import com.imsl.math.*;

public class LinearProgrammingEx2 {
    public static void main(String args[]) throws Exception {
        int[] constraintType = {3};
        double[] upperBound = {1.0, 1.0};
        double[][] a = {{1.0, 1.0}};
        double[] b = {0.5};
        double[] upperLimit = {1.5};
        double[] c = {-1.0, -3.0};
        
        LinearProgramming zf = new LinearProgramming(a, b, c);
        
        zf.setUpperLimit(upperLimit);
        zf.setConstraintType(constraintType);
        zf.setUpperBound(upperBound);
        zf.solve();
        new PrintMatrix("Solution").print(zf.getPrimalSolution());
        new PrintMatrix("Dual Solution").print(zf.getDualSolution());
        System.out.println("Optimal Value = " + zf.getOptimalValue());
    }
}

Output

Solution
    0   
0  0.5  
1  1    

Dual Solution
   0   
0  -1  

Optimal Value = -3.5
Link to Java source.