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

The createVehicles function is defined as in Chapter 4, Minimizing the Number of Vehicles, except that you add a fixed cost to each vehicle and you add an upper bound to the distance traveled by each vehicle.

You use IloVehicle::setCost(IloDimension dim, IloNum unitCost) to set the cost of each vehicle to be directly proportional to the dimension _distance. This is the way that you have set costs on vehicles in the lessons you have worked on so far.

Step 3   -  

Set the vehicle cost proportional to the dimension

Add the following code after the comment
//Set the vehicle cost proportional to the dimension

     vehicle.setCost(_distance, 1.0);

To model this problem, each vehicle also has a fixed cost associated with it in addition to a cost proportional to the distance traveled. You use IloVehicle::setCost(fixedCost) to set a fixed cost of 250 units on each vehicle. Thus, using fewer vehicles will lower the total cost of the solution.

Step 4   -  

Set the fixed vehicle cost

Add the following code after the comment //Set the fixed vehicle cost

     vehicle.setCost(250.0);

You also add a constraint using IloVisit::getCumulVar that the total distance traveled by the vehicle has an upper bound of 350 distance units. Since vehicles can return to the depot and there is a fixed cost for using a vehicle, Dispatcher will try to find a solution with as few vehicles as possible. However, a vehicle can only travel a certain distance during a work period. This constraint makes sure that the route of the vehicle is not longer than that specified distance.

Step 5   -  

Add the constraint on total distance traveled by vehicle

Add the following code after the comment
//Add the constraint on total distance traveled by vehicle

    last.getCumulVar(_distance).setUb(350);

As in Chapter 2, Modeling a Vehicle Routing Problem, you use csv reader functionality to input vehicle data from a csv file. The vehicles have start and end visits. You set the capacities of the vehicles using IloVehicle::setCapacity and the dimension _weight. Here is the complete code for defining the createVehicles function:

void RoutingModel::createVehicles(const char* vehicleFileName) {
  IloCsvReader csvVehicleReader(_env, vehicleFileName);
  IloCsvReader::LineIterator  it(csvVehicleReader);
  while(it.ok()) {
    IloCsvLine line = *it;
    char * namefirst = line.getStringByHeader("first");
    char * namelast = line.getStringByHeader("last");
    char * name = line.getStringByHeader("name");
    IloNum capacity = line.getFloatByHeader("capacity");
    IloNode node1 = IloNode::Find(_env, namefirst);
    IloNode node2 = IloNode::Find(_env, namelast);
    IloVisit first(node1, "depot");
    _mdl.add(first.getCumulVar(_weight) == 0);
    IloVisit last(node2, "depot");
    IloVehicle vehicle(first, last, name);

    vehicle.setCost(250.0);

    vehicle.setCost(_distance, 1.0);

    vehicle.setCapacity(_weight, capacity);
    last.getCumulVar(_distance).setUb(350);

    _mdl.add(vehicle);
    ++it;
  }
  csvVehicleReader.end();
}