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
By clicking on a file name, a preview is opened at the bottom of this page.
xbcoco1.cxx[download]
xbcoco2.cxx[download]
xbcoco3.cxx[download]
xbcoco.cxx[download]

Data Files





xbcoco2.cxx

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

  file xbcoco2.cxx
  `````````````````
  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
      author: S.Heipcke, Jan. 2000, rev. Mar. 2011
********************************************************/

#include <iostream>
#include <cstdio>
#include "xprb_cpp.h"

using namespace std;
using namespace ::dashoptimization;

#define NP 2            /* Number of products (p) */
#define NF 2            /*           factories (f) */
#define NR 2            /*           raw materials (r) */

#define REVFILE     XPRBDATAPATH "/coco/rev.dat"
#define CMAKEFILE   XPRBDATAPATH "/coco/cmake.dat"
#define CBUYFILE    XPRBDATAPATH "/coco/cbuy.dat"
#define REQFILE     XPRBDATAPATH "/coco/req.dat"
#define MXSELLFILE  XPRBDATAPATH "/coco/maxsell.dat"
#define MXMAKEFILE  XPRBDATAPATH "/coco/mxmake.dat"

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

void modCoco2(XPRBprob &pb)
{
 XPRBvar make[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(XPRBnewname("make_p%df%d",p+1,f+1));    

/****OBJECTIVE****/
 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=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=0;
  for(p=0;p<NP;p++) lc += make[p][f];
  pb.newCtr("MxMake", lc <= MXMAKE[f]);      
 }

/****SOLVING + OUTPUT****/
 pb.setSense(XPRB_MAXIM);  /* Choose the sense of the optimization */
 pb.lpOptimize("");        /* Solve the LP-problem */
 cout << "Objective: " << pb.getObjVal() << endl;  /* Get objective value */
    
 for(p=0;p<NP;p++)         /* Print the solution values */      
  for(f=0;f<NF;f++)   
   cout << make[p][f].getName() << ":" << make[p][f].getSol() << " ";
 cout << endl;
}

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

    /**** Read data from files ****/
void readData()
{
 FILE *datafile;
 int p,f,r;
        /* Read the revenu data file */
 datafile=fopen(REVFILE,"r");
 XPRBreadarrlinecb(XPRB_FGETS, datafile, 200, "g,", REV, NP);
 fclose(datafile);

        /* Read the production cost data file */
 datafile=fopen(CMAKEFILE,"r");
 for(p=0;p<NP;p++)
  XPRBreadarrlinecb(XPRB_FGETS, datafile, 200, "g,", CMAK[p], NF);
 fclose(datafile);

        /* Read the raw material cost data file */
 datafile=fopen(CBUYFILE,"r");
 XPRBreadarrlinecb(XPRB_FGETS, datafile, 200, "g,", CBUY, NR);
 fclose(datafile);

        /* Read the resource requirement data file */
 datafile=fopen(REQFILE,"r");
 for(p=0;p<NP;p++)
  XPRBreadarrlinecb(XPRB_FGETS, datafile, 200, "g,", REQ[p], NR);
 fclose(datafile);
  
        /* Read the max. sales quantities data file */
 datafile=fopen(MXSELLFILE,"r");
 XPRBreadarrlinecb(XPRB_FGETS, datafile, 200, "g,", MXSELL, NP);
 fclose(datafile);
  
        /* Read the production capacities data file */
 datafile=fopen(MXMAKEFILE,"r");
 XPRBreadarrlinecb(XPRB_FGETS, datafile, 200, "g,", MXMAKE, NF);
 fclose(datafile);
 
        /* 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]);
  }
}

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

int main(int argc, char **argv)
{
 XPRBprob pb("Coco2");  /* Initialize a new problem in BCL */
 readData();            /* Data input from file */
 modCoco2(pb);          /* Model and solve the problem */
  
 return 0;
} 

Back to examples browserPrevious exampleNext example