IBM ILOG Solver User's Manual > Extending the Library > Writing a Goal: Car Sequencing > Writing a goal: Car sequencing > Inputting data

The input for the program in this example is data read from a file. To read the data from the input file, you write a function, readData. This function assumes that the data are recorded in a text file with a format based on the following:

You can edit either of the two input files among the examples to get a better idea of how the file is formatted.

The function readData reads the information from the input file. That function looks like this:

IloArray<IloIntArray> readData(IloEnv env,
                               IloInt example,
                               IloInt& nbCars,
                               IloInt& nbOpt,
                               IloIntArray& nbMax,
                               IloIntArray& seqWidth,
                               IloIntArray& confs,
                               IloIntArray& nbCarsByConf){
  char globalName[200];
  switch(example){
  case 1:{
    strcpy(globalName,"../../../examples/data/carseq1.dat");
  } break;
  case 2:{
    strcpy(globalName,"../../../examples/data/carseq2.dat");
  } break;
  }
  IloInt nbConfs;
  ifstream fin(globalName,ios::in);
  if (!fin) env.out() << "problem with file:" << globalName << endl;
  fin >> nbCars >> nbOpt >> nbConfs;
 
  IloInt i;
  confs = IloIntArray(env, nbConfs); // required configurations
  for(i=0;i<nbConfs;i++){
    confs[i]=i;
  }
  IloArray<IloIntArray> confsByOption(env, nbConfs);
  for (i=0;i<nbConfs;i++){
    confsByOption[i] = IloIntArray(env, nbOpt);
  }
  IloIntArray nbConfsByOption(env, nbOpt);
  for (i=0;i<nbOpt;i++){
    nbConfsByOption[i]=0;
  }
 
  // read the maximum number of cars of each sequence that can take the
  // invoked option
  nbMax=IloIntArray(env, nbOpt);
  for(i=0;i<nbOpt;i++){
    fin >> nbMax[i];
  }
 
  // read the size of a sequence for each option
  seqWidth=IloIntArray(env, nbOpt);
  for(i=0;i<nbOpt;i++){
    fin >> seqWidth[i];
  }
 
  // read the options required by each configuration
  nbCarsByConf=IloIntArray(env, nbConfs);
  IloInt dummy;
  IloInt j;
  for(i=0;i<nbConfs;i++){
    fin >> dummy;
    fin >> nbCarsByConf[i];
    for (j=0;j<nbOpt;j++){
      fin >> confsByOption[i][j];
      if (confsByOption[i][j] == 1){
        nbConfsByOption[j]++;
      }
    }
  }
 
 

To represent the data for any given option, you need to know the configurations that require it. The following code does that.

   // compute the configurations required by each option
  IloArray<IloIntArray> optConf(env, nbOpt);
  for(i=0;i<nbOpt;i++){
    optConf[i]=IloIntArray(env, (IloInt)nbConfsByOption[i]);
  }
  IloIntArray ind(env, nbOpt);
  for(i=0;i<nbOpt;i++){
    ind[i]=0;
  }
  for(i=0;i<nbConfs;i++){
    for (j=0;j<nbOpt;j++){
      if (confsByOption[i][j] == 1){
        optConf[j][(IloInt)ind[j]]=i;
        ind[j]++;
      }
    }
  }

As it is read, the data is displayed on the screen by the following code.

  // print the configuration required by each option
  env.out() << "number of times each configuration is required:" << endl;
  for(i=0;i<nbConfs;i++){
    env.out() << nbCarsByConf[i] << " ";
  }
  env.out() << endl;
  env.out() << "configuration required by each option:" << endl;
  for(i=0;i<nbOpt;i++){
    env.out() << "option:" << i << " ";
    for (j=0;j<optConf[i].getSize();j++){
      env.out() << optConf[i][j] << " ";
    }
    env.out() << endl;
  }
  env.out() << "number of times each option is required" << endl;
  for(i=0;i<nbOpt;i++){
    IloInt cpt=0;
    for(j=0;j<optConf[i].getSize();j++){
      cpt += nbCarsByConf[(IloInt)optConf[i][j]];
    }
    env.out() << "option:" << i << " " << cpt << " x ";
    env.out() << nbMax[i] << "/" << seqWidth[i] << endl;
  }

  return optConf;
}