/********************************************************
Xpress-BCL C++ Example Problems
===============================
file xbcontr.cxx
````````````````
Contract allocation example.
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, Jan. 2000, rev. Mar. 2011
********************************************************/
#include <iostream>
#include "xprb_cpp.h"
using namespace std;
using namespace ::dashoptimization;
#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;
XPRBprob p("Contract"); /* Initialize a new problem in BCL */
XPRBexpr l1,l2,lobj;
XPRBvar x[District][Contract]; /* Variables indicating whether a project
is chosen */
XPRBvar y[District][Contract]; /* Quantities allocated to contractors */
/**** VARIABLES ****/
for(d=0;d<District;d++)
for(c=0;c<Contract;c++)
{
x[d][c] = p.newVar(XPRBnewname("x_d%dc%d",d+1,c+1),XPRB_BV);
y[d][c] = p.newVar(XPRBnewname("q_d%dc%d",d+1,c+1),XPRB_SC,0,OUTPUT[d]);
y[d][c].setLim(5);
}
/****OBJECTIVE****/
for(d=0;d<District;d++)
for(c=0;c<Contract;c++)
lobj += COST[d]*y[d][c];
p.setObj(p.newCtr("OBJ",lobj)); /* Set the objective function */
/**** CONSTRAINTS ****/
for(c=0;c<Contract;c++)
{
l1=0;
l2=0;
for(d=0;d<District;d++)
{
l1 += y[d][c];
l2 += x[d][c];
}
p.newCtr("Size", l1 >= VOLUME[c]); /* "Size": cover the required volume */
p.newCtr("Min", l2 >= 2 ); /* "Min": at least 2 districts per contract */
}
for(d=0;d<District;d++) /* Do not exceed max. output of any district */
{
l1=0;
for(c=0;c<Contract;c++)
l1 += y[d][c];
p.newCtr("Output", l1 <= 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++)
p.newCtr("XY", x[d][c] <= y[d][c]);
/****SOLVING + OUTPUT****/
p.exportProb(XPRB_MPS,"Contract"); /* Output the matrix in MPS format */
p.setSense(XPRB_MINIM); /* Choose the sense of the optimization */
p.mipOptimize(""); /* Solve the MIP-problem */
if((p.getMIPStat()==XPRB_MIP_SOLUTION) || (p.getMIPStat()==XPRB_MIP_OPTIMAL))
/* Test whether an integer sol. was found */
{
cout << "Objective: " << p.getObjVal() << endl; /* Get objective value */
for(d=0;d<District;d++) /* Print the solution values */
{
for(c=0;c<Contract;c++)
if(x[d][c].getSol()>0)
cout << y[d][c].getName() << ":" << y[d][c].getSol() << ", ";
cout << endl;
}
}
return 0;
}
|