Initializing help system before first use

Using the Optimizer with BCL C++

Everything that has been said above about the combination of BCL and Xpress Optimizer functions remains true if the BCL program is written in C++.

The examples of BCL-compatible Optimizer functions in the previous section become:

Setting and accessing parameters:

int rows;
XPRSprob opt_prob;
XPRBprob bcl_prob("Example1"); // Initialize BCL (and the Optimizer
                               // library) and create a new problem
   ...                         // Define the problem
bcl_prob.loadMat();
opt_prob = bcl_prob.getXPRSprob();
XPRSsetintcontrol(opt_prob,XPRS_MAXTIME, 60);
                               // Set a time limit of 60 seconds
XPRSsetdblcontrol(opt_prob,XPRS_MIPADDCUTOFF, 0.999);
                               // Set an ADDCUTTOFF value
XPRSgetintattrib(opt_prob,XPRS_ORIGINALROWS, &rows);
                               // Get number of rows
bcl_prob.setSense(XPRB_MAXIM); // Select maximization
bcl_prob.lpOptimize();         // Maximize the LP problem 

Using Xpress Optimizer callbacks (multi-threaded MIP):

void XPRS_CC printsol(XPRSprob opt_prob,void *my_object)
{
  XPRBprob *bcl_prob
  XPRBvar x;
  int num;

  bcl_prob = (XPRBprob*)my_object;
  bcl_prob->beginCB(opt_prob); // Use local Optimizer problem in BCL
  XPRSgetintattrib(opt_prob, XPRS_MIPSOLS, &num);
                               // Get number of the solution
  bcl_prob->sync(XPRB_XPRS_SOL);
                               // Update BCL solution values
  cout << "Solution " << num << ": Objective value: ";
  cout << bprob->getObjVal() << endl;
  x = bcl_prob->getVarByName("x_1");
  if(x.getColNum()>-1)         // Test whether variable is in the
                               // matrix
    cout << x.getName() << ": " << x.getSol() << endl;
  bcl_prob->endCB();           // Reset BCL to main problem
}

int main(int argc, char **argv)
{
  XPRBprob bcl_prob;
  XPRSprob opt_prob;
  XPRBvar x;

  bcl_prob = XPRBnewprob("Example1");  // Initialize BCL (and the Optimizer
                               // library) and create a new problem
  x = bcl_prob.newVar("x_1", XPRB_BV); // Define a variable
     ...                       // Define the rest of the problem
  opt_prob = bcl_prob.getXPRSprob();
  XPRSsetcbintsol(opt_prob, printsol, &bcl_prob);
                               // Define an integer solution callback
  bcl_prob.setSense(XPRB_MAXIM);       // Select maximization
  bcl_prob.mipOptimize();      // Maximize the MIP problem
}

The code extract below shows how to access MIP solution information directly through the Optimizer library functions using the column or row indices saved for BCL modeling objects. In this case there is no need for synchronization of BCL with the local solution information.

void XPRS_CC printsol(XPRSprob opt_prob,void *my_object)
{
  XPRBprob *bcl_prob
  XPRBvar x;
  int num, ncol;
  double *sol, objval;

  bcl_prob = (XPRBprob*)my_object;
  XPRSgetintattrib(opt_prob, XPRS_ORIGINALCOLS, &ncol);
                               // Get the number of columns
  sol = new double[ncol];      // Create the solution array
  XPRSgetintattrib(opt_prob, XPRS_MIPSOLS, &num);
                               // Get number of the solution
  XPRSgetlpsol(opt_prob, sol, NULL, NULL, NULL);  // Get the solution
  XPRSgetdblattrib(opt_prob, XPRS_LPOBJVAL, &objval);
  cout << "Solution " << num << ": Objective value: " << objval << endl;
  x = bcl_prob->getVarByName("x_1");
  if(x.getColNum()>-1)         // Test whether variable is in the
                               // matrix
    cout << x.getName() << ": " << sol[x.getColNum()] << endl;
  delete [] sol;
}

As in the C case, it is possible within a BCL program written in C++ to switch entirely to Xpress Optimizer (see Section Loading the matrix).