Metadata-Version: 2.4
Name: rltrees
Version: 0.0.13
Summary: Sum tree and min tree implementation used particularly in reinforcement learning algorithms.
Author-email: Taha Shieenavaz <tahashieenavaz@gmail.com>
License: MIT License
        
        Copyright (c) 2025 Taha Shieenavaz
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/tahashieenavaz/rltrees
Project-URL: Repository, https://github.com/tahashieenavaz/rltrees
Project-URL: Documentation, https://github.com/tahashieenavaz/rltrees#readme
Keywords: reinforcement learning,sum tree,min tree,data structure
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy
Dynamic: license-file

# 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

```bash
pip install rltrees
```

## Quickstart

```python
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

```bash
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.
