forked from tkn-tub/ns3-gym
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.cc
208 lines (180 loc) · 8.61 KB
/
sim.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
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2018 Technische Universität Berlin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Piotr Gawlowicz <[email protected]>
*/
#include "ns3/core-module.h"
#include "ns3/applications-module.h"
#include "ns3/packet-sink.h"
#include "ns3/mobility-module.h"
#include "ns3/wifi-module.h"
#include "ns3/internet-module.h"
#include "ns3/spectrum-module.h"
#include "ns3/stats-module.h"
#include "ns3/flow-monitor-module.h"
#include "mygym.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("Interference-Pattern");
int main (int argc, char *argv[])
{
// Parameters of the environment
uint32_t simSeed = 1;
double simulationTime = 10; //seconds
double envStepTime = 0.1; //seconds, ns3gym env step time interval
uint32_t openGymPort = 5555;
uint32_t testArg = 0;
//Parameters of the scenario
uint32_t nodeNum = 2;
double distance = 10.0;
bool enableFading = false;
double interfererPower = 10;
// Interference Pattern
double interferenceSlotTime = 0.1; // seconds;
// time, channel usage
std::map<uint32_t, std::vector<uint32_t> > interferencePattern;
interferencePattern.insert(std::pair<uint32_t, std::vector<uint32_t> > (0, {1,0,0,0}));
interferencePattern.insert(std::pair<uint32_t, std::vector<uint32_t> > (1, {0,1,0,0}));
interferencePattern.insert(std::pair<uint32_t, std::vector<uint32_t> > (2, {0,0,1,0}));
interferencePattern.insert(std::pair<uint32_t, std::vector<uint32_t> > (3, {0,0,0,1}));
// set channel number correctly, do not modify
std::vector<uint32_t> tmp = interferencePattern.at(0);
uint32_t channNum = tmp.size();
CommandLine cmd;
// required parameters for OpenGym interface
cmd.AddValue ("openGymPort", "Port number for OpenGym env. Default: 5555", openGymPort);
cmd.AddValue ("simSeed", "Seed for random generator. Default: 1", simSeed);
// optional parameters
cmd.AddValue ("simTime", "Simulation time in seconds. Default: 10s", simulationTime);
cmd.AddValue ("testArg", "Extra simulation argument. Default: 0", testArg);
cmd.AddValue ("enableFading", "If fading should be enabled. Default: false", enableFading);
cmd.Parse (argc, argv);
NS_LOG_UNCOND("Ns3Env parameters:");
NS_LOG_UNCOND("--simulationTime: " << simulationTime);
NS_LOG_UNCOND("--openGymPort: " << openGymPort);
NS_LOG_UNCOND("--envStepTime: " << envStepTime);
NS_LOG_UNCOND("--seed: " << simSeed);
NS_LOG_UNCOND("--testArg: " << testArg);
RngSeedManager::SetSeed (1);
RngSeedManager::SetRun (simSeed);
// OpenGym Env
Ptr<OpenGymInterface> openGymInterface = CreateObject<OpenGymInterface> (openGymPort);
Ptr<MyGymEnv> myGymEnv = CreateObject<MyGymEnv> (channNum);
myGymEnv->SetOpenGymInterface(openGymInterface);
NodeContainer nodes;
nodes.Create (nodeNum);
// Channel
Ptr<MultiModelSpectrumChannel> spectrumChannel = CreateObject<MultiModelSpectrumChannel> ();
Ptr<FriisPropagationLossModel> lossModel = CreateObject<FriisPropagationLossModel> ();
Ptr<NakagamiPropagationLossModel> fadingModel = CreateObject<NakagamiPropagationLossModel> ();
if (enableFading) {
lossModel->SetNext (fadingModel);
}
spectrumChannel->AddPropagationLossModel (lossModel);
Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel> ();
spectrumChannel->SetPropagationDelayModel (delayModel);
// Mobility model
MobilityHelper mobility;
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", DoubleValue (0.0),
"MinY", DoubleValue (0.0),
"DeltaX", DoubleValue (distance),
"DeltaY", DoubleValue (distance),
"GridWidth", UintegerValue (nodeNum), // will create linear topology
"LayoutType", StringValue ("RowFirst"));
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (nodes);
// Define channel models
std::map<uint32_t, Ptr<SpectrumModel> > spectrumModels;
for (uint32_t chanId=0; chanId<channNum; chanId++) {
double fc = 5200e6 + 20e6 * chanId;
BandInfo bandInfo;
bandInfo.fc = fc;
bandInfo.fl = fc - 10e6;
bandInfo.fh = fc + 10e6;
Bands bands;
bands.push_back (bandInfo);
Ptr<SpectrumModel> sm = Create<SpectrumModel> (bands);
spectrumModels.insert(std::pair<uint32_t, Ptr<SpectrumModel>>(chanId, sm));
}
// Spectrum Analyzer --- Channel Sensing
Ptr<Node> sensingNode = nodes.Get(0);
SpectrumAnalyzerHelper spectrumAnalyzerHelper;
spectrumAnalyzerHelper.SetChannel (spectrumChannel);
spectrumAnalyzerHelper.SetPhyAttribute ("Resolution", TimeValue (MilliSeconds (100)));
spectrumAnalyzerHelper.SetPhyAttribute ("NoisePowerSpectralDensity", DoubleValue (1e-15)); // -120 dBm/Hz
NetDeviceContainer spectrumAnalyzers;
for (uint32_t chanId=0; chanId<channNum; chanId++) {
spectrumAnalyzerHelper.SetRxSpectrumModel (spectrumModels.at(chanId));
spectrumAnalyzers.Add(spectrumAnalyzerHelper.Install (sensingNode));
}
for (uint32_t i=0; i< spectrumAnalyzers.GetN(); i++)
{
Ptr<NetDevice> netDev = spectrumAnalyzers.Get(i);
Ptr<NonCommunicatingNetDevice> nonCommNetDev = DynamicCast<NonCommunicatingNetDevice>(netDev);
Ptr<Object> spectrumPhy = nonCommNetDev->GetPhy();
Ptr<SpectrumAnalyzer> spectrumAnalyzer = DynamicCast<SpectrumAnalyzer>(spectrumPhy);
spectrumAnalyzer->Start ();
std::ostringstream oss;
oss.str ("");
uint32_t devId = netDev->GetIfIndex();
oss << "/NodeList/" << sensingNode->GetId () << "/DeviceList/" << devId << "/$ns3::NonCommunicatingNetDevice/Phy/AveragePowerSpectralDensityReport";
uint32_t channelId = i;
Config::ConnectWithoutContext (oss.str (),MakeBoundCallback (&MyGymEnv::PerformCca, myGymEnv, channelId));
}
// Signal Generator --- Generate interference pattern
Ptr<Node> interferingNode = nodes.Get(1);
WaveformGeneratorHelper waveformGeneratorHelper;
waveformGeneratorHelper.SetChannel (spectrumChannel);
waveformGeneratorHelper.SetPhyAttribute ("Period", TimeValue (Seconds (interferenceSlotTime)));
waveformGeneratorHelper.SetPhyAttribute ("DutyCycle", DoubleValue (1.0));
NetDeviceContainer waveformGeneratorDevices;
for (uint32_t chanId=0; chanId<channNum; chanId++) {
Ptr<SpectrumValue> wgPsd = Create<SpectrumValue> (spectrumModels.at(chanId));
*wgPsd = interfererPower / (20e6);
NS_LOG_DEBUG ("wgPsd : " << *wgPsd << " integrated power: " << Integral (*(GetPointer (wgPsd))));
waveformGeneratorHelper.SetTxPowerSpectralDensity (wgPsd);
waveformGeneratorDevices.Add(waveformGeneratorHelper.Install (interferingNode));
}
// Schedule interference pattern
double scheduledTime = 0;
while (scheduledTime < simulationTime)
{
std::map<uint32_t, std::vector<uint32_t> >::iterator it;
for (it=interferencePattern.begin(); it!=interferencePattern.end(); it++) {
std::vector<uint32_t> channels = (*it).second;
for (uint32_t chanId = 0; chanId < channels.size(); chanId++){
uint32_t occupied = channels.at(chanId);
NS_LOG_DEBUG("scheduledTime: " << scheduledTime << " ChanId " << chanId << " Occupied: " << occupied);
if (occupied == 1) {
Simulator::Schedule (Seconds (scheduledTime), &WaveformGenerator::Start,
waveformGeneratorDevices.Get (chanId)->GetObject<NonCommunicatingNetDevice> ()->GetPhy ()->GetObject<WaveformGenerator> ());
} else {
Simulator::Schedule (Seconds (scheduledTime), &WaveformGenerator::Stop,
waveformGeneratorDevices.Get (chanId)->GetObject<NonCommunicatingNetDevice> ()->GetPhy ()->GetObject<WaveformGenerator> ());
}
}
scheduledTime += interferenceSlotTime;
}
}
NS_LOG_UNCOND ("Simulation start");
Simulator::Stop (Seconds (simulationTime));
Simulator::Run ();
NS_LOG_UNCOND ("Simulation stop");
myGymEnv->NotifySimulationEnd();
Simulator::Destroy ();
NS_LOG_UNCOND ("Simulation exit");
}