IBM ILOG Dispatcher User's Manual > The Basics > Minimizing the Number of Vehicles > Solve > Define the improveWithNHood function

After you have found a first solution, you create the neighborhoods you will use in the solution improvement phase. As in Chapter 3, Solving a Vehicle Routing Problem, you use the predefined neighborhoods IloRelocate, IloExchange, IloCross, IloTwoOpt, and IloOrOpt. Additionally, you use three other predefined neighborhoods: IloMakePerformed, IloMakeUnperformed, and IloSwapPerform. The function IloMakePerformed returns a neighborhood that modifies a solution by inserting an unperformed visit after a performed one. The function IloMakeUnperformed returns a neighborhood that modifies a solution by causing a performed visit to be unperformed. The function IloSwapPerform returns a neighborhood that modifies a solution by exchanging a performed visit with an unperformed one. These neighborhoods are used to improve a solution after emptying a vehicle and constraining the visits assigned to it to be performed by some other vehicle.

For more information about how predefined neighborhoods work, see ïtò^ B Predefined Neighborhoods.

Step 8   -  

Create the neighborhoods

Add the following code after the comment //Create the neighborhoods

void RoutingSolver::improveWithNhood() {
  IloNHood nhood = (IloRelocate(_env) + IloExchange(_env) + IloCross(_env))
                   + (IloTwoOpt(_env) + IloOrOpt(_env))
                   + (IloMakePerformed(_env)
                    + IloMakeUnperformed(_env)
                    + IloSwapPerform(_env));

The rest of the function is defined as in Chapter 3, Solving a Vehicle Routing Problem. You use the function IloSingleMove to return a goal that makes a single local move as defined by a neighborhood and a search heuristic. The following code is provided for you:

  _solver.out() << "Improving solution" << endl;
  IloGoal improve = IloSingleMove(_env,
                                  _solution,
                                  nhood,
                                  IloImprove(_env),
                                  _instantiateCost);
  while (_solver.solve(improve)) {
  }
  _solver.solve(_restoreSolution);
}