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

Coco - A full production planning example

Description
The Coco productional planning problem: multi-item, multi-period, multi-site production planning. A sequence of model versions show how the model was developed, to (a) use more sophisticated modeling features and (b) to extend the model, taking it from a simple linear model to one with fixed prices and logical decisions.
  1. xbcoco1: initial formulation, data, variables and constraints fixed
  2. xbcoco2: use parameters, data tables and subscripted variables.

    read data tables in from text data files (short-term planning).
  3. xbcoco3: like xbcoco2.c, but several time periods (mid-term planning).
  4. xbcoco : complete problem, data defined in the model definition (long-term planning).


Source Files

Data Files





xbcoco2.cs

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

  file xbcoco2.cs
  ````````````````
  Coco Problem Phase 2.
  Use parameters, data tables and subscripted variables
  to separate the model structure from the data.
  Read data tables in from text data files.

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

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


namespace Examples
{
    public class TestCoco1
    {
        const int NP = 2;            /* Number of products (p) */
        const int NF = 2;            /*           factories (f) */
        const int NR = 2;            /*           raw materials (r) */

        //Define XPRBDATAPATH to wherever you have placed the data folder; here we expect it to be same directory as compiled example.
        static string XPRBDATAPATH = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName + "/Data";
        static string REVFILE    = XPRBDATAPATH + "/coco/rev.dat";
        static string CMAKEFILE  = XPRBDATAPATH + "/coco/cmake.dat";
        static string CBUYFILE   = XPRBDATAPATH + "/coco/cbuy.dat";
        static string REQFILE    = XPRBDATAPATH + "/coco/req.dat";
        static string MXSELLFILE = XPRBDATAPATH + "/coco/maxsell.dat";
        static string MXMAKEFILE = XPRBDATAPATH + "/coco/mxmake.dat";

        /****TABLES****/
        double[] REV = new double[NP];         /* Unit selling price of product p */
        double[,] CMAK = new double[NP,NF];    /* Unit cost to make product p at factory f */
        double[] CBUY = new double[NR];        /* Unit cost to buy raw material r */
        double[,] REQ = new double[NP,NR];     /* Requirement by unit of prod. p for raw material r */
        double[] MXSELL = new double[NP];      /* Max. amount of p that can be sold */
        double[] MXMAKE = new double[NF];      /* Max. amount factory f can make over all products */
        double[,] PROFIT = new double[NP,NF];  /* Profit contribution of product p at factory f */

        XPRBprob pb = new XPRBprob("Coco2");   /* Initialize a new problem in BCL */

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

        void modCoco2()
        {
            XPRBvar[,] make = new XPRBvar[NP,NF];
            XPRBexpr lobj, lc;
            int p,f;

            /****VARIABLES****/
            for(p=0;p<NP;p++)         /* Amount of prod. p to make at factory f */
                for(f=0;f<NF;f++)
                    make[p,f] = pb.newVar("make_p" + (p+1) + "f" + (f+1));

            /****OBJECTIVE****/
            lobj = new XPRBexpr();
            for(p=0;p<NP;p++)         /* Objective: maximize total profit */
                for(f=0;f<NF;f++)  lobj += PROFIT[p,f]* make[p,f];
            pb.setObj(pb.newCtr("OBJ",lobj));

            /****CONSTRAINTS****/

            for(p=0;p<NP;p++)         /* Limit on the amount of product p to be sold */
            {
                lc = new XPRBexpr(0);
                for(f=0;f<NF;f++)  lc += make[p,f];
                pb.newCtr("MxSell", lc <= MXSELL[p]);
            }

            for(f=0;f<NF;f++)         /* Capacity limit at factory f */
            {
                lc = new XPRBexpr(0);
                for(p=0;p<NP;p++) lc += make[p,f];
                pb.newCtr("MxMake", lc <= MXMAKE[f]);
            }

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

            for(p=0;p<NP;p++)         /* Print the solution values */
                for(f=0;f<NF;f++)
                    System.Console.Write(make[p,f].getName() + ":" + make[p,f].getSol() + " ");
            System.Console.WriteLine();
        }

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

        /**** Read data from files ****/
        void readData()
        {
            FileStream file;
            StreamReader fileStreamIn;
            int p,f,r;

            /* Read the revenue data file */
            /* Read the demand data file */
            file = new FileStream(REVFILE, FileMode.Open, FileAccess.Read);
            fileStreamIn = new StreamReader(file);
            pb.XPRBreadarrline(fileStreamIn, 200, "{g} , ", out REV, NP);
            fileStreamIn.Close();
            file.Close();

            /* Read the production cost data file */
            file = new FileStream(CMAKEFILE, FileMode.Open, FileAccess.Read);
            fileStreamIn = new StreamReader(file);
            for (p = 0; p < NP; p++)
            {
                double[] temp = new double[NF];
                pb.XPRBreadarrline(fileStreamIn, 200, "{g} , ", out temp, NF);
                for(int q=0; q<NF; q++)
                    CMAK[p,q] = temp[q];

            }
            fileStreamIn.Close();
            file.Close();

            /* Read the raw material cost data file */
            file = new FileStream(CBUYFILE, FileMode.Open, FileAccess.Read);
            fileStreamIn = new StreamReader(file);
            pb.XPRBreadarrline(fileStreamIn, 200, "{g} , ", out CBUY, NR);
            fileStreamIn.Close();
            file.Close();

            /* Read the resource requirement data file */
            file = new FileStream(REQFILE, FileMode.Open, FileAccess.Read);
            fileStreamIn = new StreamReader(file);
            for (p = 0; p < NP; p++)
            {
                double[] temp = new double[NF];
                pb.XPRBreadarrline(fileStreamIn, 200, "{g} , ", out temp, NF);
                for (int q = 0; q < NF; q++)
                    REQ[p, q] = temp[q];

            }
            fileStreamIn.Close();
            file.Close();

            /* Read the max. sales quantities data file */
            file = new FileStream(MXSELLFILE, FileMode.Open, FileAccess.Read);
            fileStreamIn = new StreamReader(file);
            pb.XPRBreadarrline(fileStreamIn, 200, "{g} , ", out MXSELL, NP);
            fileStreamIn.Close();
            file.Close();

            /* Read the production capacities data file */
            file = new FileStream(MXMAKEFILE, FileMode.Open, FileAccess.Read);
            fileStreamIn = new StreamReader(file);
            pb.XPRBreadarrline(fileStreamIn, 200, "{g} , ", out MXMAKE, NF);
            fileStreamIn.Close();
            file.Close();

            /* Calculate the table PROFIT */
            for(p=0;p<NP;p++)
                for(f=0;f<NF;f++)
                {
                    PROFIT[p,f] = REV[p] - CMAK[p,f];
                    for(r=0;r<NR;r++) PROFIT[p,f] -= (REQ[p,r]*CBUY[r]);
                }
        }

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

        public static void Main()
        {
            XPRB.init();
            TestCoco1 TestInstance = new TestCoco1();
            TestInstance.readData();            /* Data input from file */
            TestInstance.modCoco2();            /* Model and solve the problem */

            return;
        }

    }

}
Back to examples browserPrevious exampleNext example