/*************************************************************************
BCL Example Problems
====================
file xbexpl3.cs
```````````````
User error handling.
/* 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.
(c) 2008 Fair Isaac Corporation
Authors: S.Heipcke and D.Brett
*************************************************************************/
using System;
using System.Text;
using System.IO;
using BCL;
namespace Examples
{
public class TestUGExpl3
{
public static int rtsbefore = 1;
public void modexpl3(ref XPRBprob prob)
{
XPRBvar[] x = new XPRBvar[3];
XPRBctr[] ctr = new XPRBctr[2];
XPRBexpr cobj;
int i;
for(i=0;i<2;i++)
x[i] = prob.newVar("x_"+i, BCLconstant.XPRB_UI, 0, 100);
/* Create the constraints:
C1: 2x0 + 3x1 >= 41
C2: x0 + 2x1 = 13 */
XPRBexpr C1linexp = new XPRBexpr();
XPRBexpr C2linexp = new XPRBexpr();
C1linexp = 2 * x[0] + 3 * x[1];
C2linexp = x[0] + 2 * x[1];
prob.newCtr("C1", C1linexp >= 41);
prob.newCtr("C2", C2linexp == 13);
/* Uncomment the following line to cause an error in the model that
triggers the user error handling: */
//x[3] = prob.newVar("x_2", BCLconstant.XPRB_UI, 10, 1);
/* Objective: minimize x0+x1 */
cobj = new XPRBexpr(0);
for(i=0;i<2;i++)
cobj += x[i];
prob.setObj(prob.newCtr("OBJ", cobj));
/* Set objective sense to minimization */
prob.setSense(BCLconstant.XPRB_MINIM);
/* Print current problem definition */
prob.print();
/* Solve the LP */
prob.lpOptimize();
prob.printF("Problem status: " + prob.getProbStat() +
" LP status: " + prob.getLPStat() + " MIP status: " +
prob.getMIPStat() + "\n");
/* This problem is infeasible, that means the following command
* will fail.
* It prints a warning if the message level is at least 2 */
prob.printF("Objective: " + prob.getObjVal() + "\n");
/* Print solution values */
for(i=0;i<2;i++)
prob.printF(x[i].getName() + ":" + x[i].getSol() + ", ");
prob.printF("\n");
}
/***********************************************************************/
/**** User error handling function ****/
public static void usererror(IntPtr prob, object vp, int num, int type,
string t)
{
Exception eBCL = new Exception("Error in usererror().");
System.Console.WriteLine("BCL error " +num+ ": " + t);
if(type==BCLconstant.XPRB_ERR)
throw eBCL;
}
/**** User printing function ****/
public static void userprint(IntPtr prob, object vp, string msg)
{
/* Print 'BCL output' whenever a new output line starts,
otherwise continue to print the current line. */
if(rtsbefore==1)
System.Console.Write("BCL output: " + msg);
else
System.Console.Write(msg);
rtsbefore = (msg[msg.Length-1]=='\n') ? 1 : 0;
}
/***********************************************************************/
// This is where one might add custom logging
static void DoSomeErrorLogging(string msg)
{
Console.WriteLine("Here's an error message! {0}", msg);
}
public static int Main()
{
try
{
/* Switch to error handling by the user's program */
XPRB.setErrCtrl(0); // no auto quit on error
int initCode = XPRB.init();
if (initCode != 0 && initCode != 32) // both values are valid
{
DoSomeErrorLogging(Optimizer.XPRS.GetLicErrMsg());
return initCode;
}
TestUGExpl3 TestInstance = new TestUGExpl3();
XPRBprob prob = new XPRBprob("EXPL3");
if (!prob.isValid())
{
DoSomeErrorLogging("Unable to create XPRBprob \"EXPL3\"");
return 1;
}
/* Set the printing flag. Try other values:
0 - no printed output, 1 - only errors,
2 - errors and warnings, 3 - all messages */
prob.setMsgLevel(2);
/* Define the printing callback function */
prob.MessageCallbacks += new XPRBMessageCallback(userprint);
try
{
prob.ErrorCallbacks += new XPRBErrorCallback(usererror);
/* Formulate and solve the problem */
TestInstance.modexpl3(ref prob);
System.Console.WriteLine("I'm about to exit cleanly");
return 0;
}
catch
{
System.Console.WriteLine("I cannot build the problem");
return 1;
}
}
catch
{
System.Console.WriteLine("I cannot create the problem");
return 1;
}
}
}
} |
/********************************************************
Xpress-BCL C# Example Problems
==============================
file xbexpl2.cs
```````````````
Transportation model from the Release 10.5 Supplement.
(c) 2008 Fair Isaac Corporation
authors: S.Heipcke, D.Brett.
********************************************************/
using System;
using System.Text;
using System.IO;
using BCL;
namespace Examples
{
public class TestUGExpl2
{
const int MaxSuppliers = 100; /* Max. number of suppliers */
const int MaxCustomers = 1000; /* Max. number of customers */
const int MaxArcs = 10000; /* Max. number of non-zero cost values */
//Define XPRBDATAPATH to whichever folder you wish.
const string XPRBDATAPATH = "../../data";
const string DEMANDFILE = XPRBDATAPATH + "/trans/ex2dem1.dat";
/* Demand data file (comma-separated format) */
const string AVAILFILE = XPRBDATAPATH + "/trans/ex2avail.dat";
/* Supply data file (comma-separated format) */
const string COSTFILE = XPRBDATAPATH + "/trans/ex2cost.dat";
/* Cost data file (comma-separated format) */
XPRBindexSet Suppliers; /* Set of suppliers */
XPRBindexSet Customers; /* Set of customers */
double[] AVAIL = new double[MaxSuppliers]; /* Availability of products */
double[] DEMAND = new double[MaxCustomers]; /* Demand by customers */
public struct CostStruct{
public int suppl;
public int custm;
public double value;
} ; /* Cost per supplier-customer pair */
CostStruct[] COST = new CostStruct[MaxArcs];
int NSuppl=0,NCustom=0, NArc=0; /* Actual numbers of suppliers, customers,
and arcs */
XPRBprob p = new XPRBprob("Trans"); /* Initialize a new problem in BCL */
/***********************************************************************/
void modTrans()
{
XPRBexpr lobj;
XPRBexpr[] av;
XPRBexpr[] de;
int s,c,a;
XPRBvar[] x;
/****VARIABLES****/
x = new XPRBvar[NArc];
if(x==null) System.Console.WriteLine("Allocating memory for variables failed.");
for(a=0; a= DEMAND[c]);
/****SOLVING + OUTPUT****/
p.exportProb(BCLconstant.XPRB_MPS,"trans"); /* Matrix generation & output to MPS file */
p.setSense(BCLconstant.XPRB_MINIM);
p.lpOptimize();
System.Console.WriteLine("Objective: " + p.getObjVal()); /* Get objective value */
for(a=0; a0)
{
System.Console.Write(Suppliers.getIndexName(COST[a].suppl) + " (" + AVAIL[COST[a].suppl] + ") -> ");
System.Console.Write(Customers.getIndexName(COST[a].custm) + " (" + DEMAND[COST[a].custm] + "): ");
System.Console.WriteLine(x[a].getSol());
}
}
/***********************************************************************/
/**** Read data from files ****/
public void readData()
{
double value;
FileStream file;
StreamReader fileStreamIn;
string name, name2;
/* Create supplier and customer index sets */
Suppliers=p.newIndexSet("suppl",MaxSuppliers);
Customers=p.newIndexSet("custom",MaxCustomers);
/* Read the demand data file */
file = new FileStream(DEMANDFILE, FileMode.Open, FileAccess.Read);
fileStreamIn = new StreamReader(file);
object[] tempobj = new object[3];
while (p.XPRBreadarrline(fileStreamIn, 200, "{T} , {g} ", out tempobj, 1) == 2)
{
name = (string)tempobj[0];
value = (double)tempobj[1];
DEMAND[(Customers + name)] = value;
}
fileStreamIn.Close();
file.Close();
NCustom = Customers.getSize();
/* Read the supply data file */
file = new FileStream(AVAILFILE, FileMode.Open, FileAccess.Read);
fileStreamIn = new StreamReader(file);
while (p.XPRBreadarrline(fileStreamIn, 200, "{T} , {g} ", out tempobj, 1) == 2)
{
name = (string)tempobj[0];
value = (double)tempobj[1];
AVAIL[Suppliers + name] = value;
}
fileStreamIn.Close();
file.Close();
NSuppl = Suppliers.getSize();
/* Read the cost data file */
NArc = 0;
file = new FileStream(COSTFILE, FileMode.Open, FileAccess.Read);
fileStreamIn = new StreamReader(file);
while (p.XPRBreadarrline(fileStreamIn, 200, "{T} , {T} , {g} ", out tempobj, 1) == 3)
{
name = (string)tempobj[0];
name2 = (string)tempobj[1];
value = (double)tempobj[2];
COST[NArc].suppl = Suppliers.getIndex(name);
COST[NArc].custm = Customers.getIndex(name2);
if (COST[NArc].custm < 0)
System.Console.WriteLine("Cust(" + name2 + ")");
if (COST[NArc].suppl < 0)
System.Console.WriteLine("Supp(" + name + ")");
COST[NArc++].value = value;
}
fileStreamIn.Close();
file.Close();
System.Console.WriteLine("C: " + NCustom + " S: " + NSuppl + " A: " + NArc);
}
/***********************************************************************/
public static void Main()
{
XPRB.init();
TestUGExpl2 TestInstance = new TestUGExpl2();
TestInstance.readData(); /* Data input from file */
TestInstance.modTrans(); /* Formulate and solve the problem */
return;
}
}
} |
/****************************************************************
BCL Example Problems
====================
file xbairport.cs
`````````````````
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
authors: S.Heipcke, D.Brett, June 2008
****************************************************************/
using System;
using System.Text;
using System.IO;
using BCL;
namespace Examples
{
public class TestBAirport
{
const int N = 42;
public static void Main()
{
XPRB.init();
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 i, j;
XPRBvar[] x = new XPRBvar[N];
XPRBvar[] y = new XPRBvar[N];
XPRBexpr qe;
XPRBctr cobj, c;
XPRBprob prob = new XPRBprob("airport"); /* Initialize a new problem in BCL */
/**** VARIABLES ****/
for(i=0;i |
/********************************************************
Xpress-BCL C# Example Problems
==============================
file xbcontr1.cs
````````````````
Contract allocation example.
Combining BCL problem input with problem solving
in Xpress-Optimizer.
(c) 2008 Fair Isaac Corporation
authors: S.Heipcke, D.Brett.
********************************************************/
using System;
using System.Text;
using System.IO;
using Optimizer;
using BCL;
namespace Examples
{
public class TestUGContr1
{
const int District = 6; /* Number of districts */
const int 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 */
/*********************************************************************/
public static void Main()
{
XPRB.init();
int d,c;
XPRBexpr l1,l2,lobj;
/* Variables indicating whether a project is chosen */
XPRBvar[,] x = new XPRBvar[District,Contract];
/* Quantities allocated to contractors */
XPRBvar[,] y = new XPRBvar[District,Contract];
int i, ncol, len, stat;
double[] sol;
double val;
/* Initialize a new problem in BCL */
XPRBprob p = new XPRBprob("Contr1");
XPRSprob xprsp;
TestUGContr1 TestInstance = new TestUGContr1();
/**** VARIABLES ****/
for(d=0;d= TestInstance.VOLUME[c]);
/* "Min": at least 2 districts per contract */
p.newCtr("Min", l2 >= 2 );
}
/* Do not exceed max. output of any district */
for(d=0;d |
/********************************************************
Xpress-BCL C# Example Problems
==============================
file xbcontr2.cs
````````````````
Contract allocation example.
Combining BCL problem input with problem solving
and callbacks in Xpress-Optimizer.
(c) 2008 Fair Isaac Corporation
authors: S.Heipcke, D.Brett.
********************************************************/
using System;
using System.Text;
using System.IO;
using Optimizer;
using BCL;
namespace Examples
{
public class TestUGContr2
{
const int District = 6; /* Number of districts */
const int 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 */
/*********************************************************************/
public void printsolution(XPRSprob xprsp, object vp)
{
int num, d, c;
XPRBprob bprob;
XPRBvar y;
bprob = (XPRBprob)vp;
bprob.beginCB(xprsp);
num = xprsp.MIPSols;
bprob.sync(BCLconstant.XPRB_XPRS_SOL); /* Update BCL solution values */
System.Console.WriteLine("Solution " + num + ": Objective value: " + bprob.getObjVal());
for(d=0;d-1) && (y.getSol() != 0))
System.Console.WriteLine(y.getName() + ": " + y.getSol());
}
bprob.endCB();
return;
}
/*********************************************************************/
public static void Main()
{
XPRB.init();
int d,c;
XPRBexpr l1,l2,lobj;
XPRBvar[,] x = new XPRBvar[District,Contract]; /* Variables indicating whether a project
is chosen */
XPRBvar[,] y = new XPRBvar[District,Contract]; /* Quantities allocated to contractors */
XPRBprob p = new XPRBprob("Contr2"); /* Initialize a new problem in BCL */
TestUGContr2 TestInstance = new TestUGContr2();
IntsolCallback del = new IntsolCallback(TestInstance.printsolution);
XPRSprob xprsp;
/**** VARIABLES ****/
for(d=0;d= TestInstance.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.cs
````````````````
Contract allocation example.
Combining BCL problem input with problem solving
and callbacks in Xpress-Optimizer.
--- MIP Single-threaded ---
(c) 2008 Fair Isaac Corporation
authors: S.Heipcke, D.Brett.
********************************************************/
using System;
using System.Text;
using System.IO;
using Optimizer;
using BCL;
namespace Examples
{
public class TestUGContr2s
{
const int District = 6; /* Number of districts */
const int 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 */
/*********************************************************************/
public void printsolution(XPRSprob xprsp, object vp)
{
int num, d, c;
XPRBprob bprob;
XPRBvar y;
bprob = (XPRBprob)vp;
bprob.beginCB(xprsp);
num = xprsp.MIPSols;
bprob.sync(BCLconstant.XPRB_XPRS_SOL); /* Update BCL solution values */
System.Console.WriteLine("Solution " + num + ": Objective value: " + bprob.getObjVal());
for(d=0;d-1) && (y.getSol() != 0))
System.Console.WriteLine(y.getName() + ": " + y.getSol());
}
bprob.endCB();
return;
}
/*********************************************************************/
public static void Main()
{
XPRB.init();
int d,c;
XPRBexpr l1,l2,lobj;
XPRBvar[,] x = new XPRBvar[District,Contract]; /* Variables indicating whether a project
is chosen */
XPRBvar[,] y = new XPRBvar[District,Contract]; /* Quantities allocated to contractors */
XPRBprob p = new XPRBprob("Contr2"); /* Initialize a new problem in BCL */
TestUGContr2s TestInstance = new TestUGContr2s();
IntsolCallback del = new IntsolCallback(TestInstance.printsolution);
XPRSprob xprsp;
/**** VARIABLES ****/
for(d=0;d= TestInstance.VOLUME[c]); /* "Size": cover the required volume */
p.newCtr("Min", l2 >= 2 ); /* "Min": at least 2 districts per contract */
}
for(d=0;d |