IBM ILOG Dispatcher User's Manual > The Basics > Modeling a Vehicle Routing Problem > Model > Define the createIloNodes function |
Define the createIloNodes function |
INDEX
![]() |
As you remember from Chapter 1, IBM ILOG Dispatcher Concepts, Dispatcher represents physical locations as nodes. Nodes can be depots, where the vehicles pick up goods to be delivered, or customer locations, where the goods are delivered. These nodes are then used to compute distances and times--and therefore cost--between customers and the depot. In this lesson, a node is defined by coordinates that give its location.
You will use Concert Technology's csv reader functionality to input the node data from a csv file. An instance of the class IloCsvReader
is used to read a csv file. The constructor takes two parameters. The first parameter is the environment and the second parameter is the name of the csv file.
Step 5 - | Create the node csv reader |
Add the following code after the comment //Create the node csv reader
void RoutingModel::createIloNodes(const char* nodeFileName) { IloCsvReader csvNodeReader(_env, nodeFileName); |
Now, you use the nested class IloCsvReader::Iterator
to create an iterator to step through all the lines of the csv data file, except blank lines and commented lines. The IloCsvReader
operator *
returns the current instance of IloCsvLine
, which is the current line in the csv file. You use the member function IloCsvLine::getStringByHeader
to return a pointer to the string contained in the field name
in the csv line. In this lesson, this name
is the name of the customer site or depot.
Step 6 - | Create the iterator |
Add the following code after the comment //Create the iterator
IloCsvReader::LineIterator it(csvNodeReader); while(it.ok()) { IloCsvLine line = *it; char * name = line.getStringByHeader("name"); |
You then create an instance of IloNode
using the data from the csv file. The constructor for IloNode
used in this lesson takes five parameters. The first parameter is the environment. The second, third, and fourth parameters are the x, y, and z coordinates of the node. In this lesson, the z coordinate is set to 0. The x and y coordinates are obtained by using the member function IloCsvLine::getFloatByHeader
to return a reference to the floating-point values contained in the fields x
and y
in the csv line. The last parameter is a name used for debug and trace purposes.
Step 7 - | Create the node |
Add the following code after the comment //Create the node
IloNode node(_env, line.getFloatByHeader("x"), line.getFloatByHeader("y"), 0, name); |
Next, you set a unique key on the node in order to be able to access it easily when you create vehicles and visits.
Step 8 - | Set the key |
Add the following code after the comment //Set the key
node.setKey(name); |
Then, you move the iterator to the next line in the csv file using the IloCsvReader::Iterator
operator ++
.
Step 9 - | Move to the next line in the csv file |
Add the following code after the comment //Move to the next line in the csv file
++it; } |
Finally, you should use the member function IloCsvReader::end
to deallocate the memory used by the csv reader. This code is provided for you:
csvNodeReader.end(); } |
© Copyright IBM Corp. 1987, 2009. Legal terms. | PREVIOUS NEXT |