IBM ILOG Dispatcher User's Manual > Developing Dispatcher Applications > Developing Your Own Neighborhoods > Implementing the Neighborhood

Recall that the above function only swaps a single visit with a predefined set of visits. Our initial goal was to create a neighborhood which would swap all visits within a certain range of each other. This more complete neighborhood is constructed by the following function. The essential technique is quite simple. You construct an array of neighborhoods, one corresponding to each visit (an instance of LocalizedExchange). The set of visits for each LocalizedExchange is calculated according to a proximity rule and to a symmetry rule. The array of neighborhoods is then concatenated to produce one larger neighborhood. The function is passed with the following parameters:

Given this information, the implementation of the function follows:

IloNHood LocalizedExchange(IloEnv env,
                           IloVisitArray visits,
                           IloDimension2 dim,
                           IloVehicle modelVehicle,
                           IloNum roundTrip) {
  IloNHoodArray nhoods(env, visits.getSize() - 1);
  for (IloInt i = 0; i < visits.getSize() - 1; i++) {
    IloVisit v = visits[i];
    IloVisitArray arr(env);
    for (IloInt j = i + 1; j < visits.getSize(); j++) {
      IloVisit w = visits[j];
      IloNum dist2 = v.getDistanceTo(w, dim, modelVehicle) +
                     w.getDistanceTo(v, dim, modelVehicle);
      if (dist2 <= roundTrip) arr.add(w);
    }
    nhoods[i] = new (env) LocalizedExchangeI(env, v, arr);
  }
  return IloConcatenate(env, nhoods);
}

You can view the entire program and output online in the file YourDispatcherHome/examples/src/newnhood.cpp.