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

Contract - Semi-continuous variables, predefined constraint functions, combine BCL with Xpress Optimizer

Description
A small MIP-problem example demonstrating how to define semi-continuous variables, use predefined constraint functions and retrieve the problem status.

Two modified versions (documented in the 'BCL Reference Manual') show how to (1) combine BCL problem input with problem solving in Xpress Optimizer and (2) use an Xpress Optimizer solution callback with a BCL model.

Further explanation of this example: 'BCL Reference Manual', Appendix B Using BCL with the Optimizer library


Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
xbcontr.c[download]
xbcontr1.c[download]
xbcontr2.c[download]
xbcontr2s.c[download]





xbcontr2.c

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

  file xbcontr2.c
  ```````````````
  Contract allocation example.
  Combining BCL problem input with problem solving
  and callbacks in Xpress-Optimizer.

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

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

#define District 6                  /* Number of districts */
#define Contract 10                 /* Number of contracts */

/**** DATA ****/
int OUTPUT[] = {50, 40, 10, 20, 70, 50};    /* Max. output per district */
int COST[]   = {50, 20, 25, 30, 45, 40};    /* Cost per district */
int VOLUME[]   = {20, 10, 30, 15, 20, 30, 10, 50, 10, 20};
                                    /* Volume of contracts */

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

void XPRS_CC printsolution(XPRSprob oprob, void *vp)
{
 int num,d,c;
 XPRBprob bprob;
 XPRBvar y;

 bprob = (XPRBprob)vp;
 XPRBbegincb(bprob, oprob);

 XPRSgetintattrib(oprob, XPRS_MIPSOLS, &num); /* Get number of the solution */

 XPRBsync(bprob, XPRB_XPRS_SOL);              /* Update BCL solution values */
 printf("Solution %d: Objective value: %g\n", num, XPRBgetobjval(bprob));
 for(d=0;d<District;d++)
  for(c=0;c<Contract;c++)
  {
   y = XPRBgetbyname(bprob, XPRBnewname("q_d%dc%d",d+1,c+1), XPRB_VAR);
   if((XPRBgetcolnum(y)>-1) && (XPRBgetsol(y) != 0))
    printf("%s: %g\n", XPRBgetvarname(y), XPRBgetsol(y));
  }

 XPRBendcb(bprob);
}

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

int main(int argc, char **argv)
{
 int d,c;
 XPRBctr c1,c2,cobj;
 XPRBvar x[District][Contract];    /* Variables indicating whether a project
                                      is chosen */
 XPRBvar y[District][Contract];    /* Quantities allocated to contractors */
 XPRBprob bprob;

 bprob=XPRBnewprob("Contr2");      /* Initialize a new problem in BCL */

/**** VARIABLES ****/
 for(d=0;d<District;d++)
  for(c=0;c<Contract;c++)
  {
   x[d][c] = XPRBnewvar(bprob,XPRB_BV,XPRBnewname("x_d%dc%d",d+1,c+1),0,1);
   y[d][c] =
     XPRBnewvar(bprob,XPRB_SC,XPRBnewname("q_d%dc%d",d+1,c+1),0,OUTPUT[d]);
   XPRBsetlim(y[d][c],5);
  }

/****OBJECTIVE****/
 cobj = XPRBnewctr(bprob,"OBJ",XPRB_N);  /* Define objective: total cost */
 for(d=0;d<District;d++)
  for(c=0;c<Contract;c++)
   XPRBaddterm(cobj,y[d][c],COST[d]);
 XPRBsetobj(bprob,cobj);                 /* Set objective function */

/**** CONSTRAINTS ****/
 for(c=0;c<Contract;c++)
 {
  c1=XPRBnewctr(bprob,"Size",XPRB_G);  /* Cover the required contract volume */
  c2=XPRBnewctr(bprob,"Min",XPRB_G);   /* At least 2 districts per contract */
  for(d=0;d<District;d++)
  {
   XPRBaddterm(c1,y[d][c],1);
   XPRBaddterm(c2,x[d][c],1);
  }
  XPRBaddterm(c1,NULL,VOLUME[c]);
  XPRBaddterm(c2,NULL,2);
 }

 for(d=0;d<District;d++)        /* Do not exceed max. output of any district */
 {
  c1=XPRBnewctr(bprob,"Output",XPRB_L);
  for(c=0;c<Contract;c++)
   XPRBaddterm(c1,y[d][c],1);
  XPRBaddterm(c1,NULL,OUTPUT[d]);
 }

 for(d=0;d<District;d++)        /* If a contract is allocated to a district,
                                   then at least 1 unit is allocated to it */
  for(c=0;c<Contract;c++)
   XPRBnewprec(bprob,"XY",x[d][c],0,y[d][c]);

/****SOLVING + OUTPUT****/
 XPRSsetcbintsol(XPRBgetXPRSprob(bprob), printsolution, bprob);
                                /* Define an integer solution callback */
 XPRBmipoptimize(bprob,"");     /* Solve the MIP problem */

 return 0;
}


Back to examples browserPrevious exampleNext example