Reinforcement learning library written in Rust

Overview

REnforce

Build Status

Reinforcement library written in Rust

This library is still in early stages, and the API has not yet been finalized. The documentation can be found here. Contributions and comments are welcomed.

As things are right now, the main focus has been on getting some working examples to see how the library can be used, to get a feel for how reasonable the API is, and to get more comfortable with RL. Going forward, the API still needs to be improved (made more intuitive and customizable), the code needs to be safer (less prone to panic), more RL algorithms need to be incorporated, and the documentation needs a lot of work.

Adding library to your project

Use cargo to add this library to your project. This library is not in crates.io yet, so add the following to your Cargo.toml in order to include it

[dependencies]
renforce = {git = "https://github.com/NivenT/REnforce.git"}

and remember to extern it in your project

extern crate renforce;

Example Usage

See the examples and tests folders for example usage. In particular, once an environment has been set up, an agent can be trained and tested using code similar to this

fn main() {
	// Create the environment
	let mut env = ExampleEnvironment::new();
	
	// Here, the agent will use linear value approximators for each action in a given state
	// The agent will select actions based on how high of a value it assigns them
	let q_func = QLinear::default(&env.action_space());
	
	// Creates an epsilon greedy Q-agent
	// Agent will use softmax to act randomly 5% of the time
	let mut agent = EGreedyQAgent::new(q_func, env.action_space(), 0.05, Softmax::default());
	
	// Here, we use Q-learning to train the agent
	// By default the discount factor is 0.95,
	//            the learning rate is 0.1,
	//            the trainer trains for 100 episodes when called train
	// We set the learning rate (alpha) to 0.9
	let trainer = QLearner::default(&env.action_space()).alpha(0.9);

	// Magic happens
	trainer.train(&mut agent, &mut env);

	// Simulate one episode of the environment to see what the agent learned
	let mut obs = env.reset();
	while !obs.done {
		env.render();

		let action = agent.get_action(obs.state);
		obs = env.step(action);
	}
	env.render();
}

Contributing

If you see something that could be done better, or have an idea for a feature that you want to add, then fork and sumbit a PR and/or create an issue for it. If you want to contribute, but you're not sure where to start, feel free to email me with any questions.

Progress

A lot remains to be done, but the following reinforcement learning algorithms have been implemented* thus far...

* - implementations possibly flawed, but you have to start somewhere

Comments
  • The bandit tests are flaky

    The bandit tests are flaky

    As a bare minimum for thinking a new RL algorithm was possible implemented correctly, it is given a test on the N-armed bandit problem. This environment is about as simple as RL environments get, and so every algorithm should be able to "solve" it w/o problem. This is currently not the case, as some environments (I think just CrossEntropy) do not consistently pass. More care needs to be taken in choosing hyperparameters here so tests aren't flaky.

    help wanted 
    opened by NivenT 0
  • Gym examples don't represent ideal solutions

    Gym examples don't represent ideal solutions

    I guess first off, ideally there would be an example for every training algorithm added. For now, the best way I've found to set these up seems to be to use OpenAI's gym as a source of environments.

    Unfortunately though, this library really only offers tables and linear approximations as possible value functions. These are not always ideal for gym environments, so getting working examples requires appropriate choices of features and hyperparameters in order for the agent to learn well. However, doubly unfortunately, I'm still new to RL so the current solutions are not ideal, and can likely be improved.

    For some context, you can

    • compare the performance of the cartpole agent before and after this commit: 47e8d161e58277f84cbef7ba12c50add5199620d
    • see how well the mountain car agent does before it was removed in this commit: d3beac938e42d2f454ae0e944af5932238a89347

    To sum it up, the issue here is the example solutions should be designed more carefully.

    help wanted 
    opened by NivenT 0
  • We need better error handling

    We need better error handling

    How I currently envision this working is by making a custom error type, and then having most user-facing functions return Results. I like how rusty-machine has their error type set up, so I imagine doing something similar. Something like

    use std::error::Error;
    
    struct RError {
      reason: Reason,
      err: Box<Error>
    }
    
    enum Reason {
      Unknown,
      InvalidAction,
      EnvWarning,     // issue with environment, wait a bit then try again
      EnvFatal,       // environment is dead...
      blah
    }
    

    If you're curious about the EnvWarning/Fatal reasons, see #1

    help wanted 
    opened by NivenT 0
  • The documentation is pretty poor

    The documentation is pretty poor

    Basically what the title says; there needs to be more documenting of the code. Everything is required to have some documentation or the compiler will yell at you, but the minimum is not enough. In particular, there should longish explanations of each of the modules at the top of their mod.rs files, and important/useful functions should have doc tests.

    help wanted 
    opened by NivenT 0
  • Example fails when run with --release

    Example fails when run with --release

    The cartpole example works fine when I run the debug build, but if I run with --release, there seems to be a communication problem with the gym server:

    $ cargo run cartpole --release
       Compiling renforce v0.1.0 (file:///tmp/REnforce)
        Finished release [optimized] target(s) in 3.54 secs
         Running `target/release/cartpole cartpole`
    Training...
    thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Io(Error { repr: Os { code: 99, message: "Cannot assign requested address"
    } })', /checkout/src/libcore/result.rs:860
    stack backtrace:
       0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace
                 at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
       1: std::sys_common::backtrace::_print
                 at /checkout/src/libstd/sys_common/backtrace.rs:71
       2: std::panicking::default_hook::{{closure}}
                 at /checkout/src/libstd/sys_common/backtrace.rs:60
                 at /checkout/src/libstd/panicking.rs:355
       3: std::panicking::default_hook
                 at /checkout/src/libstd/panicking.rs:371
       4: std::panicking::rust_panic_with_hook
                 at /checkout/src/libstd/panicking.rs:549
       5: std::panicking::begin_panic
                 at /checkout/src/libstd/panicking.rs:511
       6: std::panicking::begin_panic_fmt
                 at /checkout/src/libstd/panicking.rs:495
       7: rust_begin_unwind
                 at /checkout/src/libstd/panicking.rs:471
       8: core::panicking::panic_fmt
                 at /checkout/src/libcore/panicking.rs:69
       9: core::result::unwrap_failed
      10: <cartpole::CartPole as renforce::environment::Environment>::step
      11: <core::iter::Map<I, F> as core::iter::iterator::Iterator>::next
      12: cartpole::main
    13: __rust_maybe_catch_panic
                 at /checkout/src/libpanic_unwind/lib.rs:98
      14: std::rt::lang_start
                 at /checkout/src/libstd/panicking.rs:433
                 at /checkout/src/libstd/panic.rs:361
                 at /checkout/src/libstd/rt.rs:59
      15: __libc_start_main
      16: _start
    
    opened by robsmith11 4
Border is a reinforcement learning library in Rust

Border Border is a reinforcement learning library in Rust. For reusability of both RL environments and agents, this library provides a reference imple

Taku Yoshioka 1 Dec 15, 2022
NEATeRS is a library for training a genetic neural net through reinforcement learning.

NEATeRS NEATeRS is a library for training a genetic neural net through reinforcement learning. It uses the NEAT algorithm developed by Ken Stanley whi

TecTrixer 3 Nov 28, 2022
A fast, safe and easy to use reinforcement learning framework in Rust.

RSRL (api) Reinforcement learning should be fast, safe and easy to use. Overview rsrl provides generic constructs for reinforcement learning (RL) expe

Thomas Spooner 139 Dec 13, 2022
Reinforcement learning with Rust

ReLearn: A Reinforcement Learning Library A reinforcement learning library and experiment runner. Uses pytorch as the neural network backend via the t

Eric Langlois 10 Jun 14, 2022
miniature: a toy deep learning library written in Rust

miniature: a toy deep learning library written in Rust A miniature is a toy deep learning library written in Rust. The miniature is: implemented for a

Takuma Seno 4 Nov 29, 2021
A Rust library with homemade machine learning models to classify the MNIST dataset. Built in an attempt to get familiar with advanced Rust concepts.

mnist-classifier Ideas UPDATED: Finish CLI Flags Parallelize conputationally intensive functions Class-based naive bayes README Image parsing Confusio

Neil Kaushikkar 0 Sep 2, 2021
Fwumious Wabbit, fast on-line machine learning toolkit written in Rust

Fwumious Wabbit is a very fast machine learning tool built with Rust inspired by and partially compatible with Vowpal Wabbit (much love! read more abo

Outbrain 115 Dec 9, 2022
A Machine Learning Framework for High Performance written in Rust

polarlight polarlight is a machine learning framework for high performance written in Rust. Key Features TBA Quick Start TBA How To Contribute Contrib

Chris Ohk 25 Aug 23, 2022
Machine Learning library for Rust

rusty-machine This library is no longer actively maintained. The crate is currently on version 0.5.4. Read the API Documentation to learn more. And he

James Lucas 1.2k Dec 31, 2022
A deep learning library for rust

Alumina An experimental deep learning library written in pure rust. Breakage expected on each release in the short term. See mnist.rs in examples or R

zza 95 Nov 30, 2022
Machine Learning Library for Rust

autograph Machine Learning Library for Rust undergoing maintenance Features Portable accelerated compute Run SPIR-V shaders on GPU's that support Vulk

null 223 Jan 1, 2023
Mars is a rust machine learning library. [Goal is to make Simple as possible]

Mars Mars (ma-rs) is an blazingly fast rust machine learning library. Simple and Powerful! ?? ?? Contribution: Feel free to build this project. This i

KoBruh 3 Dec 25, 2022
A machine learning library in Rust from scratch.

Machine Learning in Rust Learn the Rust programming language through implementing classic machine learning algorithms. This project is self-completed

Chi Zuo 39 Jan 17, 2023
convolutions-rs is a crate that provides a fast, well-tested convolutions library for machine learning

convolutions-rs convolutions-rs is a crate that provides a fast, well-tested convolutions library for machine learning written entirely in Rust with m

null 10 Jun 28, 2022
A machine learning library for supervised training of parametrized models

Vikos Vikos is a library for supervised training of parameterized, regression, and classification models Design Goals Model representations, cost func

Blue Yonder GmbH 10 May 10, 2022
Practice repo for learning Rust. Currently going through "Rust for JavaScript Developers" course.

rust-practice ?? Practice repo for learning Rust. Directories /rust-for-js-dev Files directed towards "Rust for JavaScript Developers" course. Thank y

Sammy Samkough 0 Dec 25, 2021
A Rust machine learning framework.

Linfa linfa (Italian) / sap (English): The vital circulating fluid of a plant. linfa aims to provide a comprehensive toolkit to build Machine Learning

Rust-ML 2.2k Jan 2, 2023
Machine learning crate for Rust

rustlearn A machine learning package for Rust. For full usage details, see the API documentation. Introduction This crate contains reasonably effectiv

Maciej Kula 547 Dec 28, 2022
Machine learning in Rust.

Rustml Rustml is a library for doing machine learning in Rust. The documentation of the project with a descprition of the modules can be found here. F

null 60 Dec 15, 2022