#define numOfNodes 30#define numOfNodesDouble (double) numOfNodes#define max_x 100.0#define max_y 100.0#define distance 15.0#define percentage 20.0#define percentageComp numOfNodesDouble*percentage/100
// 1- STEP 1: Generate RANDOM POSITIONS FOR THE NODES double x = 0.0; double y = 0.0; for(int i = 0; i < numOfNodes; i++) { x = ((double) rand() / (RAND_MAX))*max_x; y = ((double) rand() / (RAND_MAX))*max_y; positionAlloc->Add (Vector (x, y, 10.0)); }
mobility.SetPositionAllocator (positionAlloc); mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); //mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel","Bounds", RectangleValue (Rectangle (0, 1000, 0, 1000))); mobility.Install (nodes); int ii = 0; int jj = 0; double connectionsCounter = 0.0; double euclideanDistance = 0.0; // 2- STEP 2 ITERATE THROUGH EACH NODE POSITION for (NodeContainer::Iterator i = nodes.Begin (); i != nodes.End (); ++i) { Ptr<Node> nodePtr = *i; Ptr<MobilityModel> mob = nodePtr->GetObject<MobilityModel> (); Vector pos = mob->GetPosition (); jj = 0; connectionsCounter = 0.0; for (NodeContainer::Iterator j = nodes.Begin (); j != nodes.End (); ++j) { // NOT ITERATE ON SAME NODE if(ii != jj) { Ptr<Node> nodePtrTwo = *j; Ptr<MobilityModel> mobTwo = nodePtrTwo->GetObject<MobilityModel> (); Vector posTwo = mobTwo->GetPosition (); euclideanDistance = sqrt(abs(pos.x - posTwo.x)*abs(pos.x - posTwo.x) + abs(pos.y - posTwo.y)*abs(pos.y - posTwo.y)); if(euclideanDistance <= distance) { connectionsCounter++; } // 3- STEP 3 CHECK THE POSITION IS NOT REPEATED if((pos.x == posTwo.x) || (pos.y == posTwo.y)) { NS_LOG_UNCOND("REPEATED POSITION "); mob->SetPosition(Vector(((double) rand() / (RAND_MAX))*max_x, ((double) rand() / (RAND_MAX))*max_y, 10.0)); ii = 0; jj = 0; i = nodes.Begin (); connectionsCounter = 0.0; break; } if(jj == (numOfNodes-1)) { // 4- STEP 4 CHECK NODE CONNECTS AT LEAST WITH SPECIFIED % OF WHOLE GRAPH NS_LOG_UNCOND("Reached end of iterator "<<jj); if(connectionsCounter < percentageComp) { mob->SetPosition(Vector(((double) rand() / (RAND_MAX))*max_x, ((double) rand() / (RAND_MAX))*max_y, 10.0)); ii = 0; jj = 0; i = nodes.Begin (); connectionsCounter = 0.0; break; } } } jj++; } NS_LOG_UNCOND("percentagecomp "<<percentageComp<<" connections "<<connectionsCounter); ii++; }
#define numOfNodes 30#define numOfNodesDouble (double) numOfNodes#define max_x 100.0#define max_y 100.0#define distance 15.0#define percentage 20.0#define percentageComp numOfNodesDouble*percentage/100
// 1- STEP 1: Generate RANDOM POSITIONS FOR THE NODES double x = 0.0; double y = 0.0; for(int i = 0; i < numOfNodes; i++) { x = ((double) rand() / (RAND_MAX))*max_x; y = ((double) rand() / (RAND_MAX))*max_y; positionAlloc->Add (Vector (x, y, 10.0)); }
mobility.SetPositionAllocator (positionAlloc); mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); //mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel","Bounds", RectangleValue (Rectangle (0, 1000, 0, 1000))); mobility.Install (nodes);
double connectionsCounter = 0.0; double euclideanDistance = 0.0; // 2- STEP 2 ITERATE THROUGH EACH NODE POSITION
NodeContainer::Iterator i = nodes.End (); while(i != nodes.Begin ()) { --i;
Ptr<Node> nodePtr = *i; Ptr<MobilityModel> mob = nodePtr->GetObject<MobilityModel> (); Vector pos = mob->GetPosition ();
connectionsCounter = 0.0; for (NodeContainer::Iterator j = nodes.Begin (); j != nodes.End (); ++j) {
Ptr<Node> nodePtrTwo = *j; Ptr<MobilityModel> mobTwo = nodePtrTwo->GetObject<MobilityModel> (); Vector posTwo = mobTwo->GetPosition ();
// NOT ITERATE ON SAME NODE
if(nodePtr->GetId() != nodePtrTwo->GetId()) {
euclideanDistance = sqrt(abs(pos.x - posTwo.x)*abs(pos.x - posTwo.x) + abs(pos.y - posTwo.y)*abs(pos.y - posTwo.y)); if(euclideanDistance <= distance) { connectionsCounter++; } // 3- STEP 3 CHECK THE POSITION IS NOT REPEATED if((pos.x == posTwo.x) || (pos.y == posTwo.y)) { NS_LOG_UNCOND("REPEATED POSITION "); mob->SetPosition(Vector(((double) rand() / (RAND_MAX))*max_x, ((double) rand() / (RAND_MAX))*max_y, 10.0));
i = nodes.End (); connectionsCounter = 0.0; break; } if( nodePtrTwo->GetId() == (numOfNodes-1))
{ // 4- STEP 4 CHECK NODE CONNECTS AT LEAST WITH SPECIFIED % OF WHOLE GRAPH
NS_LOG_UNCOND("Reached end of iterator ");
if(connectionsCounter < percentageComp) { mob->SetPosition(Vector(((double) rand() / (RAND_MAX))*max_x, ((double) rand() / (RAND_MAX))*max_y, 10.0));