To develop the Optimal Routing for used the NS-3. The Optimal Routing is method of choosing the best path established on certain performance of parameter metric like as delay, throughput, energy consumption, congestion, or cost. In NS-3, optimal routing can be employed using:
- Machine Learning-based Routing
- Dijkstra’s Algorithm (Shortest Path)
- QoS-aware Routing
- Bellman-Ford Algorithm (Cost-Based Routing)
- QoS-aware Routing
- Custom NS-3 Routing Protocols
Steps to Develop an Optimal Routing Project in NS-3
- Install and Set Up NS-3
We download and enable the tool NS-3 is installed:
git clone
cd ns-3-dev
./ns3 configure –enable-examples –enable-tests
./ns3 build
- Define a Network Topology
The graph-based network topology is needed for optimal routing.
Example: Create a 6-Node Network
NodeContainer nodes;
nodes.Create(6);
We join the Nodes using for Point-to-Point connections
PointToPointHelper p2p;
p2p.SetDeviceAttribute(“DataRate”, StringValue(“1Gbps”));
p2p.SetChannelAttribute(“Delay”, StringValue(“2ms”));
NetDeviceContainer devices01 = p2p.Install(nodes.Get(0), nodes.Get(1));
NetDeviceContainer devices12 = p2p.Install(nodes.Get(1), nodes.Get(2));
NetDeviceContainer devices13 = p2p.Install(nodes.Get(1), nodes.Get(3));
NetDeviceContainer devices34 = p2p.Install(nodes.Get(3), nodes.Get(4));
NetDeviceContainer devices35 = p2p.Install(nodes.Get(3), nodes.Get(5));
- Allocate the IP Addresses
InternetStackHelper stack;
stack.Install(nodes);
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces01 = address.Assign(devices01);
address.SetBase(“10.1.2.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces12 = address.Assign(devices12);
address.SetBase(“10.1.3.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces13 = address.Assign(devices13);
address.SetBase(“10.1.4.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces34 = address.Assign(devices34);
address.SetBase(“10.1.5.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces35 = address.Assign(devices35);
- Implement Optimal Routing Algorithms
Approach 1: Dijkstra’s Algorithm (Shortest Path)
Dijkstra’s Algorithm finds the shortest path based on link costs.
(A) Create a Custom Optimal Routing Class
class OptimalRouting : public Ipv4RoutingProtocol {
public:
void ComputeOptimalPath(NodeContainer nodes, uint32_t source);
};
(B) Estimate the Dijkstra’s procedures
void OptimalRouting::ComputeOptimalPath(NodeContainer nodes, uint32_t source) {
uint32_t numNodes = nodes.GetN();
std::vector<uint32_t> distance(numNodes, INT_MAX);
std::vector<uint32_t> previous(numNodes, -1);
std::vector<bool> visited(numNodes, false);
distance[source] = 0;
for (uint32_t i = 0; i < numNodes; i++) {
uint32_t minDist = INT_MAX, minNode = -1;
for (uint32_t j = 0; j < numNodes; j++) {
if (!visited[j] && distance[j] < minDist) {
minDist = distance[j];
minNode = j;
}
}
visited[minNode] = true;
for (uint32_t neighbor = 0; neighbor < numNodes; neighbor++) {
uint32_t linkCost = Simulator::Now().GetNanoSeconds() % 10 + 1;
if (distance[minNode] + linkCost < distance[neighbor]) {
distance[neighbor] = distance[minNode] + linkCost;
previous[neighbor] = minNode;
}
}
}
for (uint32_t i = 0; i < numNodes; i++) {
NS_LOG_UNCOND(“Optimal Route from Source to Node ” << i << ” is ” << distance[i]);
}
}
(C) Execute the Optimal Routing
Ptr<OptimalRouting> optimalRouting = CreateObject<OptimalRouting>();
nodes.Get(0)->AggregateObject(optimalRouting);
optimalRouting->ComputeOptimalPath(nodes, 0);
Approach 2: Bellman-Ford Algorithm (Cost-Based Routing)
Bellman-Ford is used for link cost-sensitive routing.
(A) Implement Bellman-Ford Algorithm
void OptimalRouting::ComputeBellmanFord(NodeContainer nodes, uint32_t source) {
uint32_t numNodes = nodes.GetN();
std::vector<uint32_t> distance(numNodes, INT_MAX);
distance[source] = 0;
for (uint32_t i = 0; i < numNodes – 1; i++) {
for (uint32_t u = 0; u < numNodes; u++) {
for (uint32_t v = 0; v < numNodes; v++) {
uint32_t cost = Simulator::Now().GetNanoSeconds() % 10 + 1;
if (distance[u] + cost < distance[v]) {
distance[v] = distance[u] + cost;
}
}
}
}
for (uint32_t i = 0; i < numNodes; i++) {
NS_LOG_UNCOND(“Bellman-Ford Optimal Path from Source to Node ” << i << ” is ” << distance[i]);
}
}
- We configure the congestion applications
UdpEchoServerHelper echoServer(9);
ApplicationContainer serverApps = echoServer.Install(nodes.Get(5));
serverApps.Start(Seconds(1.0));
serverApps.Stop(Seconds(10.0));
UdpEchoClientHelper echoClient(Ipv4Address(“10.1.5.2”), 9);
echoClient.SetAttribute(“MaxPackets”, UintegerValue(5));
echoClient.SetAttribute(“Interval”, TimeValue(Seconds(1.0)));
echoClient.SetAttribute(“PacketSize”, UintegerValue(512));
ApplicationContainer clientApps = echoClient.Install(nodes.Get(0));
clientApps.Start(Seconds(2.0));
clientApps.Stop(Seconds(10.0));
- Assist the Tracing for Debugging
AsciiTraceHelper ascii;
p2p.EnableAsciiAll(ascii.CreateFileStream(“optimal-routing.tr”));
p2p.EnablePcapAll(“optimal-routing”);
- Implement the process for Simulation
Simulator::Run();
Simulator::Destroy();
- Analyze Results
We ensure for envision the tool like NetAnim:
./waf –run optimal-routing
netanim
- Extend with Advanced Features
- AI-Based Routing: We utilized the reinforcement learning for improve the routing.
- Energy-Aware Optimal Routing: We decrease the power consumption.
- QoS-Aware Routing: We enhance the performance metrices based on latency, jitter, and bandwidth.
We had completely thorough the manual and successfully executed the optimal routing project using the tool of ns3 that has setup the topology then choose the optimal routing technique after that execute and evaluate the performance metrics to analyse the outcomes. If you need more information regarding this project feel free to ask!