NS-3 manual note – 01 Random Variables
Posted on 07/03/2009 by xa_ceshi
The ns-3 network simulator
ns-3 is a discrete-event network simulator for Internet systems, targeted
primarily for research and educational use. ns-3 is free software, licensed
under the GNU GPLv2 license, and is publicly available for research,
development, and use.
ns-3 is intended as an eventual replacement for the popular ns-2 simulator.
The project acronym “nsnam” derives historically from the concatenation of
ns (network simulator) and nam (network animator).
以上是 NS3 官网的介绍,NS2 还没学好,NS3 就要出来了。。。而且 NS3 的设
计初衷就是为了取代 NS2,所以还是早起的鸟儿有虫吃,哦,错了,应该是笨
鸟先飞。先研究 manual,翻译的比较拙劣。说句公道话,原文写的也是有些晦
涩-_-!。翻译的排版比较乱,后期会出一个完整的 pdf。
NS3 官方网站 http://www.nsnam.org
manual 原文 http://www.nsnam.org/docs/release/manual.html
注意 pdf 版的 manual 和 html 版的 manual 略有不同。
翻译进行时这个 manual 是基于 ns3-3.4 的,原文章节结构为
1. Random variables
2. Callbacks
3. Attributes
4. Object model
5. Real-Time Scheduler
6. Emulation
7. Packets
8. Sockets APIs
9. Node and Internet Stack
10. TCP models in ns-3
11. Routing overview
12. Wifi NetDevice
13. CSMA NetDevice
14. PointToPoint NetDevice
15. Creating a new ns-3 model
16. Troubleshooting
1. 随机变量
ns-3 包含内建的伪随机数生成器(built-in pseudo-random number generator
(PRNG)). 对于要求严格的用户,理解 PRNG 的功能、设置及用法是很重要的,
便于判断对于他的研究该 PRNG 是否能够胜任。
——————————————————————————–
1.1 概述
ns-3 的随机数是通过 class RandomVariable 的实例来提供的。
默认情况下,ns-3 的模拟过程使用固定的种子;如果在模拟过程中存在随机性,
除非改变种子和/或运行次数,否则程序的每次运行都将产生相同的结果。
在 ns-3.3 和早期的版本中,默认情况下 ns-3 的模拟过程使用随机的种子;从
ns-3.4 开始做出了改变。
为了在若干次模拟中获得随机性,必须设置不同的种子或者设置不同的运行次数。
在程序的起始处调用 SeedManager::SetSeed(uint32_t) 来设置种子;当要用相
同的种子来设置运行次数时,才程序的起始处调用 SeedManager::SetRun
(uint32_t)。参考章节 设置种子以及独立的重复实验
每个 ns-3 中使用的随机变量都有一个虚拟随机数发生器与之相关联;所有随机
变量都是用固定的或者随机的种子(基于全局种子的使用(previous bullet));
如果你想对同一个场景使用不同的随机数进行多次运行,请一定阅读一下章节:
设置种子以及独立的重复实验.
关于 ns-3 中随机数功能的详细解释。
——————————————————————————–
1.2 背景
模拟过程使用大量的随机数; 研究[cite]表明大多数网络模拟花费多达 50%的
CPU 时间来生成随机数。用户需要关注(伪)随机数的质量以及不同随机数流
之间的独立性。
用户需要关注一些内容,比如:
随机数生成器的种子化以及某个模拟过程是否是确定性的,
如何获得彼此独立的随机数流,以及
随机数流循环一次的时间有多久。
这里我们引入一些术语:RNG 提供很长的(伪)随机数序列。这个序列的长度
叫做 cycle length 或 period,在该循环长度或周期过后,RNG 将重复自身。该
序列可以被划分为互不关联的 streams。RNG 的流是 RNG 的一个连续的子集
或者块。例如,如果 RNG 周期的长度为 N,且这个 RNG 提供两个流,则第一
个流可能使用前 N/2 个值,第二个流可能产生后边的 N/2 个值。一个重要的性
质是:这两个流是互不关联的,同样,每个流可以被划分为互不关联的若干个
substreams。底层 RNG 希望能够产生有着非常长的循环长度的伪随机数序列,
并以有效地方式将序列划分为流和子流。
ns-3 使用与 ns-2 相同的底层随机数生成器:the MRG32k3a generator from
Pierre L’Ecuyer. 详细的描述可以再这里找到:
http://www.iro.umontreal.ca/~lecuyer/myftp/papers/streams00.pdf.
MRG32k3a 生成器提供 1.8×10^19 个独立的随机数流,其中每个流都由
2.3×10^15 个子流组成。每个子流的周期(i.e., the number of random numbers
before overlap)为 7.6×10^22。整个生成器的周期为 3.1×10^57。 表 ref-streams
提供了流与子流是如何组合到一起的图形化表示。
类 ns3::RandomVariable 是底层随机数生成器的公共接口。当用户创建新的
RandomVariables(比如 UniformVariable,ExponentialVariable 等)时,将创
建一个对象,该对象使用 随机数生成器的彼此独立的流中的某一个流。因此,
每个具有类型 RandomVariable 的对象都在概念上有自己的 “虚拟的”RNG. 另
外,每个 RandomVariable 都可以被设置为使用由 main stream 获得的
substreams 中的某个子流。
另一个可选的实现是:允许每个 RandomVariable 拥有自己的(被不同的种子种
子化过的)RNG。但是,在这种情况下我们无法保证不同的序列将互不关联。
因此,我们选择使用单个的 RNG 以及由该 RGG 获得的流与子流。
——————————————————————————–
1.3 设置种子以及独立的重复实验
ns-3 的模拟过程可以被设置为来产生确定的或者随机的结果。如果模拟过程被
设置为使用固定的、确定的种子以及相同的运行次数,那么每次运行的输出都应
该是相同的。
在默认情况下,ns-3 的模拟过程使用固定的种子和运行次数。这些值存储在两
个 ns3::GlobalValue 实例中: g_rngSeed 和 g_rngRun。
典型的情况是对某个模拟过程进行多次独立试验。以便根据大量的独立运行来计
算统计信息。用户可以改变全局种子并再次运行该模拟过程,也可以推进 RNG
的子流的状态,这被称为增加运行次数。
类 ns3::SeedManager () 提供一个 API 来控制设置种子和运行次数的行为。这
个设置种子和子流状态的设置必须在创建任何随机变量之前被调用。例如:
SeedManager::SetSeed (3); // Changes seed from default of 1 to 3
SeedManager::SetRun (7); // Changes run number from default of 1 to 7
// Now, create random variables
UniformVariable x(0,10);
ExponentialVariable y(2902);
…
设置新的种子和推进子流的状态,哪一个更好?无法保证流产生的两个随机种子
不是互相重叠的(译者注:即有可能部分一致)。保证两个流不互相重叠的唯一
办法是使用由 RNG 的实现提供的子流功能。 因此,使用子流功能来实现同一
个模拟过程的多个彼此独立的运行。 换句话说,统计学上,设置独立的重复实
验更严格的方法应该是使用固定的种子并改变运行次数。这样的实现考虑到使用
子流时,独立的重复实验的最大值为 2.3×10^15。
为了方便使用,用户不必在程序中控制种子和运行次数,可以设置环境变量
NS_GLOBAL_VALUE :
NS_GLOBAL_VALUE=”RngRun=3″ ./waf –run program-name
另一个控制的方法是传递一个命令行参数,因为是 ns3 的 GlobalValue,所以等
价于如下:
./waf –command-template=”%s –RngRun=3″ –run program-name
或者,如果直接在 waf 之外运行程序:
./build/optimized/scratch/program-name –RngRun=3
上述的各种命令行用法使得由 shell 脚本通过传递不同的 RngRun 系数来运行大
量不同的模拟变得很容易。
——————————————————————————–
1.4 类 RandomVariable
所有随机变量都应该派生自 类 RandomVariable。该基类提供了一些在全局层
面设置随机数生成器的行为的静态方法。派生类提供了刻画随机特性的 API,这
些随机特性由特殊的分布决定。
模拟过程中被创建的每一个 RandomVariable 都将被给予一个生成器,该生成器
是来自于底层 PRNG 的一个新的 RNGStream。按照这种方法来使用,the
L’Ecuyer implementation 考虑到了最多 1.8×10^19 个随机变量。每个随机变量
在某个单个的实验中可以在出现重叠前产生多达 7.6×10^22 个随机数。
——————————————————————————–
1.5 基类的公共 API
以下摘录了一些 类 RandomVariable 的公共方法,这些方法处理 RNG 的全局
设置和 RNG 的状态。
/**
* brief Set seeding behavior
*
* Specify whether the POSIX device /dev/random is to
* be used for seeding. When this is used, the underlying
* generator is seeded with data from /dev/random instead of
* being seeded based upon the time of day. Defaults to true.
*/
static void UseDevRandom(bool udr = true);
/**
* brief Use the global seed to force precisely reproducible results.
*/
static void UseGlobalSeed(uint32_t s0, uint32_t s1, uint32_t s2,
uint32_t s3, uint32_t s4, uint32_t s5);
/**
* brief Set the run number of this simulation
*/
static void SetRunNumber(uint32_t n);
/**
* brief Get the internal state of the RNG
*
* This function is for power users who understand the inner workings
* of the underlying RngStream method used. It returns the internal
* state of the RNG via the input parameter.
* param seed Output parameter; gets overwritten with the internal state
* of the RNG.
*/
void GetSeed(uint32_t seed[6]) const;
我们已经在上边描述过种子的设置了。
——————————————————————————–
1.6 RandomVariable 的类型
目前提供如下随机变量,文档在 ns-3 Doxygen 中,或者阅读
src/core/random-variable.h。用户也可以通过从类 RandomVariable 派生,来
创建自定义的随机变量。
class UniformVariable
class ConstantVariable
class SequentialVariable
class ExponentialVariable
class ParetoVariable
class WeibullVariable
class NormalVariable
class EmpiricalVariable
class IntEmpiricalVariable
class DeterministicVariable
class LogNormalVariable
class TriangularVariable
——————————————————————————–
1.7 RandomVariable 对象的语义
RandomVariable 对象有值语义,这意味着他们可以按照值传递的方式传递给函
数。他们呢还可以按照引用传递的方式传递给 const。随机变量不是派生自
ns3::Object,并且我们不使用 smart pointers 来管理他们。他们可以被分配在栈
上,用户也可以显式控制任何分配在堆上的随机变量。
RandomVariable 对象还可以被 ns-3 的 attributes 使用,这表示可以通过 ns-3
的 attribute system 来给 RandomVariable 对象赋值。以下是一个关于
WifiNetDevice 的 propagation 模型的例子:
TypeId
RandomPropagationDelayModel::GetTypeId (void)
{
static TypeId tid = TypeId (“ns3::RandomPropagationDelayModel”)
.SetParent
()
.AddConstructor ()
.AddAttribute (“Variable”,
“The random variable which generates random delays (s).”,
RandomVariableValue (UniformVariable (0.0, 1.0)),
MakeRandomVariableAccessor
(&RandomPropagationDelayModel::m_variable),
MakeRandomVariableChecker ())
;
return tid;
}
这里,ns-3 的用户可以通过 attribute system 改变这个延迟模型的默认随机变
量(which is a UniformVariable ranging from 0 to 1)。
——————————————————————————–
1.8 使用其他 PRNG
目前没有关于替换底层随机数生成器的支持(例如:the GNU Scientific Library
or the Akaroa package),欢迎提供程序包。
——————————————————————————–
1.9 More advanced usage
To be completed
——————————————————————————–
1.10 Publishing your results
当发布模拟结果时,你需要陈述的一个关键配置信息是你是如何使用随机数生成
器的。
你使用了什么样的种子,what seeds you used,
如果没有使用默认的 RNG,那么你使用的 RNG 是什么,
彼此独立的运行时如何执行的,
对于大规模的模拟,你如何检验没有发生循环。
研究者应该对发布的结果提供足够的信息以使得别人能够再现他的结果,这一点
是责无旁贷的。研究者还应该充分弄明白所使用的随机数是统计学上合法的,并
在文章中陈述为什么做出这样的假定,这一点也是责无旁贷的。
——————————————————————————–
1.11 小结
让我们回顾一下在创建模拟过程时应该做些什么。
决定使用固定的种子还是随机的种子,默认情况使用随机的种子,
决定如何处理独立的重复实验,对于长时间的模拟,如果可行的话,确认自己没
有产生多于循环长度的随机值,
当发布结果时,按照上述的指导来对随机数生成器的使用书写文档。
程序 samples/main-random.cc 包含一些用法的例子。
NS-3 manual note – 01 Random Variables
by gaocong
The ns-3 network simulator
ns-3 is a discrete-event network simulator for Internet systems, targeted
primarily for research and educational use. ns-3 is free software, licensed
under the GNU GPLv2 license, and is publicly available for research,
development, and use.
ns-3 is intended as an eventual replacement for the popular ns-2 simulator.
The project acronym “nsnam” derives historically from the concatenation of
ns (network simulator) and nam (network animator).
以上是 NS3 官网的介绍,NS2 还没学好,NS3 就要出来了。。。而且 NS3 的设
计初衷就是为了取代 NS2,所以还是早起的鸟儿有虫吃,哦,错了,应该是笨
鸟先飞。先研究 manual,翻译的比较拙劣。说句公道话,原文写的也是有些晦
涩-_-!。翻译的排版比较乱,后期会出一个完整的 pdf。
NS3 官方网站 http://www.nsnam.org
manual 原文 http://www.nsnam.org/docs/release/manual.html
注意 pdf 版的 manual 和 html 版的 manual 略有不同。
翻译进行时这个 manual 是基于 ns3-3.4 的,原文章节结构为
1. Random variables
2. Callbacks
3. Attributes
4. Object model
5. Real-Time Scheduler
6. Emulation
7. Packets
8. Sockets APIs
9. Node and Internet Stack
10. TCP models in ns-3
11. Routing overview
12. Wifi NetDevice
13. CSMA NetDevice
14. PointToPoint NetDevice
15. Creating a new ns-3 model
16. Troubleshooting
1. 随机变量
ns-3 包含内建的伪随机数生成器(built-in pseudo-random number generator
(PRNG)). 对于要求严格的用户,理解 PRNG 的功能、设置及用法是很重要的,
便于判断对于他的研究该 PRNG 是否能够胜任。
——————————————————————————–
1.1 概述
ns-3 的随机数是通过 class RandomVariable 的实例来提供的。