A Discrete-Event Network Simulator
API
empirical-random-variable-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Lawrence Livermore National Laboratory
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: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
18 */
19
20#include "ns3/command-line.h"
21#include "ns3/histogram.h"
22#include "ns3/nstime.h"
23#include "ns3/ptr.h"
24#include "ns3/random-variable-stream.h"
25#include "ns3/simulator.h"
26
27#include <iomanip>
28#include <iostream>
29#include <map>
30
50using namespace ns3;
51
59void
61{
62 std::cout << "------------------------------" << std::endl;
63 std::cout << "Sampling " << mode << std::endl;
64
65 std::cout << std::endl;
66 std::cout << "Binned sample" << std::endl;
67 double value = erv->GetValue();
68 std::cout << "Binned sample: " << value << std::endl;
69 std::cout << std::endl;
70
71 std::cout << "Interpolated sample" << std::endl;
72 erv->SetInterpolate(true);
73 value = erv->GetValue();
74 std::cout << "Interpolated sample:" << value << std::endl;
75 erv->SetInterpolate(false);
76}
77
86void
87PrintStatsLine(const double value, const long count, const long n)
88{
89 std::cout << std::fixed << std::setprecision(3) << std::setw(10) << std::right << value
90 << std::setw(10) << std::right << count << std::setw(10) << std::right
91 << count / static_cast<double>(n) * 100.0 << std::endl;
92}
93
103void
104PrintSummary(long sum, long n, double weighted, double expected)
105{
106 std::cout << std::endl;
107 std::cout << " --------" << std::endl;
108 std::cout << " Total " << std::setprecision(3) << std::fixed << std::setw(10)
109 << std::right << sum / static_cast<double>(n) * 100.0 << std::endl;
110 std::cout << " Average " << std::setprecision(3) << std::fixed << std::setw(6)
111 << std::right << weighted / n << std::endl;
112 std::cout << " Expected " << std::setprecision(3) << std::fixed << std::setw(6)
113 << std::right << expected << std::endl
114 << std::endl;
115}
116
125void
126RunBothModes(std::string mode, Ptr<EmpiricalRandomVariable> erv, long n)
127{
128 std::cout << std::endl;
129 std::cout << "Sampling " << mode << std::endl;
130 std::map<double, int> counts;
131 counts[0] = 0;
132 for (long i = 0; i < n; ++i)
133 {
134 ++counts[erv->GetValue()];
135 }
136 long sum = 0;
137 double weighted = 0;
138 std::cout << std::endl;
139 std::cout << " Value Counts %" << std::endl;
140 std::cout << "---------- -------- --------" << std::endl;
141 for (auto c : counts)
142 {
143 long count = c.second;
144 double value = c.first;
145 sum += count;
146 weighted += value * count;
147 PrintStatsLine(value, count, n);
148 }
149 PrintSummary(sum, n, weighted, 8.75);
150
151 std::cout << "Interpolating " << mode << std::endl;
152 erv->SetInterpolate(true);
153 Histogram h(0.5);
154 for (long i = 0; i < n; ++i)
155 {
156 h.AddValue(erv->GetValue());
157 // This could also be expressed as
158 // h.AddValue (erv->Interpolate ());
159 }
160 erv->SetInterpolate(false);
161 sum = 0;
162 weighted = 0;
163 std::cout << std::endl;
164 std::cout << " Bin Start Counts %" << std::endl;
165 std::cout << "---------- -------- --------" << std::endl;
166 for (uint32_t i = 0; i < h.GetNBins(); ++i)
167 {
168 long count = h.GetBinCount(i);
169 double start = h.GetBinStart(i);
170 double value = start + h.GetBinWidth(i) / 2.;
171 sum += count;
172 weighted += count * value;
173 PrintStatsLine(start, count, n);
174 }
175 PrintSummary(sum, n, weighted, 6.25);
176}
177
178int
179main(int argc, char* argv[])
180{
181 long n = 1000000;
182 bool disableAnti = false;
183 bool single = false;
185 cmd.AddValue("count", "how many draws to make from the rng", n);
186 cmd.AddValue("antithetic", "disable antithetic sampling", disableAnti);
187 cmd.AddValue("single", "sample a single time", single);
188 cmd.Parse(argc, argv);
189 std::cout << std::endl;
190 std::cout << cmd.GetName() << std::endl;
191 if (!single)
192 {
193 std::cout << "Sample count: " << n << std::endl;
194 }
195 else
196 {
197 std::cout << "Sampling a single time" << std::endl;
198 }
199 if (disableAnti)
200 {
201 std::cout << "Antithetic sampling disabled" << std::endl;
202 }
203
204 // Create the ERV in sampling mode
205 Ptr<EmpiricalRandomVariable> erv = CreateObject<EmpiricalRandomVariable>();
206 erv->SetInterpolate(false);
207 erv->CDF(0.0, 0.0);
208 erv->CDF(5.0, 0.25);
209 erv->CDF(10.0, 1.0);
210
211 if (single)
212 {
213 RunSingleSample("normal", erv);
214 if (!disableAnti)
215 {
216 std::cout << std::endl;
217 std::cout << "Antithetic" << std::endl;
218 erv->SetAntithetic(true);
219 RunSingleSample("antithetic", erv);
220 erv->SetAntithetic(false);
221 }
222
223 std::cout << std::endl;
224 return 0;
225 }
226
227 RunBothModes("normal", erv, n);
228
229 if (!disableAnti)
230 {
231 erv->SetAntithetic(true);
232 RunBothModes("antithetic", erv, n);
233 erv->SetAntithetic(false);
234 }
235
236 return 0;
237}
Parse command-line arguments.
Definition: command-line.h:232
Class used to store data and make an histogram of the data frequency.
Definition: histogram.h:46
uint32_t GetBinCount(uint32_t index)
Get the number of data added to the bin.
Definition: histogram.cc:74
double GetBinWidth(uint32_t index) const
Returns the bin width.
Definition: histogram.cc:61
uint32_t GetNBins() const
Returns the number of bins in the histogram.
Definition: histogram.cc:43
void AddValue(double value)
Add a value to the histogram.
Definition: histogram.cc:81
double GetBinStart(uint32_t index)
Returns the bin start, i.e., index*binWidth.
Definition: histogram.cc:49
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
void PrintSummary(long sum, long n, double weighted, double expected)
Prints the summary.
void RunBothModes(std::string mode, Ptr< EmpiricalRandomVariable > erv, long n)
Sample the random variable.
void RunSingleSample(std::string mode, Ptr< EmpiricalRandomVariable > erv)
Sample the random variable only once.
void PrintStatsLine(const double value, const long count, const long n)
Prints a stat line.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
value
Definition: second.py:41
cmd
Definition: second.py:33
def start()
Definition: core.py:1861