How to Implement Network Clustering in NS2

To implement the Network Implementing network clustering in NS, we have to arrange a network into bundles of nodes with each node (or cluster) has a cluster head (CH) accountable for collaborating interactions inside the cluster and amongst clusters. This clustering is often used in Wireless Sensor Networks (WSNs), Mobile Ad Hoc Networks (MANETs) and other dispersed systems to enhance energy consumption, decrease communication overhead and improve scalability.

In NS2, clustering can be established by:

  1. Defining clusters: Bundling nodes into clusters, with a cluster head accountable for communication into the cluster.
  2. Cluster head election: Picking a node as the cluster head depends on particular criteria (for instance: energy level, proximity, or random selection).
  3. Intra-cluster communication: Nodes within a cluster interact over the cluster head.
  4. Inter-cluster communication: Cluster heads communicate with one another or with a base station.

Steps to Implement Network Clustering in NS2

  1. Install NS2

Make certain that you have installed the ns2 on your computer.

  1. Set Up a Basic Network Topology with Clusters

In this simulation, we will build a wireless sensor network with several clusters, where each cluster has a designated cluster head.

Example: Network Clustering in NS2

# Define the simulator object

set ns [new Simulator]

# Define trace and nam files for output

set tracefile [open out.tr w]

set namfile [open out.nam w]

$ns trace-all $tracefile

$ns namtrace-all $namfile

# Define a ‘finish’ procedure to end the simulation and visualize in NAM

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam out.nam &

exit 0

}

# Set up the topography for the simulation area

set topo [new Topography]

$topo load_flatgrid 1000 1000

# Define the wireless channel

set chan [new Channel/WirelessChannel]

# Configure wireless nodes (adhoc) and use TwoRayGround for signal propagation

$ns node-config -adhocRouting DSDV \

-llType LL \

-macType Mac/802_11 \

-ifqType Queue/DropTail/PriQueue \

-ifqLen 50 \

-antType Antenna/OmniAntenna \

-propType Propagation/TwoRayGround \

-phyType Phy/WirelessPhy \

-channel $chan

# Create sensor nodes in clusters

set node1 [$ns node]

set node2 [$ns node]

set node3 [$ns node]

set node4 [$ns node]

set node5 [$ns node]

set node6 [$ns node]

# Assign node positions (group them by proximity for clustering)

$node1 set X_ 200; $node1 set Y_ 200; $node1 set Z_ 0

$node2 set X_ 220; $node2 set Y_ 210; $node2 set Z_ 0

$node3 set X_ 240; $node3 set Y_ 190; $node3 set Z_ 0

$node4 set X_ 800; $node4 set Y_ 800; $node4 set Z_ 0

$node5 set X_ 820; $node5 set Y_ 810; $node5 set Z_ 0

$node6 set X_ 840; $node6 set Y_ 790; $node6 set Z_ 0

# Define cluster heads for two clusters

set cluster_head1 $node1  ;# Cluster head for first cluster

set cluster_head2 $node4  ;# Cluster head for second cluster

# Procedure to send data to the cluster head

proc send_data_to_ch {node ch} {

global ns

# Define TCP agent at the node

set tcp [new Agent/TCP]

set sink [new Agent/TCPSink]

$ns attach-agent $node $tcp

$ns attach-agent $ch $sink

# Connect the node to the cluster head

$ns connect $tcp $sink

# Create FTP traffic over TCP (to simulate data transmission within the cluster)

set ftp [new Application/FTP]

$ftp attach-agent $tcp

$ftp start 1.0  ;# Start data transmission at 1 second

$ftp stop 4.0   ;# Stop data transmission at 4 seconds

}

# Intra-cluster communication (nodes send data to their cluster head)

$ns at 1.0 “send_data_to_ch $node2 $cluster_head1”

$ns at 1.5 “send_data_to_ch $node3 $cluster_head1”

$ns at 2.0 “send_data_to_ch $node5 $cluster_head2”

$ns at 2.5 “send_data_to_ch $node6 $cluster_head2”

# Inter-cluster communication (cluster heads communicate with each other)

proc send_data_between_chs {ch1 ch2} {

global ns

# Define TCP agent at cluster head 1

set tcp_ch1 [new Agent/TCP]

set sink_ch2 [new Agent/TCPSink]

$ns attach-agent $ch1 $tcp_ch1

$ns attach-agent $ch2 $sink_ch2

# Connect the two cluster heads

$ns connect $tcp_ch1 $sink_ch2

# Create FTP traffic over TCP to simulate inter-cluster communication

set ftp_ch [new Application/FTP]

$ftp_ch attach-agent $tcp_ch1

$ftp_ch start 3.0  ;# Start communication at 3 seconds

$ftp_ch stop 5.0   ;# Stop communication at 5 seconds

}

# Inter-cluster communication between cluster_head1 and cluster_head2

$ns at 3.0 “send_data_between_chs $cluster_head1 $cluster_head2”

# Schedule the end of the simulation

$ns at 6.0 “finish”

# Run the simulation

$ns run

  1. Explanation of the Script
  • Nodes and Clusters: Six nodes are generated, break down into two clusters. Each cluster has a designated cluster head (node1 for cluster 1, and node4 for cluster 2).
  • Intra-cluster Communication: Nodes inside a cluster deliver their data to the cluster head using TCP traffic (send_data_to_ch procedure).
  • Inter-cluster Communication: The cluster heads interact with one another (send_data_between_chs procedure), replicating how data is interchanged amongst clusters.
  • Positions: The nodes are located in the simulation area based on their proximity, with closer nodes forming clusters.
  1. Run the Simulation

Store the script as network_clustering.tcl and execute it using:

ns network_clustering.tcl

This will design a trace file (out.tr) and a NAM file (out.nam). The trace file logs the packet transmission and events, while the NAM file permits you to visualize the clustering behavior.

  1. Visualize the Network Clustering in NAM

NAM tool is used to visualize the simulation:

nam out.nam

You can monitor how nodes inside a cluster deliver their data to the cluster head and how the cluster heads interact with one another.

  1. Cluster Head Election

You can expand the example by dynamically selecting a cluster head according to the specific criteria like energy level, proximity, or random selection.

Example: Random Cluster Head Election

# Procedure for random cluster head election within a cluster

proc elect_cluster_head {nodes} {

global ns

# Randomly select a node from the list of nodes as the cluster head

set num_nodes [llength $nodes]

set random_index [expr {int(rand() * $num_nodes)}]

set cluster_head [lindex $nodes $random_index]

puts “Cluster head elected: $cluster_head”

return $cluster_head

}

# Elect cluster heads dynamically

set cluster_head1 [elect_cluster_head [list $node1 $node2 $node3]]

set cluster_head2 [elect_cluster_head [list $node4 $node5 $node6]]

This procedure randomly picks a cluster head from a list of nodes in the cluster.

  1. Energy-Aware Clustering

Execute energy-aware clustering by allocating an energy model to each node and pick the cluster head depends on the energy level of the nodes.

Example: Energy-Based Cluster Head Election

# Energy model for each node

$ns node-config -energyModel EnergyModel \

-initialEnergy 100.0 \

-txPower 0.5 \

-rxPower 0.3 \

-idlePower 0.05

# Procedure for energy-based cluster head election

proc elect_energy_based_ch {nodes} {

global ns

set best_node “”

set max_energy -1

# Iterate through nodes and find the one with the highest energy level

foreach node $nodes {

set energy [$node energy]

if { $energy > $max_energy } {

set max_energy $energy

set best_node $node

}

}

puts “Energy-based cluster head: $best_node”

return $best_node

}

# Elect cluster heads based on energy

set cluster_head1 [elect_energy_based_ch [list $node1 $node2 $node3]]

set cluster_head2 [elect_energy_based_ch [list $node4 $node5 $node6]]

This script dynamically chooses the node with the highest energy level to be the cluster head.

This procedure contains the valuable insights like selecting cluster head and execution of energy aware clustering regarding the implementation of Network Clustering in the ns2 tool. For further process, we will deliver the additional details on this approach for you.

Ns2project.com is your go-to resource for implementing Network Clustering using the NS2 tool. We deliver cutting-edge ideas and comprehensive project assistance. Our knowledge spans both link-layer and application-layer elements that are crucial for your projects.