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

How to Calculate Normalized MSE in Ns3

Calculating the Normalized Mean Squared Error (NMSE) in ns3 involves comparing the estimated values against the actual values over a set of data points. To achieve this, we have to run a simulator which collects the actual and estimated values, then computes the NMSE on basis of these values. Here is a complete guide on implementing and calculating NMSE in ns3.

Steps for calculation

  1. Set up your ns3 :
  • Make sure that ns3 is installed in the computer. If not, install it.

 

  1. Create a new ns3 script :
  • In the scratch directory of ns3, create a new script.

 

  1. Include necessary libraries :
  • In your script, include the necessary libraries.

 

  1. Define network topology :
  • For your network topology, create multiple nodes.
  1. Collect data :
  • To collect actual and estimated values, run a simulation.

 

  1. Calculate NMSE :
  • To calculate NMSE based on the collected data, implement a function.

Example for calculating NMSE in ns3

Here is the example for the calculation of NMSE :

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/wifi-module.h”

#include “ns3/mobility-module.h”

#include “ns3/applications-module.h”

#include <vector>

#include <numeric>

#include <cmath>

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“NMSECalculationExample”);

std::vector<double> actualValues;

std::vector<double> estimatedValues;

void CollectData(double actual, double estimated)

{

actualValues.push_back(actual);

estimatedValues.push_back(estimated);

}

double CalculateNMSE()

{

double mse = 0.0;

double meanActual = std::accumulate(actualValues.begin(), actualValues.end(), 0.0) / actualValues.size();

for (size_t i = 0; i < actualValues.size(); ++i)

{

mse += std::pow(estimatedValues[i] – actualValues[i], 2);

}

mse /= actualValues.size();

double variance = 0.0;

for (size_t i = 0; i < actualValues.size(); ++i)

{

variance += std::pow(actualValues[i] – meanActual, 2);

}

variance /= actualValues.size();

return mse / variance;

}

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

{

uint32_t nNodes = 10; // Number of nodes

double simulationTime = 10.0; // Total simulation time in seconds

CommandLine cmd;

cmd.AddValue(“nNodes”, “Number of nodes”, nNodes);

cmd.AddValue(“simulationTime”, “Total simulation time”, simulationTime);

cmd.Parse(argc, argv);

NodeContainer nodes;

nodes.Create(nNodes);

YansWifiChannelHelper channel = YansWifiChannelHelper::Default();

YansWifiPhyHelper phy = YansWifiPhyHelper::Default();

phy.SetChannel(channel.Create());

WifiHelper wifi = WifiHelper::Default();

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

WifiMacHelper mac;

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

mac.SetType(“ns3::StaWifiMac”, “Ssid”, SsidValue(ssid), “ActiveProbing”, BooleanValue(false));

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

MobilityHelper mobility;

Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();

for (uint32_t i = 0; i < nNodes; ++i)

{

positionAlloc->Add(Vector(rand() % 100, rand() % 100, 0.0)); // Random positions for nodes

}

mobility.SetPositionAllocator(positionAlloc);

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

mobility.Install(nodes);

InternetStackHelper stack;

stack.Install(nodes);

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign(devices);

UdpEchoServerHelper echoServer(9);

ApplicationContainer serverApps = echoServer.Install(nodes.Get(0));

serverApps.Start(Seconds(1.0));

serverApps.Stop(Seconds(simulationTime));

UdpEchoClientHelper echoClient(interfaces.GetAddress(0), 9);

echoClient.SetAttribute(“MaxPackets”, UintegerValue(1));

echoClient.SetAttribute(“Interval”, TimeValue(Seconds(1.0)));

echoClient.SetAttribute(“PacketSize”, UintegerValue(1024));

ApplicationContainer clientApps = echoClient.Install(nodes.Get(nNodes – 1));

clientApps.Start(Seconds(2.0));

clientApps.Stop(Seconds(simulationTime));

// Example: Collect actual and estimated data

Simulator::Schedule(Seconds(1.0), &CollectData, 1.0, 1.2); // Replace with actual data collection logic

Simulator::Schedule(Seconds(2.0), &CollectData, 1.1, 1.15);

Simulator::Schedule(Seconds(3.0), &CollectData, 0.9, 0.95);

Simulator::Stop(Seconds(simulationTime));

Simulator::Run();

Simulator::Destroy();

double nmse = CalculateNMSE();

std::cout << “Normalized Mean Squared Error (NMSE): ” << nmse << std::endl;

return 0;

}

Explanation

  1. Nodes and links :

Nodes are created. Wi-Fi network is configured for communication.

  1. Mobility :

Random position for the nodes are configured. To keep the nodes stationary, ConstantPositionMobilityModel is used.

  1. Applications :

On one node, a UDP echo server is installed. and On another node, a UDP echo server is installed to generate traffic.

  1. Data collection :

To collect actual and estimated values, the CollectData is used. Here you should add logic to collect these values based on your specific simulation requirements.

  1. NMSE calculation logic :

To calculate the Normalized Mean Squared Error based on the collected data, CalculateNMSE is implemented. o       The NMSE is calculated as the Mean Squared Error (MSE) divided by the variance of the actual values.

  1. Running the Simulation :

By collecting data at specified times, simulation runs. NMSE is calculated and printed, after the simulation.

Overall, we had successfully learned on calculating NMSE in ns3 by simulator which can collect both the actual and estimated values, then computing the NMSE based on these values.

Do you want help in comparison analysis on Calculating the Normalized Mean Squared Error (NMSE)  for your project then share with us your parameters we assure you with detailed result.