Initializing help system before first use

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


Type: Contract allocation
Rating: 3 (intermediate)
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.
File(s): xbcontr.c


xbcontr.c
/********************************************************
  BCL Example Problems
  ====================

  file xbcontr.c
  ``````````````
  Contract allocation example.

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

#include <stdio.h>
#include "xprb.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 */
 XPRBprob prob;

 prob=XPRBnewprob("Contract");     /* Initialize a new problem in BCL */

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

/****OBJECTIVE****/
 cobj = XPRBnewctr(prob,"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(prob,cobj);                 /* Set objective function */

/**** CONSTRAINTS ****/
 for(c=0;c<Contract;c++)
 {
  c1=XPRBnewctr(prob,"Size",XPRB_G);  /* Cover the required contract volume */
  c2=XPRBnewctr(prob,"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(prob,"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(prob,"XY",x[d][c],0,y[d][c]);

/****SOLVING + OUTPUT****/
 XPRBexportprob(prob,XPRB_MPS,"Contract");   /* Output matrix in MPS format */

 XPRBsetsense(prob,XPRB_MINIM);      /* Choose the sense of the optimization */
 XPRBmipoptimize(prob,"");           /* Solve the MIP-problem */

 if((XPRBgetmipstat(prob)==XPRB_MIP_SOLUTION) ||
    (XPRBgetmipstat(prob)==XPRB_MIP_OPTIMAL))
                                /* Test whether an integer solution was found */
 {
  printf("Objective: %g\n",XPRBgetobjval(prob));  /* Get objective value */

  for(d=0;d<District;d++)       /* Print the solution values */
  {
   for(c=0;c<Contract;c++)
    if(XPRBgetsol(x[d][c])>0)
     printf("%s:%g, ", XPRBgetvarname(y[d][c]), XPRBgetsol(y[d][c]));
   printf("\n");
  }
 }
 return 0;
}