Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

How to Implement Network Localization schemes in ns3

To implement network localization schemes in ns3, we need to simulate a network. In which that based on various localization algorithms position of the nodes has been estimated. In this type of network oftenly location of the nodes need to be analyzed without the help of GPS which is mainly used in wireless sensor networks (WSN). Below given steps will guide on how to set-up basic simulation of network localization schemes in ns3.

Step-by-step guide to implement network localization schemes in ns3

Step 1: Setup ns3 Environment

Make sure that ns3 is installed and properly configured.

git clone https://gitlab.com/nsnam/ns-3-dev.git

cd ns-3-dev

./waf configure

./waf build

Step 2: Create the Network Localization Simulation Script

We will create a script that sets up a network with anchor nodes (nodes with known positions) and normal nodes that will estimate their positions based on the anchor nodes.

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/mobility-module.h”

#include “ns3/applications-module.h”

#include “ns3/wifi-module.h”

#include “ns3/flow-monitor-module.h”

using namespace ns3;

NS_LOG_COMPONENT_DEFINE(“NetworkLocalizationExample”);

class LocalizationNode

{

public:

LocalizationNode(Ptr<Node> node, bool isAnchor = false, Vector position = Vector())

: m_node(node), m_isAnchor(isAnchor), m_position(position)

{

if (isAnchor)

{

Ptr<MobilityModel> mobility = m_node->GetObject<MobilityModel>();

mobility->SetPosition(position);

}

}

bool IsAnchor() const { return m_isAnchor; }

Vector GetPosition() const { return m_position; }

void EstimatePosition(const std::vector<LocalizationNode> &anchorNodes)

{

if (m_isAnchor)

{

return;

}

Vector estimatedPosition(0.0, 0.0, 0.0);

uint32_t count = 0;

for (const auto &anchor : anchorNodes)

{

if (anchor.IsAnchor())

{

estimatedPosition += anchor.GetPosition();

count++;

}

}

if (count > 0)

{

m_position = estimatedPosition / count;

Ptr<MobilityModel> mobility = m_node->GetObject<MobilityModel>();

mobility->SetPosition(m_position);

}

NS_LOG_UNCOND(Simulator::Now().GetSeconds() << “s: Node estimated position: (” << m_position.x << “, ” << m_position.y << “, ” << m_position.z << “)”);

}

private:

Ptr<Node> m_node;

bool m_isAnchor;

Vector m_position;

};

void CourseChange(std::string context, Ptr<const MobilityModel> model)

{

Vector position = model->GetPosition();

NS_LOG_UNCOND(Simulator::Now().GetSeconds() << “s: Node position: (” << position.x << “, ” << position.y << “, ” << position.z << “)”);

}

int main(int argc, char *argv[])

{

CommandLine cmd;

cmd.Parse(argc, argv);

// Create nodes

NodeContainer nodes;

nodes.Create(10); // Ten nodes in total

// Set up mobility model

MobilityHelper mobility;

mobility.SetPositionAllocator(“ns3::GridPositionAllocator”,

“MinX”, DoubleValue(0.0),

“MinY”, DoubleValue(0.0),

“DeltaX”, DoubleValue(10.0),

“DeltaY”, DoubleValue(10.0),

“GridWidth”, UintegerValue(5),

“LayoutType”, StringValue(“RowFirst”));

mobility.SetMobilityModel(“ns3::ConstantPositionMobilityModel”);

mobility.Install(nodes);

// Create localization nodes

std::vector<LocalizationNode> localizationNodes;

for (uint32_t i = 0; i < nodes.GetN(); ++i)

{

if (i < 3) // First 3 nodes are anchors

{

localizationNodes.emplace_back(nodes.Get(i), true, Vector(i * 10.0, 0.0, 0.0));

}

else

{

localizationNodes.emplace_back(nodes.Get(i));

}

}

// Set up WiFi

WifiHelper wifi;

wifi.SetRemoteStationManager(“ns3::AarfWifiManager”);

WifiMacHelper mac;

Ssid ssid = Ssid(“ns-3-ssid”);

mac.SetType(“ns3::StaWifiMac”,

“Ssid”, SsidValue(ssid),

“ActiveProbing”, BooleanValue(false));

YansWifiPhyHelper phy = YansWifiPhyHelper::Default();

YansWifiChannelHelper channel = YansWifiChannelHelper::Default();

phy.SetChannel(channel.Create());

NetDeviceContainer devices = wifi.Install(phy, mac, nodes);

mac.SetType(“ns3::ApWifiMac”,

“Ssid”, SsidValue(ssid));

NetDeviceContainer apDevices = wifi.Install(phy, mac, nodes.Get(0));

// Install the internet stack

InternetStackHelper stack;

stack.Install(nodes);

// Assign IP addresses

Ipv4AddressHelper address;

address.SetBase(“10.1.1.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces = address.Assign(devices);

address.Assign(apDevices);

// Log position changes

Config::Connect(“/NodeList/*/$ns3::MobilityModel/CourseChange”, MakeCallback(&CourseChange));

// Estimate positions

for (auto &node : localizationNodes)

{

node.EstimatePosition(localizationNodes);

}

// Enable FlowMonitor to measure performance metrics

FlowMonitorHelper flowmon;

Ptr<FlowMonitor> monitor = flowmon.InstallAll();

// Run the simulation

Simulator::Stop(Seconds(10.0));

Simulator::Run();

// Print per-flow statistics

monitor->CheckForLostPackets();

Ptr<Ipv4FlowClassifier>classifier= DynamicCast<Ipv4FlowClassifier>(flowmon.GetClassifier());

std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats();

for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin(); i != stats.end(); ++i)

{

Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow(i->first);

NS_LOG_UNCOND(“Flow ” << i->first << ” (” << t.sourceAddress << ” -> ” << t.destinationAddress << “)”);

NS_LOG_UNCOND(”  Tx Packets: ” << i->second.txPackets);

NS_LOG_UNCOND(”  Tx Bytes:   ” << i->second.txBytes);

NS_LOG_UNCOND(”  Rx Packets: ” << i->second.rxPackets);

NS_LOG_UNCOND(”  Rx Bytes:   ” << i->second.rxBytes);

NS_LOG_UNCOND(”  Lost Packets: ” << i->second.lostPackets);

NS_LOG_UNCOND(”  Throughput: ” << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds() – i->second.timeFirstTxPacket.GetSeconds()) / 1024 / 1024 << ” Mbps”);

}

// Clean up

Simulator::Destroy();

return 0;

}

Step 3: Compile and Run the Simulation

  1. Compile the Simulation:

./waf configure –enable-examples

./waf build

Run the Simulation:

./waf –run scratch/network-localization-example

Step 4: Analyze Results

A network topology has setted up by the simulation script with nodes and for localization we had used a simple average position estimation algorithm. The positions and estimated positions of the sensor nodes are logged as they are calculated. FlowMonitor is used to collect and print out statistics about the traffic flows, such as packet loss, throughput, and delay.

Additional Considerations

To extend the functionality of your network localization simulation, consider the following:

1. Advanced Localization Algorithms

Implement more advanced localization algorithms such as DV-Hop, Centroid, or APIT to improve the accuracy of position estimates.

2. Dynamic Network Topologies

Simulate dynamic network topologies where nodes can move, join, or leave the network, and evaluate the performance of the localization algorithm in such scenarios.

3. Communication Protocols

Integrate communication protocols to allow nodes to exchange position and connectivity information, which can be used for localization.

4. Performance Metrics

To evaluate the performance of the localization algorithm we have to Collect and analyze additional metrics such as localization error, network latency, energy consumption, and packet delivery ratio.

5. Real-World Scenarios

To test the effectiveness of the localization approach in practical applications we need to simulate real-world scenarios such as environmental monitoring, disaster response, or wildlife tracking.

From the given example and steps we have clearly understand the concept of simulating the network localization schemes in ns3 by using the estimating their position with various localization algorithm which is particularly used in wireless sensor networks.

Approach ns3simulation.com to get best outcomes for performance analysis and Implementation of network Localization schemes in ns3tool which are carried out by us.