Skip to content

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

Closed

Description

@codebreaker32

In Continuous Space, we currently hardcodes a 20% growth fraction inside its _add_agent method.

if self._agent_positions.shape[0] <= index:
# we are out of space
fraction = 0.2 # we add 20% Fixme
n = round(fraction * self._n_agents, None)
self._agent_positions = np.vstack(
[
self._agent_positions,
np.empty(
(n, self.dimensions.shape[0]),
),
]
)

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.

def _expand_storage(self) -> None:
"""Expand the internal array when out of space."""
current_size = self._agent_data.shape[0]
growth = max(int(current_size * (self._GROWTH_FACTOR - 1)), self._MIN_GROWTH)
new_size = current_size + growth
new_data = np.empty((new_size, len(self._attributes)), dtype=self.dtype)
new_data[:current_size] = self._agent_data
self._agent_data = new_data

Can we use this approach in Continuous Space as well?

Activity

  1. quaquel commented on Mar 15, 2026

    @quaquel
    Member

    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.

  2. added a commit that references this issue on Mar 15, 2026
    6f52b9b
  3. codebreaker32 commented on Mar 15, 2026

    @codebreaker32
    CollaboratorAuthor

    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

  4. quaquel commented on Mar 15, 2026

    @quaquel
    Member

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

  5. codebreaker32 commented on Mar 16, 2026

    @codebreaker32
    CollaboratorAuthor

    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

  6. codebreaker32 commented on Mar 16, 2026

    @codebreaker32
    CollaboratorAuthor

    What do you prefer now @quaquel ?

  7. quaquel commented on Mar 16, 2026

    @quaquel
    Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions