mesa icon indicating copy to clipboard operation
mesa copied to clipboard

Replace `np.vstack` in `ContinuousSpace` by array growth approach inspired from `NumpyAgentDataset`

Open codebreaker32 opened this issue 8 months ago • 6 comments

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?

codebreaker32 avatar Mar 15 '26 17:03 codebreaker32

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.

quaquel avatar Mar 15 '26 18:03 quaquel

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

codebreaker32 avatar Mar 15 '26 18:03 codebreaker32

I'd be curious to see some microbenchmarks comparing both.

quaquel avatar Mar 15 '26 18:03 quaquel

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

codebreaker32 avatar Mar 16 '26 05:03 codebreaker32

What do you prefer now @quaquel ?

codebreaker32 avatar Mar 16 '26 12:03 codebreaker32

preallocate is clearly faster, so the answer seems obvious :).

quaquel avatar Mar 16 '26 13:03 quaquel