npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

browser-gym

v0.0.17

Published

Physics simulations for reinforcement learning using WebAssembly and Box2D.

Downloads

3

Readme

Browser Gym

Browser Gym is a library for visualization and comparison of reinforcement learning algorithms. It gives you access to a set of standardized environments that you can control using JavaScript.

The core features of the library are

  • [x] Access to multiple environments through a unified API
  • [x] Procedurally generated environments
  • [x] Rendering of environments using WebGL
  • [x] Web worker support (and rendering communication done for you)
  • [x] Implementation of common RL data structures and logic, i.e. replay buffers and samplers
  • [x] Built on top of WebAssembly and Box2D

Environments

The library currently implements the following environments.

| Environment | Name | Agent count | Status | Example | | ------------ | ---------------- | --------------------- | :------------: | :----------------------------------------------------------------: | | Mountain car | MountainCarEnv | 1 | Fully featured | Link | | Pong | PongEnv | 2 | Fully featured | Link | | Cheetah | CheetahEnv | 1 | Reward missing | Link | | Maze | MazeEnv | 1 (eventually more) | Reward missing | Link |

Install

npm install browser-gym

Setup for web

We recommend to use Webpack for bundling. Setup Webpack using this guide. To load the WebAssembly files, it's best to use file-loader.

{
   test: /\.wasm$/i,
   type: "javascript/auto",
   use: [
      {
         loader: "file-loader",
         options: { esModule: false }
      }
   ]
}

Setup for Node

To run Browser Gym with Node.js, import it and load the backend as follows.

const gym = require("browser-gym")
gym.loadBackend().then(() => {
   console.log("Gym backend loaded 🎉")
})

Now you are ready to go.

Build

First, make sure you have emscripten installed. You will need it to compile the WebAssembly binaries. To build browser-gym run the following commands.

git submodule update --init --remote
npm install

Example

Here is a quick example of how to use browser-gym using a web worker. Rendering is facilitated for you. All you need to worry about is supplying an actor. In this example we are using the keyboard as the "actor". The following is the code for the worker script.

index.worker.js:

import * as Gym from "browser-gym"
async function main() {
   // Load WebAssembly file
   await Gym.loadBackend({ locateFile: () => require("browser-gym/backend.wasm") })
   // Create environment and worker instance
   let env = new Gym.Environment({ name: "MountainCarEnv" })
   let app = new Gym.Worker(1.0 / 50.0, env) // world operates at 50Hz
   let action = 0
   // Listen for keystrokes and set action appropriately
   app.addEventListener("keyboard", (e) => {
      if (e.code === 65 || e.code === 37) action = 100 // Left and `A` key
      if (e.code === 68 || e.code === 39) action = -100 // Right and `D` key
   })
   // Main loop
   app.run(() => {
      env.agent.action[0] = action // Update action
      app.step() // Take world step
      app.render() // Render to screen by sending to host
   })
}
console.log("Hi from worker!")
main()

To run the worker and render the environment to screen, you need to create a host which runs on the main thread. The RenderHost class helps you with this. Have a look at the following script for that.

index.js:

import { RenderHost } from "browser-gym/renderer"
import Worker from "./index.worker.js"

const renderer = new RenderHost({ worker: new Worker() }) // Create render host and worker instance
renderer.setup(document.getElementById("renderer")) // Pass in the canvas element
renderer.hydrate({
   // Start the worker
   keyPropagation: [37, 39, 65, 68], // Forward key codes for left and right keys to worker
})

And add the following html:

<div>
   <canvas id="renderer" />
</div>

For deep reinforcement learning you would likely want to create a replay buffer which stores experiences of an agent. For that we provide a Gym.ReplayBuffer class that encapsulates this logic directly in WebAssembly. Simply run the following snippet after you created an environment:

const replayBuffer = new Gym.ReplayBuffer(stateSize, actionSize, bufferCapacity)
replayBuffer.attach(env) // where _env_ is the environment instance

Now you can sample batches using the replayBuffer.sample() method and retrieve the size of the buffer using replayBuffer.size.

For more information and an example how to setup Webpack, take a look at the example folder.