Dooders / Experiments

Repository for all specific experiments and tests
0 stars 0 forks source link

Feature: Performance Optimization: Distance Calculations with KD-trees #23

Open csmangum opened 1 week ago

csmangum commented 1 week ago

Repeated distance calculations for proximity queries can create a performance bottleneck, especially when there are many agents or resources to check. Optimizing these calculations will help improve overall system efficiency.

Proposed Solution:

  1. Utilize Spatial Indexing Structures: Use KD-trees or spatial grids to efficiently handle proximity queries.

    • This can significantly reduce the computational load by only querying relevant nearby resources/agents instead of iterating through all possible objects.

    Example Implementation:

    from scipy.spatial import KDTree
    
    resource_positions = [r.position for r in agent.environment.resources]
    tree = KDTree(resource_positions)
    
    # Query for resources within a certain range of the agent
    indices = tree.query_ball_point(agent.position, r=agent.config.gathering_range)
    nearby_resources = [agent.environment.resources[i] for i in indices]
  2. Vectorization: Ensure all distance and other repetitive calculations are vectorized using NumPy or PyTorch where possible. This leverages hardware acceleration, improving the speed and efficiency of the code.

Benefits:

Acceptance Criteria:

Additional Context: This change will improve the agent action system, especially in dense environments with numerous resources or other agents.