Allen Wang
Allen Wang

Reputation: 2502

simpy how to run a simulation multiple times

I have a basic simpy question. I have created a basic simulation and now want to run it many times to get a distribution of possible values. Is there a more efficient way to do this than to run the script multiple times? Is there a way to reset the environment to the initial state and rerun that way, without having to load in all external classes and other setup for every single run?

Should I put the entire simulation in a for loop and collect data this way, or is the recommended approach to run the script multiple times from outside of python and append to some data cache?

Upvotes: 3

Views: 5146

Answers (2)

Harry Munro
Harry Munro

Reputation: 324

Here is an example of how to run a simulation multiple times with nested for loops:

for variable_a in a_variables:
    for variable_b in b_variables:
        for variable_c in c_variables:
            for variable_d in d_variables:
                print 'Starting sim %d' % x

                # setup environment
                random.seed(random.random())
                env = simpy.Environment()

                # resource
                resource = simpy.Resource(env, capacity = 2)

                env.process(source(env))
                env.run(until=SIM_TIME)

This is a good way of running experiments where you need to sweep across a multiple variables. This is called a "full factorial" experiment. Beware though that the number of simulations that you end up creating increases rapidly as you add factors and increase the number of points within each factor.

Upvotes: 0

Stefan Scherfke
Stefan Scherfke

Reputation: 3232

Put all your simulation setup (environment creation, resources, processes, …) into a single function and map it to a process pool (see https://docs.python.org/3/library/multiprocessing.html).

Upvotes: 3

Related Questions