Example 2: Linear Constrained Optimization

The problem

{\rm {min}} \,\, f(x) = -x_0x_1x_2

subject to

-x_0 - 2x_1 -2x_2 \le 0

x_0 + 2x_1 + 2x_2 \le 72

0 \le x_0 \le 20

0 \le x_1 \le 11

0 \le x_2 \le 42

is solved with an initial guess of x_0 = 10, x_1 = 10 and x_2 = 10.

using System;
using Imsl.Math;

public class MinConGenLinEx2 : MinConGenLin.IGradient
{
    public double F(double[] x)
    {
        return - x[0] * x[1] * x[2];
    }
    
    public void Gradient(double[] x, double[] g)
    {
        g[0] = - x[1] * x[2];
        g[1] = - x[0] * x[2];
        g[2] = - x[0] * x[1];
    }
    
    public static void  Main(String[] args)
    {
        int neq = 0;
        int ncon = 2;
        int nvar = 3;
        double[] a = new double[]{- 1.0, - 2.0, - 2.0, 1.0, 2.0, 2.0};
        double[] xlb = new double[]{0.0, 0.0, 0.0};
        double[] xub = new double[]{20.0, 11.0, 42.0};
        double[] b = new double[]{0.0, 72.0};
        
        MinConGenLin.IGradient fcn = new MinConGenLinEx2();
        MinConGenLin zf = new MinConGenLin(fcn, nvar, ncon, neq, a, b,
            xlb, xub);
        zf.SetGuess(new double[]{10.0, 10.0, 10.0});
        zf.Solve();
        new PrintMatrix("Solution").Print(zf.GetSolution());
        Console.Out.WriteLine("Objective value = " + 
            zf.ObjectiveValue);
    }
}

Output

Solution
Link to C# source.