A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
main-random-variable-stream.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 Timo Bingmann
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Timo Bingmann <timo.bingmann@student.kit.edu>
18 */
19#include "ns3/command-line.h"
20#include "ns3/double.h"
21#include "ns3/gnuplot.h"
22#include "ns3/integer.h"
23#include "ns3/ptr.h"
24#include "ns3/random-variable-stream.h"
25#include "ns3/string.h"
26
27#include <cmath>
28#include <fstream>
29#include <iostream>
30#include <map>
31
32/**
33 * \file
34 * \ingroup core-examples
35 * \ingroup randomvariable
36 * Example program illustrating use of RandomVariableStream
37 */
38
39using namespace ns3;
40
41namespace
42{
43
44/**
45 * Round a double number to the given precision.
46 * For example, `dround(0.234, 0.1) = 0.2`
47 * and `dround(0.257, 0.1) = 0.3`
48 * \param [in] number The number to round.
49 * \param [in] precision The least significant digit to keep in the rounding.
50 * \returns \pname{number} rounded to \pname{precision}.
51 */
52double
53dround(double number, double precision)
54{
55 number /= precision;
56 if (number >= 0)
57 {
58 number = std::floor(number + 0.5);
59 }
60 else
61 {
62 number = std::ceil(number - 0.5);
63 }
64 number *= precision;
65 return number;
66}
67
68/**
69 * Generate a histogram from a RandomVariableStream.
70 * \param [in] rndvar The RandomVariableStream to sample.
71 * \param [in] probes The number of samples.
72 * \param [in] precision The precision to round samples to.
73 * \param [in] title The title for the histogram.
74 * \param [in] impulses Set the plot style to IMPULSES.
75 * \return The histogram as a GnuPlot data set.
76 */
79 unsigned int probes,
80 double precision,
81 const std::string& title,
82 bool impulses = false)
83{
84 typedef std::map<double, unsigned int> histogram_maptype;
85 histogram_maptype histogram;
86
87 for (unsigned int i = 0; i < probes; ++i)
88 {
89 double val = dround(rndvar->GetValue(), precision);
90
91 ++histogram[val];
92 }
93
95 data.SetTitle(title);
96
97 if (impulses)
98 {
100 }
101
102 for (auto hi = histogram.begin(); hi != histogram.end(); ++hi)
103 {
104 data.Add(hi->first, (double)hi->second / (double)probes / precision);
105 }
106
107 return data;
108}
109
110} // unnamed namespace
111
112int
113main(int argc, char* argv[])
114{
115 CommandLine cmd(__FILE__);
116 cmd.Parse(argc, argv);
117
118 unsigned int probes = 1000000;
119 double precision = 0.01;
120
121 const std::string plotFile{"main-random-variables"};
122 GnuplotCollection gnuplots(plotFile + ".pdf");
123 gnuplots.SetTerminal("pdf enhanced");
124
125 {
126 std::cout << "UniformRandomVariable......." << std::flush;
127 Gnuplot plot;
128 plot.SetTitle("UniformRandomVariable");
129 plot.AppendExtra("set yrange [0:]");
130
131 auto x = CreateObject<UniformRandomVariable>();
132 x->SetAttribute("Min", DoubleValue(0.0));
133 x->SetAttribute("Max", DoubleValue(1.0));
134
135 plot.AddDataset(Histogram(x, probes, precision, "UniformRandomVariable [0.0 .. 1.0)"));
136 plot.AddDataset(Gnuplot2dFunction("1.0", "0 <= x && x <= 1 ? 1.0 : 0"));
137
138 gnuplots.AddPlot(plot);
139 std::cout << "done" << std::endl;
140 }
141
142 {
143 std::cout << "ExponentialRandomVariable..." << std::flush;
144 Gnuplot plot;
145 plot.SetTitle("ExponentialRandomVariable");
146 plot.AppendExtra("set xrange [0:4]");
147 plot.AppendExtra("ExpDist(x,l) = 1/l * exp(-1/l * x)");
148
149 auto x1 = CreateObject<ExponentialRandomVariable>();
150 x1->SetAttribute("Mean", DoubleValue(0.5));
151
152 plot.AddDataset(Histogram(x1, probes, precision, "ExponentialRandomVariable m=0.5"));
153
154 plot.AddDataset(Gnuplot2dFunction("ExponentialDistribution mean 0.5", "ExpDist(x, 0.5)"));
155
156 auto x2 = CreateObject<ExponentialRandomVariable>();
157 x2->SetAttribute("Mean", DoubleValue(1.0));
158
159 plot.AddDataset(Histogram(x2, probes, precision, "ExponentialRandomVariable m=1"));
160
161 plot.AddDataset(Gnuplot2dFunction("ExponentialDistribution mean 1.0", "ExpDist(x, 1.0)"));
162
163 auto x3 = CreateObject<ExponentialRandomVariable>();
164 x3->SetAttribute("Mean", DoubleValue(1.5));
165
166 plot.AddDataset(Histogram(x3, probes, precision, "ExponentialRandomVariable m=1.5"));
167
168 plot.AddDataset(Gnuplot2dFunction("ExponentialDistribution mean 1.5", "ExpDist(x, 1.5)"));
169
170 gnuplots.AddPlot(plot);
171 std::cout << "done" << std::endl;
172 }
173
174 {
175 std::cout << "ParetoRandomVariable........" << std::flush;
176 Gnuplot plot;
177 plot.SetTitle("ParetoRandomVariable");
178 plot.AppendExtra("set xrange [0:4]");
179
180 auto x1 = CreateObject<ParetoRandomVariable>();
181 x1->SetAttribute("Scale", DoubleValue(1.0));
182 x1->SetAttribute("Shape", DoubleValue(1.5));
183
184 plot.AddDataset(
185 Histogram(x1, probes, precision, "ParetoRandomVariable scale=1.0 shape=1.5"));
186
187 auto x2 = CreateObject<ParetoRandomVariable>();
188 x2->SetAttribute("Scale", DoubleValue(1.0));
189 x2->SetAttribute("Shape", DoubleValue(2.0));
190
191 plot.AddDataset(
192 Histogram(x2, probes, precision, "ParetoRandomVariable scale=1.0 shape=2.0"));
193
194 auto x3 = CreateObject<ParetoRandomVariable>();
195 x3->SetAttribute("Scale", DoubleValue(1.0));
196 x3->SetAttribute("Shape", DoubleValue(2.5));
197
198 plot.AddDataset(
199 Histogram(x3, probes, precision, "ParetoRandomVariable scale=1.0 shape=2.5"));
200
201 gnuplots.AddPlot(plot);
202 std::cout << "done" << std::endl;
203 }
204
205 {
206 std::cout << "WeibullRandomVariable......." << std::flush;
207 Gnuplot plot;
208 plot.SetTitle("WeibullRandomVariable");
209 plot.AppendExtra("set xrange [0:4]");
210
211 auto x1 = CreateObject<WeibullRandomVariable>();
212 x1->SetAttribute("Scale", DoubleValue(1.0));
213 x1->SetAttribute("Shape", DoubleValue(1.0));
214
215 plot.AddDataset(
216 Histogram(x1, probes, precision, "WeibullRandomVariable scale=1.0 shape=1.0"));
217
218 auto x2 = CreateObject<WeibullRandomVariable>();
219 x2->SetAttribute("Scale", DoubleValue(1.0));
220 x2->SetAttribute("Shape", DoubleValue(2.0));
221
222 plot.AddDataset(
223 Histogram(x2, probes, precision, "WeibullRandomVariable scale=1.0 shape=2.0"));
224
225 auto x3 = CreateObject<WeibullRandomVariable>();
226 x3->SetAttribute("Scale", DoubleValue(1.0));
227 x3->SetAttribute("Shape", DoubleValue(3.0));
228
229 plot.AddDataset(
230 Histogram(x3, probes, precision, "WeibullRandomVariable scale=1.0 shape=3.0"));
231
232 gnuplots.AddPlot(plot);
233 std::cout << "done" << std::endl;
234 }
235
236 {
237 std::cout << "NormalRandomVariable........" << std::flush;
238 Gnuplot plot;
239 plot.SetTitle("NormalRandomVariable");
240 plot.AppendExtra("set xrange [-4:4]");
241 plot.AppendExtra(
242 "NormalDist(x,m,s) = 1 / (s * sqrt(2*pi)) * exp(-1.0 / 2.0 * ((x-m) / s)**2)");
243
244 auto x1 = CreateObject<NormalRandomVariable>();
245 x1->SetAttribute("Mean", DoubleValue(0.0));
246 x1->SetAttribute("Variance", DoubleValue(1.0));
247
248 plot.AddDataset(Histogram(x1, probes, precision, "NormalRandomVariable m=0.0 v=1.0"));
249
250 plot.AddDataset(Gnuplot2dFunction("NormalDist {/Symbol m}=0.0 {/Symbol s}=1.0",
251 "NormalDist(x,0.0,1.0)"));
252
253 auto x2 = CreateObject<NormalRandomVariable>();
254 x2->SetAttribute("Mean", DoubleValue(0.0));
255 x2->SetAttribute("Variance", DoubleValue(2.0));
256
257 plot.AddDataset(Histogram(x2, probes, precision, "NormalRandomVariable m=0.0 v=2.0"));
258
259 plot.AddDataset(Gnuplot2dFunction("NormalDist {/Symbol m}=0.0 {/Symbol s}=sqrt(2.0)",
260 "NormalDist(x,0.0,sqrt(2.0))"));
261
262 auto x3 = CreateObject<NormalRandomVariable>();
263 x3->SetAttribute("Mean", DoubleValue(0.0));
264 x3->SetAttribute("Variance", DoubleValue(3.0));
265
266 plot.AddDataset(Histogram(x3, probes, precision, "NormalRandomVariable m=0.0 v=3.0"));
267
268 plot.AddDataset(Gnuplot2dFunction("NormalDist {/Symbol m}=0.0 {/Symbol s}=sqrt(3.0)",
269 "NormalDist(x,0.0,sqrt(3.0))"));
270
271 gnuplots.AddPlot(plot);
272 std::cout << "done" << std::endl;
273 }
274
275 {
276 std::cout << "EmpiricalVariable..........." << std::flush;
277 Gnuplot plot;
278 plot.SetTitle("EmpiricalRandomVariable");
279 plot.AppendExtra("set xrange [*:*]");
280
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);
288
289 plot.AddDataset(
290 Histogram(x, probes, precision, "EmpiricalRandomVariable (Sampling)", true));
291
292 x->SetInterpolate(true);
293 plot.AppendExtra("set y2range [0:*]");
294 auto d2 = Histogram(x, probes, precision, "EmpiricalRandomVariable (Interpolate)");
295 d2.SetExtra(" axis x1y2");
296 plot.AddDataset(d2);
297
298 gnuplots.AddPlot(plot);
299 std::cout << "done" << std::endl;
300 }
301
302 {
303 std::cout << "DeterministicVariable......." << std::flush;
304 Gnuplot plot;
305 plot.SetTitle("DeterministicRandomVariable");
306 plot.AppendExtra("set xrange [*:*]");
307
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]));
311
312 plot.AddDataset(Histogram(x1, probes, precision, "DeterministicRandomVariable", true));
313
314 gnuplots.AddPlot(plot);
315 std::cout << "done" << std::endl;
316 }
317
318 {
319 std::cout << "LogNormalRandomVariable....." << std::flush;
320 Gnuplot plot;
321 plot.SetTitle("LogNormalRandomVariable");
322 plot.AppendExtra("set xrange [0:4]");
323
324 plot.AppendExtra("LogNormalDist(x,m,s) = 1.0/x * NormalDist(log(x), m, s)");
325
326 auto x1 = CreateObject<LogNormalRandomVariable>();
327 x1->SetAttribute("Mu", DoubleValue(0.0));
328 x1->SetAttribute("Sigma", DoubleValue(1.0));
329
330 plot.AddDataset(Histogram(x1, probes, precision, "LogNormalRandomVariable m=0.0 s=1.0"));
331
332 plot.AddDataset(
333 Gnuplot2dFunction("LogNormalDist(x, 0.0, 1.0)", "LogNormalDist(x, 0.0, 1.0)"));
334
335 auto x2 = CreateObject<LogNormalRandomVariable>();
336 x2->SetAttribute("Mu", DoubleValue(0.0));
337 x2->SetAttribute("Sigma", DoubleValue(0.5));
338
339 plot.AddDataset(Histogram(x2, probes, precision, "LogNormalRandomVariable m=0.0 s=0.5"));
340
341 auto x3 = CreateObject<LogNormalRandomVariable>();
342 x3->SetAttribute("Mu", DoubleValue(0.0));
343 x3->SetAttribute("Sigma", DoubleValue(0.25));
344
345 plot.AddDataset(Histogram(x3, probes, precision, "LogNormalRandomVariable m=0.0 s=0.25"));
346
347 plot.AddDataset(
348 Gnuplot2dFunction("LogNormalDist(x, 0.0, 0.25)", "LogNormalDist(x, 0.0, 0.25)"));
349
350 auto x4 = CreateObject<LogNormalRandomVariable>();
351 x4->SetAttribute("Mu", DoubleValue(0.0));
352 x4->SetAttribute("Sigma", DoubleValue(0.125));
353
354 plot.AddDataset(Histogram(x4, probes, precision, "LogNormalRandomVariable m=0.0 s=0.125"));
355
356 auto x5 = CreateObject<LogNormalRandomVariable>();
357 x5->SetAttribute("Mu", DoubleValue(0.0));
358 x5->SetAttribute("Sigma", DoubleValue(2.0));
359
360 plot.AddDataset(Histogram(x5, probes, precision, "LogNormalRandomVariable m=0.0 s=2.0"));
361
362 plot.AddDataset(
363 Gnuplot2dFunction("LogNormalDist(x, 0.0, 2.0)", "LogNormalDist(x, 0.0, 2.0)"));
364
365 auto x6 = CreateObject<LogNormalRandomVariable>();
366 x6->SetAttribute("Mu", DoubleValue(0.0));
367 x6->SetAttribute("Sigma", DoubleValue(2.5));
368
369 plot.AddDataset(Histogram(x6, probes, precision, "LogNormalRandomVariable m=0.0 s=2.5"));
370
371 gnuplots.AddPlot(plot);
372 std::cout << "done" << std::endl;
373 }
374
375 {
376 std::cout << "TriangularRandomVariable...." << std::flush;
377 Gnuplot plot;
378 plot.SetTitle("TriangularRandomVariable");
379 plot.AppendExtra("set xrange [*:*]");
380
381 auto x1 = CreateObject<TriangularRandomVariable>();
382 x1->SetAttribute("Min", DoubleValue(0.0));
383 x1->SetAttribute("Max", DoubleValue(1.0));
384 x1->SetAttribute("Mean", DoubleValue(0.5));
385
386 plot.AddDataset(
387 Histogram(x1, probes, precision, "TriangularRandomVariable [0.0 .. 1.0) m=0.5"));
388
389 auto x2 = CreateObject<TriangularRandomVariable>();
390 x2->SetAttribute("Min", DoubleValue(0.0));
391 x2->SetAttribute("Max", DoubleValue(1.0));
392 x2->SetAttribute("Mean", DoubleValue(0.4));
393
394 plot.AddDataset(
395 Histogram(x2, probes, precision, "TriangularRandomVariable [0.0 .. 1.0) m=0.4"));
396
397 auto x3 = CreateObject<TriangularRandomVariable>();
398 x3->SetAttribute("Min", DoubleValue(0.0));
399 x3->SetAttribute("Max", DoubleValue(1.0));
400 x3->SetAttribute("Mean", DoubleValue(0.65));
401
402 plot.AddDataset(
403 Histogram(x3, probes, precision, "TriangularRandomVariable [0.0 .. 1.0) m=0.65"));
404
405 gnuplots.AddPlot(plot);
406 std::cout << "done" << std::endl;
407 }
408
409 {
410 std::cout << "GammaRandomVariable........." << std::flush;
411 Gnuplot plot;
412 plot.SetTitle("GammaRandomVariable");
413 plot.AppendExtra("set xrange [0:10]");
414 plot.AppendExtra("set yrange [0:1]");
415 plot.AppendExtra("GammaDist(x,a,b) = x**(a-1) * 1/b**a * exp(-x/b) / gamma(a)");
416
417 plot.AppendExtra(
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");
420
421 auto x1 = CreateObject<GammaRandomVariable>();
422 x1->SetAttribute("Alpha", DoubleValue(1.0));
423 x1->SetAttribute("Beta", DoubleValue(1.0));
424
425 plot.AddDataset(Histogram(x1, probes, precision, "GammaRandomVariable a=1.0 b=1.0"));
426
427 plot.AddDataset(Gnuplot2dFunction("{/Symbol g}(x, 1.0, 1.0)", "GammaDist(x, 1.0, 1.0)"));
428
429 auto x2 = CreateObject<GammaRandomVariable>();
430 x2->SetAttribute("Alpha", DoubleValue(1.5));
431 x2->SetAttribute("Beta", DoubleValue(1.0));
432
433 plot.AddDataset(Histogram(x2, probes, precision, "GammaRandomVariable a=1.5 b=1.0"));
434
435 plot.AddDataset(Gnuplot2dFunction("{/Symbol g}(x, 1.5, 1.0)", "GammaDist(x, 1.5, 1.0)"));
436
437 auto x3 = CreateObject<GammaRandomVariable>();
438 x3->SetAttribute("Alpha", DoubleValue(2.0));
439 x3->SetAttribute("Beta", DoubleValue(1.0));
440
441 plot.AddDataset(Histogram(x3, probes, precision, "GammaRandomVariable a=2.0 b=1.0"));
442
443 plot.AddDataset(Gnuplot2dFunction("{/Symbol g}(x, 2.0, 1.0)", "GammaDist(x, 2.0, 1.0)"));
444
445 auto x4 = CreateObject<GammaRandomVariable>();
446 x4->SetAttribute("Alpha", DoubleValue(4.0));
447 x4->SetAttribute("Beta", DoubleValue(1.0));
448
449 plot.AddDataset(Histogram(x4, probes, precision, "GammaRandomVariable a=4.0 b=1.0"));
450
451 plot.AddDataset(Gnuplot2dFunction("{/Symbol g}(x, 4.0, 1.0)", "GammaDist(x, 4.0, 1.0)"));
452
453 auto x5 = CreateObject<GammaRandomVariable>();
454 x5->SetAttribute("Alpha", DoubleValue(2.0));
455 x5->SetAttribute("Beta", DoubleValue(2.0));
456
457 plot.AddDataset(Histogram(x5, probes, precision, "GammaRandomVariable a=2.0 b=2.0"));
458
459 plot.AddDataset(Gnuplot2dFunction("{/Symbol g}(x, 2.0, 2.0)", "GammaDist(x, 2.0, 2.0)"));
460
461 auto x6 = CreateObject<GammaRandomVariable>();
462 x6->SetAttribute("Alpha", DoubleValue(2.5));
463 x6->SetAttribute("Beta", DoubleValue(3.0));
464
465 plot.AddDataset(Histogram(x6, probes, precision, "GammaRandomVariable a=2.5 b=3.0"));
466
467 plot.AddDataset(Gnuplot2dFunction("{/Symbol g}(x, 2.5, 3.0)", "GammaDist(x, 2.5, 3.0)"));
468
469 auto x7 = CreateObject<GammaRandomVariable>();
470 x7->SetAttribute("Alpha", DoubleValue(2.5));
471 x7->SetAttribute("Beta", DoubleValue(4.5));
472
473 plot.AddDataset(Histogram(x7, probes, precision, "GammaRandomVariable a=2.5 b=4.5"));
474
475 plot.AddDataset(Gnuplot2dFunction("{/Symbol g}(x, 2.5, 4.5)", "GammaDist(x, 2.5, 4.5)"));
476
477 gnuplots.AddPlot(plot);
478 std::cout << "done" << std::endl;
479 }
480
481 {
482 std::cout << "ErlangRandomVariable........" << std::flush;
483 Gnuplot plot;
484 plot.SetTitle("ErlangRandomVariable");
485 plot.AppendExtra("set xrange [0:10]");
486 plot.AppendExtra("ErlangDist(x,k,l) = x**(k-1) * 1/l**k * exp(-x/l) / (k-1)!");
487
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");
490
491 auto x1 = CreateObject<ErlangRandomVariable>();
492 x1->SetAttribute("K", IntegerValue(1));
493 x1->SetAttribute("Lambda", DoubleValue(1.0));
494
495 plot.AddDataset(
496 Histogram(x1, probes, precision, "ErlangRandomVariable k=1 {/Symbol l}=1.0"));
497
498 plot.AddDataset(Gnuplot2dFunction("Erlang(x, 1, 1.0)", "ErlangDist(x, 1, 1.0)"));
499
500 auto x2 = CreateObject<ErlangRandomVariable>();
501 x2->SetAttribute("K", IntegerValue(2));
502 x2->SetAttribute("Lambda", DoubleValue(1.0));
503
504 plot.AddDataset(
505 Histogram(x2, probes, precision, "ErlangRandomVariable k=2 {/Symbol l}=1.0"));
506
507 plot.AddDataset(Gnuplot2dFunction("Erlang(x, 2, 1.0)", "ErlangDist(x, 2, 1.0)"));
508
509 auto x3 = CreateObject<ErlangRandomVariable>();
510 x3->SetAttribute("K", IntegerValue(3));
511 x3->SetAttribute("Lambda", DoubleValue(1.0));
512
513 plot.AddDataset(
514 Histogram(x3, probes, precision, "ErlangRandomVariable k=3 {/Symbol l}=1.0"));
515
516 plot.AddDataset(Gnuplot2dFunction("Erlang(x, 3, 1.0)", "ErlangDist(x, 3, 1.0)"));
517
518 auto x4 = CreateObject<ErlangRandomVariable>();
519 x4->SetAttribute("K", IntegerValue(5));
520 x4->SetAttribute("Lambda", DoubleValue(1.0));
521
522 plot.AddDataset(
523 Histogram(x4, probes, precision, "ErlangRandomVariable k=5 {/Symbol l}=1.0"));
524
525 plot.AddDataset(Gnuplot2dFunction("Erlang(x, 5, 1.0)", "ErlangDist(x, 5, 1.0)"));
526
527 auto x5 = CreateObject<ErlangRandomVariable>();
528 x5->SetAttribute("K", IntegerValue(2));
529 x5->SetAttribute("Lambda", DoubleValue(2.0));
530
531 plot.AddDataset(
532 Histogram(x5, probes, precision, "ErlangRandomVariable k=2 {/Symbol l}=2.0"));
533
534 plot.AddDataset(Gnuplot2dFunction("Erlang(x, 2, 2.0)", "ErlangDist(x, 2, 2.0)"));
535
536 auto x6 = CreateObject<ErlangRandomVariable>();
537 x6->SetAttribute("K", IntegerValue(2));
538 x6->SetAttribute("Lambda", DoubleValue(3.0));
539
540 plot.AddDataset(
541 Histogram(x6, probes, precision, "ErlangRandomVariable k=2 {/Symbol l}=3.0"));
542
543 plot.AddDataset(Gnuplot2dFunction("Erlang(x, 2, 3.0)", "ErlangDist(x, 2, 3.0)"));
544
545 auto x7 = CreateObject<ErlangRandomVariable>();
546 x7->SetAttribute("K", IntegerValue(2));
547 x7->SetAttribute("Lambda", DoubleValue(5.0));
548
549 plot.AddDataset(
550 Histogram(x7, probes, precision, "ErlangRandomVariable k=2 {/Symbol l}=5.0"));
551
552 plot.AddDataset(Gnuplot2dFunction("Erlang(x, 2, 5.0)", "ErlangDist(x, 2, 5.0)"));
553
554 gnuplots.AddPlot(plot);
555 std::cout << "done" << std::endl;
556 }
557
558 {
559 std::cout << "BinomialRandomVariable......." << std::flush;
560 Gnuplot plot;
561 plot.SetTitle("BinomialRandomVariable");
562 plot.AppendExtra("set yrange [0:10]");
563
564 auto x = CreateObject<BinomialRandomVariable>();
565 x->SetAttribute("Trials", IntegerValue(10));
566 x->SetAttribute("Probability", DoubleValue(0.5));
567
568 plot.AddDataset(Histogram(x, probes, precision, "BinomialRandomVariable n=10 p=0.5"));
569
570 gnuplots.AddPlot(plot);
571 std::cout << "done" << std::endl;
572 }
573
574 {
575 std::cout << "BernoulliRandomVariable......." << std::flush;
576 Gnuplot plot;
577 plot.SetTitle("BernoulliRandomVariable");
578 plot.AppendExtra("set yrange [0:1]");
579
580 auto x = CreateObject<BernoulliRandomVariable>();
581 x->SetAttribute("Probability", DoubleValue(0.5));
582
583 plot.AddDataset(Histogram(x, probes, precision, "BernoulliRandomVariable p=0.5"));
584
585 gnuplots.AddPlot(plot);
586 std::cout << "done" << std::endl;
587 }
588
589 {
590 std::string gnuFile = plotFile + ".plt";
591 std::cout << "Writing Gnuplot file: " << gnuFile << "..." << std::flush;
592 std::ofstream gnuStream(gnuFile);
593 gnuplots.GenerateOutput(gnuStream);
594 gnuStream.close();
595
596 std::cout << "done\nGenerating " << plotFile << ".pdf..." << std::flush;
597 std::string shellcmd = "gnuplot " + gnuFile;
598 int returnValue = std::system(shellcmd.c_str());
599 if (returnValue)
600 {
601 std::cout << std::endl
602 << "Error: shell command failed with " << returnValue << "; no pdf generated."
603 << std::endl;
604 }
605 else
606 {
607 std::cout << "done" << std::endl;
608 }
609 }
610
611 return 0;
612}
Parse command-line arguments.
Definition: command-line.h:232
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
Class to represent a 2D points plot.
Definition: gnuplot.h:116
Class to represent a 2D function expression plot.
Definition: gnuplot.h:244
a simple class to group together multiple gnuplots into one file, e.g.
Definition: gnuplot.h:484
Abstract class to store a plot line to be used by ns3::Gnuplot.
Definition: gnuplot.h:39
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:370
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:796
void AppendExtra(const std::string &extra)
Definition: gnuplot.cc:789
void SetTitle(const std::string &title)
Definition: gnuplot.cc:770
Class used to store data and make an histogram of the data frequency.
Definition: histogram.h:46
Hold a signed integer type.
Definition: integer.h:45
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t data[writeSize]