1

I'm trying to create a copy of a class instance that I can simulate without affecting the original instance of the class. I've tried using copy.copy, but I run into this problem:

system.simulate(until=100)

print(system.env.now) # prints 100

copy_of_system = copy.copy(system)
copy_of_system.simulate(until=200)

print(copy_of_system.env.now) # prints 200
print(system.env.now) # prints 200, but should print 100

When I use copy.deepcopy I get TypeError: can't pickle generator objects. Is there any effective way to create an independent copy of the system object?

hoffee
  • 470
  • 3
  • 16

2 Answers2

0

One way of having an existing simpy.Environment take several different paths of execution in parallel would be to use os.fork() when you're done setting up the Environment. You then can, for example, leverage the interprocess communication primitives in multiprocessing, such as Queues, etc, for collecting the interesting results of the different simulations. This requires writing a fair amount of boilerplate associated with manual forking, but it can be more than worth it.

NB: os.fork(), that I know of, is available only under Unix-like OSes.

Lester Jack
  • 179
  • 8
0

Another way is to create two instances of the your model, each in their own environment, and just run one longer then the other. You can use a common seed for any random generators. You can use a process with a time out to make changes at your branch points. Of course the longer the warm up, the slower this method will be.

Michael
  • 1,671
  • 2
  • 4
  • 8