jeudi 12 mars 2015

Implementing common random numbers in a simulation

I am building a small simulation in Python and I would like to use Common Random Numbers to reduce variation. I know that I must achieve synchronization for CRN to work:



CRN requires synchronization of the random number streams, which ensures that in addition to using the same random numbers to simulate all configurations, a specific random number used for a specific purpose in one configuration is used for exactly the same purpose in all other configurations.



I was wondering if the way I wanted to implement it in my simulation was valid or if I should be using a different approach.


My simulation has three different classes (ClassA, ClassB, ClassC), and ClassA objects have random travel times, ClassB objects have random service times and random usage rates, and ClassC objects have random service times. Of course there can be multiple instances of each class of object.


At the start of the simulation I specify a single random number seed (replication_seed) so that I can use a different seed for each simulation replication.



import numpy.random as npr
rep_rnd_strm = npr.RandomState().seed(replication_seed)


Then in the constructor for each Class, I use rep_rnd_strm to generate a seed that is used to initialize the random number stream for the instance of the class:



self.class_rnd_strm = npr.RandomState().seed(rep_rnd_strm.randint(10000000))


I then use self.class_rnd_strm to generate a seed for each random number stream needed for the class instance. For example the constructor of ClassA has:



self.travel_time_strm = npr.RandomState().seed(self.class_rnd_strm.randint(10000000))


while the constructor of ClassB has:



self.service_time_strm = npr.RandomState().seed(self.class_rnd_strm.randint(10000000))
self.usage_rate_strm = npr.RandomState().seed(self.class_rnd_strm.randint(10000000))


Is what I am doing here a valid approach to getting synchronization to work, or should I be doing things differently?





Aucun commentaire:

Enregistrer un commentaire