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.

using System;
using Imsl.Math;

public class MinConGenLinEx1 : MinConGenLin.IFunction
{
    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];
    }

    public static void  Main(String[] args)
    {
        int neq = 2;
        int ncon = 2;
        int nvar = 5;
        double[] a = new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 
                                  0.0, 0.0, 1.0, - 2.0, - 2.0};
        double[] b = new double[]{5.0, - 3.0};
        double[] xlb = new double[]{0.0, 0.0, 0.0, 0.0, 0.0};
        double[] xub = new double[]{10.0, 10.0, 10.0, 10.0, 10.0};
        
        MinConGenLin.IFunction fcn = new MinConGenLinEx1();
        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 C# source.