0% found this document useful (0 votes)
9 views

Code 02

This document contains code for simulating LTE resource allocation and user activity over time. It defines constants for the number of users, PRB capacity, and simulation time. It creates nodes for an eNodeB and users, installs LTE and IP stacks, attaches users to the eNodeB, and simulates user data demand and PRB allocation in a loop over the simulation time.

Uploaded by

julien PELE
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Code 02

This document contains code for simulating LTE resource allocation and user activity over time. It defines constants for the number of users, PRB capacity, and simulation time. It creates nodes for an eNodeB and users, installs LTE and IP stacks, attaches users to the eNodeB, and simulates user data demand and PRB allocation in a loop over the simulation time.

Uploaded by

julien PELE
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#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/point-to-point-module.h"
#include "ns3/lte-module.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE("WifiOffloadSimulator");

const int NUM_USERS = 50;


const double PRB_CAPACITY = 1000.0; // Total number of available PRBs
const double PRB_USAGE_RATE = 0.02; // Rate at which PRBs are used per user (adjust
based on your scenario)
const double SIMULATION_TIME = 10.0; // in seconds

struct User {
double dataDemand; // User's data demand in PRBs
Ptr<Node> node; // User's node in ns-3
};

double usedPRBs = 0.0;

void SimulateUserActivity(std::vector<User>& users) {


for (size_t i = 0; i < users.size(); ++i) {
// Simulate user data demand
users[i].dataDemand = (rand() % 10) + 1; // Example: random data demand
between 1 and 10 PRBs

// Check if there are enough available PRBs


if (usedPRBs + users[i].dataDemand <= PRB_CAPACITY) {
// Allocate PRBs to the user
usedPRBs += users[i].dataDemand;
} else {
// Handle congestion or queuing mechanisms
NS_LOG_INFO("Congestion: User " << i << " cannot get required PRBs.");
}
}
}

void ReleaseUnusedPRBs(std::vector<User>& users) {


// Simulate release of PRBs for users who have finished using resources
for (size_t i = 0; i < users.size(); ++i) {
if (usedPRBs >= users[i].dataDemand) {
usedPRBs -= users[i].dataDemand;
} else {
// Handle potential error or queuing mechanisms
NS_LOG_INFO("Error: User " << i << " trying to release more PRBs than
allocated.");
}
}
}

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


LogComponentEnable("WifiOffloadSimulator", LOG_LEVEL_INFO);

// Seed for random number generation


srand(time(0));

// Create nodes
NodeContainer enbNode;
enbNode.Create(1);

NodeContainer ueNodes;
ueNodes.Create(NUM_USERS);

// Mobility model for UEs (random walk)


MobilityHelper mobility;
mobility.SetMobilityModel("ns3::RandomWalk2dMobilityModel",
"Bounds", RectangleValue(Rectangle(-500, 500, -500,
500)));
mobility.Install(ueNodes);

// Install LTE
NetDeviceContainer enbLteDevice, ueLteDevice;
enbLteDevice = lte.Install(enbNode);
ueLteDevice = lte.Install(ueNodes);

// Internet stack
InternetStackHelper stack;
stack.Install(enbNode);
stack.Install(ueNodes);

// Install IP/EPC stack on eNodeB


lteHelper->Attach(enbLteDevice, ueLteDevice);

// Internet simulation
InternetHelper internet;
NodeContainer internetNode;
internetNode.Create(1);
NetDeviceContainer internetDevices = p2p.Install(internetNode.Get(0),
enbNode.Get(0));
internet.Install(internetNode);

// Configure the remote node as the Internet


Ipv4AddressHelper ipv4h;
ipv4h.SetBase("10.1.2.0", "255.255.255.0");
Ipv4InterfaceContainer internetInterfaces = ipv4h.Assign(internetDevices);

// Simulation loop
std::vector<User> users(NUM_USERS);
for (size_t i = 0; i < NUM_USERS; ++i) {
users[i].node = ueNodes.Get(i);
}

for (double currentTime = 0.0; currentTime < SIMULATION_TIME; currentTime +=


1.0) {
SimulateUserActivity(users);
ReleaseUnusedPRBs(users);

Simulator::Stop(Seconds(currentTime + 1.0));
Simulator::Run();
Simulator::Destroy();
}

return 0;
}

You might also like