21 #include "ns3/simulator.h"
22 #include "ns3/nstime.h"
23 #include "ns3/command-line.h"
24 #include "ns3/random-variable-stream.h"
25 #include "ns3/histogram.h"
55 std::cout <<
"------------------------------" << std::endl;
56 std::cout <<
"Sampling " << mode << std::endl;
58 std::cout << std::endl;
59 std::cout <<
"Binned sample" << std::endl;
60 double value = erv->GetValue ();
61 std::cout <<
"Binned sample: " << value << std::endl;
62 std::cout << std::endl;
64 std::cout <<
"Interpolated sample" << std::endl;
65 erv->SetInterpolate (
true);
66 value = erv->GetValue ();
67 std::cout <<
"Interpolated sample:" << value << std::endl;
68 erv->SetInterpolate (
false);
74 std::cout << std::fixed << std::setprecision (3)
75 << std::setw (10) << std::right << value
76 << std::setw (10) << std::right << count
77 << std::setw (10) << std::right
78 << count /
static_cast<double> (
n) * 100.0
85 std::cout << std::endl;
86 std::cout <<
" --------" << std::endl;
87 std::cout <<
" Total "
88 << std::setprecision (3) << std::fixed
89 << std::setw (10) << std::right
90 << sum /
static_cast<double> (
n) * 100.0
92 std::cout <<
" Average "
93 << std::setprecision (3) << std::fixed
94 << std::setw (6) << std::right << weighted /
n
96 std::cout <<
" Expected "
97 << std::setprecision (3) << std::fixed
98 << std::setw (6) << std::right << expected
106 std::cout << std::endl;
107 std::cout <<
"Sampling " << mode << std::endl;
108 std::map <double, int> counts;
110 for (
long i = 0; i <
n; ++i)
112 ++counts[erv->GetValue ()];
116 std::cout << std::endl;
117 std::cout <<
" Value Counts %" << std::endl;
118 std::cout <<
"---------- -------- --------" << std::endl;
119 for (
auto c : counts)
121 long count = c.second;
122 double value = c.first;
124 weighted += value * count;
129 std::cout <<
"Interpolating " << mode << std::endl;
130 erv->SetInterpolate (
true);
132 for (
long i = 0; i <
n; ++i)
138 erv->SetInterpolate (
false);
141 std::cout << std::endl;
142 std::cout <<
" Bin Start Counts %" << std::endl;
143 std::cout <<
"---------- -------- --------" << std::endl;
144 for (uint32_t i = 0; i < h.
GetNBins (); ++i)
150 weighted += count * value;
157 int main (
int argc,
char *argv[])
160 bool disableAnti =
false;
163 cmd.AddValue (
"count",
"how many draws to make from the rng",
n);
164 cmd.AddValue (
"antithetic",
"disable antithetic sampling", disableAnti);
165 cmd.AddValue (
"single",
"sample a single time", single);
166 cmd.Parse (argc, argv);
167 std::cout << std::endl;
168 std::cout <<
cmd.GetName () << std::endl;
171 std::cout <<
"Sample count: " <<
n << std::endl;
175 std::cout <<
"Sampling a single time" << std::endl;
179 std::cout <<
"Antithetic sampling disabled" << std::endl;
184 erv->SetInterpolate (
false);
185 erv->CDF ( 0.0, 0.0);
186 erv->CDF ( 5.0, 0.25);
187 erv->CDF (10.0, 1.0);
194 std::cout << std::endl;
195 std::cout <<
"Antithetic" << std::endl;
196 erv->SetAntithetic (
true);
198 erv->SetAntithetic (
false);
201 std::cout << std::endl;
209 erv->SetAntithetic (
true);
211 erv->SetAntithetic (
false);
Parse command-line arguments.
Class used to store data and make an histogram of the data frequency.
uint32_t GetBinCount(uint32_t index)
Get the number of data added to the bin.
double GetBinWidth(uint32_t index) const
Returns the bin width.
uint32_t GetNBins() const
Returns the number of bins in the histogram.
void AddValue(double value)
Add a value to the histogram.
double GetBinStart(uint32_t index)
Returns the bin start, i.e., index*binWidth.
Smart pointer class similar to boost::intrusive_ptr.
void PrintSummary(long sum, long n, double weighted, double expected)
void RunBothModes(std::string mode, Ptr< EmpiricalRandomVariable > erv, long n)
void RunSingleSample(std::string mode, Ptr< EmpiricalRandomVariable > erv)
void PrintStatsLine(const double value, const long count, const long n)
Every class exported by the ns3 library is enclosed in the ns3 namespace.