IBM ILOG Solver User's Manual > More on Solving > Using Parallel Solver: Multithreaded Warehouse Location > Using Parallel Solver

The example whouse_mt.cpp is a variation of the warehouse allocation example introduced in Chapter 13, Controlling the Search: Locating Warehouses. It illustrates how to use Parallel Solver.

The complete program follows. You can also view it online in the file YourSolverHome/examples/src/whouse_mt.cpp.

#if defined(ILCUSEMT) || defined(ILOUSEMT)
 
# include <ilsolver/ilopsolver.h>
ILOSTLBEGIN
 
void readCosts(const char*   name,
               IloInt&       buildingCost,
               IloIntArray2& costs,
               IloInt&       nbClients,
               IloInt&       nbWhouses) {
  ifstream in(name);
  in >> buildingCost >> costs;
  nbClients = costs.getSize();
  if ( nbClients ) nbWhouses = costs[0].getSize();
  else             nbWhouses = 0;
}
 
int
main(int argc, char** argv)
{
  IloInitMT();
  IloEnv env;
  try {
    IloInt i;
    IloModel model(env);
 
    IloInt       buildingCost, nbClients, nbWhouses;
    IloIntArray2 costs(env);
    const char* fileName;
    if ( argc < 2 ) {
      env.warning() << "usage: " << argv[0] << " <filename>" << endl;
      env.warning() << "Using default file" << endl;
      fileName = "../../../examples/data/whouse.dat";
    } else fileName = argv[1];
    readCosts(fileName, buildingCost, costs, nbClients, nbWhouses);
 
 
    IloIntVarArray offer(env, nbClients, 0, nbWhouses-1);
    IloIntVarArray transCost(env, nbClients, 0, 10000);
    IloBoolVarArray open(env, nbWhouses);
 
    for (i=0; i < nbClients; i++){
      model.add(transCost[i] == costs[i](offer[i]));
      model.add(open(offer[i]) == 1);
    }
 
    IloIntVar cost(env, 0, 10000, "Cost\t");
    model.add(cost == IloSum(transCost) + IloSum(open)*buildingCost);
 
    IloGoal goal = IloGenerate(env, offer) && IloInstantiate(env, cost);
    model.add(IloMinimize(env, cost));
 
    // end of model
 
    // We create the Parallel Solver
 
    IloParallelSolver psolver(model, 3);
 
    // We search for an optimal solution
 
    psolver.solve(goal);
 
    IloSolver solver = psolver.getWorker(psolver.getSuccessfulWorkerId());
    solver.out() << endl << "Optimal Solution" << endl;
    solver.out() << "Cost " << solver.getValue(cost) << endl;
    solver.out() << "Offer ";
    for (i=0; i<nbClients; i++)
      solver.out() << solver.getValue(offer[i]) << " ";
    solver.out() << endl;
    solver.out() << "TransCost ";
    for (i=0; i<nbClients; i++)
      solver.out() << solver.getValue(transCost[i]) << " ";
    solver.out() << endl;
    solver.out() << "Open ";
    for (i=0; i<nbWhouses; i++)
      solver.out() << solver.getValue(open[i]) << " ";
    solver.out() << endl;
    psolver.end();
  }
  catch (IloException& ex) {
    cout << "Error: " << ex << endl;
  }
  env.end();
  IloEndMT();
  return 0;
}