> Discrete Optimization > Early Tardy Scheduling > Example: etsp.cpp

You can see the entire example here or online in the standard distribution of ILOG CPLEX at yourCPLEXhome/examples/src/etsp.cpp.

// -------------------------------------------------- -*- C++ -*-
// File: examples/src/etsp.cpp
// Version 9.0    
// --------------------------------------------------------------
//  Copyright (C) 1999-2003 by ILOG.
//  All Rights Reserved.
//  Permission is expressly granted to use this example in the
//  course of developing applications that use ILOG products.
// --------------------------------------------------------------
//

#include <ilcplex/ilocplex.h>

ILOSTLBEGIN

typedef IloArray<IloIntArray>    IntMatrix;
typedef IloArray<IloNumArray>    NumMatrix;
typedef IloArray<IloNumVarArray> NumVarMatrix;

const IloInt Horizon = 10000; 

int main(int argc, char** argv){
  IloEnv env;
  try {
    IloInt i, j; 

    const char* filename; 
    if (argc > 1)
      filename = argv[1];
    else
      filename = "../../../examples/data/etsp.dat";
    ifstream f(filename, ios::in);
    if (!f) {
      cerr << "No such file: " << filename << endl;
      throw(1);
    }

    IloEnv env; 

    IloInt nbJob;
    IloInt nbResource;

    f >> nbJob;
    nbResource = nbJob;

    // Activity processed by a resource
    IntMatrix   activityOnAResource(env, nbResource); 
    for(i = 0; i < nbResource; i++) {  
      activityOnAResource[i] = IloIntArray(env); 
    }

    IloNumArray jobDueDate(env, nbJob);        // Due date of a job
    IloNumArray jobEarlinessCost(env, nbJob);  // Earliness cost of a job
    IloNumArray jobTardinessCost(env, nbJob);  // Tardiness cost of a job 
 
    NumMatrix   duration(env, nbJob);          // Duration for an activity
    for(j = 0; j < nbJob; j++) {                   
      duration[j] = IloNumArray(env, nbResource); 
      // Read jobs duration
      for (i = 0; i < nbResource; i++) {
        IloInt resourceIndex;    
        f >> resourceIndex; 
        f >> duration[j][i];
        activityOnAResource[resourceIndex].add(i); 
      } 
      // Read cost information
      f >> jobDueDate[j];
      f >> jobEarlinessCost[j];
      f >> jobTardinessCost[j]; 
    } 

    IloModel model(env); 

    // Create start variables 
    NumVarMatrix s(env, nbJob);
    for(j = 0; j < nbJob; j++){
      s[j] = IloNumVarArray(env, nbResource, 0, Horizon);
    } 

    // State precedence constraints 
    for(j = 0; j < nbJob; j++){
      for(i = 1; i < nbResource; i++){
        model.add(s[j][i] >= s[j][i-1] + duration[j][i-1]); 
      } 
    } 

    // State disjunctive constraints for each resource
    for(i = 0; i < nbResource; i++) {
      IloInt end = nbJob - 1; 
      for(j = 0; j < end; j++){
        IloInt a = activityOnAResource[i][j];
        for(IloInt k = j + 1; k < nbJob; k++){
          IloInt b = activityOnAResource[i][k];
          model.add(s[j][a] >= s[k][b] + duration[k][b] 
                    || 
                    s[k][b] >= s[j][a] + duration[j][a]);
        } 
      } 
    } 

    // The cost is the sum of earliness or tardiness cost of each job 
    IloInt last = nbResource - 1; 
    IloExpr costSum(env); 
    for(j = 0; j < nbJob; j++) {
      costSum += IloPiecewiseLinear(s[j][last] + duration[j][last], 
                                    IloNumArray(env, 1, jobDueDate[j]), 
                                    IloNumArray(env, 2, 
                                                -jobEarlinessCost[j], 
                                                 jobTardinessCost[j]), 
                                    jobDueDate[j], 0);
    } 
    model.add(IloMinimize(env, costSum)); 

    IloCplex cplex(env);

    cplex.extract(model); 
    cplex.setParam(IloCplex::MIPEmphasis, 4); 

    if (cplex.solve()){
      cout << " Optimal Value = " << cplex.getObjValue() << endl; 
    } 
  } 
  catch (IloException& ex) {
    cerr << "Error: " << ex << endl;
   }
   catch (...) {
     cerr << "Error" << endl;
  }
  env.end(); 
  return 0; 
}