python: Run mesa simulation - how to save results of each run?

276 Views Asked by At

I am using this mesa framework which has two main files: model.py and server.py. The simulation as built runs exactly once but I would like to run it several times ex 5 times but save the values for each run. I am using the command line to run the code and after each run I can click the 'Reset' button 'Reset' button to run the simulation again. The code currently clears everything when I click 'Reset' but I was wondering if it is possible to save the results of the simulation somewhere so I can calculate averages after multiple iterations. I thought of creating a monte carlo simulation but realized that is not possible because it requires human intervention to run and monte carlo simulations run on their own.

Now I want to simplify and be able to save all values after each iteration. This sounds easy but due to the nature of the framework, I am unsure if this possible. I need to be able to store the values before this is called again

server = ModularServer(
Schelling, [canvas_element, happy_element, happy_chart], "Schelling", model_params

Does anyone have any ideas if this can be accomplished? I spent the past few days on this and am unsure if this is possible. I checked stackoverflow for similar questions but did not find any.

Sorry for all the updates, I tried to simplify to the basic problem.

Thanks in advance for any help.

2

There are 2 best solutions below

2
On

help if any has a solution i need to save my runs to a file in MESA

0
On

You can run the model without using the GUI.

def run_n_times(n=5):
    """Run the model the given number of times

    n: the number of times to run the model
    """
    for run_number in range(n):
        model = SchellingModel(20, 20, 0.8, 0.2, 4)
        while model.running:
            model.step()
            # processing/logging for each step
        # processing/logging/saving of results for this run
   # Aggregate the results of all runs and return or save

It might be useful to create a new random seed before each run and store that seed so you can replay a given run in the GUI.