How to Calculate Network Number of Handovers in NS2

To calculate the Network Number of Handovers in Network Simulator 2 (NS2) means that the process in which the mobile node varies its links from one base station or access point to another when it travels through the network. It is often used in mobile wireless network where nodes move from one region (cell) to another. It can be done by tracking the movement of mobile nodes and stating how many times they shift from one access point (or base station) to another. Below are the steps to calculate the number of handovers in the ns2 simulator tool:

Steps to Calculate Number of Handovers in NS2:

  1. Set Up a Mobile Network with Base Stations: Replicate a mobile wireless network where nodes are linked to various base stations (or access points), and they travel across the network.
  2. Simulate Node Mobility: NS2’s mobility model (like Random Waypoint) is used to simulate node movement over the network. The movement will cause the node to shift amongst base stations as it moves from one area of coverage to another.
  3. Monitor Handover Events: Track the amount of times a node switches its connection from one base station to another. This can be accomplished by logging handover events in the trace file.
  4. Count Handover Events: After the simulation, assess the trace file to sum up the number of times each mobile node varying its connection to a new base station.

Example of Simulating Handover in NS2:

Here’s an example NS2 Tcl script that imitates a simple mobile wireless network where nodes travel between base stations, activating handovers.

# Create a new simulator instance

set ns [new Simulator]

# Define the simulation area (X and Y dimensions)

set val(x) 500   ;# X dimension of the simulation area

set val(y) 500   ;# Y dimension of the simulation area

set val(stop) 10.0 ;# Simulation end time

# Create a topography object

set topo [new Topography]

$topo load_flatgrid $val(x) $val(y)

# Define a mobile node configuration

set chan_1_ [new Channel/WirelessChannel]

# Configure mobile nodes

$ns node-config -adhocRouting AODV \

-llType LL \

-macType Mac/802_11 \

-ifqType Queue/DropTail/PriQueue \

-ifqLen 50 \

-antType Antenna/OmniAntenna \

-propType Propagation/TwoRayGround \

-phyType Phy/WirelessPhy \

-topoInstance $topo \

-channel $chan_1_ \

-agentTrace ON \

-routerTrace ON \

-macTrace ON

# Create 3 base stations (access points)

for {set i 0} {$i < 3} {incr i} {

set bs($i) [$ns node]

$bs($i) set X_ [expr $i * 150 + 50]

$bs($i) set Y_ [expr 250]

$bs($i) set Z_ 0.0

}

# Create a mobile node

set mobileNode [$ns node]

# Set initial position for the mobile node

$mobileNode set X_ 50

$mobileNode set Y_ 100

$mobileNode set Z_ 0

# Define the mobility model for the mobile node (Random Waypoint model)

$ns at 0.0 “$mobileNode setdest 450 100 10.0”  ;# Move from (50,100) to (450,100) at 10m/s

# Attach agents (UDP traffic)

set udp0 [new Agent/UDP]

set null0 [new Agent/Null]

$ns attach-agent $mobileNode $udp0

$ns attach-agent $bs(2) $null0

$ns connect $udp0 $null0

# Create traffic source (CBR)

set cbr0 [new Application/Traffic/CBR]

$cbr0 attach-agent $udp0

$cbr0 set packetSize_ 512

$cbr0 set rate_ 1Mb

# Start and stop traffic

$ns at 1.0 “$cbr0 start”

$ns at 9.0 “$cbr0 stop”

# Open trace file to log events

set tracefile [open trace.tr w]

$ns trace-all $tracefile

# Define finish procedure

proc finish {} {

global ns tracefile

$ns flush-trace

close $tracefile

exit 0

}

# End simulation

$ns at $val(stop) “finish”

$ns run

Explanation of the Script:

  1. Base Stations (Access Points): Three base stations (bs(0), bs(1), bs(2)) are designed at fixed positions to serve as the access points for the mobile node.
  2. Mobile Node Movement: A mobile node is generated with initial coordinates (X_, Y_) and travels across the simulation area using the setdest command, which sets the destination and speed for the node.
  3. Traffic: The mobile node produces traffic to one of the base stations (bs(2)), and the node travel through the simulation area.
  4. Logging Events: The simulation logs all events (sending, receiving, routing, handovers) into the trace file.
  1. Detecting Handovers:

A handover happens when the mobile node shifts its connection from one base station to another. In the trace file, you can identify handover events by tracking which base station (or access point) is serving the mobile node at various times.

Example Trace File Output:

The trace file logs events like when packets are delivered, obtained, and routed. For handover events, you can track the variations in which base station the mobile node is interacting with:

r 1.0 _0_ AGT — 512 [0 0 0 0] ——- [0:0 2:0 32 0]

r 2.0 _1_ AGT — 512 [0 0 0 0] ——- [0:0 2:0 32 0]

r 3.0 _2_ AGT — 512 [0 0 0 0] ——- [0:0 2:0 32 0]

In this example:

  • _0_, _1_, and _2_ indicate various base stations or access points.
  • As the mobile node moves, the base station it links to changes, signifying a handover.
  1. Post-Simulation Analysis:

Once the simulation is done, you can evaluate the trace file to state how many times the mobile node switched from one base station to another.

Bash Script to Count Handovers:

# Extract lines where packets are received by the mobile node

grep “^r” trace.tr | awk ‘{print $3}’ > handovers.txt

# Count the number of handover events (changes in base station)

awk ‘{

if (prev != $1) {

handovers++

}

prev = $1

} END {

print “Number of Handovers:”, handovers

}’ handovers.txt

This script:

  • Extracts the node IDs (base station or access point IDs) allied with the mobile node’s communication.
  • Sums up how many times the mobile node shifted to a various base station.
  1. Manual Counting:

Otherwise, you can manually verify the trace file to monitor the number of times the mobile node’s connection changes from one base station to another.

Summary:

  1. Set Up a Mobile Network: Configure base stations (access points) and mobile nodes in your NS2 simulation.
  2. Simulate Node Mobility: Use a mobility model (e.g., Random Waypoint) to travel the mobile node across the network.
  3. Track Handovers: Observe how many times the mobile node switches from one base station to another.
  4. Count Handovers: Use the trace file to identify and total the occurence of handovers that happend during the simulation.

In conclusion, we have provided the step-by-step procedure which is easier for you to understand and to calculate the number of handovers occurred in the network during the simulation process using the ns2 simulator. We also offer some formulas and methods to calculate it with examples.

Provide us with all relevant parameter details, and we will assist you with a network comparison analysis. The number of handovers in the NS2 tool pertaining to your project ideas and topics has been shared by us, so you may have your analysis conducted by our experts.