Skip to main content

ReplayBuffer for Reinforcement Learning written by C++ and Cython

Project description

img img img img img

img

Overview

cpprb is a python (CPython) module providing replay buffer classes for reinforcement learning.

Major target users are researchers and library developers.

You can build your own reinforcement learning algorithms together with your favorite deep learning library (e.g. TensorFlow, PyTorch).

cpprb forcuses speed, flexibility, and memory efficiency.

By utilizing Cython, complicated calculations (e.g. segment tree for prioritized experience replay) are offloaded onto C++. (The name cpprb comes from "C++ Replay Buffer".)

In terms of API, initially cpprb referred to OpenAI Baselines' implementation. In the current version, cpprb has much more flexibility. Any NumPy compatible types of any numbers of values can be stored (as long as memory capacity is sufficient). For example, you can store the next action and the next next observation, too.

Installation

cpprb requires following softwares before installation.

  • C++17 compiler (for installation from source)
  • Python 3
  • pip

Cuurently, clang, which is a default Xcode C/C++ compiler at Apple macOS, cannot compile cpprb.

If you are macOS user, you need to install GCC and set environment values of CC and CXX to g++, or just use virtual environment (e.g. Docker).

Step by step installation is described here.

Additionally, here are user's good feedbacks for installation at macOS and Ubuntu. (Thanks!)

Install from PyPI (Recommended)

The following command installs cpprb together with other dependancies.

pip install cpprb

Depending on your environment, you might need sudo or --user flag for installation.

On supported platflorms (Linux x86-64 and Windows amd64), binary packages are hosted on PyPI can be used, so that you don't need C++ compiler.

If you have trouble to install from binary, you can fall back to source installation to passk --no-binary option to the above pip command.

Currently, no other platforms, such as macOS, and 32bit or arm-architectured Linux and Windows, cannot install from binary, and need to compile by yourself. Please be patient, we will plan to support wider platforms in future.

Install from source code

First, download source code manually or clone the repository;

git clone https://gitlab.com/ymd_h/cpprb.git

Then you can install same way;

cd cpprb
pip install .

For this installation, you need to convert extended Python (.pyx) to C++ (.cpp) during installation, it takes longer time than installation from PyPI.

Usage

Basic Usage

Basic usage is following step;

  1. Create replay buffer (ReplayBuffer.__init__)
  2. Add transitions (ReplayBuffer.add)
    1. Reset at episode end (ReplayBuffer.on_episode_end)
  3. Sample transitions (ReplayBuffer.sample)

Example Code

Here is a simple example for storing standard environment (aka. "obs", "act", "rew", "next_obs", and "done").

from cpprb import ReplayBuffer

buffer_size = 256
obs_shape = 3
act_dim = 1
rb = ReplayBuffer(buffer_size,
		  env_dict ={"obs": {"shape": obs_shape},
			     "act": {"shape": act_dim},
			     "rew": {},
			     "next_obs": {"shape": obs_shape},
			     "done": {}})

obs = np.ones(shape=(obs_shape))
act = np.ones(shape=(act_dim))
rew = 0
next_obs = np.ones(shape=(obs_shape))
done = 0

for i in range(500):
    rb.add(obs=obs,act=act,rew=rew,next_obs=next_obs,done=done)

    if done:
	# Together with resetting environment, call ReplayBuffer.on_episode_end()
	rb.on_episode_end()

batch_size = 32
sample = rb.sample(batch_size)
# sample is a dictionary whose keys are 'obs', 'act', 'rew', 'next_obs', and 'done'

Construction Parameters

(See also API reference)

Name Type Optional Discription
`size` `int` No Buffer size
`env_dict` `dict` Yes (but unusable) Environment definition (See [here](https://ymd_h.gitlab.io/cpprb/features/flexible_environment/))
`next_of` `str` or array-like of `str` Yes Memory compression (See [here](https://ymd_h.gitlab.io/cpprb/features/memory_compression/))
`stack_compress` `str` or array-like of `str` Yes Memory compression (See [here](https://ymd_h.gitlab.io/cpprb/features/memory_compression/))
`default_dtype` `numpy.dtype` Yes Fall back data type
`Nstep` `dict` Yes Nstep configuration (See [here](https://ymd_h.gitlab.io/cpprb/features/nstep/))
`mmap_prefix` `str` Yes mmap file prefix (See [here](https://ymd_h.gitlab.io/cpprb/features/mmap/))

Notes

Flexible environment values are defined by env_dict when buffer creation. The detail is described at document.

Since stored values have flexible name, you have to pass to ReplayBuffer.add member by keyword.

Features

cpprb provides buffer classes for building following algorithms.

Algorithms cpprb class Paper
Experience Replay `ReplayBuffer` [L. J. Lin](https://link.springer.com/article/10.1007/BF00992699)
[Prioritized Experience Replay](https://ymd_h.gitlab.io/cpprb/features/per/) `PrioritizedReplayBuffer` [T. Schaul et. al.](https://arxiv.org/abs/1511.05952)
[Multi-step (Nstep) Learning](https://ymd_h.gitlab.io/cpprb/features/nstep/) `ReplayBuffer`, `PrioritizedReplayBuffer`  

cpprb features and its usage are described at following pages:

Contributing to cpprb

Any contribution are very welcome!

Making Community Larger

Bigger commumity makes development more active and improve cpprb.

  • Star this repository (and/or GitHub Mirror)
  • Publish your code using cpprb
  • Share this repository to your friend and/or followers.

Report Issue

When you have any problems or requests, you can check issues on GitLab.com. If you still cannot find any information, you can open your own issue.

Merge Request (Pull Request)

cpprb follows local rules:

  • Branch Name
    • "HotFix***" for bug fix
    • "Feature***" for new feature implementation
  • docstring
  • Unit Test
    • Put test code under "test/" directory
    • Can test by python -m unittest <Your Test Code> command
    • Continuous Integration on GitLab CI configured by .gitlab-ci.yaml
  • Open an issue and associate it to Merge Request

Step by step instruction for beginners is described at here.

Links

cpprb sites

cpprb users' repositories

Lisence

cpprb is available under MIT lisence.

MIT License

Copyright (c) 2020 Yamada Hiroyuki

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 details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

cpprb-9.3.0.tar.gz (331.9 kB view details)

Uploaded Source

Built Distributions

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

cpprb-9.3.0-cp38-cp38-win_amd64.whl (245.6 kB view details)

Uploaded CPython 3.8Windows x86-64

cpprb-9.3.0-cp38-cp38-manylinux2010_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.12+ x86-64

cpprb-9.3.0-cp38-cp38-manylinux1_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.8

cpprb-9.3.0-cp37-cp37m-win_amd64.whl (237.9 kB view details)

Uploaded CPython 3.7mWindows x86-64

cpprb-9.3.0-cp37-cp37m-manylinux2010_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.12+ x86-64

cpprb-9.3.0-cp37-cp37m-manylinux1_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.7m

cpprb-9.3.0-cp36-cp36m-win_amd64.whl (237.6 kB view details)

Uploaded CPython 3.6mWindows x86-64

cpprb-9.3.0-cp36-cp36m-manylinux2010_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.6mmanylinux: glibc 2.12+ x86-64

cpprb-9.3.0-cp36-cp36m-manylinux1_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.6m

cpprb-9.3.0-cp35-cp35m-manylinux2010_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.5mmanylinux: glibc 2.12+ x86-64

cpprb-9.3.0-cp35-cp35m-manylinux1_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.5m

File details

Details for the file cpprb-9.3.0.tar.gz.

File metadata

  • Download URL: cpprb-9.3.0.tar.gz
  • Upload date:
  • Size: 331.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for cpprb-9.3.0.tar.gz
Algorithm Hash digest
SHA256 172448f4139e66fda994cbe005f2c6febb0bd81188c09230215b9fef4a9f4a9e
MD5 e6e6ef244dc973504a3c14e24a8f5da9
BLAKE2b-256 0f3cb3478c897de99e5067369543984e17dbb96ddb3905ccf275b8e6663fafd2

See more details on using hashes here.

File details

Details for the file cpprb-9.3.0-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: cpprb-9.3.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 245.6 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for cpprb-9.3.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 7d2845f41f53d6253d08aea36ba3adb8ed66b17b097e2f9a2f76fe28587d5e01
MD5 f2a7bbb54bc7f0b0ea198bbb779ad95b
BLAKE2b-256 a07e0485da0a122ee3a862b591db4d81dcaa6902e34121272151093dd0b8247f

See more details on using hashes here.

File details

Details for the file cpprb-9.3.0-cp38-cp38-manylinux2010_x86_64.whl.

File metadata

  • Download URL: cpprb-9.3.0-cp38-cp38-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: CPython 3.8, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for cpprb-9.3.0-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 736d71683f7e7131e4ade2a2c85d56127d52b6ee7c23af3d6e65cd9cb03fb160
MD5 3f714bdec261bf029d0dccfe1e668c8f
BLAKE2b-256 b7d6e3be058b7a13d093a165335d9a1ac0e2aa40f6f02eac4dc010141716f626

See more details on using hashes here.

File details

Details for the file cpprb-9.3.0-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: cpprb-9.3.0-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for cpprb-9.3.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b18d2dca9db1cdf2bc663b76c4755058a6d0e55497deae7f76167a58a67223c7
MD5 10714580383f20d920938c00717797a8
BLAKE2b-256 4e85d75eeb526c0cc2695dd8a3c5ac1cee332d51ae3f982726ecc38f82709921

See more details on using hashes here.

File details

Details for the file cpprb-9.3.0-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: cpprb-9.3.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 237.9 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for cpprb-9.3.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 4e168248dfbdbdb6a28670f2caff3b4efbdc77c88276d7146dc0bd3e1ed94955
MD5 b9293987e16642c0d68155d9dbd72eae
BLAKE2b-256 68749b6b1d93897bb852cdb1f52f79251d5bab4f64a6ad04fc7d748640593143

See more details on using hashes here.

File details

Details for the file cpprb-9.3.0-cp37-cp37m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: cpprb-9.3.0-cp37-cp37m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.7m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for cpprb-9.3.0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 ff2dcdeceb85264cc9862156e7e4f1473e276be11fef405822a74ad295bd77a4
MD5 bd1b915abea839f2139fdfe205d5be74
BLAKE2b-256 0121d54633112c2425940f99fd0010a252081ab7535761fc1038f22ce1b53b26

See more details on using hashes here.

File details

Details for the file cpprb-9.3.0-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: cpprb-9.3.0-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for cpprb-9.3.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f356b34f0bec4aa4976c452facc76399b9d5ca5e609f4593e123970af5d40900
MD5 6e19df19554dc2685ff77aec16a2c1e3
BLAKE2b-256 6057ad53781aacfeb8706b6a46e877fca55fa37ed438acc59e19bf516e7fde18

See more details on using hashes here.

File details

Details for the file cpprb-9.3.0-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: cpprb-9.3.0-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 237.6 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.8

File hashes

Hashes for cpprb-9.3.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 14a15d850db2d49db14dae69bf142221994c6691ef8edd1729a94ccdb02bc62b
MD5 847d826f317a35f2a1e73fde172352a0
BLAKE2b-256 fccb12cd46431b055f359ab450709bc2dac84eb0b1969b95354c774993302668

See more details on using hashes here.

File details

Details for the file cpprb-9.3.0-cp36-cp36m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: cpprb-9.3.0-cp36-cp36m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.6m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for cpprb-9.3.0-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 5f32db41b01b1c15cdf334da83782b0da1788e124b7a19e403bc2e7382963036
MD5 ed3743475360d43c8accf5d3895351f2
BLAKE2b-256 c46dd16582178ec97cc7526d39db1f6fd7d966c6205f9e2205a7c72efbe768b7

See more details on using hashes here.

File details

Details for the file cpprb-9.3.0-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: cpprb-9.3.0-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for cpprb-9.3.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a5ea1f282e5f76959f646d3585ba82f2ae804b9cc8ff5d79fd57ba76629ddd37
MD5 dd39018f637d3d3b6e952a195266a4c8
BLAKE2b-256 91c51a8721b7d7def6c467d205d0a6c1304e928226185842ac954c9c4adb674a

See more details on using hashes here.

File details

Details for the file cpprb-9.3.0-cp35-cp35m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: cpprb-9.3.0-cp35-cp35m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: CPython 3.5m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for cpprb-9.3.0-cp35-cp35m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 8c66193d54095779d437f821811d52932d68cf2ff0a898b7107022d07601371c
MD5 14bf2a9a33f02ff925f90b69ec91a1b3
BLAKE2b-256 e28672ae45665ba0405c630c3e1ae1ce99c4a1affd7caa2de418d321aeef9489

See more details on using hashes here.

File details

Details for the file cpprb-9.3.0-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

  • Download URL: cpprb-9.3.0-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for cpprb-9.3.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c0fd2acd9d41044b565f40f445ce92b32fef4c1fff7d063165892d8eb801d3e3
MD5 e6204e97298cd77b01782ec9c6be295b
BLAKE2b-256 e770003719833139d04b264aa917a7e22edea0849c1ae8e9c83f57347266db38

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