Example 3: Linear Programming Maximize Example

Maximize the linear programming problem

{\rm {max}} \,\, f(x) = 10x_1 + 15x_2 + 15x_3 + 13x_4 + 9x_5

subject to the following set of restrictions:

100x_1 + 50x_2 + 50x_3 + 40x_4 + 120x_5 \le 300
40x_1 + 50x_2 + 50x_3 + 15x_4 + 30x_5 \le 40
0 \le x_1 \le 1.0; 0 \le x_2 \le 1.0; 0 \le x_3 \le 1.0; 0 \le x_4 \le 1.0; 0 \le x_5 \le 1.0

Since DenseLP minimizes , the sign of the objective function must be changed to compute this solution. The signs of the dual solution components and the optimal value must also be changed. Because x_2 and x_3 are not uniquely determined within the bounds, this problem has a convex family of solutions. DenseLP issues an exception, MultipleSolutionsException . A particular solution is available and retrieved in the finally block.

using System;
using Imsl.Math;

public class DenseLPEx3
{

    public static void  Main(System.String[] args)
    {
        
        int[] constraintType = new int[]{1, 1}; /* Ax <= b */
        double[] lowerVariableBound = 
            new double[]{0.0, 0.0, 0.0, 0.0, 0.0};
        double[] upperVariableBound = 
            new double[]{1.0, 1.0, 1.0, 1.0, 1.0};
        
        
        double[,] A = new double[,]{
            {100.0, 50.0, 50.0, 40.0, 120.0}, 
            {40.0, 50.0, 50.0, 15.0, 30.0}
        };
        /* constraint type Ax <= b */
        double[] b = new double[]{300.0, 40.0}; 
        double[] c = new double[]{10.0, 15.0, 15.0, 13.0, 9.0};
        
        /*  Since DenseLP minimizes, change signs of the
            objective coefficients. */
        double[] negC = new double[c.Length];
        for (int i = 0; i < c.Length; i++)
            negC[i] = - c[i];
        
        DenseLP zf = new DenseLP(A, b, negC);
        zf.SetLowerBound(lowerVariableBound);
        zf.SetConstraintType(constraintType);
        zf.SetUpperBound(upperVariableBound);
        
        
        try
        {
            zf.Solve();
        }
        catch (MultipleSolutionsException e)
        {
            /* x_2 and x_3 are not uniquely determined, expect multiple 
             * solutions. Catch the exception, but continue to print 
             * result found. */
            System.Console.Out.WriteLine(e.Message);
        }
        finally
        {
            
            double[] dSolution = zf.GetDualSolution();
            /* Change the sign of the dual solution and optimal value 
             * since DenseLP minimizes. */
            for (int i = 0; i < dSolution.Length; i++)
                dSolution[i] = - dSolution[i];
            double optimalValue = -zf.ObjectiveValue;
            
            new PrintMatrix("Solution").Print(zf.GetSolution());
            new PrintMatrix("Dual Solution").Print(dSolution);
            System.Console.Out.WriteLine(
                "Optimal Value = " + optimalValue);
        }
    }
}

Output

Multiple solutions giving essentially the same minimum exist.
       Solution
           0          
0  0                  
1  0.184987694831829  
2  0.315012305168171  
3  1                  
4  0                  

Dual Solution
    0   
0  0    
1  0.3  

Optimal Value = 20.5

Link to C# source.