Multiple model runs /batchrunner runs not independent #1402
-
Hello, I have constructed the following model and file. If I run the script one time, or many times from scratch again, the model runs fine and as I expect it to. However if I loop through multiple runs, using a simple loop or the batchrunner (doesn't matter same behaviour), the runs are not independent anymore but information is passed on to subsequent runs. I suspect it has something to do with how the class "model" is instantiated or how I set up some specific code within the classes "model" or "agent". It is like there is some memory from run to run or certain variables are not correctly passed on. However I am 2 days on it and cannot find my mistake. As said, a single model run is exactly as i expect it to work. I tried several things including carefully passing variables and objects with deepcopy() and deleteting instances of classes after all their data is used. Nothing has worked so far. I am quite new to OOP so my mistake might be more trivial than it seems to me and not sure it is MESA specific. I did not encounter the same problem with the example model from the tutorials. You can try exactly what I mean by going to the 2nd code cell and either set no_of_iterations to 1 and running the whole script multiple times, then the output variable of interest will be following mostly values between 100 and 200 sometimes maybe stuck at lower values but clearly following a different distribution as when you set numer of iteratios to 10 and there you can see in "result_array" that the output has stored monotonically increasing values (which is non-sense and shows the dependence of the results). There is a variable in the agent class "minimum difference" to which all of this is related, so much I have found out, but I don't know exactly why this is caused by my code. I know some of the code pieces e.g. the compute_distance() function is horribly long but any help appreciated.
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Doing that class instances is not recommended, and is most likely the cause of the unexpected behavior you observed. If you want to access all the initialized agents, you do it this way: # Inside the compute_distance method
for agent in self.model.schedule.agents:
# do something with the agent object |
Beta Was this translation helpful? Give feedback.
-
I solved my issue. The following way for now. In the future I'll think about how to avoid this issue from scratch. I just added a reset method to my agent class:
and call it after every model iteration this way
not sure though how to use batchrunner with this |
Beta Was this translation helpful? Give feedback.
I solved my issue. The following way for now. In the future I'll think about how to avoid this issue from scratch.
I just added a reset method to my agent class:
and call it after every model iteration this way
not sure though how to use batchrunner with this