FICO
FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browserPrevious exampleNext example

Workshop - Displaying solution information

Description
A small planning problem. The example composes constraints, LP solves the problem and then prints the variables.

Model version 'xbworkrng' shows how to retrieve ranging information for variables and constraints and how to change the number-printing format.


Source Files





xbworkrng.cs

/********************************************************
  Xpress-BCL C# Example Problems
  ==============================

  file xbworkrng.cs
  `````````````````
  Workshop planning example.
  Test ranges and number printing format.

  (c) 2008 Fair Isaac Corporation
      authors: S.Heipcke, D.Brett.
********************************************************/

using System;
using System.Text;
using System.IO;
using BCL;


namespace Examples
{

    public class TestWorkshopRange
    {
        const int NProd = 2;                 /* Number of products */
        const int NShop = 3;                 /* Number of workshops */
        const int WMAX = 40;                 /* Maximum weekly working time */

        /****DATA****/
        int[,] DUR =  {{5, 9, 7},  /* Duration of product p on shop s */
        {10, 2, 5}};
        int[] RES   = {10, 8};           /* Man hours per unit */
        int[] PRICE = {108, 84};         /* Selling price per unit */

        /********************************************************************/

        public static void Main()
        {
            XPRB.init();
            int p,s;
            XPRBexpr l;
            
            /* Amount of product p */
            XPRBvar[] x = new XPRBvar[NProd];               
            
            /* Initialize a new problem in BCL */
            XPRBprob pb = new XPRBprob("Workshop");        
            
            XPRBctr[] c = new XPRBctr[NShop];
            TestWorkshopRange TestInstance = new TestWorkshopRange();

            /****VARIABLES****/
            for(p=0;p<NProd;p++)  x[p] = pb.newVar("x");

            /****OBJECTIVE****/
            l = new XPRBexpr();
            for(p=0;p<NProd;p++)
                l += (TestInstance.PRICE[p] - 5 * TestInstance.RES[p]) * x[p];
            
            /* Set obj. function: maximize benefit */ 
            pb.setObj(pb.newCtr("OBJ",l));  

            /****CONSTRAINTS****/
            /* Limit on weekly working hours */
            for(s=0;s<NShop;s++)            
            {
                l = new XPRBexpr(0);
                for (p = 0; p < NProd; p++) l += TestInstance.DUR[p, s] * x[p];
                c[s] = pb.newCtr("ResMax",l <= WMAX);
            }

            /****SOLVING + OUTPUT****/  
            pb.setRealFmt("%4.2e");
            for(p=0;p<NProd;p++)
            { x[p].print(); System.Console.Write("  "); }
            System.Console.WriteLine();

            /* Solve the LP-problem */
            pb.setSense(BCLconstant.XPRB_MAXIM);
            pb.lpOptimize();
            
            /* Get objective value */
            System.Console.WriteLine("Objective: " + pb.getObjVal());  

            pb.setRealFmt("%g, ");
            for(p=0;p<NProd;p++)            /* Print the solution values */
                x[p].print();
            System.Console.WriteLine();

            pb.setRealFmt("%8.4f");
            pb.print();
            
            /* Row ranges */ 
            System.Console.WriteLine("Ctr: Lower activity, Upper activity, " +
                              "Unit cost DN, Unit cost UP");
            for(s=0;s<NShop;s++)
            {
                System.Console.Write(c[s].getName() + ": " + 
                        c[s].getRNG(BCLconstant.XPRB_LOACT) + ", ");
                System.Console.Write(c[s].getRNG(BCLconstant.XPRB_UPACT) + 
                        ", " + c[s].getRNG(BCLconstant.XPRB_UDN));
                System.Console.WriteLine(", " + 
                        c[s].getRNG(BCLconstant.XPRB_UUP));
            }
            /* Column ranges */
            System.Console.WriteLine("Var: Lower activity, Upper activity, " +
                   "Unit cost DN, Unit cost UP, Lower profit, Upper profit");
            for(p=0;p<NProd;p++)
            {
                System.Console.Write(x[p].getName() + ": " + 
                        x[p].getRNG(BCLconstant.XPRB_LOACT) + ", ");
                System.Console.Write(x[p].getRNG(BCLconstant.XPRB_UPACT) + ", "
                        + x[p].getRNG(BCLconstant.XPRB_UDN) + ", ");
                System.Console.Write(x[p].getRNG(BCLconstant.XPRB_UUP) + ", " +
                        x[p].getRNG(BCLconstant.XPRB_LCOST) + ", ");
                System.Console.WriteLine(x[p].getRNG(BCLconstant.XPRB_UCOST));
            }
            return;
        } 
    }
}
Back to examples browserPrevious exampleNext example