Skip to main content

Sum tree and min tree implementation used particularly in reinforcement learning algorithms.

Project description

Reinforcement Learning Trees

Efficient sum-tree and min-tree data structures for reinforcement learning workloads (e.g., prioritized experience replay buffers). Both trees are implemented with flat NumPy arrays for speed and small memory overhead.

Features

  • Sum tree with log-time updates and prefix-sum retrieval for weighted sampling.
  • Min tree with log-time updates for tracking the current minimum priority.
  • Simple API: just construct with a capacity, call update, then query with total(), retrieve(), or min().
  • Pure Python with a single NumPy dependency; works anywhere Python 3.9+ runs.

Installation

pip install rltrees

Quickstart

import numpy as np
from rltrees import SumTree, MinTree

capacity = 8
sum_tree = SumTree(capacity)
min_tree = MinTree(capacity)

# Assign priorities/weights at specific indices
priorities = np.linspace(0.1, 0.8, capacity)
for i, p in enumerate(priorities):
    sum_tree.update(i, p)
    min_tree.update(i, p)

# Sample an index proportional to its weight
sampled_idx = sum_tree.retrieve(value=np.random.random() * sum_tree.total())

# Check the smallest priority tracked so far
lowest_priority = min_tree.min()

API

  • SumTree(capacity: int): create a sum tree with fixed capacity.
    • update(idx: int, value: float) -> None: set the value at idx.
    • total() -> float: sum of all stored values.
    • retrieve(value: float) -> int: return the index whose prefix sum covers value (use random * total() for sampling).
  • MinTree(capacity: int): create a min tree with fixed capacity.
    • update(idx: int, value: float) -> None: set the value at idx.
    • min() -> float: smallest value stored in the tree.

All indices are zero-based and must be < capacity.

Development

pip install -e ".[dev]"

Run your tests or scripts against the two tree classes in rltrees/ and open issues or PRs with any findings.

License

MIT License. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rltrees-0.0.12.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rltrees-0.0.12-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file rltrees-0.0.12.tar.gz.

File metadata

  • Download URL: rltrees-0.0.12.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for rltrees-0.0.12.tar.gz
Algorithm Hash digest
SHA256 fb0c021148c10059bc453784829509217436d0e96882ebca4bbf1eac68a99390
MD5 ba175295338d5a502efba4193498988c
BLAKE2b-256 12ad202ba89d3a571459393437cc0af0ec2cf1ce90c0c68839ad2df2cb1182fc

See more details on using hashes here.

File details

Details for the file rltrees-0.0.12-py3-none-any.whl.

File metadata

  • Download URL: rltrees-0.0.12-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for rltrees-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 fb69dbdba0a5ba1555a0a9ed4b43a7a65077aafed4d6844cb9a544db81e3df50
MD5 2e9d8f1cd5526abb9128d68f37866447
BLAKE2b-256 e68c13d60a09d0451291a10576392354c3e946b37f75fd3802c992830d7e67fe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page