IBM ILOG Dispatcher User's Manual > Transportation Industry Solutions > Adding Vehicle Breaks > Solve

The solve portion of the example needs to add the breaks to the solution and instantiate them. You will add the breaks to the solution with a function called addBreaks.

Step 7   -  

Create the RoutingSolver class

l

Add the following code after the comment //Create the RoutingSolver class.

class RoutingSolver {
  IloModel _mdl;
  IloEnv _env;
  IloSolver _solver;
  IloRoutingSolution _solution;
  IloRoutingSolution _breaks;

  IloBool findFirstSolution(IloGoal goal);
  void greedyImprove(IloNHood nhood, IloGoal subGoal);
  void improve(IloGoal subgoal);
  void addBreaks();

Notice the differences in RoutingSolver compared to the PDP version: there is a second IloRoutingSolution called _breaks. Additionally, a function addBreaks is present, which adds the breaks to _breaks.

Step 8   -  

Add breaks to the _breaks routing solution

l

Add the following code after the comment
//Add breaks to the _breaks routing solution.

//add breaks to _breaks routing solution
void RoutingSolver::addBreaks() {
  IloVehicleBreakConIterator it(_mdl);
  while ( it.ok() ) {
    _breaks.add(*it);
    ++it;
  }
}

Next, the breaks must be instantiated to ensure that the start times and durations are set in the solution.

Step 9   -  

Instantiate the vehicle breaks

l

Add the following code after the comment //Instantiate the vehicle breaks.

void RoutingSolver::solve() {
  IloDispatcher dispatcher(_solver);

  //add breaks to _breaks routing solution
  addBreaks();

  // Subgoals

  //The IloInstantiateVehicleBreaks goal uses a precision of 1 minutes
  //and treats all vehicles independently in the instantiation of 
  //vehicle breaks
  IloGoal instantiateBreaks = IloLimitSearch(_env,
                                             IloInstantiateVehicleBreaks(_env, 
1.0/60.0, IloTrue),
                                             IloFailLimit(_env, 100));

  IloGoal instantiateCost = IloDichotomize(_env,
                                           dispatcher.getCostVar(),
                                           IloFalse);

  IloGoal subGoal = instantiateBreaks && instantiateCost;
  IloGoal goal = IloSavingsGenerate(_env, instantiateBreaks) && 
instantiateCost;
  IloGoal restoreSolution = IloRestoreSolution(_env, _solution) && 
IloRestoreSolution(_env, _breaks);

The goal IloInstantiateVehicleBreaks instantiates the breaks of all vehicles (decides exactly where, when, and for how long they are taken) in the environment _env. Many other break instantiation goals are provided to increase flexibility and allow you to build your own goals, if desired (see the IBM ILOG Dispatcher Reference Manual for more information).

The parameter 1.0/60.0 ensures that a precision of one minute is used in calculating the break times, and IloTrue means that the goal will instantiate all the breaks in the routing plan, independent vehicle by independent vehicle.

The goal instantiateBreaks is created with an instance of IloLimitSearch. This ensures that the solution search will fail if no initial solution is found after 100 iterations. A goal is created to instantiate the cost variable for the dispatcher. A first solution is then sought. The goal IloSavingsGenerate builds that first solution. If a first solution is found, the solution is improved using neighborhoods. Note that in the proposed solution, only a simple greedy search is used. You might want to try improvement methods other than the basic IloImprove; for example IloDispatcherGLS or IloDispatcherTabuSearch.

If no first solution is found, the program ends.

Step 10   -  

Compile and run the program

The solution improvement phase finds a solution using 6 vehicles with a cost of 1097.5 units:

 1127.84
Improving solution
Number of fails               : 0
Number of choice points       : 0
Number of variables           : 3364
Number of constraints         : 1118
Reversible stack (bytes)      : 554784
Solver heap (bytes)           : 2460520
Solver global heap (bytes)    : 156444
And stack (bytes)             : 20124
Or stack (bytes)              : 44244
Search Stack (bytes)          : 4044
Constraint queue (bytes)      : 17172
Total memory used (bytes)     : 3257332
Elapsed time since creation   : 0.04
Number of nodes               : 51
Number of visits              : 80
Number of vehicles            : 15
Number of dimensions          : 3
Number of accepted moves      : 4
===============
Cost         : 1097.5
Number of vehicles used : 6
 

The complete program and output are listed in "Complete Program". You can also view it online in the YourDispatcherHome/examples/src/breaks.cpp file.