Sie sind auf Seite 1von 13

T RAFFIC FLOW SIMULATION

Final Report
Krishna Dutt AE11B017
3rd Undergraduate Dept. of Aerospace Engineering IIT Madras

May 2, 2014
While choosing Project for Object Oriented Programming,topics that use
a variety of design patterns, interaction between objects, and provide an opportunity to make a realistic design are always welcomed. Object oriented
languages were designed initially for simulations. This report summarises
the design concepts, methodology and processes involved in developing a code
to simulate Traffic at an Intersection.

Part I

Introduction :
This report documents the Final Project , titled Traffic Flow Simulation
At An Intersection, to be submitted as part of the coursework on Object Oriented Programming .It summarizes the concepts, methodology and
implementation of Traffic simulation, limited to a single intersection.

Motivation :
The class concept allowed me to map my application concepts
directly into the language constructs in a direct way that made
my code more readable than I had seen in any other language
- Bjarne Stroustrup

The above is a quote from Bjarne Stroustrup, who when he first started
designing C++ ,wanted to develop a better tool for simulation. He was one
of the pioneers who found out how effective, it was to use object oriented
approach in coding simulations.
The motivation behind choosing Traffic simulation is it is ideally suited
to implement object oriented design. Each component of the Traffic, be it a
vehicle or a road or a traffic light or an intersection can be modelled using an
object, which specifies both its characteristics and behaviour. The simulation
models the interaction of each individual object with its surrounding, thus
making the design more realistic .

Objective :

The main objectives of this project involve :


1. To simulate the traffic flow at an intersection.
2. To optimize the the frequency of traffic lights, with a given initial frequency of input of vehicles.
2

3. To animate the flow through time with traffic shown drawn-to-scale


4. And, finally to collect data about any traffic flow possible at the given
intersection.
The initial frequency of the Traffic lights and the vehicles, is accepted
from the user and later , as time progresses, the signal frequency is optimised based on the frequency of incoming vehicles.
This project is not aimed at any specific user group. It can be used
anyone with an access to the code or by by a technical analysts, who wants
to run simulation of specific Traffic flows.

Part II

Design and Implementation :


The programming language chosen for this project was C++ . This was mainly
due to its familiarity and the availability of a compiler on almost every machine.
The entire code is around 1300 lines of original C++ code spread over a
set of 9 classes.The code was written with extension in mind and it has been
commented where ever necessary to explain briefly the various methods and
classes used.

Classes :

The following gives the list of classes implemented in this project :


1. Environment - Main class Container class
2. Lane - Models Lanes in a road and manages the vehicles running
through it.
3. Incoming Lane - Child class of Lane which provides the incoming
vehicles into the system .
4. Intersection - Manages the flow at the cross section.
5. Vehicle - Manages the characteristics and behaviour of a vehicle.
6. Database Vehicle - Manages the database of vehicles.
7. Trafficlight - Models a traffic light in the system.
8. Coordinate - Provides and manages coordinates.
9. Simulator - Simulates the entire system .

Component Design

The following section deals with the structure and design of individual classes
.

4.1

Coordinate :

The class Coordinate encapsulates :


Data
1. xcord - X- coordinate
2. ycord - Y- coordinate
Methods
1. x() - returns the value of xcord.
2. y() - returns the values of ycord.
3. setx() - sets the value of xcord to the value passed to the function.
4. sety() - sets the value of ycord to the value passed to the function.
Apart from these methods, Operators like + , - , = are also overloaded.
Coordinate defines the underlying coordinte system used for the project,
which here is the cartesian system.

4.2

Trafficlight :

The class Trafficlight encapsulates :


Data
1. state - index referring to the array element of color[3]
2. color[3] - Array of 3 containing characters R,Y,G.
Methods
5

1. change state() - sets the index of the array element to a number


between 1 and 3 , passed to the function.
2. show color() - returns the present signal color.

4.3

Vehicle :

The class Vehicle encapsulates :


Data
1. dimension - stores the length and width of the vehicle.
2. velocity - stores the velocities of the vehicle in the considered two
perpendicular direction.
3. accelerate - stores the acceleration of the vehicle in the considered
two perpendicular direction.
4. coordinates - stores present coordinates of the Vehicle.
5. influence - stores factor of safety (in terms of length), the vehicle
needs to keep in order to prevent collision.
6. t - stores time for which the vehicle remains in the simulation.
7. direction - stores the direction in which the vehicle wants to turn at
the intersection.
8. front - points to the vehicle immediately in front of it.
9. back - points to the vehicle immediately behind it.
10. Max

accelerate - store the max acceleration of the vehicle.

11. Max

velocity - store the max velocity of the vehicle.

Methods
Apart from the regular methods to provide access and return the value
of a data, it contains, a method called compute() which computes the next

location / coordinates based on the revised acceleration and velocities.


Each vehicle is represented by an object of the vehicle class. At the time
of generation, each vehicle is assigned data of a type of vehicle as stored in
the Vehicle Database. The dimensions of the vehicle is limited such that a
maximum of three vehicles can align themselves parallel along the lanes .

Database Vehicle :

4.4

The class Database

Vehicle encapsulates :

Data
1. count - stores the no of types of vehicles whose data is currently stored
2.

element - An object of a structure, which stores all the relevant


information about a type of vehicle like its dimensions, its influence
region, its maximum acceleration and velocity.

Methods
All basic methods like Add() , edit() ,Delete() are provided, which
helps in modifying the database. The database is initialised with data of
3 types of vehicles namely, Car, Bus and Scooter.But, no hard copy of the
database is maintained as of now.

4.5

Lane :

The class Lane encapsulates :


Data
1. dimension - the length and width of the lane
2. points[2] - Starting and End coordinates of the lane.
3. V - linked list of vehicles to maintain the queue in respective lane.
4. T - Traffic light associated with respective lane.
7

5. axis - stores the orientation of each individual lane ie;its direction of


alignment.
Methods
The major methods associated with this class are given below :
1. Queue() - It rearranges the position of individual vehicle, taking into
consideration if overtaking is possible or not,and also prevents collisions
2. allocate() and deallocate() - adds and removes vehicles from the
queue.
3. transfer() - transfers all the vehicles that have reached the end of the
lane to the next lane.
Lane is an object of the class Lane .It is responsible for the flow of vehicles
in respective lane, making sure that vehicles do not collide and can overtake
where ever possible. It has a traffic light associated with it, which controls
the flow of vehicles into the lane.

4.6

Incoming Lane :

The class Incoming Lane inherits publicly from class Lane and has the
following extra behaviour :
Data
1. frequency - stores the frequency at which vehicles need to be generated.
Methods
1. generate() - generates vehicles, assigns initial values to the vehicle at
random from the database .
2. exit boundary() - stops the vehicles at the end of the lane if the
signal turns Red.
8

4.7

Intersection :

The class Intersection encapsulates :


Data
1. Lanes[2] - Intermediate paths to facilitate transfer of vehicles from one
lane to another.
2. entry lanes - Array containing the lanes to which the vehicles have
to enter.
Methods
1. receive() - facilitates the transfer of vehicles at the end of the Incoming
Lanes to the intermediate Lanes.
2. exchange() - transfers the vehicles to the lanes that they want to go,
depending on whether the lanes are free or not.
3. change signal()- change the signal of the lanes.
The object of class Intersection manages the flow within the intersection.
It sees if the required lanes are free or not and respectively queue the vehicles
along respective lanes.

4.8

Simulator :

The class Simulator encapsulates :


Data
1. Handle - File pointer to keep track of which stream is being used.
Methods
1. display environment() - to display the entire initial intersection.
2. display Lane() - to display Lanes
The object of this class takes care of all graphical display .The idea
of creating a separate class for this purpose was to decouple the graphical
interfaces/ tools from the rest of the code, so that the graphics tools could
be changed later without affecting the rest of the code.
9

4.9

Environment :

The class Environment encapsulates the objects of all other classes and
facilitates the interaction between them.

Architecture

The following section deals with the basic architecture of the design.
Figure 1: Design Flow Chart

The above flow chart gives the design flow of the code .The MAIN function
initially obtains the user input, passes them onto the object of environment.
The environment class invokes the simulator to display the initial layout of
the intersection.

10

After finding which Incoming Lane is free the control then passes to
intersection ,which facilitates the transfer between the Incoming Lane and
Lane .Finally, the new positions of the vehicles are displayed by invoking the
simulator and the control passes to the next time iteration.

11

Part III

Design Problems and


Improvements
Any design process inevitably faces some challenges or problems. A few of
the problems faced are listed below:
* The simulator was originally planned based on a graphics tool/ software,
but had to be done using plotting software Gnuplot.
* Though the shape of the lanes have not been specified , current code works
only for straight lanes.
* The initial proposal of varying the frequency of vehicles has not been
implemented
The future improvements include incorporating methods to overcome
above mentioned drawbacks of present design.Also, though not initially proposed, the database of vehicles could be extended to keep a hard copy of the
various details.Also, the simulation could be extended to that over multiple
intersections.

12

Part IV

Conclusion :
The project was aimed at modelling the traffic flow at an intersection.The
results couldnt be obtained properly due to lack of proper graphics tools.
Overall, this project gave me a good insight into the design process involved
in an object oriented design.It, also reconfirmed once again that simulations
are one of the best areas to use Object oriented design.

13

Das könnte Ihnen auch gefallen