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

Delivery - Data input from file; infeasibility analysis

Description
A simple supply and demand network example showing data input from file and the use of "views": incremental definition of arrays of variables. Also uses constraint templates with the arrays of variables.

A second version of this model (file xbdlvriis) has modified data making the problem infeasible. This example shows how to analyze infeasibility with the help of IIS (irreducible infeasible sets), it retrieves the IIS and prints out their contents.

It is possible to retrieve more detailed information on the IIS, such as isolation rows or bounds, using Xpress Optimizer functions (file xbdlvriis2iso) or to use the infeasibility repair functionality of the Optimizer (file xbdlvriis2rep) with models defined in BCL.


Source Files

Data Files





xbdelvr.c

/********************************************************
  BCL Example Problems
  ====================

  file xbdelivr.c
  ```````````````
  Transportation problem.
  Use of arrays.

  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, Jan. 2000, rev. Mar. 2011
********************************************************/

#include <stdio.h>
#include "xprb.h"

/* define or undefine ARRAY in order to switch between model formulations
   with or without variable arrays in constraint definition */
#define ARRAY

#define NSupp 10                        /* Number of suppliers */
#define NCust 7                         /* Number of customers */
#define MaxArcs 100                     /* Max. num. of non-zero cost values */

#define VANFILE XPRBDATAPATH "/delivery/ifvan.dat"    /* Van data file */
#define COSTFILE XPRBDATAPATH "/delivery/cost.dat"    /* Cost data file */

/****DATA****/
/* Supplier:      London  Luton  B'ham Bristl  Derby Stckpt   York */
double SUPPLY[] = {140.0, 600.0,  50.0,  10.0, 400.0, 200.0,  20.0,
/* Supplier: Derby  Soton Scnthp */
             90.0,  30.0,  12.0};
/* Customer:      London Livpol Doncst   York   Hull Manchr Shffld */
double DEMAND[] = {123.3,  56.4,  17.1, 192.8, 310.0,  47.0,  86.0};

double COST[NSupp][NCust];        /* Cost per supplier-customer pair */

double IFVAN[NSupp][NCust];       /* Non-zero if route uses vans instead
                                     of lorries */
double VANCAP=40.0;               /* Capacity on routes that use vans */

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

void moddelivery(XPRBprob prob)
{
 XPRBctr ctr;
 int s,c;
 XPRBvar x[NSupp][NCust];

/****VARIABLES****/
 for(s=0;s<NSupp;s++)
  for(c=0; c<NCust; c++)
   x[s][c]=XPRBnewvar(prob,XPRB_PL,XPRBnewname("x_s%d",s), 0, XPRB_INFINITY);

/****OBJECTIVE****/
 ctr = XPRBnewctr(prob,"OBJ",XPRB_N);
 for(s=0;s<NSupp;s++)                  /* Objective: Minimize total cost */
  for(c=0; c<NCust; c++)
   XPRBaddterm(ctr, x[s][c], COST[s][c]);
 XPRBsetobj(prob,ctr);                 /* Set objective function */

/****CONSTRAINTS****/
 for(c=0; c<NCust; c++)                /* Satisfy demand of each customer */
 {
  ctr = XPRBnewctr(prob,"Demand", XPRB_G);
  for(s=0;s<NSupp;s++) XPRBaddterm(ctr, x[s][c], 1);
  XPRBaddterm(ctr, NULL, DEMAND[c]);
 }

 for(s=0;s<NSupp;s++)                  /* Keep within supply at each supplier*/
 {
  ctr = XPRBnewctr(prob,"Supply",XPRB_L);
  for(c=0; c<NCust; c++)
   XPRBaddterm(ctr, x[s][c], 1);
  XPRBaddterm(ctr, NULL, SUPPLY[s]);
 }

/****BOUNDS****/
 for(s=0;s<NSupp;s++)
  for(c=0; c<NCust; c++)
   if(IFVAN[s][c]!=0) XPRBsetub(x[s][c], VANCAP);

/****SOLVING + OUTPUT****/
 XPRBexportprob(prob,XPRB_MPS,"delivery");  /* Write out an MPS file */

 XPRBsetsense(prob, XPRB_MINIM);      /* Set objective sense to minimization */
 XPRBlpoptimize(prob, "");            /* Solve the LP-problem */
 printf("Objective: %g\n", XPRBgetobjval(prob));  /* Get objective value */

 for(s=0;s<NSupp;s++)                 /* Print out the solution values */
  for(c=0; c<NCust; c++)
   printf("%s:%g ", XPRBgetvarname(x[s][c]), XPRBgetsol(x[s][c]));
 printf("\n");
}

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

    /**** Array-based constraint formulation ****/
void moddelivery_array(XPRBprob prob)
{
 int s,c;
 XPRBvar x[NSupp][NCust];
 XPRBarrvar va;
 XPRBctr ctr;

/****VARIABLES****/
 for(s=0;s<NSupp;s++)
  for(c=0; c<NCust; c++)
   x[s][c]=XPRBnewvar(prob,XPRB_PL,XPRBnewname("x_s%d",s), 0, XPRB_INFINITY);

/****OBJECTIVE****/
 ctr = XPRBnewctr(prob,"OBJ",XPRB_N);
 for(s=0;s<NSupp;s++)                  /* Objective: Minimize total cost */
  for(c=0; c<NCust; c++)
   XPRBaddterm(ctr, x[s][c], COST[s][c]);
 XPRBsetobj(prob,ctr);                      /* Select objective function */


/****CONSTRAINTS****/
 for(c=0; c<NCust; c++)                /* Satisfy demand of each customer */
 {
  va = XPRBstartarrvar(prob,NSupp,"arr");   /* Define an array of size NSupp */
  for(s=0;s<NSupp;s++)
   XPRBapparrvarel(va, x[s][c]);       /* Add a variable to the array */
  XPRBendarrvar(va);                   /* Terminate definition of the array */
  XPRBnewsum(prob,"Demand", va, XPRB_G, DEMAND[c]);
 }

 for(s=0;s<NSupp;s++)                  /* Keep within supply at each supplier */
 {
  ctr = XPRBnewctr(prob,"Supply",XPRB_L);
  for(c=0; c<NCust; c++)
   XPRBaddterm(ctr, x[s][c], 1);
  XPRBaddterm(ctr, NULL, SUPPLY[s]);
 }

/****BOUNDS****/
 for(s=0;s<NSupp;s++)
  for(c=0; c<NCust; c++)
   if(IFVAN[s][c]!=0) XPRBsetub(x[s][c], VANCAP);

/****SOLVING + OUTPUT****/
 XPRBexportprob(prob,XPRB_MPS,"delivery");  /* Write out an MPS file */

 XPRBsetsense(prob,XPRB_MINIM);             /* Set objective sense to minimization */
 XPRBsolve(prob,"");                        /* Solve the LP-problem */
 printf("Objective: %g\n", XPRBgetobjval(prob));  /* Get objective value */

 for(s=0;s<NSupp;s++)                       /* Print out the solution values */
  for(c=0; c<NCust; c++)
   printf("%s:%g ", XPRBgetvarname(x[s][c]), XPRBgetsol(x[s][c]));
 printf("\n");
}

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

    /**** Read data from files ****/
void readdata(void)
{
 FILE *datafile;
 int s,c;

/* Initialize data tables to 0: in the van data file some entries that are
 * zero are simply left out, but the function XPRBreadarrline only initializes
 * those elements to 0 that have been read, e.g. a data line ",,," results
 * in the first 4 elements of the array to be set to zero, even if the
 * maximum number of elements to be read (=last parameter of XPRBreadarrline)
 * has a much larger value */
 for(s=0;s<NSupp;s++)
  for(c=0; c<NCust; c++)
  {
   COST[s][c] = 0;
   IFVAN[s][c] = 0;
  }
        /* Read the demand data file */
 datafile=fopen(COSTFILE,"r");
 for(s=0;s<NSupp;s++)
  XPRBreadarrlinecb(XPRB_FGETS, datafile, 99, "g,", COST[s], NCust);
 fclose(datafile);

        /* Read the van data file */
 datafile=fopen(VANFILE,"r");
 for(s=0;s<NSupp;s++)
  XPRBreadarrlinecb(XPRB_FGETS, datafile, 99, "g,", IFVAN[s], NCust);
 fclose(datafile);
}

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

int main(int argc, char **argv)
{
 XPRBprob prob;

 prob=XPRBnewprob("Delivery");   /* Initialize a new problem in BCL */
 readdata();                     /* Data input from file */

#ifdef ARRAY
 moddelivery_array(prob);        /* Constraint formulation using arrays */
#else
 moddelivery(prob);              /* Straight-forward problem formulation */
#endif

 return 0;
}

Back to examples browserPrevious exampleNext example