Now we need to extend the links so that they contain the vehicles. For our cellular automata (CA) approach, we represent the road by a 1-lane sequence of cells. In consequence,
{} class Link { ... private: typedef vector<Veh*> Cells ; Cells cells_ ; public: build() ; }As one sees, the road is a vector of pointers to
Veh
. If this
pointer is NULL
, then the corresponding cell is empty.
For modular programming, one would in fact introduce a new class, say
simlink
, and make it inherit from the link
class.
Unfortunately, this eventually means to templatize the link and node
classes, which we do not want to do at this point. Further details
are discussed in Chap. 10.
The build()
command builds the road, i.e. reserves memory
etc.:7.1
/home/nagel/src/book/sim/book/Link::build
LCELL
is a global constant containing the length of a cell
which we set to 7.5 meters. According to the code, the number of
cells is
push_back
is the command to add elements to a
vector
.7.2
We also need functions to add vehicles at the upstream end and remove them at the downstream end of the link. Similarly, one needs to be able to test for the availability of space, and get access to the most downstream of the vehicles. The code segment looks as follows:
{} class Link { ... void addToLink( Veh* veh ) { assert( cells_[0]==NULL ); cells_[0] = veh ; } veh* firstOnLink() { return cells_.back() ; } void rmFirstOnLink() { assert( cells_.back()!=NULL ) ; cells_.back() = NULL ; } bool hasSpace() { return cells_.front()==NULL ; } }
cells_.front()
and cells_.back()
are STL functions and
provide access to the first and the last element of the vector.
Finally, we need a method to move vehicles forward. This can look as follows:
{} class Link { ... void moveOnLink( int& nVehs ) ; void move( int& nVehs ) { moveOnLink( int& nVehs ) ; // more here to be added later ... } } ;and:
/home/nagel/src/book/sim/book/Link::moveOnLink
Note that this uses traditional array syntax, so alternative models
can be easily implemented even by programmers not fluent in C++.