27 June 2016

SUMO, Open Street Maps and NS2 - A Real Traffic Simulation

SUMO is the Simulation of Urban mobility software that enables to simulate the road traffic. Open Street map (www.openstreetmap.org)  provides the xml based .osm file for any part of the world selected through their website. #openstreetmap

#SUMO can able to convert the osm file to its native xml file. The post tells you the simulation of a real traffic network and how it is ported to #ns2 for network animation and tracing.

Step 1: Prerequisites

  • SUMO to be installed - in my case I installed sumo this way 
sudo add-apt-repository pap:sumo/stable
sudo apt-get update
sudo apt-get install sumo sumo-doc sumo-tools

Also, Download the sumo source sumo-src-0.26.tar.gz from this link and unzip or untar it to the home directory (/home/pradeepkumar). There are some python files that are needed to generate random trips and to export xml files to tcl files. the commands sumo, sumo-gui will run only the graphical simulation.
Once the software is unzipped, set the SUMO_HOME in the environment, Open /home/pradeepkumar/.profile (for Linux Mint) and /home/pradeepkumar/.bashrc (for Ubuntu) and give this command (in my case the home is pradeepkumar, it might be different for you)
export SUMO_HOME=/home/pradeepkumar/sumo-0.26.0 

Step 2: Steps to create a Traffic

  1. Open browser and type http://www.openstreetmap.org and search a particular area and click export in the top. 
  2. click "Manually select an area" and select the area (as shown in the video below) and click export again (it will download a file called map.osm, rename this file as per your convenience)
  3. Open the terminal and type the commands one by one (assume my file name is guindy.osm, so i maintain the same file name guindy for all the xml files)
$] netconvert --osm-files guindy.osm -o guindy.net.xml

Now, copy the osmPolyconvert.typ.xml from the $SUMO_HOME/data/typemap/ and copy it to the folder where you put all the files. 


$] polyconvert --osm-files guindy.osm --net-file guindy.net.xml --type-file osmPolyconvert.typ.xml -o guindy.poly.xml



$] python $SUMO_HOME/tools/randomTrips.py -n guindy.net.xml -r guindy.rou.xml -e 100 -l

Now, create a new file (to be named as guindy.sumo.cfg) and paste the following lines 

 <configuration>
     <input>
         <net-file value="guindy.net.xml"/>
<route-files value="guindy.rou.xml"/>
         <additional-files value="guindy.poly.xml"/>
     </input>
<time>
<begin value="0"/>
<end value="100"/>
<step-length value="0.1"/>
</time>
 </configuration>

Now you can run the above using sumo-gui guindy.sumo.cfg (now you can see as per the following screenshot)
sumo
SUMO


Step 3: Exporting to NS2
from sumo trace to ns2, here is the step to do
Open terminal and type the following commands,

$] sumo -c guindy.sumo.cfg --fcd-output guindy.sumo.xml


$] python /home/pradeepkumar/sumo-0.26.0/tools/traceExporter.py --fcd-input guindy.sumo.xml --ns2config-output guindy.tcl --ns2activity-output activity.tcl --ns2mobility-output mobility.tcl​

This will generate three tcl files (guindy.tcl, activity.tcl and mobility.tcl). Among this activity.tcl might not be needed, but mobility.tcl file is mandatory)

The generated guindy.tcl have to be modified or altered as per the networking parameters like routing protocol, Mac layer, physical layer, link layer, etc.

You can see the following video for all the instructions.




A Sample TCL File for your Use

Here is the guindy.tcl file, you can use it for your purpose
set val(chan)           Channel/WirelessChannel    ;# channel type
set val(prop)           Propagation/TwoRayGround   ;# radio-propagation model
set val(netif)          Phy/WirelessPhy            ;# network interface type
set val(mac)            Mac/802_11                 ;# MAC type
set val(ifq)            Queue/DropTail/PriQueue    ;# interface queue type
set val(ll)             LL                         ;# link layer type
set val(ant)            Antenna/OmniAntenna        ;# antenna model
set val(ifqlen)         50                         ;# max packet in ifq
set val(nn)            41                          ;# number of mobilenodes
set val(rp)             AODV                       ;# routing protocol
set opt(x) 4707
set opt(y) 3002
# ======================================================================
# Main Program
# ======================================================================


#
# Initialize Global Variables
#
set ns_ [new Simulator]
set tracefd     [open guindy.tr w]
$ns_ trace-all $tracefd

set namf [open guindy.nam w]
$ns_ namtrace-all-wireless $namf $opt(x) $opt(y)
# set up topography object
set topo       [new Topography]

$topo load_flatgrid $opt(x) $opt(y)
#
# Create God
#
create-god $val(nn)

#
#  Create the specified number of mobilenodes [$val(nn)] and "attach" them
#  to the channel. 
#  Here two nodes are created : node(0) and node(1)

# configure node

        $ns_ node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON
 
for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns_ node]
$node_($i) random-motion 0 ;# disable random motion
$ns_ initial_node_pos $node_($i) 20
}

#

#

source mobility.tcl

set tcp [new Agent/TCP]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns_ attach-agent $node_(0) $tcp
$ns_ attach-agent $node_(22) $sink
$ns_ connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns_ at 10.0 "$ftp start" 

#
# Tell nodes when the simulation ends
#
for {set i 0} {$i < $val(nn) } {incr i} {
    $ns_ at 100.0 "$node_($i) reset";
}
$ns_ at 100.0 "stop"
$ns_ at 100.01 "puts \"NS EXITING...\" ; $ns_ halt"
proc stop {} {
    global ns_ tracefd
    $ns_ flush-trace
    close $tracefd
}

puts "Starting Simulation..."
$ns_ run

Just comment below if you need any queries (the above video will give you full information).
T S Pradeep Kumar
SaveSave