Git Product home page Git Product logo

python_mcts's Introduction

Monte Carlo Tree Search (MCTS) in Python

This project offers a Python module with an extensible implementation of MCTS (Monte Carlo Tree Search).

Description

This MCTS implementation can be used to find the best action for a single-player problem or a multi-player competitive turn-based game with discrete actions.

Typical examples are two-player board games such as: checkers or chess. Ot other 2-player games such as connect-4 or tic-tac-toe. Games with more than 2 players are also supported.

Features

  • Supports from 1 to multiple players
  • Automatic random rollout policy
  • Rollout policy can be replaced by custom implementation

Using

To use this module:

  1. Copy "py_mcts" folder to your project's folder.
  2. In your code, import py_mcts. You will use ProblemState and MCTS classes.
  3. Define a new class to represent your problem state, as a subclass of ProblemState. Implement these methods:
    • get_player(self) -> in non-terminal states, returns the next player to move (use any data type to identify the players, players will be compared with==); in terminal states, should return None
    • get_valid_actions(self) -> return a list or any iterable object of actions (each represented by any type you want)
    • is_terminal(self) -> returns boolean to indicate if it is a terminal state
    • game_result(self, player) -> used in terminal states, indicates the score for any given player
    • move(self, action) -> call this method to indicate that get_player() has performed the given action; should return a new "state" (instance of the same class)
  4. Instantiate you MCTS solver. In the constructor:
    • you may set a custom rollout_policy if you want (default: random policy)
    • you may also set the c parameter of the tree policy (default: 1.41)
  5. Then, whenever you need to find a "good" (quasi-optimal) action in a given state of your game:
    • instantiate your class with the current state information
    • in your MCTS solver call choose_action(state, duration) with the current state and with the desired duration in seconds
    • it will run for the desired time, then it will return the recommended action!

Example

This project includes an example of the implementation of an AI for tic-tac-toe, which you can play against.

python_mcts's People

Contributors

pablo-sampaio avatar

Watchers

 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.