IBM ILOG Scheduler User's Manual > Advanced Concepts > Moving Activities from Discrete to Unary Resources > Solving the Problem

The following code defines a selector which selects the earliest activity to which no resource is assigned. This selector needs a visitor to visit all the activities of the environment, a predicate to filter the ones that are still not allocated, and an evaluator to select the activity with the smallest minimal start time.

// selector for non-assigned activity
 
ILOVISITOR0(ForAllActivitiesInEnv,
	    IloActivity, 
	    IloEnv, env) {
  for(IloIterator<IloActivity> ite(env); ite.ok(); ++ite) {
    visit(*ite);
  }
}
 
ILOPREDICATE0(IsAllocatedPredicate,
	      IloActivity, act) {
  if (act.getObject() == 0)
    return IloFalse;
  else
    return IloTrue;
}
 
ILOEVALUATOR0(StartMinEvaluator,
	      IloActivity, act) {
  return act.getStartMin(); 
}
 
  IloPredicate<IloActivity> notAllocated = !IsAllocatedPredicate(env);
  IloSelector<IloActivity,IloEnv> selector =
    IloBestSelector<IloActivity,IloEnv>
    (ForAllActivitiesInEnv(env), 
     notAllocated , 
     StartMinEvaluator(env).makeLessThanComparator());
 

Solving the problem consequently consists of iterating the following loop.

  1. Select the next activity in chronological order.
  2. Find a resource for this activity. For each resource, try to assign the resource to the activity. If the assignment succeeds, proceed to the next activity. If the assignment fails, proceed to the next resource for the activity under consideration.

The following function implements the algorithm. It returns IloTrue if the problem has a solution. Otherwise, it returns IloFalse. The function can return IloFalse only if there exists a time point t such that the number of activities executing at t exceeds the number of unary resources that are available. In other words, the function returns IloTrue if the global resource capacity constraint is satisfied by the solution; it returns IloFalse if the global resource capacity constraint is not satisfied by the solution.

IloBool
SolveProblem(IloSolver solver, IloModel model) {
  IloEnv env = model.getEnv();
  IlcScheduler scheduler = IlcScheduler(solver);
  IloActivity act;
 
  IloPredicate<IloActivity> notAllocated = !IsAllocatedPredicate(env);
  IloSelector<IloActivity,IloEnv> selector =
    IloBestSelector<IloActivity,IloEnv>
    (ForAllActivitiesInEnv(env), 
     notAllocated , 
     StartMinEvaluator(env).makeLessThanComparator());
 
  while ( selector.select(act, env) ) {
    for (IloIterator<IloUnaryResource> iterator(model.getEnv());
         notAllocated(act);
         ++iterator) {
      if (iterator.ok()) {
        IloUnaryResource res = *iterator;
        IloConstraint ct = act.requires(res);
        model.add(ct);
 
        if(solver.solve()) 
        {
          // store the assigned resource for the given activity
          act.setObject( (void*)(*iterator).getImpl() );
        }
        else {
          model.remove(ct);
        }
      }
      else
        return IloFalse;
    }
  }
  return IloTrue;
}