IBM ILOG Solver User's Manual > Local Search > Combining Complete and Local Search: Locating Warehouses > Model > Read problem data from a file

We define a function readProblem to read the problem data from a file. We define each of the problem variables: the number of potential warehouses (nbWhouses), the total number of clients (nbClients), the array of capacities of the warehouses (capacities), the array of the demands of each of the clients (demands), the array of the costs to build each of the warehouses (buildCosts), and the two-dimensional array of the costs for each potential warehouse to service each of the clients (serveCosts).

// Function to read the problem from a file

void readProblem(IloEnv env,
                 istream &is,
                 IloInt &nbClients,
                 IloInt &nbWhouses,
                 IloIntArray &capacities,
                 IloIntArray &demands,
                 IloIntArray &buildCosts,
                 IloArray<IloIntArray> &serveCosts) {
  is >> nbWhouses >> nbClients;
  capacities = IloIntArray(env, nbWhouses);
  demands = IloIntArray(env, nbClients);
  buildCosts = IloIntArray(env, nbWhouses);
  serveCosts = IloArray<IloIntArray>(env, nbClients);
  env.out() << nbClients << " clients, " << nbWhouses << " warehouses" << endl;
  IloInt i;
  for (i = 0; i < nbWhouses; i++) {
    is >> capacities[i];
    is >> buildCosts[i];
  }
  env.out() << "Capacities: " << capacities << endl;
  env.out() << "Build costs: " << buildCosts << endl;
  for (i = 0; i < nbClients; i++) {
    serveCosts[i] = IloIntArray(env, nbWhouses);
    is >> demands[i];
    for (IloInt j = 0; j < nbWhouses; j++) is >> serveCosts[i][j];
  }
  env.out() << "Demands: " << demands << endl;
}

The following code opens the input file; determines whether to perform local search, complete (proof) search, or both (the default is to perform both); defines the variable arrays; and uses the function readProblem to read the problem data into those arrays. The choice of which type of search to perform can also be made on the command line.

    // Open input file.
    ifstream infile;
    const char *fname;
    if (argc > 1) fname = argv[1];
    else          fname = "../../../examples/data/cap71.txt";
    infile.open(fname);
    if (!infile.good()) {
      env.out() << "Could not open " << fname << endl;
      env.end();
      return 0;
    }
    // Decide on local search, proof, or local search, then proof.
    const char *lp = "lp";
    if (argc > 2) lp = argv[2];
    IloBool local = IloFalse;
    IloBool proof = IloFalse;
    if (strchr(lp, 'l')) local = IloTrue;
    if (strchr(lp, 'p')) proof = IloTrue;
  
    // Read in the problem
    IloInt nbWhouses, nbClients;
    IloIntArray capacities(env);
    IloIntArray demands(env);
    IloIntArray buildCosts(env);
    IloArray<IloIntArray> serveCosts;
    readProblem(env, infile, nbClients, nbWhouses,
                capacities, demands, buildCosts, serveCosts);
    infile.close();