How to Implement Multi Criteria Routing in NS2
To implement the Multi-Criteria Routing (MCR) in Network Simulator 2 encompasses making routing decisions in terms of numerous factors (or policies) like delay, bandwidth, energy utilization, link consistency or network congestion. Unlike old-fashioned routing protocols that may only consider a individual criterion (such as shortest path or maximum hops), multi-criteria routing enhances the route selection process by impacting several metrics instantaneously.
Here is a detailed guide to implementing a Multi-Criteria Routing (MCR) protocol in NS2.
Key Steps in Designing and Implementing Multi-Criteria Routing in NS2
- Identify Routing Metrics
Start by defining the criteria that will be considered in the routing decisions. These could contain:
- Delay: End-to-end delay of the path.
- Bandwidth: Existed bandwidth sideways the path.
- Energy: The energy utilization or remaining battery of nodes (for wireless sensor networks).
- Link Quality: The dependability or quality of the wireless link.
- Hop Count: The amount of hops (as a secondary criterion).
You can integrate these metrics using a weighted sum approach, a utility function, or another method that allocates priority to each criterion.
- Define Packet Structure
Set up the structure of the packets used in your routing protocol. You will add fields to store routing information like source, destination, and the values of the various criteria.
Example of Packet Header Definition:
Configure your custom packet type with numerous criteria fields by modify the C++ header file.
struct hdr_mcr {
int pkt_type; // Packet type (RREQ, RREP, DATA, etc.)
int src_addr; // Source address
int dest_addr; // Destination address
double delay; // Delay metric
double bandwidth; // Bandwidth metric
double energy; // Energy metric
int hop_count; // Hop count
static int offset_;
inline static hdr_mcr* access(const Packet* p) {
return (hdr_mcr*) p->access(offset_);
}
};
// Initialize the packet header
int hdr_mcr::offset_;
Here, hdr_mcr is a custom header that has the fields essential for multi-criteria routing like delay, bandwidth, energy, and hop count.
- Implement the Multi-Criteria Routing Algorithm
You will execute the routing agent that processes incoming packets, computes the metrics for several paths, and picks the optimal path according to the selected criteria.
Example C++ Code for the Multi-Criteria Routing Agent:
class MCRRoutingAgent : public Agent {
public:
MCRRoutingAgent();
void recv(Packet* p, Handler* h); // Receive function to handle incoming packets
void sendRREQ(int dest_addr); // Send Route Request (RREQ) to discover routes
void sendRREP(int src_addr); // Send Route Reply (RREP) in response to RREQ
protected:
map<int, double> routingTable; // Routing table storing the best paths
double calculateRouteMetric(double delay, double bandwidth, double energy, int hops); // Multi-criteria calculation
void forwardPacket(Packet* p); // Forward data packets
double delayWeight;
double bandwidthWeight;
double energyWeight;
double hopWeight;
};
// Constructor
MCRRoutingAgent::MCRRoutingAgent() : Agent(PT_UDP) {
// Define the weights for different metrics
delayWeight = 0.4;
bandwidthWeight = 0.3;
energyWeight = 0.2;
hopWeight = 0.1;
}
// Receive incoming packets and process them
void MCRRoutingAgent::recv(Packet* p, Handler* h) {
hdr_ip* iph = HDR_IP(p);
hdr_mcr* hdr = hdr_mcr::access(p);
if (hdr->pkt_type == RREQ) {
if (iph->dst() == node()->address()) {
sendRREP(iph->src()); // Destination found, send RREP back
} else {
forwardPacket(p); // Forward RREQ to the next node
}
}
else if (hdr->pkt_type == RREP) {
// Update the routing table with the best route based on multi-criteria
double routeMetric = calculateRouteMetric(hdr->delay, hdr->bandwidth, hdr->energy, hdr->hop_count);
routingTable[hdr->src_addr] = routeMetric;
forwardPacket(p); // Forward RREP to the source
}
else {
forwardPacket(p); // Forward data packets
}
}
// Calculate the metric for route selection based on multiple criteria
double MCRRoutingAgent::calculateRouteMetric(double delay, double bandwidth, double energy, int hops) {
// Normalize and combine metrics using weighted sum
double metric = (delayWeight * (1 / delay)) +
(bandwidthWeight * bandwidth) +
(energyWeight * energy) +
(hopWeight * (1 / hops));
return metric;
}
// Send Route Request (RREQ) to discover routes
void MCRRoutingAgent::sendRREQ(int dest_addr) {
Packet* p = allocpkt();
hdr_ip* iph = HDR_IP(p);
hdr_mcr* hdr = hdr_mcr::access(p);
hdr->pkt_type = RREQ;
hdr->src_addr = node()->address();
hdr->dest_addr = dest_addr;
send(p, 0); // Send the packet
}
// Send Route Reply (RREP) back to the source
void MCRRoutingAgent::sendRREP(int src_addr) {
Packet* p = allocpkt();
hdr_ip* iph = HDR_IP(p);
hdr_mcr* hdr = hdr_mcr::access(p);
hdr->pkt_type = RREP;
hdr->src_addr = node()->address();
hdr->dest_addr = src_addr;
send(p, 0); // Send the packet
}
// Forward data packets based on the routing table
void MCRRoutingAgent::forwardPacket(Packet* p) {
hdr_ip* iph = HDR_IP(p);
int nextHop = routingTable[iph->dst()]; // Get the best next hop based on the multi-criteria metric
sendto(p, nextHop);
}
In this sample:
- The MCRRoutingAgent estimates the route metric depends on multiple criteria (delay, bandwidth, energy, and hop count).
- The calculateRouteMetric() function integrates these metrics using a weighted sum approach. You can modify the weights (delayWeight, bandwidthWeight, etc.) based on the network’s particular demands.
- Create a Tcl Interface for NS2
To use the multi-criteria routing protocol in NS2 simulations, build a Tcl interface to register the protocol.
Example Tcl Interface:
extern “C” int McRouting_Init() {
// Register the routing agent with NS2
TclClass* tclClass = new TclClass(“Agent/MCRRoutingAgent”, createMCRRoutingAgent);
tclClass->bind();
return TCL_OK;
}
Agent* createMCRRoutingAgent() {
return new MCRRoutingAgent();
}
Make certain to fine-tune ns-lib.tcl to register the new protocol so it can be used in simulations.
- Simulate the Multi-Criteria Routing Protocol
Now that the protocol is executed, you can simulate it using a Tcl script in NS2. The script will state the network topology, node movement, and traffic patterns.
Example Tcl Script for Multi-Criteria Routing Simulation:
# Create a simulator instance
set ns [new Simulator]
# Create nodes
set node1 [$ns node]
set node2 [$ns node]
set node3 [$ns node]
# Attach the multi-criteria routing agent to the nodes
set mcrAgent1 [new Agent/MCRRoutingAgent]
set mcrAgent2 [new Agent/MCRRoutingAgent]
$ns attach-agent $node1 $mcrAgent1
$ns attach-agent $node2 $mcrAgent2
# Set up traffic between nodes
set udp0 [new Agent/UDP]
$ns attach-agent $node1 $udp0
$ns connect $udp0 $mcrAgent2
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 512
$cbr0 set interval_ 0.5
$cbr0 attach-agent $udp0
# Start traffic
$ns at 1.0 “$cbr0 start”
# Run the simulation
$ns run
This script configures a simplified simulation with nodes executing the multi-criteria routing protocol and creating UDP traffic.
- Evaluate the Protocol’s Performance
After running the simulation, you can assess the performance of your multi-criteria routing protocol using different metrics like:
- Packet Delivery Ratio (PDR): Estimates the percentage of successfully sent packets.
- Average Latency: Measures the delay amongst sending and receiving packets.
- Throughput: Computes the total data successfully transferred over the network.
- Energy Efficiency: Assess how well the protocol preserves energy in wireless sensor networks.
- Path Optimality: Calculate how well the selected paths balance many criteria.
As we saw in this process, it will help you to set up the simulation network, to fine-tune the simulator and to establish the Multi Criteria Routing in the ns2 by defining routing protocol that presents some samples. If needed, we can help you with another approach. If you are uncertain about how to implement multi-criteria Routing in the NS2 tool, you may reach out to the team at ns2project.com for personalized support.