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.13.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.13-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: rltrees-0.0.13.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.13.tar.gz
Algorithm Hash digest
SHA256 6a820a61a1079c6805eac94fb5db7e1708f209b4fd0db763e3f427c4fd12325e
MD5 ece69cf1553d9f9fe2c60e5bb26b5193
BLAKE2b-256 cbe8519acf2919638b40729fbc9e0ac707883934e5aa79fc0045983840830f30

See more details on using hashes here.

File details

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

File metadata

  • Download URL: rltrees-0.0.13-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.13-py3-none-any.whl
Algorithm Hash digest
SHA256 f138d732abf7043e8cddb932cafbe6e6536e4be0c48754fbff48f382abaa95da
MD5 8989779d70b19b16166ea648cbdf4c8d
BLAKE2b-256 70fcc7a6ce5f10afbde3fdea0ac5fa655dc0870445993d5185ca94369ffec645

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