Replace `np.vstack` in `ContinuousSpace` by array growth approach inspired from `NumpyAgentDataset`
In Continuous Space, we currently hardcodes a 20% growth fraction inside its _add_agent method.
https://github.com/mesa/mesa/blob/2b23c16b7f13f3fdcf9c11c90853d859d1acedec/mesa/experimental/continuous_space/continuous_space.py#L116-L127
However, NumpyAgentDataSet uses geometric doubling, controlled by class-level constants,
It then uses growth = max(int(current_size * (self._GROWTH_FACTOR - 1)), self._MIN_GROWTH) to calculate the new size.
https://github.com/mesa/mesa/blob/2b23c16b7f13f3fdcf9c11c90853d859d1acedec/mesa/experimental/data_collection/dataset.py#L359-L367
Can we use this approach in Continuous Space as well?
There is nothing stopping us from using this approach in continuous space as well. There is just a clear memory cost, and which is better (20% growth or doubling) depends on the use case.
There is nothing stopping us from using this approach in continuous space as well. There is just a clear memory cost, and which is better (20% growth or doubling) depends on the use case.
Agreed! Just to clarify, I actually wasn't suggesting we increase the default growth factor.
The proposal is strictly to drop the np.vstack in favor of the method used in NumpyAgentDataSet
I'd be curious to see some microbenchmarks comparing both.
Min of 10,000 runs
For GROWTH_FACTOR = 0.2
| Initial Array Size | np.vstack |
Pre-allocation | Speedup |
|---|---|---|---|
| 10 | 0.0003774s | 0.0001056s | 3.57x |
| 100 | 0.0003966s | 0.0001225s | 3.24x |
| 500 | 0.0004417s | 0.0001484s | 2.98x |
| 1,000 | 0.0004849s | 0.0001910s | 2.54x |
| 1,500 | 0.0005704s | 0.0002563s | 2.23x |
| 2,000 | 0.0006226s | 0.0003081s | 2.02x |
OS Spec - Ubuntu 24.04.2
What do you prefer now @quaquel ?
preallocate is clearly faster, so the answer seems obvious :).