A Discrete-Event Network Simulator
API
empirical-random-variable-example.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2020 Lawrence Livermore National Laboratory
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
19  */
20 
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"
26 #include "ns3/ptr.h"
27 
28 #include <iomanip>
29 #include <iostream>
30 #include <map>
31 
32 
53 using namespace ns3;
54 
62 void
64 {
65  std::cout << "------------------------------" << std::endl;
66  std::cout << "Sampling " << mode << std::endl;
67 
68  std::cout << std::endl;
69  std::cout << "Binned sample" << std::endl;
70  double value = erv->GetValue ();
71  std::cout << "Binned sample: " << value << std::endl;
72  std::cout << std::endl;
73 
74  std::cout << "Interpolated sample" << std::endl;
75  erv->SetInterpolate (true);
76  value = erv->GetValue ();
77  std::cout << "Interpolated sample:" << value << std::endl;
78  erv->SetInterpolate (false);
79 }
80 
89 void
90 PrintStatsLine (const double value, const long count, const long n)
91 {
92  std::cout << std::fixed << std::setprecision (3)
93  << std::setw (10) << std::right << value
94  << std::setw (10) << std::right << count
95  << std::setw (10) << std::right
96  << count / static_cast<double> (n) * 100.0
97  << std::endl;
98 }
99 
109 void
110 PrintSummary (long sum, long n, double weighted, double expected)
111 {
112  std::cout << std::endl;
113  std::cout << " --------" << std::endl;
114  std::cout << " Total "
115  << std::setprecision (3) << std::fixed
116  << std::setw (10) << std::right
117  << sum / static_cast<double> (n) * 100.0
118  << std::endl;
119  std::cout << " Average "
120  << std::setprecision (3) << std::fixed
121  << std::setw (6) << std::right << weighted / n
122  << std::endl;
123  std::cout << " Expected "
124  << std::setprecision (3) << std::fixed
125  << std::setw (6) << std::right << expected
126  << std::endl
127  << std::endl;
128 }
129 
138 void
139 RunBothModes (std::string mode, Ptr<EmpiricalRandomVariable> erv, long n)
140 {
141  std::cout << std::endl;
142  std::cout << "Sampling " << mode << std::endl;
143  std::map <double, int> counts;
144  counts[0] = 0;
145  for (long i = 0; i < n; ++i)
146  {
147  ++counts[erv->GetValue ()];
148  }
149  long sum = 0;
150  double weighted = 0;
151  std::cout << std::endl;
152  std::cout << " Value Counts %" << std::endl;
153  std::cout << "---------- -------- --------" << std::endl;
154  for (auto c : counts)
155  {
156  long count = c.second;
157  double value = c.first;
158  sum += count;
159  weighted += value * count;
160  PrintStatsLine (value, count, n);
161  }
162  PrintSummary (sum, n, weighted, 8.75);
163 
164  std::cout << "Interpolating " << mode << std::endl;
165  erv->SetInterpolate (true);
166  Histogram h (0.5);
167  for (long i = 0; i < n; ++i)
168  {
169  h.AddValue (erv->GetValue ());
170  // This could also be expressed as
171  // h.AddValue (erv->Interpolate ());
172  }
173  erv->SetInterpolate (false);
174  sum = 0;
175  weighted = 0;
176  std::cout << std::endl;
177  std::cout << " Bin Start Counts %" << std::endl;
178  std::cout << "---------- -------- --------" << std::endl;
179  for (uint32_t i = 0; i < h.GetNBins (); ++i)
180  {
181  long count = h.GetBinCount (i);
182  double start = h.GetBinStart (i);
183  double value = start + h.GetBinWidth (i) / 2.;
184  sum += count;
185  weighted += count * value;
186  PrintStatsLine (start, count, n);
187  }
188  PrintSummary (sum, n, weighted, 6.25);
189 }
190 
191 
192 int main (int argc, char *argv[])
193 {
194  long n = 1000000;
195  bool disableAnti = false;
196  bool single = false;
198  cmd.AddValue ("count", "how many draws to make from the rng", n);
199  cmd.AddValue ("antithetic", "disable antithetic sampling", disableAnti);
200  cmd.AddValue ("single", "sample a single time", single);
201  cmd.Parse (argc, argv);
202  std::cout << std::endl;
203  std::cout << cmd.GetName () << std::endl;
204  if (!single)
205  {
206  std::cout << "Sample count: " << n << std::endl;
207  }
208  else
209  {
210  std::cout << "Sampling a single time" << std::endl;
211  }
212  if (disableAnti)
213  {
214  std::cout << "Antithetic sampling disabled" << std::endl;
215  }
216 
217  // Create the ERV in sampling mode
218  Ptr<EmpiricalRandomVariable> erv = CreateObject<EmpiricalRandomVariable> ();
219  erv->SetInterpolate (false);
220  erv->CDF ( 0.0, 0.0);
221  erv->CDF ( 5.0, 0.25);
222  erv->CDF (10.0, 1.0);
223 
224  if (single)
225  {
226  RunSingleSample ("normal", erv);
227  if (!disableAnti)
228  {
229  std::cout << std::endl;
230  std::cout << "Antithetic" << std::endl;
231  erv->SetAntithetic (true);
232  RunSingleSample ("antithetic", erv);
233  erv->SetAntithetic (false);
234  }
235 
236  std::cout << std::endl;
237  return 0;
238  }
239 
240  RunBothModes ("normal", erv, n);
241 
242  if (!disableAnti)
243  {
244  erv->SetAntithetic (true);
245  RunBothModes ("antithetic", erv, n);
246  erv->SetAntithetic (false);
247  }
248 
249  return 0;
250 }
Parse command-line arguments.
Definition: command-line.h:229
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:73
double GetBinWidth(uint32_t index) const
Returns the bin width.
Definition: histogram.cc:60
uint32_t GetNBins() const
Returns the number of bins in the histogram.
Definition: histogram.cc:42
void AddValue(double value)
Add a value to the histogram.
Definition: histogram.cc:80
double GetBinStart(uint32_t index)
Returns the bin start, i.e., index*binWidth.
Definition: histogram.cc:48
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
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.
cmd
Definition: second.py:35
def start()
Definition: core.py:1853