pri_liza
unread,May 21, 2012, 2:42:05 PM5/21/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to ns-3-users
Thanks Konstantinos.
Actually I got this algorithm on the internet to analyze the
functioning of it. I'll post it in full here so that they can help me
change it so that it runs.
// -*- mode: c++; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
/*
* Test case: 6*n nodes on a six lane highway
*
* 6*n nodes are put on a highway with 6 lanes. Each lane is 5 meters
apart
* from neighboring lanes. Cars are spaced at 90 meters on each lane
(15 meters
* between two nodes along the x axis) yielding a total of 66.6 nodes
per kilometer.
*/
#include "ns3/core-module.h"
#include "ns3/simulator-module.h"
#include "ns3/node-module.h"
#include "ns3/wifi-module.h"
#include "ns3/helper-module.h"
#include "ns3/traffic-application.h"
#include <iostream>
#include <iomanip>
#include <numeric>
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("Main");
class Experiment
{
public:
static const double m_simulatedTime = 60.0;
unsigned int m_appTxPackets;
unsigned int m_appRxPackets;
unsigned int m_phyRxErrors;
void
Run(unsigned int numNodes)
{
// Create nodes and store them in the container.
NodeContainer nodes;
nodes.Create(numNodes);
// Add packet socket handlers.
PacketSocketHelper packetSocket;
packetSocket.Install(nodes);
// Install wifi devices on the nodes.
Ns2ExtWifiChannelHelper wifiChannel;
wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
wifiChannel.AddPropagationLoss("ns3::ThreeLogDistancePropagationLossModel");
wifiChannel.AddPropagationLoss("ns3::NakagamiPropagationLossModel",
"m0", DoubleValue(1.5),
"m1", DoubleValue(1.0),
"m2", DoubleValue(1.0));
Ns2ExtWifiPhyHelper wifiPhy = Ns2ExtWifiPhyHelper::Default();
wifiPhy.SetChannel(wifiChannel.Create());
wifiPhy.Set("UseConstantNoiseFloor", BooleanValue(true));
wifiPhy.Set("ConstantNoiseFloor", DoubleValue(-99.0));
wifiPhy.Set("PreambleCapture", BooleanValue(true));
wifiPhy.Set("DataCapture", BooleanValue(true));
WifiHelper wifi = WifiHelper::Default();
wifi.SetMac("ns3::AdhocWifiMac");
wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
"DataMode", StringValue("wifia-6mbs"),
"NonUnicastMode", StringValue("wifia-6mbs"));
wifi.Install(wifiPhy, nodes);
// Position nodes on to highway lanes.
Ptr<ListPositionAllocator> positionAlloc
= CreateObject<ListPositionAllocator>();
for (unsigned int i = 0; i < numNodes; ++i)
{
positionAlloc->Add(Vector(i * 15, (i % 6) * 5, 0.0));
}
MobilityHelper mobility;
mobility.SetPositionAllocator(positionAlloc);
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.Install(nodes);
// Use broadcast packet address for applications.
PacketSocketAddress socketBroadcast;
socketBroadcast.SetAllDevices();
socketBroadcast.SetPhysicalAddress(Mac48Address::GetBroadcast());
socketBroadcast.SetProtocol(1);
// Install TrafficApplication on each node.
Ptr<SimpleTrafficPacketFactory> packetFactory
= CreateObject<SimpleTrafficPacketFactory>("Size",
UintegerValue(400));
TrafficHelper trafficApp("ns3::PacketSocketFactory",
socketBroadcast);
trafficApp.SetAttribute("PacketFactory",
PointerValue(packetFactory) );
trafficApp.SetAttribute("OnTime",
RandomVariableValue( ConstantVariable(m_simulatedTime) ));
trafficApp.SetAttribute("OffTime",
RandomVariableValue( UniformVariable(0.0, 0.1) ));
trafficApp.SetAttribute("Interval",
RandomVariableValue( ConstantVariable(0.1) ));
ApplicationContainer app = trafficApp.Install(nodes);
app.Start( Seconds(0.0) );
app.Stop( Seconds(m_simulatedTime) );
// Add Trace callbacks to gather statistics.
Config::Connect("/NodeList/*/ApplicationList/*/
$ns3::TrafficApplication/Tx",
MakeCallback(&Experiment::AppTxTrace, this));
Config::Connect("/NodeList/*/ApplicationList/*/
$ns3::TrafficApplication/Rx",
MakeCallback(&Experiment::AppRxTrace, this));
Config::Connect("/NodeList/*/DeviceList/*/Phy/State/RxError",
MakeCallback(&Experiment::PhyRxErrorTrace, this));
// Zero counters and run simulation.
m_appTxPackets = 0;
m_appRxPackets = 0;
m_phyRxErrors = 0;
Simulator::Run();
Simulator::Destroy();
}
void
AppTxTrace(std::string context, Ptr<const Packet> p)
{
NS_LOG_DEBUG(context << " TX size=" << p->GetSize());
++m_appTxPackets;
}
void
AppRxTrace(std::string context, Ptr<const Packet> p, const Address&
from)
{
NS_LOG_DEBUG(context << " RX from=" << from << " size=" << p-
>GetSize());
++m_appRxPackets;
}
void
PhyRxErrorTrace(std::string context, Ptr<const Packet> p,
Ptr<const WifiPhyTag> phytag, WifiPhy::RxErrorReason reason)
{
NS_LOG_DEBUG(context << " PHYRXERROR"
<< " reason=" << WifiPhy::RxErrorReasonToString(reason)
<< " phytag={" << *phytag << "} p={" << *p << "}");
++m_phyRxErrors;
}
};
template <typename Container>
double meanValue(const Container& c)
{
return std::accumulate(c.begin(), c.end(), 0.0) / c.size();
}
template <typename Container>
double standardDeviation(const Container& c)
{
double squareSum = 0.0;
double sum = 0.0;
for (typename Container::const_iterator ei = c.begin();
ei != c.end(); ++ei)
{
squareSum += (double)(*ei) * (double)(*ei);
sum += *ei;
}
double mean = sum / c.size();
return sqrt( (squareSum / c.size()) - (mean * mean) );
}
template <typename Container>
double errorMargin(const Container& c)
{
return 2.576 * standardDeviation(c) / sqrt(c.size());
}
int main(int argc, char *argv[])
{
CommandLine cmd;
int replications = 1;
unsigned int fixedNumNodes = 0;
cmd.AddValue("Replications", "Perform independent replications.",
replications);
cmd.AddValue("NumNodes", "Run for a fixed number of node.",
fixedNumNodes);
cmd.Parse(argc, argv);
for (unsigned int numNodes = 6; numNodes <= 180; numNodes += 6)
{
if (fixedNumNodes != 0 && numNodes != fixedNumNodes) continue;
std::vector<unsigned int> appTxPackets;
std::vector<unsigned int> appRxPackets;
std::vector<unsigned int> phyRxErrors;
for(int rep = 0; rep < replications; ++rep)
{
SeedManager::SetRun(rep);
Experiment experiment;
experiment.Run(numNodes);
appTxPackets.push_back( experiment.m_appTxPackets );
appRxPackets.push_back( experiment.m_appRxPackets );
phyRxErrors.push_back( experiment.m_phyRxErrors );
}
std::cout << std::fixed
<< numNodes
<< " " << meanValue(appTxPackets) << " " << errorMargin(appTxPackets)
<< " " << meanValue(appRxPackets) << " " << errorMargin(appRxPackets)
<< " " << meanValue(phyRxErrors) << " " << errorMargin(phyRxErrors)
<< std::endl;
}
return 0;
}
I've done the following changes:
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/wifi-module.h"
#include "ns3/applications-module.h"