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

UG - Examples from 'BCL Reference Manual'

Description
The following examples are discussed in detail in the 'BCL User Guide and Reference Manual':
  • modeling and solving a small MIP scheduling problem (xbexpl1.c version BASIC)
  • using variable arrays and constraint templates (xbexpl1.c versions ARRAY and ARRAYC)
  • definition of SOS-1 (xbexpl1 version SOS)
  • data input from file, index sets (xbexpl1i)
  • user error handling, output redirection (xbexpl3)
  • solving multiple scenarios of a transportation problem in parallel (xbexpl2: standard, single thread version; xbscenar: defining multiple problems, each in a separate thread)
  • cut generation / adding cuts at MIP tree nodes (xbcutex)
  • quadratic programming (quadratic objective: xbqpr12, quadratic constraints: xbairport)
  • combine BCL problem input with problem solving in Xpress Optimizer (xbcontr1)
  • use an Xpress Optimizer solution callback with a BCL model (xbcontr2s: single MIP thread; xbcontr2: multiple MIP threads)
Further explanation of this example: 'BCL Reference Manual', Appendix B Using BCL with the Optimizer library


Source Files

Data Files





xbexpl3.c

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

  file xbexpl3.c
  ``````````````
  User error handling.

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

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

/* This small, infeasible example shows how the error handling and all
   printed messages can be intercepted by the user's program. This is done
   by defining the corresponding BCL callback functions and changing
   the error handling flag. */

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

jmp_buf model_failed;

void modexpl3(XPRBprob prob)
{
 XPRBvar x[3];
 XPRBctr ctr[2], cobj;
 int i;

 for(i=0;i<2;i++) x[i]=XPRBnewvar(prob,XPRB_UI,XPRBnewname("x_%d",i),0,100);

                /* Create the constraints:
                   C1: 2x0 + 3x1 >= 41
                   C2:  x0 + 2x1  = 13 */
 ctr[0]=XPRBnewctr(prob,"C1",XPRB_G);
 XPRBaddterm(ctr[0],x[0],2);
 XPRBaddterm(ctr[0],x[1],3);
 XPRBaddterm(ctr[0],NULL,41);

 ctr[1]=XPRBnewctr(prob,"C2",XPRB_E);
 XPRBaddterm(ctr[1],x[0],1);
 XPRBaddterm(ctr[1],x[1],2);
 XPRBaddterm(ctr[1],NULL,13);

/* Uncomment the following line to cause an error in the model that
   triggers the user error handling: */

/* x[2]=XPRBnewvar(prob,XPRB_UI,"x_2",10,1); */

                /* Objective: minimize x0+x1 */
 cobj = XPRBnewctr(prob,"OBJ",XPRB_N);
 for(i=0;i<2;i++) XPRBaddterm(cobj,x[i],1);
 XPRBsetobj(prob,cobj);          /* Select objective function */
 XPRBsetsense(prob,XPRB_MINIM);  /* Set objective sense to minimization */

 XPRBprintprob(prob);            /* Print current problem definition */

 XPRBlpoptimize(prob,"");        /* Solve the LP */
 XPRBprintf(prob,"problem status: %d  LP status: %d  MIP status: %d\n",
    XPRBgetprobstat(prob),XPRBgetlpstat(prob),XPRBgetmipstat(prob));

/* This problem is infeasible, that means the following command will fail.
   It prints a warning if the message level is at least 2 */

 XPRBprintf(prob,"Objective: %g\n",XPRBgetobjval(prob));

 for(i=0;i<2;i++)                /* Print solution values */
  XPRBprintf(prob,"%s:%g, ", XPRBgetvarname(x[i]), XPRBgetsol(x[i]));
 XPRBprintf(prob,"\n");
}

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

/**** User error handling function ****/
void XPRB_CC usererror(XPRBprob prob, void *vp, int num, int type,
                       const char *t)
{
 printf("BCL error %d: %s\n", num, t);
 if(type==XPRB_ERR) longjmp(model_failed,1);
}

/**** User printing function ****/
void XPRB_CC userprint(XPRBprob prob, void *vp, const char *msg)
{
 static int rtsbefore=1;

    /* Print 'BCL output' whenever a new output line starts,
       otherwise continue to print the current line. */
 if(rtsbefore)
  printf("BCL output: %s", msg);
 else
  printf("%s",msg);
 rtsbefore=(msg[strlen(msg)-1]=='\n');
}

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

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

 XPRBseterrctrl(0);         /* Switch to error handling by the user's program */

 XPRBsetmsglevel(NULL,2);   /* Set the printing flag. Try other values:
                                0 - no printed output, 1 - only errors,
                                2 - errors and warnings, 3 - all messages */
 XPRBdefcbmsg(NULL,userprint, NULL);
                            /* Define the printing callback function */

 if((prob=XPRBnewprob("Expl3"))==NULL)
 {                          /* Initialize a new problem in BCL */
  fprintf(stderr,"I cannot create the problem\n");
  return 1;
 }
 else
  if(setjmp(model_failed))
  {
   fprintf(stderr,"I cannot build the problem\n");
   XPRBdelprob(prob);
   XPRBdefcberr(prob,NULL, NULL);
   return 1;
  }
  else
  {
   XPRBdefcberr(prob,usererror, NULL);
   modexpl3(prob);          /* Formulate and solve the problem */
   XPRBdefcberr(prob,NULL, NULL);
   return 0;
  }
}


Back to examples browserPrevious exampleNext example