IBM ILOG Dispatcher User's Manual > The Basics > Multiple Tours per Vehicle > Solve > Define the insertAllReturnVisits function

Next, you define the insertAllReturnVisits function which does the following:

You loop on each vehicle and create two return visits to the depot for each vehicle. This allows each vehicle to perform three tours. During the solution improvement phase, the number of tours can be rebalanced between vehicles.

Step 8   -  

Create the return visits

Add the following code after the comment //Create the return visits

void RoutingSolver::insertAllReturnVisits () {
  _solver.out() << "Inserting return visits" << endl;
  IloNode depot = IloNode::Find(_env, "depot");
  for (IloVehicleIterator vehIt(_mdl); vehIt.ok(); ++vehIt) {
    IloVehicle vehicle = *vehIt;
    for (IloInt i = 0; i < 2; i++) {
      IloVisit visit(depot, "depot");

Now, you use IloVisit::setPenaltyCost to set a negative penalty cost on the return visit. By default, the penalty cost for not performing a visit is IloInfinity, which forces the visit to be performed. If the return visit is performed, there is no additional cost added to the solution. If the return visit is not performed, then there is a small negative penalty cost. This makes not performing the return visit a slightly more favorable decision than performing the return visit, since you are minimizing the total cost. However, the negative penalty is so small that not performing the visit does not greatly affect the overall cost of the solution.

Step 9   -  

Set the return visit negative penalty cost

Add the following code after the comment
//Set the return visit negative penalty cost

      visit.setPenaltyCost(-0.1);
 

Next, you add the return visit to the model using IloModel::add.

Step 10   -  

Add the return visit to the model

Add the following code after the comment //Add the return visit to the model

      _mdl.add    (visit);

You insert the return visit directly into the route of the vehicle by creating a goal using IloInsertVisit. This function makes visit performed without the need to recompute the routing plan from scratch. If the visit cannot be inserted, the goal fails and the visit is removed from the model. If the visit is inserted in the vehicle route, you use the function IloRoutingSolution::add to directly add the return visit to the solution. The new solution is then stored.

Step 11   -  

Insert the return visits into the first solution

Add the following code after the comment
//Insert the return visits into the first solution

      IloGoal insert =
          IloInsertVisit(_env, visit, vehicle, _solution, _instantiateCost);
      if (!_solver.solve(insert)) {
        _solver.out() << "Cannot insert new visit in solution" << endl;
        _mdl.remove(visit);
      }
      else {
        _solution.add(visit);
        _solution.store(_solver);
      }
    }
  }
}