My model needs to import a certain amount of patch-related information (via import-world
) when setting up and it takes around 10 seconds to do that.
When using Behavior Space, these seconds add up very quickly and increase greatly the time needed to run experiments.
I need to shorten this time so I wanted to set things in a way that, when launching Behavior Space, the import-world
command is run only on the first run, while all the other times it can be avoided.
If something like that was possible, I could arrange my code in such a way that those 10 seconds would be needed only once each time Behavior Space is launched.
However, as far as I am aware, Behavior Space only asks you for a setup
command to be run at the beginning of each run.
I might be able to work out some ways to achieve what I want, but I only have in mind things that would look a bit error-prone or bad coding style (e.g. not using clear-all
upon setup but clearing "manually", at the end of my code, one by one the things that I want to clear, which would allow me to not clear the imported patch data, and then use if
upon setup to check if that data already exists, and if it exists then do not import it).
However, I would like to hear if there is a better way to achieve my goal here.
Your "bad coding style" idea is the correct one in this case, there is no other way to leave patch data intact while clearing the rest of the model data. The clear-all
primitive is pretty clear about what it does in the docs, so you just need to replace it with all of its component commands besides clear-patches
:
Combines the effects of clear-globals, clear-ticks, clear-turtles, clear-patches, clear-drawing, clear-all-plots, and clear-output.
Then you need to clear any patch data that isn't the "special" data you want to save. ask patches [ set pcolor 0 ]
, as an example if pcolor
is changed in the model run.
You do need to check some conditional on setup
to see if loading patch data is necessary. Not just for the first run, but because each thread you run in BehaviorSpace gets it own "world", so each will need to run your data import commands. From the sixth item in the BehaviorSpace gotchas:
Sixth, each parallel run will get its own world for the model to run in. This world is not cleared automatically by BehaviorSpace if a parallel run gets re-used for another repetition, which happens quite frequently. This means, for example, if you do
ask patches [ set pcolor red ]
in one run and do not useclear-all
orclear-patches
in the setup commands of the next run, then the patches will all still be red. In general usingclear-all
before each run would be a best practice, but there are times when you might not want to, such as loading data from a file that doesn’t change run-to-run. Just be careful with whatever data is not cleared out.
I hope that helps.