Example 1: Linear Constrained Optimization

The problem

{\rm {min}} \,\, f(x) = x_1^2 + x_2^2 + x_3^2 + x_4^2 + x_5^2 - 2x_2x_3 - 2x_4x_5 - 2x_1

subject to

x_1 + x_2 + x_3 + x_4 + x_5 = 5

x_3 - 2x_4 - 2x_5 = -3

0 \le x \le 10

is solved.


import com.imsl.math.*;

public class MinConGenLinEx1 {

    public static void main(String args[]) throws Exception {
        int neq = 2;
        int ncon = 2;
        int nvar = 5;
        double a[] = {1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, -2.0, -2.0};
        double b[] = {5.0, -3.0};
        double xlb[] = {0.0, 0.0, 0.0, 0.0, 0.0};
        double xub[] = {10.0, 10.0, 10.0, 10.0, 10.0};

        MinConGenLin.Function fcn = new MinConGenLin.Function() {
            public double f(double[] x) {
                return x[0] * x[0] + x[1] * x[1] + x[2] * x[2] + x[3] * x[3]
                        + x[4] * x[4] - 2.0 * x[1] * x[2] - 2.0 * x[3] * x[4]
                        - 2.0 * x[0];
            }
        };

        MinConGenLin zf
                = new MinConGenLin(fcn, nvar, ncon, neq, a, b, xlb, xub);

        zf.solve();
        new PrintMatrix("Solution").print(zf.getSolution());
    }
}

Output

Solution
   0  
0  1  
1  1  
2  1  
3  1  
4  1  

Link to Java source.