tags:

views:

35

answers:

1

Sorry if the title is not very clear. I was not sure about the appropriate title. Let me explain what I need.

I am doing multiple runs of a simulation, where each run corresponds to a different seed. However, I want the starting characteristics of the instances of a class to remain the same across the different runs. For example, consider the class of people in a city. In the code below the command city = people() creates person objects each of whom has some wealth chosen randomly from a distribution. Let F(.) be the realized initial distribution of wealth in the population. As one particular run of a simulation is made, things change in the population and various attributes of the person objects get updated. For example, a person's income changes. The final values of these attributes depend on some random realizations that occur during the simulation run. Now I want to again run the simulation with a different random seed, where before the run begins all attributes are reset to their initial values (including the wealth distribution that was randomly determined). Should I make a shallowCopy or a deepCopy? Is there a third way that is better?

Thanks a ton.

city = people()

for seedValue in ListOfSeeds:

    cityThisInstance = city.copy()

    cityThisInstance.someAttribute = xxxxx
    cityThisInstance.anotherAttribute = yyyyy

    Rest of the code
+2  A: 

From what I understand, you always want your initial conditions (e.g. the state of city before you even get to your loop) to be the same. If that's the case, I would prefer to just reinitialize the class whenever you run through the loop, as it's much clearer.

initargs = 21, 50000
initkwargs = {car: 'blue', make: 'mazda'}

for loop:
    cityThisInstance = people(*initargs, **initkwargs)

If whenever you initialize the class some significant amount of code runs, it may be better to simply copy it's state. Deepcopy would be preferred, so that way all the variables it uses will be copied, especially mutables, rather than point to where the originals are.

import copy

for loop:
    cityThisInstance = copy.deepcopy(city)
Nick T
Thanks Nick. I cannot reinitialize the class since the initial state has some variables that were determined using the random.uniform function. These values will change if I reinitialize using the first method you have suggested. But the second one should work. I will try that. Thanks.
Curious2learn