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]





xbcontr1.c

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

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

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

#include <stdio.h>
#include <stdlib.h>
#include <string.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 */

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

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 */
 int i, ncol, len, stat, offset;
 double *sol, val;
 char *names;
 XPRSprob oprob;
 XPRBprob bprob;

 bprob=XPRBnewprob("Contr1");      /* 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****/
 XPRBloadmat(bprob);                   /* Load the matrix explicitely */
 oprob = XPRBgetXPRSprob(bprob);       /* Retrieve the Optimizer problem */
 XPRSchgobjsense(oprob, XPRS_OBJ_MINIMIZE);  /* Set sense to minimization */
 XPRSmipoptimize(oprob, "");           /* Solve the MIP problem */

 XPRSgetintattrib(oprob, XPRS_MIPSTATUS, &stat);  /* Get MIP status */
 if((stat==XPRS_MIP_SOLUTION) || (stat==XPRS_MIP_OPTIMAL))
                                /* Test whether an integer solution was found */
 {
  XPRSgetdblattrib(oprob, XPRS_MIPOBJVAL, &val);
  printf("Objective: %g\n", val);
  XPRSgetintattrib(oprob, XPRS_ORIGINALCOLS, &ncol);
  sol = (double *)malloc(ncol * sizeof(double));
  XPRSgetmipsol(oprob, sol, NULL);     /* Get the primal solution values */

  XPRSgetnamelist(oprob, 2, NULL, 0, &len, 0, ncol-1);
                        /* Get number of bytes required for retrieving names */
  names = (char *)malloc(len*sizeof(char));
  XPRSgetnamelist(oprob, 2, names, len, NULL, 0, ncol-1);
                                       /* Get the variable names */
  offset=0;
  for(i=0; i<ncol; i++) {              /* Print out the solution */
   if(sol[i]!=0)
    printf("%s: %g, ", names+offset, sol[i]);
   offset += strlen(names+offset)+1;
  }
  printf("\n");
  free(names);
  free(sol);
 }

 return 0;
}


Back to examples browserPrevious exampleNext example