-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadr-bandit-example-multi-gw.cc
507 lines (381 loc) · 19.1 KB
/
adr-bandit-example-multi-gw.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
* This program creates a simple network which uses an ADR algorithm to set up
* the Spreading Factors of the devices in the Network.
*/
#include "ns3/point-to-point-module.h"
#include "ns3/forwarder-helper.h"
#include "ns3/network-server-helper.h"
#include "ns3/lora-channel.h"
#include "ns3/mobility-helper.h"
#include "ns3/lora-phy-helper.h"
#include "ns3/lorawan-mac-helper.h"
#include "ns3/lora-helper.h"
#include "ns3/gateway-lora-phy.h"
#include "ns3/periodic-sender.h"
#include "ns3/periodic-sender-helper.h"
#include "ns3/log.h"
#include "ns3/string.h"
#include "ns3/command-line.h"
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/lora-device-address-generator.h"
#include "ns3/random-variable-stream.h"
#include "ns3/config.h"
#include "ns3/rectangle.h"
#include "ns3/hex-grid-position-allocator.h"
#include "ns3/correlated-shadowing-propagation-loss-model.h"
#include "ns3/building-penetration-loss.h"
#include "ns3/building-allocator.h"
#include "ns3/buildings-helper.h"
using namespace ns3;
using namespace lorawan;
NS_LOG_COMPONENT_DEFINE ("AdrBanditExampleMultiGW");
// Trace sources that are called when a node changes its DR or TX power
void OnDataRateChange (uint8_t oldDr, uint8_t newDr)
{
NS_LOG_DEBUG ("DR" << unsigned(oldDr) << " -> DR" << unsigned(newDr));
}
void OnTxPowerChange (double oldTxPower, double newTxPower)
{
NS_LOG_DEBUG (oldTxPower << " dBm -> " << newTxPower << " dBm");
}
int main (int argc, char *argv[])
{
bool verbose = false;
bool adrEnabled = true;
bool initializeSF = false;
int nDevices = 400;
int nPeriods = 20;
// Topology of the Simulation (re-taken from adr-example.cc)
double mobileNodeProbability = 0;
int gatewayDistance = 2000;
double sideLength = gatewayDistance * 1.1;
// Channel model
bool realisticChannelModel = true;
bool isPrintBuildings = true;
double maxRandomLoss = 10; // For the not-realisticChannelModel that uses a random loss
double minSpeed = 2;
double maxSpeed = 16;
std::string adrType = "ns3::AdrComponent"; /* [Renzo] Here we can use different ADR implementations (at NS) */
CommandLine cmd;
cmd.AddValue ("verbose", "Whether to print output or not", verbose);
cmd.AddValue ("MultipleGwCombiningMethod",
"ns3::AdrComponent::MultipleGwCombiningMethod");
cmd.AddValue ("MultiplePacketsCombiningMethod",
"ns3::AdrComponent::MultiplePacketsCombiningMethod");
cmd.AddValue ("HistoryRange", "ns3::AdrComponent::HistoryRange");
cmd.AddValue ("MType", "ns3::EndDeviceLorawanMac::MType");
cmd.AddValue ("EDDRAdaptation", "ns3::EndDeviceLorawanMac::EnableEDDataRateAdaptation");
cmd.AddValue ("ChangeTransmissionPower",
"ns3::AdrComponent::ChangeTransmissionPower");
cmd.AddValue ("AdrEnabled", "Whether to enable ADR", adrEnabled);
cmd.AddValue ("nDevices", "Number of devices to simulate", nDevices);
cmd.AddValue ("PeriodsToSimulate", "Number of periods to simulate", nPeriods);
cmd.AddValue ("MobileNodeProbability",
"Probability of a node being a mobile node",
mobileNodeProbability);
cmd.AddValue ("sideLength",
"Length of the side of the rectangle nodes will be placed in",
sideLength);
cmd.AddValue ("maxRandomLoss",
"Maximum amount in dB of the random loss component",
maxRandomLoss);
cmd.AddValue ("gatewayDistance",
"Distance between gateways",
gatewayDistance);
cmd.AddValue ("initializeSF",
"Whether to initialize the SFs",
initializeSF);
cmd.AddValue ("MinSpeed",
"Minimum speed for mobile devices",
minSpeed);
cmd.AddValue ("MaxSpeed",
"Maximum speed for mobile devices",
maxSpeed);
cmd.AddValue ("MaxTransmissions",
"ns3::EndDeviceLorawanMac::MaxTransmissions");
cmd.Parse (argc, argv);
int gatewayRings = 2 + (std::sqrt(2) * sideLength) / (gatewayDistance);
//int nGateways = 3*gatewayRings*gatewayRings-3*gatewayRings+1;
int nGateways = 7;
std::cout << "sideLength" << sideLength << std::endl;
std::cout << "gatewayDistance" << gatewayDistance << std::endl;
std::cout << "gatewayRings" << gatewayRings << std::endl;
std::cout << "nGateways" << nGateways << std::endl;
// Logging
//////////
LogComponentEnable ("AdrBanditExampleMultiGW", LOG_LEVEL_ALL);
//LogComponentEnable ("LoraHelper", LOG_LEVEL_ALL);
// LogComponentEnable ("LoraPacketTracker", LOG_LEVEL_ALL);
// LogComponentEnable ("NetworkServer", LOG_LEVEL_ALL);
// LogComponentEnable ("NetworkController", LOG_LEVEL_ALL);
// LogComponentEnable ("NetworkScheduler", LOG_LEVEL_ALL);
// LogComponentEnable ("NetworkStatus", LOG_LEVEL_ALL);
// LogComponentEnable ("EndDeviceStatus", LOG_LEVEL_ALL);
// LogComponentEnable ("AdrComponent", LOG_LEVEL_ALL);
//LogComponentEnable("ClassAEndDeviceLorawanMac", LOG_LEVEL_ALL);
// LogComponentEnable("ClassAEndDeviceLorawanMacBandit", LOG_LEVEL_ALL);
//LogComponentEnable("ClassAEndDeviceLorawanMacBandit", LOG_LEVEL_LOGIC);
// LogComponentEnable("BanditPolicy", LOG_LEVEL_ALL);
// LogComponentEnable("ADRBanditAgent", LOG_LEVEL_ALL);
//LogComponentEnable ("EndDeviceLorawanMac", LOG_LEVEL_ALL);
// LogComponentEnable ("LogicalLoraChannelHelper", LOG_LEVEL_ALL);
//LogComponentEnable ("MacCommand", LOG_LEVEL_ALL);
// LogComponentEnable ("AdrExploraSf", LOG_LEVEL_ALL);
// LogComponentEnable ("AdrExploraAt", LOG_LEVEL_ALL);
// LogComponentEnable ("SimpleGatewayLoraPhy", LOG_LEVEL_ALL);
/*Debugging new MAC command */
// LogComponentEnable ("SimpleEndDeviceLoraPhy", LOG_LEVEL_ALL);
// LogComponentEnable ("EndDeviceLoraPhy", LOG_LEVEL_ALL);
// //LogComponentEnable ("LoraFrameHeader", LOG_LEVEL_ALL);
// LogComponentEnable ("EndDeviceLorawanMac", LOG_LEVEL_ALL);
//
//
//
// LogComponentEnable ("NetworkControllerComponentBandit", LOG_LEVEL_ALL); // We can debug how the NS answers to the MAC BanditRewardReq and prepares the Reward in the BanditRewardAns
// LogComponentEnable ("BanditDelayedRewardIntelligence", LOG_LEVEL_ALL); // We can debug (among many things) how the bandits creates the MAC BanditRewardReq
// LogComponentEnable("ClassAEndDeviceLorawanMacBandit", LOG_LEVEL_ALL); // We can debug when the bandit receives the MAC BanditRewardAns
//
//
//
// LogComponentEnable ("PeriodicSender", LOG_LEVEL_ALL);
// LogComponentEnable ("LoraInterferenceHelper", LOG_LEVEL_ALL); // To test interference Matrix
LogComponentEnableAll (LOG_PREFIX_FUNC);
LogComponentEnableAll (LOG_PREFIX_NODE);
LogComponentEnableAll (LOG_PREFIX_TIME);
// Set the EDs to require Data Rate control from the NS
Config::SetDefault ("ns3::EndDeviceLorawanMac::DRControl", BooleanValue (true));
/************************
* Create the channel *
************************/
// [RN] Buildings and Shadowing From complete-network-example.cc
// 1) LogDistancePropagationLossModel
Ptr<LogDistancePropagationLossModel> loss = CreateObject<LogDistancePropagationLossModel> ();
loss->SetPathLossExponent (3.76);
loss->SetReference (1, 7.7);
// [RN] Discussion about this values (7.7) https://gitter.im/ns-3-lorawan/Lobby?at=5c1e3fe72863d8612b71b730
// 1b) Disable Random Loss
// Ptr<UniformRandomVariable> x = CreateObject<UniformRandomVariable> ();
// x->SetAttribute ("Min", DoubleValue (0.0));
// x->SetAttribute ("Max", DoubleValue (maxRandomLoss));
//
// Ptr<RandomPropagationLossModel> randomLoss = CreateObject<RandomPropagationLossModel> ();
// randomLoss->SetAttribute ("Variable", PointerValue (x));
//
// loss->SetNext (randomLoss);
if (realisticChannelModel)
{
// Create the correlated shadowing component
Ptr<CorrelatedShadowingPropagationLossModel> shadowing =
CreateObject<CorrelatedShadowingPropagationLossModel> ();
// Aggregate shadowing to the logdistance loss
loss->SetNext (shadowing);
// Add the effect to the channel propagation loss
Ptr<BuildingPenetrationLoss> buildingLoss = CreateObject<BuildingPenetrationLoss> ();
shadowing->SetNext (buildingLoss);
}
else // Not-so reqlistic model: we add a random loss
{
Ptr<UniformRandomVariable> x = CreateObject<UniformRandomVariable> ();
x->SetAttribute ("Min", DoubleValue (0.0));
x->SetAttribute ("Max", DoubleValue (maxRandomLoss));
Ptr<RandomPropagationLossModel> randomLoss = CreateObject<RandomPropagationLossModel> ();
randomLoss->SetAttribute ("Variable", PointerValue (x));
loss->SetNext (randomLoss);
}
// Z)
Ptr<PropagationDelayModel> delay = CreateObject<ConstantSpeedPropagationDelayModel> ();
Ptr<LoraChannel> channel = CreateObject<LoraChannel> (loss, delay);
/************************
* Create the helpers *
************************/
//MobilityHelpers (Help to set the position of the devices, in this case will be static)
MobilityHelper mobilityEd, mobilityGw;
// a) End Device mobility
mobilityEd.SetPositionAllocator ("ns3::RandomRectanglePositionAllocator",
"X", PointerValue (CreateObjectWithAttributes<UniformRandomVariable>
("Min", DoubleValue(-sideLength),
"Max", DoubleValue(sideLength))),
"Y", PointerValue (CreateObjectWithAttributes<UniformRandomVariable>
("Min", DoubleValue(-sideLength),
"Max", DoubleValue(sideLength))));
// b) Gateway mobility
//Ptr<ListPositionAllocator> positionAllocGw = CreateObject<ListPositionAllocator> ();
//positionAllocGw->Add (Vector (0.0, 0.0, 15.0)); // a gateway in the center of the cartesian cordinates!
// positionAllocGw->Add (Vector (-5000.0, -5000.0, 15.0));
// positionAllocGw->Add (Vector (-5000.0, 5000.0, 15.0));
// positionAllocGw->Add (Vector (5000.0, -5000.0, 15.0));
// positionAllocGw->Add (Vector (5000.0, 5000.0, 15.0));
// mobilityGw.SetPositionAllocator (positionAllocGw);
// mobilityGw.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
Ptr<HexGridPositionAllocator> hexAllocator = CreateObject<HexGridPositionAllocator> (gatewayDistance / 2);
mobilityGw.SetPositionAllocator (hexAllocator);
mobilityGw.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
//mobilityGw.SetPositionAllocator ("ns3::UniformDiscPositionAllocator", "rho", DoubleValue (radius),
// "X", DoubleValue (0.0), "Y", DoubleValue (0.0));// [RN] Super Wrong! I was putting the GW at random!!
//mobilityGw.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
// Other Helpers
// Create the LoraPhyHelper
LoraPhyHelper phyHelper = LoraPhyHelper ();
phyHelper.SetChannel (channel);
// Create the LorawanMacHelper
LorawanMacHelper macHelper = LorawanMacHelper ();
// Create the LoraHelper
LoraHelper helper = LoraHelper ();
helper.EnablePacketTracking ();
/*********************
* Create Gateways *
*********************/
NodeContainer gateways;
gateways.Create (nGateways);
mobilityGw.Install (gateways);
// Create the LoraNetDevices of the gateways
phyHelper.SetDeviceType (LoraPhyHelper::GW);
macHelper.SetDeviceType (LorawanMacHelper::GW);
helper.Install (phyHelper, macHelper, gateways);
/************************
* Create End Devices *
************************/
NodeContainer endDevices;
endDevices.Create (nDevices);
// Install mobility model on fixed nodes (all nodes are fixed on this modified example)
mobilityEd.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
for (int i = 0; i < nDevices; ++i) // i < (int) endDevices.GetN ()
{
mobilityEd.Install (endDevices.Get (i));
}
// Se adr-example.cc line 209 to create nodes with mobility
// Make it so that nodes are at a certain height > 0
for (NodeContainer::Iterator j = endDevices.Begin (); j != endDevices.End (); ++j)
{
Ptr<MobilityModel> mobility = (*j)->GetObject<MobilityModel> ();
Vector position = mobility->GetPosition ();
position.z = 1.2;
mobility->SetPosition (position);
}
// Create a LoraDeviceAddressGenerator
uint8_t nwkId = 54;
uint32_t nwkAddr = 1864;
Ptr<LoraDeviceAddressGenerator> addrGen = CreateObject<LoraDeviceAddressGenerator> (nwkId,nwkAddr);
// Create the LoraNetDevices of the end devices
phyHelper.SetDeviceType (LoraPhyHelper::ED);
//macHelper.SetDeviceType (LorawanMacHelper::ED_A); // We create normal ADR nodes
macHelper.SetDeviceType (LorawanMacHelper::ED_A_ADR_BANDIT); // We create ADR Bandits nodes :)
macHelper.SetAddressGenerator (addrGen);
macHelper.SetRegion (LorawanMacHelper::EU);
helper.Install (phyHelper, macHelper, endDevices);
/**********************
* Handle buildings *
**********************/
double xLength = 130;
double deltaX = 32;
double yLength = 64;
double deltaY = 17;
double radius = sideLength;
int gridWidth = 2 * radius / (xLength + deltaX); //2* sideLength ;
int gridHeight = 2 * radius / (yLength + deltaY);
if (realisticChannelModel == false)
{
gridWidth = 0;
gridHeight = 0;
}
Ptr<GridBuildingAllocator> gridBuildingAllocator;
gridBuildingAllocator = CreateObject<GridBuildingAllocator> ();
gridBuildingAllocator->SetAttribute ("GridWidth", UintegerValue (gridWidth));
gridBuildingAllocator->SetAttribute ("LengthX", DoubleValue (xLength));
gridBuildingAllocator->SetAttribute ("LengthY", DoubleValue (yLength));
gridBuildingAllocator->SetAttribute ("DeltaX", DoubleValue (deltaX));
gridBuildingAllocator->SetAttribute ("DeltaY", DoubleValue (deltaY));
gridBuildingAllocator->SetAttribute ("Height", DoubleValue (6));
gridBuildingAllocator->SetBuildingAttribute ("NRoomsX", UintegerValue (2));
gridBuildingAllocator->SetBuildingAttribute ("NRoomsY", UintegerValue (4));
gridBuildingAllocator->SetBuildingAttribute ("NFloors", UintegerValue (2));
gridBuildingAllocator->SetAttribute (
"MinX", DoubleValue (-gridWidth * (xLength + deltaX) / 2 + deltaX / 2));
gridBuildingAllocator->SetAttribute (
"MinY", DoubleValue (-gridHeight * (yLength + deltaY) / 2 + deltaY / 2));
BuildingContainer bContainer = gridBuildingAllocator->Create (gridWidth * gridHeight);
BuildingsHelper::Install (endDevices);
BuildingsHelper::Install (gateways);
// Print the buildings
if (isPrintBuildings)
{
std::ofstream myfile;
myfile.open ("buildings.txt");
std::vector<Ptr<Building>>::const_iterator it;
int j = 1;
for (it = bContainer.Begin (); it != bContainer.End (); ++it, ++j)
{
Box boundaries = (*it)->GetBoundaries ();
myfile << "set object " << j << " rect from " << boundaries.xMin << "," << boundaries.yMin
<< " to " << boundaries.xMax << "," << boundaries.yMax << std::endl;
}
myfile.close ();
}
/*********************************************
* Install applications on the end devices *
*********************************************/
// Install applications in EDs
int appPeriodSeconds = 1200; // One packet every 20 minutes
//int appPeriodSeconds = 300; // One packet every 5 minutes (300 s)
/*Renzo: BEWARE! When the max size is excedeed (eg by a MAC command) there simulation does not work and does not print proper debug*/
int packetSizeBytes = 32; /* 49, 50 --> When we piggyback MAC answers at SF=12 cause problems: (LoraTap has a header of 15 Bytes)
EndDeviceLorawanMac:DoSend(): Attempting to send a packet larger than the maximum allowed size at this DataRate (DR0). Transmission canceled. */
PeriodicSenderHelper appHelper = PeriodicSenderHelper ();
appHelper.SetPacketSize (packetSizeBytes);
appHelper.SetPeriod (Seconds (appPeriodSeconds));
ApplicationContainer appContainer = appHelper.Install (endDevices);
// Do not set spreading factors up: we will wait for the NS to do this
if (initializeSF)
{
macHelper.SetSpreadingFactorsUp (endDevices, gateways, channel);
}
////////////
// Create NS
////////////
NodeContainer networkServers;
networkServers.Create (1);
// Install the NetworkServer application on the network server
NetworkServerHelper networkServerHelper;
networkServerHelper.SetGateways (gateways);
networkServerHelper.SetEndDevices (endDevices);
networkServerHelper.EnableAdr (adrEnabled);
networkServerHelper.SetAdr (adrType);
networkServerHelper.Install (networkServers);
// Install the Forwarder application on the gateways
ForwarderHelper forwarderHelper;
forwarderHelper.Install (gateways);
// Connect our traces ([Renzo] Disable for big simulations to go faster)
//Config::ConnectWithoutContext ("/NodeList/*/DeviceList/0/$ns3::LoraNetDevice/Mac/$ns3::EndDeviceLorawanMac/TxPower",
// MakeCallback (&OnTxPowerChange));
//Config::ConnectWithoutContext ("/NodeList/*/DeviceList/0/$ns3::LoraNetDevice/Mac/$ns3::EndDeviceLorawanMac/DataRate",
// MakeCallback (&OnDataRateChange));
// Activate printing of ED MAC parameters
Time stateSamplePeriod = Seconds (1200);
helper.DoPrintDeviceStatus (gateways, "gwData.txt"); // Renzo: We save the GWs position
helper.EnablePeriodicDeviceStatusPrinting (endDevices, gateways, "nodeData.txt", stateSamplePeriod); // Renzo: currently I disabled info from gateways
helper.EnablePeriodicPhyPerformancePrinting (gateways, "phyPerformance.txt", stateSamplePeriod);
helper.EnablePeriodicGlobalPerformancePrinting ("globalPerformance.txt", stateSamplePeriod);
// phyPerformance: SENT RECEIVED INTERFERED NO_MORE_RECEIVERS UNDER_SENSITIVITY LOST_BECAUSE_TX
// nodeData : currentTime.GetSeconds () , object->GetId () ; pos.x ; pos.y ; dr (data rate) ; unsigned(txPower)
//[Renzo] PCAP Helper
// https://gitlab.com/nsnam/ns-3-dev/-/blob/master/src/lr-wpan/helper/lr-wpan-helper.cc#L319
// https://gitlab.com/nsnam/ns-3-dev/-/blob/master/src/wifi/helper/wifi-helper.cc#L612
//helper.EnablePcapAll(std::string ("ADR-Bandit-Example"), true);
// [Renzo] Disable for big simulations to go faster (but not that much):
//helper.EnablePcap(std::string ("ADR-Bandit-Example-ED"), endDevices , false);
//helper.EnablePcap(std::string ("ADR-Bandit-Example-GW"), gateways , true);
//helper.EnablePcap(std::string ("ADR-Bandit-Example-NS"), networkServers , true);
/*Renzo Notes:
* ADR component: ./waf --run "src/lorawan/examples/adr-bandit-example --nDevices=1 --HistoryRange=19 --PeriodsToSimulate=5"
* Only decreases 3 setps, but with modifs could do 4 steps at once. (Resume: it takes two messageq to decease 4 steps total) */
LoraPacketTracker& tracker = helper.GetPacketTracker ();
// Start simulation
Time simulationTime = Seconds (1200 * nPeriods);
Simulator::Stop (simulationTime);
Simulator::Run ();
Simulator::Destroy ();
std::cout << tracker.CountMacPacketsGlobally(Seconds (1200 * (nPeriods - 2)),
Seconds (1200 * (nPeriods - 1))) << std::endl;
return 0;
}