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

You create vehicles in the same way you did 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 add side constraints that the vehicles must leave the depot after it opens and return to the depot before it closes. You set the capacities of the vehicles using IloVehicle::setCapacity and the dimension _weight. Using IloVehicle::setCost, the cost of each vehicle is set to be directly proportional to the dimension _time. This code is provided for you:

void RoutingModel::createVehicles(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);
    _mdl.add(first.getCumulVar(_time) >= line.getFloatByHeader("open"));
    IloVisit last(node2, "depot");
    _mdl.add(last.getCumulVar(_time) <= line.getFloatByHeader("close"));
    IloVehicle vehicle(first, last, name);
    vehicle.setCost(_time,   1.0);
    vehicle.setCapacity(_weight, capacity);
    _mdl.add(vehicle);
    ++it;
  }
  csvVehicleReader.end();
}