Code Monkey home page Code Monkey logo

mcts-general's Introduction

Monte Carlo Tree Search for OpenAI gym framework

General Python implementation of Monte Carlo Tree Search for the use with Open AI Gym environments.

The MCTS Algorithm is based on the one from muzero-general which is forked from here.

This code was part of my Bachelor Thesis:

The source code of the experiments covered by the thesis can be found here.

Dependencies

Python 3.8 is used. Dependencies are mainly numpy and gym. Simply run:

pip install -r requirements.txt

How to use

This implementation follows the common agent-environment scheme. The environment is Wrapped by the Game class defined, in game.py, which ensures that the game's state can be deep copied. The main Game implementations for usage with OpenAI gym environments are DiscreteGymGame and ContinuousGymGame.

A simple example would be:

import gym
from mcts_general.agent import MCTSAgent
from mcts_general.config import MCTSAgentConfig
from mcts_general.game import DiscreteGymGame


# configure agent
config = MCTSAgentConfig()
config.num_simulations = 200
agent = MCTSAgent(config)

# init game
game = DiscreteGymGame(env=gym.make('CartPole-v0'))
state = game.reset()
done = False
reward = 0

# run a trajectory
while not done:
    action = agent.step(game, state, reward, done)
    state, reward, done = game.step(action)
    
    # game.render()     # uncomment for environment rendering

game.close()

A continuous environment can be set up similarly. Note that you have to choose mu and sigma for (Gaussian Normal) sampling actions. Usually it is a good choice to start with mu being at the middle of your action space and sigma being half your action space. So for example for Pendulum-v0 the action space is in [-2 ,2] hence a good choice to start with is mu = 0. and sigma = 2.

Example for Continuous Control:

import gym
from mcts_general.agent import ContinuousMCTSAgent
from mcts_general.config import MCTSContinuousAgentConfig
from mcts_general.game import ContinuousGymGame


# configure agent
config = MCTSContinuousAgentConfig()
agent = ContinuousMCTSAgent(config)

# init game
game = ContinuousGymGame(env=gym.make('Pendulum-v0'), mu=0., sigma=2.)
state = game.reset()
done = False
reward = 0

while not done:
    action = agent.step(game, state, reward, done)
    state, reward, done = game.step(action)
    game.render()

game.close()

Features

Please have a look at the game package for using different time-discretization during planning, and what hyper parameters can be chosen in the config class. You might also find some useful gym.Wrappers in common/wrapper.py. An extensive example on how to use this implementation for MCTS-research can be found in the thesis experiments.

Bugs and Colab

If you have any questions regarding this code or want to contribute mail me at:

[email protected]

mcts-general's People

Contributors

patrickkorus avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.