19 #include "ns3/command-line.h"
20 #include "ns3/double.h"
21 #include "ns3/gnuplot.h"
22 #include "ns3/integer.h"
24 #include "ns3/random-variable-stream.h"
25 #include "ns3/string.h"
53 dround(
double number,
double precision)
58 number = std::floor(number + 0.5);
62 number = std::ceil(number - 0.5);
81 const std::string& title,
82 bool impulses =
false)
84 typedef std::map<double, unsigned int> histogram_maptype;
85 histogram_maptype histogram;
87 for (
unsigned int i = 0; i < probes; ++i)
102 for (
auto hi = histogram.begin(); hi != histogram.end(); ++hi)
104 data.Add(hi->first, (
double)hi->second / (
double)probes / precision);
113 main(
int argc,
char* argv[])
116 cmd.Parse(argc, argv);
118 unsigned int probes = 1000000;
119 double precision = 0.01;
121 const std::string plotFile{
"main-random-variables"};
123 gnuplots.SetTerminal(
"pdf enhanced");
126 std::cout <<
"UniformRandomVariable......." << std::flush;
128 plot.
SetTitle(
"UniformRandomVariable");
131 auto x = CreateObject<UniformRandomVariable>();
138 gnuplots.AddPlot(plot);
139 std::cout <<
"done" << std::endl;
143 std::cout <<
"ExponentialRandomVariable..." << std::flush;
145 plot.
SetTitle(
"ExponentialRandomVariable");
147 plot.
AppendExtra(
"ExpDist(x,l) = 1/l * exp(-1/l * x)");
149 auto x1 = CreateObject<ExponentialRandomVariable>();
156 auto x2 = CreateObject<ExponentialRandomVariable>();
163 auto x3 = CreateObject<ExponentialRandomVariable>();
170 gnuplots.AddPlot(plot);
171 std::cout <<
"done" << std::endl;
175 std::cout <<
"ParetoRandomVariable........" << std::flush;
177 plot.
SetTitle(
"ParetoRandomVariable");
180 auto x1 = CreateObject<ParetoRandomVariable>();
185 Histogram(x1, probes, precision,
"ParetoRandomVariable scale=1.0 shape=1.5"));
187 auto x2 = CreateObject<ParetoRandomVariable>();
192 Histogram(x2, probes, precision,
"ParetoRandomVariable scale=1.0 shape=2.0"));
194 auto x3 = CreateObject<ParetoRandomVariable>();
199 Histogram(x3, probes, precision,
"ParetoRandomVariable scale=1.0 shape=2.5"));
201 gnuplots.AddPlot(plot);
202 std::cout <<
"done" << std::endl;
206 std::cout <<
"WeibullRandomVariable......." << std::flush;
208 plot.
SetTitle(
"WeibullRandomVariable");
211 auto x1 = CreateObject<WeibullRandomVariable>();
216 Histogram(x1, probes, precision,
"WeibullRandomVariable scale=1.0 shape=1.0"));
218 auto x2 = CreateObject<WeibullRandomVariable>();
223 Histogram(x2, probes, precision,
"WeibullRandomVariable scale=1.0 shape=2.0"));
225 auto x3 = CreateObject<WeibullRandomVariable>();
230 Histogram(x3, probes, precision,
"WeibullRandomVariable scale=1.0 shape=3.0"));
232 gnuplots.AddPlot(plot);
233 std::cout <<
"done" << std::endl;
237 std::cout <<
"NormalRandomVariable........" << std::flush;
239 plot.
SetTitle(
"NormalRandomVariable");
242 "NormalDist(x,m,s) = 1 / (s * sqrt(2*pi)) * exp(-1.0 / 2.0 * ((x-m) / s)**2)");
244 auto x1 = CreateObject<NormalRandomVariable>();
251 "NormalDist(x,0.0,1.0)"));
253 auto x2 = CreateObject<NormalRandomVariable>();
260 "NormalDist(x,0.0,sqrt(2.0))"));
262 auto x3 = CreateObject<NormalRandomVariable>();
269 "NormalDist(x,0.0,sqrt(3.0))"));
271 gnuplots.AddPlot(plot);
272 std::cout <<
"done" << std::endl;
276 std::cout <<
"EmpiricalVariable..........." << std::flush;
278 plot.
SetTitle(
"EmpiricalRandomVariable");
281 auto x = CreateObject<EmpiricalRandomVariable>();
282 x->CDF(0.0, 0.0 / 15.0);
283 x->CDF(0.2, 1.0 / 15.0);
284 x->CDF(0.4, 3.0 / 15.0);
285 x->CDF(0.6, 6.0 / 15.0);
286 x->CDF(0.8, 10.0 / 15.0);
287 x->CDF(1.0, 15.0 / 15.0);
290 Histogram(
x, probes, precision,
"EmpiricalRandomVariable (Sampling)",
true));
292 x->SetInterpolate(
true);
294 auto d2 =
Histogram(
x, probes, precision,
"EmpiricalRandomVariable (Interpolate)");
295 d2.SetExtra(
" axis x1y2");
298 gnuplots.AddPlot(plot);
299 std::cout <<
"done" << std::endl;
303 std::cout <<
"DeterministicVariable......." << std::flush;
305 plot.
SetTitle(
"DeterministicRandomVariable");
308 auto x1 = CreateObject<DeterministicRandomVariable>();
309 double values[] = {0.0, 0.2, 0.2, 0.4, 0.2, 0.6, 0.8, 0.8, 1.0};
310 x1->SetValueArray(values,
sizeof(values) /
sizeof(values[0]));
314 gnuplots.AddPlot(plot);
315 std::cout <<
"done" << std::endl;
319 std::cout <<
"LogNormalRandomVariable....." << std::flush;
321 plot.
SetTitle(
"LogNormalRandomVariable");
324 plot.
AppendExtra(
"LogNormalDist(x,m,s) = 1.0/x * NormalDist(log(x), m, s)");
326 auto x1 = CreateObject<LogNormalRandomVariable>();
335 auto x2 = CreateObject<LogNormalRandomVariable>();
341 auto x3 = CreateObject<LogNormalRandomVariable>();
348 Gnuplot2dFunction(
"LogNormalDist(x, 0.0, 0.25)",
"LogNormalDist(x, 0.0, 0.25)"));
350 auto x4 = CreateObject<LogNormalRandomVariable>();
354 plot.
AddDataset(
Histogram(x4, probes, precision,
"LogNormalRandomVariable m=0.0 s=0.125"));
356 auto x5 = CreateObject<LogNormalRandomVariable>();
365 auto x6 = CreateObject<LogNormalRandomVariable>();
371 gnuplots.AddPlot(plot);
372 std::cout <<
"done" << std::endl;
376 std::cout <<
"TriangularRandomVariable...." << std::flush;
378 plot.
SetTitle(
"TriangularRandomVariable");
381 auto x1 = CreateObject<TriangularRandomVariable>();
387 Histogram(x1, probes, precision,
"TriangularRandomVariable [0.0 .. 1.0) m=0.5"));
389 auto x2 = CreateObject<TriangularRandomVariable>();
395 Histogram(x2, probes, precision,
"TriangularRandomVariable [0.0 .. 1.0) m=0.4"));
397 auto x3 = CreateObject<TriangularRandomVariable>();
403 Histogram(x3, probes, precision,
"TriangularRandomVariable [0.0 .. 1.0) m=0.65"));
405 gnuplots.AddPlot(plot);
406 std::cout <<
"done" << std::endl;
410 std::cout <<
"GammaRandomVariable........." << std::flush;
412 plot.
SetTitle(
"GammaRandomVariable");
415 plot.
AppendExtra(
"GammaDist(x,a,b) = x**(a-1) * 1/b**a * exp(-x/b) / gamma(a)");
418 "set label 1 '{/Symbol g}(x,{/Symbol a},{/Symbol b}) = x^{/Symbol a-1} e^{-x {/Symbol "
419 "b}^{-1}} ( {/Symbol b}^{/Symbol a} {/Symbol G}({/Symbol a}) )^{-1}' at 0.7, 0.9");
421 auto x1 = CreateObject<GammaRandomVariable>();
429 auto x2 = CreateObject<GammaRandomVariable>();
437 auto x3 = CreateObject<GammaRandomVariable>();
445 auto x4 = CreateObject<GammaRandomVariable>();
453 auto x5 = CreateObject<GammaRandomVariable>();
461 auto x6 = CreateObject<GammaRandomVariable>();
469 auto x7 = CreateObject<GammaRandomVariable>();
477 gnuplots.AddPlot(plot);
478 std::cout <<
"done" << std::endl;
482 std::cout <<
"ErlangRandomVariable........" << std::flush;
484 plot.
SetTitle(
"ErlangRandomVariable");
486 plot.
AppendExtra(
"ErlangDist(x,k,l) = x**(k-1) * 1/l**k * exp(-x/l) / (k-1)!");
488 plot.
AppendExtra(
"set label 1 'Erlang(x,k,{/Symbol l}) = x^{k-1} e^{-x {/Symbol l}^{-1}} ( "
489 "{/Symbol l}^k (k-1)! )^{-1}' at 0.7, 0.9");
491 auto x1 = CreateObject<ErlangRandomVariable>();
496 Histogram(x1, probes, precision,
"ErlangRandomVariable k=1 {/Symbol l}=1.0"));
500 auto x2 = CreateObject<ErlangRandomVariable>();
505 Histogram(x2, probes, precision,
"ErlangRandomVariable k=2 {/Symbol l}=1.0"));
509 auto x3 = CreateObject<ErlangRandomVariable>();
514 Histogram(x3, probes, precision,
"ErlangRandomVariable k=3 {/Symbol l}=1.0"));
518 auto x4 = CreateObject<ErlangRandomVariable>();
523 Histogram(x4, probes, precision,
"ErlangRandomVariable k=5 {/Symbol l}=1.0"));
527 auto x5 = CreateObject<ErlangRandomVariable>();
532 Histogram(x5, probes, precision,
"ErlangRandomVariable k=2 {/Symbol l}=2.0"));
536 auto x6 = CreateObject<ErlangRandomVariable>();
541 Histogram(x6, probes, precision,
"ErlangRandomVariable k=2 {/Symbol l}=3.0"));
545 auto x7 = CreateObject<ErlangRandomVariable>();
550 Histogram(x7, probes, precision,
"ErlangRandomVariable k=2 {/Symbol l}=5.0"));
554 gnuplots.AddPlot(plot);
555 std::cout <<
"done" << std::endl;
559 std::cout <<
"BinomialRandomVariable......." << std::flush;
561 plot.
SetTitle(
"BinomialRandomVariable");
564 auto x = CreateObject<BinomialRandomVariable>();
570 gnuplots.AddPlot(plot);
571 std::cout <<
"done" << std::endl;
575 std::cout <<
"BernoulliRandomVariable......." << std::flush;
577 plot.
SetTitle(
"BernoulliRandomVariable");
580 auto x = CreateObject<BernoulliRandomVariable>();
585 gnuplots.AddPlot(plot);
586 std::cout <<
"done" << std::endl;
590 std::string gnuFile = plotFile +
".plt";
591 std::cout <<
"Writing Gnuplot file: " << gnuFile <<
"..." << std::flush;
592 std::ofstream gnuStream(gnuFile);
593 gnuplots.GenerateOutput(gnuStream);
596 std::cout <<
"done\nGenerating " << plotFile <<
".pdf..." << std::flush;
597 std::string shellcmd =
"gnuplot " + gnuFile;
598 int returnValue = std::system(shellcmd.c_str());
601 std::cout << std::endl
602 <<
"Error: shell command failed with " << returnValue <<
"; no pdf generated."
607 std::cout <<
"done" << std::endl;
Parse command-line arguments.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Class to represent a 2D points plot.
Class to represent a 2D function expression plot.
a simple class to group together multiple gnuplots into one file, e.g.
Abstract class to store a plot line to be used by ns3::Gnuplot.
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
void AddDataset(const GnuplotDataset &dataset)
void AppendExtra(const std::string &extra)
void SetTitle(const std::string &title)
Class used to store data and make an histogram of the data frequency.
Hold a signed integer type.
virtual double GetValue()=0
Get the next random value drawn from the distribution.
static double dround(double number, double precision)
Round a double number to the given precision.
Every class exported by the ns3 library is enclosed in the ns3 namespace.