How to Simulate Cognitive Ad Hoc Network Projects Using NS2

To simulate Cognitive Ad-Hoc Networks (CANs) using NS2, which encompasses making a dynamic spectrum access mechanism for secondary users (SUs) and primary users (PUs). Cognitive Ad-Hoc Networks permit SUs to access obtainable spectrum without interfering with PUs, integrating aspects such as spectrum sensing, dynamic frequency selection, and cognitive routing.

For optimal Cognitive Ad-Hoc Networks simulation guidance, please reach out to phdprime.com, where we are committed to assisting you in achieving the best results.

Below is a step-by-step method to simulate cognitive ad-hoc network projects in NS2:

Steps to Simulate Cognitive Ad Hoc Network Projects in NS2

  1. Install NS2
  • We can download NS2 from the official NS2 website.
  • Make certain all dependencies (Tcl/Tk, OTcl, NAM) are installed appropriately to run and visualize the simulations.
  1. Understand Cognitive Ad-Hoc Network Concepts
  • Primary Users (PUs): Licensed users with exclusive access to the spectrum.
  • Secondary Users (SUs): Cognitive radio users who opportunistically access the spectrum without triggering interference to PUs.
  • Spectrum Sensing: SUs periodically sense the spectrum to identify the presence of PUs and choose which channels are free.
  • Dynamic Spectrum Access (DSA): SUs switch channels actively rely on the availability of the spectrum that permitting interference with PUs.
  • Cognitive Routing: SUs should select routes according to the spectrum availability.
  1. Modify NS2 for Cognitive Ad-Hoc Network (CAN) Simulation

NS2 doesn’t natively support cognitive radio functionality, thus changes are needed to insert spectrum sensing and dynamic access aspects. Here’s how to proceed:

  • Spectrum Sensing and Switching: Change NS2’s PHY layer to replicate spectrum sensing and channel switching.

Example C++ Modifications:

Make new classes for spectrum sensing and dynamic spectrum access by changing phy-wireless.cc and mac-802_11.cc. For instance, insert spectrum sensing and channel switching capabilities to the WirelessPhy class.

class CognitiveRadio {

public:

bool isChannelFree(int channel) {

// Logic to check if the channel is occupied by PUs

// Simulate the sensing using random function or fixed pattern

return rand() % 2 == 0; // Random simulation of channel availability

}

void switchChannel(int newChannel) {

// Logic to switch the SU to a new channel

this->currentChannel = newChannel;

printf(“Switching to channel %d\n”, newChannel);

}

};

  1. Create the Network Topology

In the OTcl script, we describe the network topology containing PUs and SUs. Make a links among PUs and SUs, and indicate how secondary users will sense and utilize available channels.

Example OTcl Code to Define Topology:

# Create the simulator instance

set ns [new Simulator]

# Define the network topology

set topo [new Topography]

$topo load_flatgrid 1000 1000

# Create primary users (PUs) and secondary users (SUs)

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

set pu($i) [$ns node]

set su($i) [$ns node]

}

# Establish links between nodes

$ns simplex-link $su(0) $su(1) 10Mb 10ms DropTail

$ns simplex-link $su(1) $su(2) 10Mb 10ms DropTail

$ns simplex-link $pu(0) $su(0) 10Mb 10ms DropTail

In this example:

  • PUs and SUs are made and allocated nodes.
  • SUs are linked with other SUs, and a few links are associate PUs with SUs.
  1. Simulate Spectrum Sensing

SUs should periodically sense the spectrum to verify if the channels they are using are occupied by PUs.

Spectrum Sensing OTcl Procedure:

# Spectrum sensing procedure

proc sense_spectrum {su_node} {

global ns

set available_channel [expr int(rand() * 10)]  ;# Simulate random channel availability

puts “SU $su_node switching to available channel $available_channel”

$ns at [$ns now] “$su_node switch-channel $available_channel”

}

# Set spectrum sensing events for secondary users

$ns at 1.0 “sense_spectrum $su(0)”

$ns at 2.0 “sense_spectrum $su(1)”

In this instance, the sense_spectrum process replicates SUs sensing the spectrum and switching to an available channel when required.

  1. Dynamic Spectrum Access (DSA)

After sensing the spectrum then SUs should dynamically switch to obtainable channels if their present channels are occupied by PUs.

Dynamic Spectrum Access OTcl Code:

# Switch channels based on availability

proc switch_channel {su_node new_channel} {

global ns

puts “Switching SU node $su_node to channel $new_channel”

$su_node set-channel $new_channel

}

# Simulate dynamic spectrum access for SUs

$ns at 3.0 “switch_channel $su(0) 2”

$ns at 4.0 “switch_channel $su(1) 4”

  1. Implement Cognitive Routing

The routing protocol must adjust to the spectrum availability, intending that SUs should use a dynamic routing protocol, which deliberates available spectrum when choosing routes.

  • We can be utilized a changed version of AODV (Ad-Hoc On-Demand Distance Vector) or DSR (Dynamic Source Routing) to adjust to spectrum conditions.
  • Alter AODV to prioritize routes with obtainable channels. For instance, insert channel data to the AODV routing table.

Example OTcl Code for Cognitive Routing:

# Use AODV routing for SUs

$su(0) set agent [new Agent/DSR]

$ns at 1.0 “$su(0) start-routing”

  1. Traffic Generation for Cognitive SUs

Make traffic among the cognitive SUs utilizing UDP or TCP agents.

Example UDP Traffic Configuration:

# Create UDP traffic between SUs

set udp [new Agent/UDP]

$ns attach-agent $su(0) $udp

set null [new Agent/Null]

$ns attach-agent $su(1) $null

$ns connect $udp $null

# Create a CBR traffic generator

set cbr [new Application/Traffic/CBR]

$cbr set packetSize_ 512

$cbr set rate_ 1Mb

$cbr attach-agent $udp

# Start the traffic

$ns at 1.5 “$cbr start”

  1. Run the Simulation

We can save the script as cognitive_ad_hoc.tcl and run the simulation in NS2.

ns cognitive_ad_hoc.tcl

  1. Analyze the Results

We can be used trace files to examine performance parameters like spectrum utilization, packet delivery ratio, throughput, and end-to-end delay. We can process trace files utilizing awk, Perl, or Python scripts.

Example Trace File Analysis:

awk -f analyze_trace.awk cognitive_ad_hoc.tr

  1. Visualize the Simulation Using NAM

We can use Network Animator (NAM) to envision the behaviour of SUs, spectrum sensing, and dynamic spectrum access.

nam cognitive_ad_hoc.nam

Example Simulation Script Outline for Cognitive Ad-Hoc Network

# Cognitive Ad-Hoc Network simulation in NS2

set ns [new Simulator]

set topo [new Topography]

$topo load_flatgrid 1000 1000  ;# Grid topology for the network

# Create PUs and SUs

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

set pu($i) [$ns node]  ;# Primary Users

set su($i) [$ns node]  ;# Secondary Users (Cognitive)

}

# Establish links between SUs and PUs

$ns simplex-link $su(0) $su(1) 10Mb 10ms DropTail

$ns simplex-link $su(1) $su(2) 10Mb 10ms DropTail

$ns simplex-link $pu(0) $su(0) 10Mb 10ms DropTail

# Spectrum sensing function

proc sense_spectrum {su_node} {

global ns

set available_channel [expr int(rand() * 10)]  ;# Random channel availability

puts “SU $su_node switching to channel $available_channel”

$ns at [$ns now] “$su_node switch-channel $available_channel”

}

# Set up spectrum sensing events

$ns at 1.0 “sense_spectrum $su(0)”

$ns at 2.0 “sense_spectrum $su(1)”

# Define UDP traffic between cognitive SUs

set udp [new Agent/UDP]

$ns attach-agent $su(0) $udp

set null [new Agent/Null]

$ns attach-agent $su(1) $null

$ns connect $udp $null

# Generate CBR traffic between SUs

set cbr [new Application/Traffic/CBR]

$cbr set packetSize_ 512

$cbr set rate_ 1Mb

$cbr attach-agent $udp

$ns at 1.5 “$cbr start”

# Run simulation for 10 seconds

$ns at 10.0 “finish”

Key Points:

  • Spectrum Sensing: Execute sensing logic to identify when PUs are using certain channels.
  • Dynamic Spectrum Access: Enable SUs to switch channels actively according to the sensing outcomes.
  • Routing Protocol: Change AODV or DSR to deliberate spectrum availability when creating routing decisions.
  • Traffic Simulation: We can utilize UDP/TCP to replicate traffic among SUs and route via available spectrum.

We had offered step-by-step detailed procedure with necessary examples and its key points are useful to simulate and analyse the Cognitive Ad Hoc Network projects in NS2 platform. Should more informations be required, we are ready to deliver it in another manual.

Opening Time

9:00am

Lunch Time

12:30pm

Break Time

4:00pm

Closing Time

6:30pm

  • award1
  • award2