Skip to main content

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.

Metadata

Release files for rltrees 0.0.13

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rltrees 0.0.13
File Size Uploaded Provenance Actions
rltrees-0.0.13.tar.gz 5.8 kB Without provenance Details for rltrees-0.0.13.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rltrees 0.0.13
File Interpreter ABI Platform Actions
rltrees-0.0.13-py3-none-any.whl Python 3 none any Details for rltrees-0.0.13-py3-none-any.whl

Total release size: 10.8 kB

Release files / rltrees-0.0.13.tar.gz

Download URL rltrees-0.0.13.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6a820a61a1079c6805eac94fb5db7e1708f209b4fd0db763e3f427c4fd12325e
BLAKE2b-256 checksum
How to use checksums
cbe8519acf2919638b40729fbc9e0ac707883934e5aa79fc0045983840830f30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release files / rltrees-0.0.13-py3-none-any.whl

Download URL rltrees-0.0.13-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f138d732abf7043e8cddb932cafbe6e6536e4be0c48754fbff48f382abaa95da
BLAKE2b-256 checksum
How to use checksums
70fcc7a6ce5f10afbde3fdea0ac5fa655dc0870445993d5185ca94369ffec645
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Security for rltrees 0.0.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page