Sumo ns3 Tutorial
Sumo ns3 Tutorial
SUMO (Simulation of Urban MObility) is an open source, highly portable, microscopic and continuous multi-modal traffic
simulation package designed to handle large networks. It can be used to simulate vehicular traffic, from which we can
export a trace of vehicle positions that can be used in ns-3 simulations. The steps to perform this are:
1. Create a road map in SUMO. The format is and XML file that we will name map.net.xml
2. Define traffic routes that are used in a SUMO simulation. These flows can be generated randomly with the randomTrips.py
tool, or created manually be defining flows. We will name these file with a .rou.xml extension.
3. Create a SUMO simulation file that points to the map file, and a list of route files.
4. Run a SUMO simulation to generate a trace that we name sumoTrace.xml
5. Convert the trace to ns-2 mobility file using traceExporter.py tool.
6. Use the generated ns-2 trace with ns-3 by configuring it with Ns2MobilityHelper.
1 SUMO Overview
An installation of SUMO will contain a list of binaries and python scripts. I am mainly interested in the following binaries
sumo Runs a SUMO simulation in command-line, i.e. no graphics. This is what we use to generate an XML trace of
a simulation.
sumo-gui A GUI tool to run a SUMO simulation.
netedit A GUI tool to create SUMO maps.
netconvert A tool to generate a SUMO network map from existing files. This can be used to convert a map in
OpenStreetMap format to a SUMO map, but it can also be used to generate a map from files representing nodes and
edges.
On Ubuntu, if you installed SUMO using apt, these binaries will be in /usr/bin directory, and therefore, they will be
part of the $PATH. Otherwise, if you have built SUMO from sources, then you will have to add it to the path. For example,
if we installed it at $HOME/sumo then we will need to edit ˜/.bashrc with these lines
export SUMO_HOME=$HOME/sumo
export PATH=$PATH:$SUMO_HOME/bin:$SUMO_HOME/tools
1
SUMO mobility in NS3 Fall 2018
Click somewhere on the white map area to create a junction, then move the mouse to create another junction. This
process will create a road between the two red points.
While we still have the junction mode active, click on the second junction created to make, which will make it turn
green, then click somewhere else to create a third junction. This should look like this:
This is the bare minimum for a SUMO map to work, an independent set of road segments should at least have three segment
to work properly.
Notice the scale (0-10m), indicating that this is a very short road. Let’s now click the inspection tool from the tool
bar, then click on one of the red edges to change its position. Edit the position by changing 125 to 1000 so that the road
length is at about 1000 meters long.
<nodes>
<node id="west" x="0.0" y="25.0" />
<node id="east" x="1000.0" y="25.0" />
<node id="north" x="500.0" y="-525.0" />
<node id="south" x="500.0" y="525.0" />
<node id="mid" x="500.0" y="25.0" type="traffic_light"/>
</nodes>
<edges>
<!-- West to East-->
<edge from="west" id="eastbound1" to="mid" numLanes="3" />
<edge from="mid" id="eastbound2" to="east" numLanes="3" />
3 Creating routes
3.1 Creating flows manually
Alternatively, we can create a route file manually by defining traffic flows. Let’s create the file routes.rou.xml manually
as follows
<routes>
<vtype id="car_type" type="passenger" accel=3.5 decel=2.2 />
<vtype id="bus_type" type="bus" accel=1.2 decel=1.8 />
<flows>
<interval begin="0" end="360">
<flow id="0" from="genE1" to="genE2" probability="0.9" type="car_type"/>
<flow id="0" from="-genE2" to="genE1" probability="0.9" type="car_type"/>
You will have to make sure that the edges in the from and to fields are valid. Notice that we created two types of vehicles
with their corresponding acceleration & deceleration limits. Notice how the bus accelerates slower than a normal car. You
can create as many types as you want. Refer to SUMO documentation for information about all kinds of vehicle classes
you can use.
Which will create the routes file routes.rou.xml and we are done!
We can pass more parameters to randomTrips.py to control the frequency of vehicles generation, vehicle classes and
attributes if we want to. I’ll touch on that later.
<configuration>
<input>
<net-file value="map.net.xml"/>
<route-files value="routes.rou.xml"/>
</input>
</configuration>
sumo-gui sim.sumocfg
Before you run it by clicking on the play green button , let’s first change the “Delay (ms)” from 0 to 100 so that
simulation does not go faster than we can visualize it. Also, from the drop down menu that shows “standard” select “real
world” to have vehicles drawn instead of triangles.
SUMO mobility in NS3 Fall 2018
Once you click on the play button , simulation will start, and you can see vehicles moving in your map. Zoom in to get
a better view.
However, we would like to run the SUMO simulation, and store it in a trace file as follows:
and then export the resulting sumoTrace.xml file to an ns-2 file as follows:
The resulting ns-2 mobility file is in a language called Tcl, which ns-2 uses. The ns-2 file looks like this, showing the
first and last few lines:
As you can see, the file generates 1449 nodes (from 0 to 1448) and simulation time runs for 2316 seconds.
NodeContainer nodes;
nodes.Create(node_count);
If node count is less than the nodes in the SUMO generated trace, the simulation will work, but only the first node count
nodes will have that mobility installed. If node count is greater than SUMO nodes, the simulation will have a run-time
error.
SIM_TIME=600
#The value after -p is the period at which vehicles are generated in seconds. Change this as you need
PASS_PARAMS=-e $(SIM_TIME) -p 1 --vehicle-class passenger --trip-attributes="color=\"255,255,255\""
BUS_PARAMS=-e $(SIM_TIME) -p 30 --vehicle-class bus --trip-attributes="accel=\"0.8\""
TRUCK_PARAMS=-e $(SIM_TIME) -p 15 --vehicle-class truck --trip-attributes="color=\"179,223,183\""
TRAILER_PARAMS=-e $(SIM_TIME) -p 150 --vehicle-class trailer --trip-attributes="color=\"223,179,180\"
,→ accel=\"0.5\""
DELIVERY_PARAMS=-e $(SIM_TIME) -p 30 --vehicle-class delivery --trip-attributes="color=\"115,211,230\""
trace:
sumo -c sim.sumocfg --fcd-output sumoTrace.xml
traceExporter.py --fcd-input sumoTrace.xml --ns2mobility-output ns2mobility.tcl
trips:
# Generate random trips for different vehicle types
randomTrips.py -n map.net.xml -r bus_routes.rou.xml -o bus_trips.xml $(BUS_PARAMS)
randomTrips.py -n map.net.xml -r truck_routes.rou.xml -o truck_trips.xml $(TRUCK_PARAMS)
randomTrips.py -n map.net.xml -r delivery_routes.rou.xml -o delivery_trips.xml $(DELIVERY_PARAMS)
randomTrips.py -n map.net.xml -r passenger_routes.rou.xml -o passenger_trips.xml $(PASS_PARAMS)
randomTrips.py -n map.net.xml -r trailer_routes.rou.xml -o trailer_trips.xml $(TRAILER_PARAMS)
#Vehicles should have unique IDs, so I'll add a prefix to the ID according to type.
sed -i "s/vehicle id=\"/vehicle id=\"bus/g" bus_routes.rou.xml
sed -i "s/vehicle id=\"/vehicle id=\"truck/g" truck_routes.rou.xml
sed -i "s/vehicle id=\"/vehicle id=\"pass/g" passenger_routes.rou.xml
sed -i "s/vehicle id=\"/vehicle id=\"deliv/g" delivery_routes.rou.xml
sed -i "s/vehicle id=\"/vehicle id=\"trailer/g" trailer_routes.rou.xml
osm:
netconvert --osm-files map.osm -o map.net.xml
#To create a nicer looking map, we'll also obtain buildings information
polyconvert --net-file map.net.xml --osm-files map.osm -o map.poly.xml
sim:
sumo-gui sim.sumocfg
clean:
rm -f sumoTrace.xml ns2mobility.tcl
rm -f *.rou.xml *.rou.alt.xml *trips.xml
rm -f map.net.xml map.poly.xml
<configuration>
<input>
<net-file value="map.net.xml"/>
<route-files value="truck_routes.rou.xml, passenger_routes.rou.xml, bus_routes.rou.xml,
,→ trailer_routes.rou.xml, delivery_routes.rou.xml"/>
<additional-files value="map.poly.xml"/>
</input>
</configuration>
SUMO mobility in NS3 Fall 2018
make osm
make trips
make sim
If you want to generate an ns-2 mobility trace, then run the rule trace
make trace