Method to automate simulation runs Method to automate simulation runs json json

Method to automate simulation runs


I implemented the JSON file technique and it works very well so I thought I would share the answer.

A JSON file is created. In may case the top level is a key that identifies a "run" - essentially a unique combination of inputs. The value is another dictionary that contains all the values used to setup the simulation, such as the number of replications - as a DES needs multiple reps. It is simply loaded using the in built JSON functionality in Python.

with open(file_name) as data_file:input_data = json.load(data_file)  # dict of the whole file

So at this stage we have input_data which is simply a dictionary of the whole file. By extracting the top level key and looping through each you are able to sequentially pass each runs inputs into the simulation. For each run you are only passing the set of inputs you need to pass, not the whole dictionary.

list_of_runs = sorted(list(input_data.keys()), key=int) for run in list_of_runs:replications = input_data[run]['replications']reps = 0

The next loop runs the simulation the defined number of times using the same set of inputs each time.

for reps in range(replications):    current_run = initialize.Run(input_data[run])    '''some other code here'''    env.run(until=end)    reps += 1

In my actual code "Run" is a class and within that class other classes are defined each being passed the portion of the original JSON file they need. This means when a class needs to access a value if only needs look within the dictionary that is has been passed and all of these dictionaries are updated automatically as the code runs through each of the runs.


If you have an input table in which each row refers to a set of inputs for a single run, you could do something like the following.

import sqlite3db_path = "C:/.../mydatabase.db"connection = sqlite3.connect(db_path)cursor = connection.cursor()#get parameters from tablefor row in cursor.execute('SELECT * FROM input_table'):    param1 = row[0]    param2 = row[1]    ...    go_for_simulation(param1, param2,...)

Each row variable in the loop is a tuple that contains all your parameters. I hope this approach could help you.