IBM ILOG Dispatcher User's Manual > The Basics > Modeling a Vehicle Routing Problem > Model > Define the createVehicles function

As you remember from Chapter 1, IBM ILOG Dispatcher Concepts, vehicles have start and end visits and capacities. In this lesson, you will use Concert Technology's csv functionality to input vehicle data from a csv file. You use the classes IloCsvReader, IloCsvReader::Iterator, and IloCsvLine. This code is provided for you:

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");

Next, you use the static member function IloNode::Find to find the nodes associated with the first and last visits the vehicle makes. This static member function takes two parameters: the environment and the key associated to the node.

Step 10   -  

Find the first and last nodes

Add the following code after the comment //Find the first and last nodes

    IloNode node1 = IloNode::Find(_env, namefirst);
    IloNode node2 = IloNode::Find(_env, namelast);
 

Next, you create the vehicle's first visit, an instance of the class IloVisit. In a VRP, the first and last visits of a vehicle usually take place at the depot. The constructor for IloVisit takes two parameters: an instance of IloNode and a name used for debug and trace purposes.

Step 11   -  

Create the first visit

Add the following code after the comment //Create the first visit

    IloVisit first(node1, "depot");

Then, you add constraints on the first visit to the model. The first constraint is that when the vehicle makes its first visit to the depot, it will not have delivered any weight. You also add the constraint that the vehicle cannot leave the first visit--the depot--until after the depot's opening time.

Step 12   -  

Add constraints to the first visit

Add the following code after the comment //Add constraints to the first visit

    _mdl.add(first.getCumulVar(_weight) == 0);
    _mdl.add(first.getCumulVar(_time) >= line.getFloatByHeader("open"));

Next, you create the vehicle's last visit, which also takes place at the depot. You add the constraint that the vehicle must arrive at the last visit--the depot--before the depot's closing time.

Step 13   -  

Create the last visit and add constraints

Add the following code after the comment
//Create the last visit and add constraints

    IloVisit last(node2, "depot");
    _mdl.add(last.getCumulVar(_time) <= line.getFloatByHeader("close"));

Then you create the vehicle, an instance of the class IloVehicle. The constructor for IloVehicle takes three parameters. The first two parameters are instances of IloVisit representing the first and last visits of this vehicle--in this lesson, these both take place at the depot. The last parameter is a name used for debug and trace purposes.

Step 14   -  

Create the vehicle

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

    IloVehicle vehicle(first, last, name);

After you create a vehicle, you set a proportional cost associated with operating this vehicle using the member function IloVehicle::setCost. This member function takes two parameters. The first parameter is a dimension. In this lesson, the cost is proportional to the distance the vehicle travels. The second parameter associates a unit of cost per unit of dimension. In this lesson, the cost is directly proportional to the distance traveled and this parameter is set to 1.0.

Step 15   -  

Set a cost for vehicle operation

Add the following code after the comment //Set a cost for vehicle operation

    vehicle.setCost(_distance, 1.0);

You also set the capacity of the vehicle using the member function IloVehicle::setCapacity.

Step 16   -  

Set the vehicle capacity

Add the following code after the comment //Set the vehicle capacity

    vehicle.setCapacity(_weight, capacity);

When you have finished creating the vehicle, you add it to the model.

Step 17   -  

Add the vehicle to the model

Add the following code after the comment //Add the vehicle to the model

    _mdl.add(vehicle);

Finally, you move to the next line in the csv file. When the end of the file is reached, you deallocate the memory used by the csv reader. This code is provided for:

    ++it;
  }
  csvVehicleReader.end();
}