/********************************************************
Xpress-BCL C++ Example Problems
===============================
file xbexpl3.cxx
````````````````
User error handling and output redirection.
(c) 2008 Fair Isaac Corporation
author: Y.Colombani, 2006, rev. Mar. 2011
********************************************************/
#include
#include
#include
#include "xprb_cpp.h"
using namespace std;
using namespace ::dashoptimization;
/* 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. */
/***********************************************************************/
class bcl_exception
{
public:
string msg;
int code;
bcl_exception(int c,const char *m)
{
code=c;
msg=string(m);
cout << "EXCP:" << msg << "\n";
}
};
/***********************************************************************/
/**** User error handling function ****/
void XPRB_CC usererror(xbprob* prob, void *vp, int num, int type,
const char *t)
{
throw bcl_exception(num, t);
}
/**** User printing function ****/
void XPRB_CC userprint(xbprob* 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)
cout << "BCL output: " << msg;
else
cout << msg;
rtsbefore=(msg[strlen(msg)-1]=='\n');
}
/***********************************************************************/
void modexpl3(XPRBprob &p)
{
XPRBvar x[3];
XPRBlinExp le;
int i;
for(i=0;i<2;i++) x[i]=p.newVar(XPRBnewname("x_%d",i), XPRB_UI, 0, 100);
/* Create the constraints:
C1: 2x0 + 3x1 >= 41
C2: x0 + 2x1 = 13 */
p.newCtr("C1", 2*x[0] + 3*x[1] >= 41);
p.newCtr("C2", x[0] + 2*x[1] == 13);
// Uncomment the following line to cause an error in the model that
// triggers the user error handling:
// x[2]=p.newVar("x_2", XPRB_UI, 10,1);
// Objective: minimize x0+x1
le=0;
for(i=0;i<2;i++) le += x[i];
p.setObj(le); // Select objective function
p.setSense(XPRB_MINIM); // Set objective sense to minimization
p.print(); // Print current problem definition
p.lpOptimize(""); // Solve the LP
XPRBprintf(p.getCRef(), "problem status: %d LP status: %d MIP status: %d\n",
p.getProbStat(), p.getLPStat(), p.getMIPStat());
// This problem is infeasible, that means the following command will fail.
// It prints a warning if the message level is at least 2
XPRBprintf(p.getCRef(), "Objective: %g\n", p.getObjVal());
for(i=0;i<2;i++) // Print solution values
XPRBprintf(p.getCRef(), "%s:%g, ", x[i].getName(), x[i].getSol());
XPRBprintf(p.getCRef(), "\n");
}
/***********************************************************************/
int main()
{
XPRBprob *p;
XPRBseterrctrl(0); // Switch to error handling by the user's program
XPRB::setMsgLevel(2); // Set the printing flag. Try other values:
// 0 - no printed output, 1 - only errors,
// 2 - errors and warnings, 3 - all messages
// Define the callback functions:
XPRBdefcbmsg(NULL, userprint, NULL);
XPRBdefcberr(NULL, usererror, NULL);
try
{
p=new XPRBprob("Expl3"); // Initialize a new problem in BCL
}
catch(bcl_exception &e)
{
cout << e.code << ":" << e.msg;
return 1;
}
try
{
modexpl3(*p); // Formulate and solve the problem
}
catch(bcl_exception &e)
{
cout << e.code << ":" << e.msg << "\n";
// return 2;
}
catch(const char *m)
{
cout << m << "\n";
return 3;
}
catch(...)
{
cout << "other exception\n";
return 4;
}
return 0;
}
|
/********************************************************
Xpress-BCL C++ Example Problems
===============================
file xbexpl2.cxx
````````````````
Transportation model demonstrating use of index sets.
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, Jan. 2000, rev. Mar. 2011
********************************************************/
#include
#include
#include "xprb_cpp.h"
using namespace std;
using namespace ::dashoptimization;
#define MaxSuppliers 100 /* Max. number of suppliers */
#define MaxCustomers 1000 /* Max. number of customers */
#define MaxArcs 10000 /* Max. number of non-zero cost values */
#define DEMANDFILE XPRBDATAPATH "/trans/ex2dem1.dat"
/* Demand data file (comma-separated format) */
#define AVAILFILE XPRBDATAPATH "/trans/ex2avail.dat"
/* Supply data file (comma-separated format) */
#define COSTFILE XPRBDATAPATH "/trans/ex2cost.dat"
/* Cost data file (comma-separated format) */
XPRBindexSet Suppliers; /* Set of suppliers */
XPRBindexSet Customers; /* Set of customers */
double AVAIL[MaxSuppliers]; /* Availability of products */
double DEMAND[MaxCustomers]; /* Demand by customers */
struct {
int suppl;
int custm;
double value;
} COST[MaxArcs]; /* Cost per supplier-customer pair */
int NSuppl=0,NCustom=0, NArc=0; /* Actual numbers of suppliers, customers,
and arcs */
XPRBprob p("Trans"); /* Initialize a new problem in BCL */
/***********************************************************************/
void modTrans()
{
XPRBexpr lobj, *av,*de;
int s,c,a;
XPRBvar *x;
/****VARIABLES****/
x = new XPRBvar[NArc];
if(x==NULL) cout << "Allocating memory for variables failed." << endl;
for(a=0; a= DEMAND[c]);
/****SOLVING + OUTPUT****/
p.exportProb(XPRB_MPS,"trans"); /* Matrix generation & output to MPS file */
p.lpOptimize(""); /* Solve the LP-problem */
cout << "Objective: " << p.getObjVal() << endl; /* Get objective value */
for(a=0; a0)
{
cout << Suppliers[COST[a].suppl] << " (" << AVAIL[COST[a].suppl] << ") -> ";
cout << Customers[COST[a].custm] << " (" << DEMAND[COST[a].custm] << "): ";
cout << x[a].getSol() << endl;
}
delete [] de;
delete [] av;
delete [] x;
}
/***********************************************************************/
/**** Read data from files ****/
void readData()
{
double value;
FILE *datafile;
char name[100], name2[100];
/* Create supplier and customer index sets */
Suppliers=p.newIndexSet("suppl",MaxSuppliers);
Customers=p.newIndexSet("custom",MaxCustomers);
/* Read the demand data file */
datafile=fopen(DEMANDFILE,"r");
while (XPRBreadlinecb(XPRB_FGETS, datafile, 200, "T,g", name, &value) == 2)
DEMAND[Customers+=name]=value;
fclose(datafile);
NCustom = Customers.getSize();
/* Read the supply data file */
datafile=fopen(AVAILFILE,"r");
while (XPRBreadlinecb(XPRB_FGETS, datafile, 200, "T,g", name, &value) == 2)
AVAIL[Suppliers+=name]=value;
fclose(datafile);
NSuppl = Suppliers.getSize();
/* Read the cost data file */
NArc = 0;
datafile=fopen(COSTFILE,"r");
while (XPRBreadlinecb(XPRB_FGETS, datafile, 200, "T,T,g", name,
name2, &value) == 3)
{
COST[NArc].suppl = Suppliers[name];
COST[NArc].custm = Customers[name2];
if(COST[NArc].custm<0) printf("Cust(%s)\n",name2);
if(COST[NArc].suppl<0) printf("Supp(%s)\n",name);
COST[NArc++].value = value;
}
fclose(datafile);
printf("C: %d S: %d A: %d\n",NCustom,NSuppl,NArc);
}
/***********************************************************************/
int main(int argc, char **argv)
{
readData(); /* Data input from file */
modTrans(); /* Formulate and solve the problem */
return 0;
}
|
/********************************************************
BCL Example Problems
====================
file xbairport.cxx
``````````````````
QCQP problem by
Rodrigo de Barros Nabholz & Maria Aparecida Diniz Ehrhardt
November 1994, DMA - IMECC- UNICAMP.
Based on AMPL model airport.mod by Hande Y. Benson
(Source: http://www.orfe.princeton.edu/~rvdb/ampl/nlmodels/ )
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, June 2008, rev. Mar. 2011
********************************************************/
#include
#include "xprb_cpp.h"
#include "xprs.h"
using namespace std;
using namespace ::dashoptimization;
#define N 42
double CX[] = {-6.3, -7.8, -9, -7.2, -5.7, -1.9, -3.5, -0.5, 1.4, 4,
2.1, 5.5, 5.7, 5.7, 3.8, 5.3, 4.7, 3.3, 0, -1, -0.4, 4.2,
3.2, 1.7, 3.3, 2, 0.7, 0.1, -0.1, -3.5, -4, -2.7, -0.5, -2.9,
-1.2, -0.4, -0.1, -1, -1.7, -2.1, -1.8, 0};
double CY[] = {8, 5.1, 2, 2.6, 5.5, 7.1, 5.9, 6.6, 6.1, 5.6, 4.9, 4.7,
4.3, 3.6, 4.1, 3, 2.4, 3, 4.7, 3.4, 2.3, 1.5, 0.5, -1.7, -2,
-3.1, -3.5, -2.4, -1.3, 0, -1.7, -2.1, -0.4, -2.9, -3.4, -4.3,
-5.2, -6.5, -7.5, -6.4, -5.1, 0};
double R[] = {0.09, 0.3, 0.09, 0.45, 0.5, 0.04, 0.1, 0.02, 0.02, 0.07, 0.4,
0.045, 0.05, 0.056, 0.36, 0.08, 0.07, 0.36, 0.67, 0.38, 0.37,
0.05, 0.4, 0.66, 0.05, 0.07, 0.08, 0.3, 0.31, 0.49, 0.09,
0.46, 0.12, 0.07, 0.07, 0.09, 0.05, 0.13, 0.16, 0.46, 0.25, 0.1};
int main(int argc, char **argv)
{
int i,j;
XPRBvar x[N],y[N];
XPRBexpr qe;
XPRBctr cobj, c;
XPRBprob prob("airport"); // Initialize a new problem in BCL
/**** VARIABLES ****/
for(i=0;i |
/********************************************************
Xpress-BCL C++ Example Problems
===============================
file xbcontr1.cxx
`````````````````
Contract allocation example.
Combining BCL problem input with problem solving
in Xpress-Optimizer.
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, Jan. 2000, rev. Oct. 2010
********************************************************/
#include
#include
#include "xprb_cpp.h"
#include "xprs.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;
XPRBexpr l1,l2,lobj;
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 op;
XPRBprob p("Contr1"); /* Initialize a new problem in BCL */
/**** VARIABLES ****/
for(d=0;d= VOLUME[c]); /* "Size": cover the required volume */
p.newCtr("Min", l2 >= 2 ); /* "Min": at least 2 districts per contract */
}
for(d=0;d |
/********************************************************
Xpress-BCL C++ Example Problems
===============================
file xbcontr2.cxx
`````````````````
Contract allocation example.
Combining BCL problem input with problem solving
and callbacks in Xpress-Optimizer.
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, Jan. 2000, rev. Mar. 2011
********************************************************/
#include
#include "xprb_cpp.h"
#include "xprs.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 */
/***********************************************************************/
void XPRS_CC printsolution(XPRSprob oprob, void *vp)
{
int num, d, c;
XPRBprob *bprob;
XPRBvar y;
bprob = (XPRBprob *)vp;
bprob->beginCB(oprob);
XPRSgetintattrib(oprob, XPRS_MIPSOLS, &num); /* Get number of the solution */
bprob->sync(XPRB_XPRS_SOL); /* Update BCL solution values */
cout << "Solution " << num << ": Objective value: " << bprob->getObjVal() << endl;
for(d=0;dgetVarByName(XPRBnewname("q_d%dc%d",d+1,c+1));
if( (y.getColNum()>-1) && (y.getSol() != 0))
cout << y.getName() << ": " << y.getSol() << endl;
}
bprob->endCB();
}
/***********************************************************************/
int main(int argc, char **argv)
{
int d,c;
XPRBexpr l1,l2,lobj;
XPRBvar x[District][Contract]; /* Variables indicating whether a project
is chosen */
XPRBvar y[District][Contract]; /* Quantities allocated to contractors */
XPRBprob p("Contr2"); /* Initialize a new problem in BCL */
/**** VARIABLES ****/
for(d=0;d= VOLUME[c]); /* "Size": cover the required volume */
p.newCtr("Min", l2 >= 2 ); /* "Min": at least 2 districts per contract */
}
for(d=0;d |
/********************************************************
Xpress-BCL C++ Example Problems
===============================
file xbcontr2s.cxx
``````````````````
Contract allocation example.
Combining BCL problem input with problem solving
and callbacks in Xpress-Optimizer.
-- Single MIP thread --
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, Jan. 2000, rev. Mar. 2011
********************************************************/
#include
#include "xprb_cpp.h"
#include "xprs.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 */
/***********************************************************************/
void XPRS_CC printsolution(XPRSprob oprob, void *vp)
{
int num, d, c;
XPRBprob *bprob;
XPRBvar y;
bprob = (XPRBprob *)vp;
XPRSgetintattrib(oprob, XPRS_MIPSOLS, &num); /* Get number of the solution */
bprob->sync(XPRB_XPRS_SOL); /* Update BCL solution values */
cout << "Solution " << num << ": Objective value: " << bprob->getObjVal() << endl;
for(d=0;dgetVarByName(XPRBnewname("q_d%dc%d",d+1,c+1));
if( (y.getColNum()>-1) && (y.getSol() != 0))
cout << y.getName() << ": " << y.getSol() << endl;
}
}
/***********************************************************************/
int main(int argc, char **argv)
{
int d,c;
XPRBexpr l1,l2,lobj;
XPRBvar x[District][Contract]; /* Variables indicating whether a project
is chosen */
XPRBvar y[District][Contract]; /* Quantities allocated to contractors */
XPRBprob p("Contr2"); /* Initialize a new problem in BCL */
/**** VARIABLES ****/
for(d=0;d= VOLUME[c]); /* "Size": cover the required volume */
p.newCtr("Min", l2 >= 2 ); /* "Min": at least 2 districts per contract */
}
for(d=0;d |