IBM ILOG Scheduler User's Manual > Getting Started with Scheduler > Using the Building Blocks > Adding Resources and Resource Constraints > Complete Program and Output--Example 2

You can see the entire program gsUnary.cpp here or view it online in the standard distribution.

#include <ilsched/iloscheduler.h>
 
ILOSTLBEGIN
 
#if defined(ILO_SDXLOUTPUT)
#include "sdxloutput.h"
#endif
 
///////////////////////////////////////////////////////////////////////////////
//
// PROBLEM DEFINITION
//
///////////////////////////////////////////////////////////////////////////////
 
IloModel DefineModel(IloEnv env, IloNumVar& makespan)
{
  IloModel model(env);
 
  /* CREATE THE ACTIVITIES. */
  IloActivity masonry(env,   7, "masonry   ");
  IloActivity carpentry(env, 3, "carpentry ");
  IloActivity plumbing(env,  8, "plumbing  ");
  IloActivity ceiling(env,   3, "ceiling   ");
  IloActivity roofing(env,   1, "roofing   ");
  IloActivity painting(env,  2, "painting  ");
  IloActivity windows(env,   1, "windows   ");
  IloActivity facade(env,    2, "facade    ");
  IloActivity garden(env,    1, "garden    ");
  IloActivity moving(env,    1, "moving    ");
  
  /* ADD THE TEMPORAL CONSTRAINTS. */
  model.add(carpentry.startsAfterEnd(masonry));
  model.add(plumbing.startsAfterEnd(masonry));
  model.add(ceiling.startsAfterEnd(masonry));
  model.add(roofing.startsAfterEnd(carpentry));
  model.add(painting.startsAfterEnd(ceiling));
  model.add(windows.startsAfterEnd(roofing));
  model.add(facade.startsAfterEnd(roofing));
  model.add(facade.startsAfterEnd(plumbing));
  model.add(garden.startsAfterEnd(roofing));
  model.add(garden.startsAfterEnd(plumbing));
  model.add(moving.startsAfterEnd(windows));
  model.add(moving.startsAfterEnd(facade));
  model.add(moving.startsAfterEnd(garden));
  model.add(moving.startsAfterEnd(painting));
  /* CREATE THE RESOURCE. */
  IloUnaryResource worker(env);
  /* ADD THE RESOURCE CONSTRAINTS. */
  model.add(masonry.requires(worker));
  model.add(carpentry.requires(worker));
  model.add(plumbing.requires(worker));
  model.add(ceiling.requires(worker));
  model.add(roofing.requires(worker));
  model.add(painting.requires(worker));
  model.add(windows.requires(worker));
  model.add(facade.requires(worker));
  model.add(garden.requires(worker));
  model.add(moving.requires(worker));
  /* SET THE MAKESPAN VARIABLE. */
  makespan = IloNumVar(env, 0, IloInfinity, ILOINT);
  model.add(moving.endsAt(makespan));
 
  /* SET THE OBJECTIVE */
  model.add(IloMinimize(env, makespan));
  return model;
}
 
 
///////////////////////////////////////////////////////////////////////////////
//
// PRINTING OF SOLUTIONS
//
///////////////////////////////////////////////////////////////////////////////
 
void PrintSolution(const IloSolver solver, const IloNumVar makespan)
{
  IlcScheduler scheduler(solver);
  IloEnv env = solver.getEnv();
  env.out() << "Solution with makespan " 
            << solver.getIntVar(makespan).getMin() << endl;
  for(IloIterator<IloActivity> act(env);
      act.ok();
      ++act)
    env.out() << scheduler.getActivity(*act) << endl;
  solver.printInformation();
}
 
///////////////////////////////////////////////////////////////////////////////
//
// MAIN FUNCTION
//
///////////////////////////////////////////////////////////////////////////////
 
int main()
{
  try {
    IloEnv env;
    IloNumVar makespan;
    IloModel model = DefineModel(env, makespan);
 
    IloGoal goal = IloRankForward(env, makespan);
    IloSolver solver(model);
    if (solver.solve(goal)) {
      PrintSolution(solver,makespan);
 
#if defined(ILO_SDXLOUTPUT)
      IloSDXLOutput output(env);
      ofstream outFile("gsUnary.xml");
      output.write(IlcScheduler(solver), outFile, solver.getIntVar(makespan));
      outFile.close();
#endif
    }
    else
      solver.out() << "No Solution" << endl;
    env.end();
    
  } catch (IloException& exc) {
    cout << exc << endl;
  }
  return 0;
}
 
///////////////////////////////////////////////////////////////////////////////
//
// RESULTS
//
///////////////////////////////////////////////////////////////////////////////
 
/*
  Solution with makespan 29
  masonry    [0 -- 7 --> 7]
  carpentry  [15 -- 3 --> 18]
  plumbing   [7 -- 8 --> 15]
  ceiling    [18 -- 3 --> 21]
  roofing    [21 -- 1 --> 22]
  painting   [24 -- 2 --> 26]
  windows    [27 -- 1 --> 28]
  facade     [22 -- 2 --> 24]
  garden     [26 -- 1 --> 27]
  moving     [28 -- 1 --> 29]
*/
 

The start and end times of all activities are fixed in this example.

Figure 3.3 provides a graphic display of the solution to our problem.

useBuildBlocksv3.gif

Figure 3.3 Solution with Resource Constraints