Skip to main content

Random and Queuing Models using NS2 | NS2 Basics | Lecture 11

Random and Queuing Models using NS2


In this post, the readers may learn

  • how to create random numbers for various distributions using different seeds

  • Simulation of various queuing models like M/M/1 and M/D/1


13.1 Random Number Generation


Random variables is an important concept in networks as the modelling of network traffic and other packet arrival times are mostly random models. Hence, there is a necessity to model such metrics in ns2. NS2 supports various random models using different seed generations.


Seeds are numbers that help generate random numbers. The seed number 0 indicates that the random number order changes every time the simulation is run. But other than 0, the order in which the random numbers are generated same.


The following listing 13.1 shows the random number generation for various distributions. This listing will just tell you how to create random number generation for various distributions. For seeing the output, refer to listing 13.2

Listing 13.1 – Random Number Generation Sample

#create the random number generation using RNG

set rand1 [new RNG]

set rand2 [new RNG]

set rand3 [new RNG]

#$repli is the value set already and it can be either 1 or <1 or >1

for {set i 1} {$i < $repli} {incr i} {

$rand1 next-substream;

$rand2 next-substream;

$rand3 next-substream;

}


Here are the distributions

Pareto Distribution

set r1 [new RandomVariable/Pareto]

$r1 use-rng $rand1

$r1 set avg_ 10.0

$r1 set shape_ 1.2


Constant – Specifies the constant number

set r2 [new RandomVariable/Constant]

$r2 use-rng $rand2

$r2 set val_ 5.0


Uniform distribution – Specifies the min and max number

set r3 [new RandomVariable/Uniform]

$r3 use-rng $rand3

$r3 set min_ 0.0

$r3 set max_ 10.0


Exponential distribution.- Specified the average value:

set r4 [new RandomVariable/Exponential]

$r4 use-rng $rand1

$r4 set avg_ 5


Hyperexponential distribution

set r5 [new RandomVariable/HyperExponential]

$r5 use-rng $rand2

$r5 set avg_ 1.0

$r5 set cov_ 4.0


The following listing 13.2 creates the random number generation for two different distributions, uniform and exponential. The replication number is supplied through the command line option. If no replication number is supplied, it will be taken as 0, and if a value less than 1 is supplied, then it will run with value 1. So the following example uses the replication number as 0, 1 or any higher number > 1, and it uses a seed value of 9999

The syntax for setting the seed value is

$defaultRNG seed N

Where N is an integer which may take 0 or 1, or any positive number

Listing 13.2 – Random number generation – Case 1

#filename: randtest1.tcl

#

# Usage: ns randtest.tcl [replication number]

#


if {$argc > 1} {

    puts "Usage: ns randtest1.tcl \[replication number\]"

    exit

}

set run 1

if {$argc == 1} {

    set run [lindex $argv 0]

}

if {$run < 1} {

    set run 1

}


# seed the default RNG

global defaultRNG

$defaultRNG seed 9999


# Create the RNGs and set them to the correct substream

set arrivaldist [new RNG]

set size [new RNG]

for {set j 1} {$j < $run} {incr j} {

    $arrivaldist next-substream

    $size next-substream

}


# arrival_ is an exponential random variable describing the time between

# consecutive packet arrivals

set arrival_ [new RandomVariable/Exponential]

$arrival_ set avg_ 5

$arrival_ use-rng $arrivaldist


# size_ is a uniform random variable describing packet sizes

set size_ [new RandomVariable/Uniform]

$size_ set min_ 100

$size_ set max_ 5000

$size_ use-rng $size


# print the first 5 arrival times and sizes

for {set j 0} {$j < 5} {incr j} {

    puts [format "%-8.3f  %-4d" [$arrival_ value] \

    [expr round([$size_ value])]]

}

Here is the sample output for the above file

Prompt] ~/ns-allinone-2.35/ns-2.35/tcl/ex $ ns randtest1.tcl 0

6.358     4783

5.828     1732

1.469     2188

0.732     3076

4.002     626

Prompt]~/ns-allinone-2.35/ns-2.35/tcl/ex $ ns randtest1.tcl 1

6.358     4783

5.828     1732

1.469     2188

0.732     3076

4.002     626

Prompt]~/ns-allinone-2.35/ns-2.35/tcl/ex $ ns randtest1.tcl 2

2.091     153

12.085    4732

3.588     2329

1.201     230

5.161     2980

Prompt]~/ns-allinone-2.35/ns-2.35/tcl/ex $ ns randtest1.tcl 3

2.515     1119

3.154     3118

9.673     1201

13.346    2515

7.052     2115

Prompt]~/ns-allinone-2.35/ns-2.35/tcl/ex $ ns randtest1.tcl 100

8.940     2149

0.888     4870

0.998     4860

1.801     1205

18.224    2534

Prompt]~/ns-allinone-2.35/ns-2.35/tcl/ex $ ns randtest1.tcl 0.5

6.358     4783

5.828     1732

1.469     2188

0.732     3076

4.002     626


13.2 Queuing Models in NS2

M/M/1 is a system with Poisson arrival time, servicing exponentially and a queue of unlimited capacity and type of FIFO Queue. This is the simplest queuing system.  NS2 supports various distributions like Pareto, exponential, constant, uniform, etc to handle the network dynamics and metrics.

So it is very easy to test the given network link to monitor a given queue using any of these queuing models. The listings 13.3 and 13.4 are monitoring the link when the DropTail queue is used with a capacity of finite and infinite.

Listing 13.3 uses infinite capacity and Listing 13.4 uses Finite capacity. The output screenshot is shown below the scripts for further understanding


Listing 13.3 – M/M/1 Queuing Model

#new Simulator creation

set ns [new Simulator]

# Trace file creation for capturing the UDP data

set tf [open out.tr w]

$ns trace-all $tf


# Setting the exponential distribution parameter

set lambda 30.0

set mu     33.0


#creation of nodes

set n1 [$ns node]

set n2 [$ns node]

#The queue limit is 1Lakh as the capacity is infinite. Simulation will take # more time when the limit is increased.

set link [$ns simplex-link $n1 $n2 100kb 0ms DropTail]

$ns queue-limit $n1 $n2 100000


# generate random interarrival times and packet sizes

set InterArrivalTime [new RandomVariable/Exponential]

$InterArrivalTime set avg_ [expr 1/$lambda]

set pktSize [new RandomVariable/Exponential]

$pktSize set avg_ [expr 100000.0/(8*$mu)]


#create new agent UDP

set src [new Agent/UDP]

$ns attach-agent $n1 $src


# queue monitoring and send the output to the queue.out file

set qmon [$ns monitor-queue $n1 $n2 [open queue.out w] 0.1]

$link queue-sample-timeout


proc finish {} {

    global ns tf

    $ns flush-trace

    close $tf

    exit 0

}


proc sendpacket {} {

    global ns src InterArrivalTime pktSize

    set time [$ns now]

    $ns at [expr $time + [$InterArrivalTime value]] "sendpacket"

    set bytes [expr round ([$pktSize value])]

    $src send $bytes

}


set sink [new Agent/Null]

$ns attach-agent $n2 $sink

$ns connect $src $sink

$ns at 0.0001 "sendpacket"

$ns at 1000.0 "finish"


$ns run

To see the output of the above script, execute the following in the terminal

Prompt] gawk '{ print $1, $4, $5, $6, $7, $8, $9, $10, $11 }' queue.out > qm.pre

Prompt] xgraph qm.pre -geometry 500x500 -t “MM1 Queuing Example”

Here is the output screen



In the above Listing 13.3, large buffers are used to avoid losses. One can use smaller buffers and observe losses. The way to compute the loss probability from the simulation is simply to divide the total number of losses by the total number of arrivals, both given in the last line of the monitor-queue file. The command

$src set packetSize_ 100000

may be added.

The loss may be computed using the formula

P = pow(p,k)/∑Pow(p,i);


Listing 13.4 – M/M/1 with Finite capacity

#create a new simulator

set ns [new Simulator]

#open the trace file for capturing UDP

set tf [open out.tr w]

$ns trace-all $tf


# Setting the parameters for the Exponential distribution

set lambda   30.0

set mu       33.0

set qsize     2

set duration  2000


#creation of new nodes

set n1 [$ns node]

set n2 [$ns node]

#creation of link with queue

set link [$ns simplex-link $n1 $n2 100kb 0ms DropTail]

#setting the queue size as 2

$ns queue-limit $n1 $n2 $qsize


# generate random interarrival times and packet sizes

set InterArrivalTime [new RandomVariable/Exponential]

$InterArrivalTime set avg_ [expr 1/$lambda]

set pktSize [new RandomVariable/Exponential]

$pktSize set avg_ [expr 100000.0/(8*$mu)]


#creation of new UDP Agent

set src [new Agent/UDP]

$src set packetSize_ 100000

$ns attach-agent $n1 $src


#queue monitoring

set qmon [$ns monitor-queue $n1 $n2 [open queue1.out w] 0.1]

$link queue-sample-timeout


proc finish {} {

    global ns tf

    $ns flush-trace

    close $tf

    exit 0

}


proc sendpacket {} {

    global ns src InterArrivalTime pktSize

    set time [$ns now]

    $ns at [expr $time + [$InterArrivalTime value]] "sendpacket"

    set bytes [expr round ([$pktSize value])]

    $src send $bytes

}


set sink [new Agent/Null]

$ns attach-agent $n2 $sink

$ns connect $src $sink

$ns at 0.0001 "sendpacket"

$ns at $duration "finish"


$ns run

To see the output of the above script, execute the following in the terminal

Prompt] gawk '{ print $1, $4, $5, $6, $7, $8, $9, $10, $11 }' queue1.out > qm1.pre

Prompt] xgraph qm1.pre -geometry 500x500 -t “MM1 Queuing Example”

Here is the output screen




13.3 Conclusion

This chapter tells the basics of random and queuing models to support the simulation of network dynamics. This chapter shows scripts for random number generation for various distributions like exponential, uniform, etc, and also it shows the script for M/M/1 models with  finite and infinite buffer capacity to handle the packet loss probability


Comments

Popular posts from this blog

Installing ns3 in Ubuntu 22.04 | Complete Instructions

In this post, we are going to see how to install ns-3.36.1 in Ubuntu 22.04. You can follow the video for complete details Tools used in this simulation: NS3 version ns-3.36.1  OS Used: Ubuntu 22.04 LTS Installation of NS3 (ns-3.36.1) There are some changes in the ns3 installation procedure and the dependencies. So open a terminal and issue the following commands Step 1:  Prerequisites $ sudo apt update In the following packages, all the required dependencies are taken care and you can install all these packages for the complete use of ns3. $ sudo apt install g++ python3 python3-dev pkg-config sqlite3 cmake python3-setuptools git qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 openmpi-bin openmpi-common openmpi-doc libopenmpi-dev autoconf cvs bzr unrar gsl-bin libgsl-dev libgslcblas0 wireshark tcpdump sqlite sqlite3 libsqlite3-dev  libxml2 libxml2-dev libc6-dev libc6-dev-i386 libc...

Simulation of URDF, Gazebo and Rviz | ROS Noetic Tutorial 8

Design a User-defined robot of your choice (or you can use the URDF file) and enable the LIDAR Scanner so that any obstacle placed on the path of the light scan will cut the light rays. Visualize the robot in the Gazebo workspace, and also show the demonstration in RViz.   (NB: Gain knowledge on wiring URDF file and .launch file for enabling any user-defined robot to get launched in the gazebo platform.) SLAM : One of the most popular applications of ROS is SLAM(Simultaneous Localization and Mapping). The objective of the SLAM in mobile robotics is to construct and update the map of an unexplored environment with the help of the available sensors attached to the robot which will be used for exploring. URDF: Unified Robotics Description Format, URDF, is an XML specification used in academia and industry to model multibody systems such as robotic manipulator arms for manufacturing assembly lines and animatronic robots for amusement parks. URDF is especially popular with users of the ...

Installation of NS2 in Ubuntu 22.04 | NS2 Tutorial 2

NS-2.35 installation in Ubuntu 22.04 This post shows how to install ns-2.35 in Ubuntu 22.04 Operating System Since ns-2.35 is too old, it needs the following packages gcc-4.8 g++-4.8 gawk and some more libraries Follow the video for more instructions So, here are the steps to install this software: To download and extract the ns2 software Download the software from the following link http://sourceforge.net/projects/nsnam/files/allinone/ns-allinone-2.35/ns-allinone-2.35.tar.gz/download Extract it to home folder and in my case its /home/pradeepkumar (I recommend to install it under your home folder) $ tar zxvf ns-allinone-2.35.tar.gz or Right click over the file and click extract here and select the home folder. $ sudo apt update $ sudo apt install build-essential autoconf automake libxmu-dev gawk To install gcc-4.8 and g++-4.8 $ sudo gedit /etc/apt/sources.list make an entry in the above file deb http://in.archive.ubuntu.com/ubuntu/ bionic main universe $ sudo apt update Since, it...