Skip to main content

A simple package to allow users to run Monte Carlo Tree Search on any perfect information domain

Project description

MCTS

This package provides a simple way of using Monte Carlo Tree Search in any perfect information domain.

It was originally authored by pbsinclair42. This fork however complies with the Python Naming Convention, provides base classes for implementing states and actions, and includes more comprehensive examples.

Installation

With pip: pip install monte-carlo-tree-search

Without pip: Download the zip/tar.gz file of the latest release, extract it, and run python setup.py install

Quick Usage

In order to run MCTS, you must implement your own State class that extends mcts.base.base.BaseState which can fully describe the state of the world. It must implement four methods:

  • get_current_player(): Returns 1 if it is the maximizer player's turn to choose an action, or -1 for the minimiser player
  • get_possible_actions(): Returns an iterable of all actions which can be taken from this state
  • take_action(action): Returns the state which results from taking action action
  • is_terminal(): Returns True if this state is a terminal state
  • get_reward(): Returns the reward for this state. Only needed for terminal states.

You must also choose a hashable representation for an action as used in get_possible_actions and take_action. Typically, this would be a class with a custom __hash__ method, but it could also simply be a tuple, a string, etc. A BaseAction class is provided for this purpose.

Once these have been implemented, running MCTS is as simple as initializing your starting state, then running:

from mcts.base.base import BaseState
from mcts.searcher.mcts import MCTS


class MyState(BaseState):
    """
    TODO: Implement your state
    """


initial_state = MyState()

searcher = MCTS(time_limit=1000)
bestAction = searcher.search(initial_state=initial_state)

Here the unit of time_limit=1000 is milliseconds. You can also use for example iteration_limit=100 to specify the number of rollouts. Exactly one of time_limit and iteration_limit should be specified.

best_action = searcher.search(initial_state=initial_state)
print(best_action)  # the best action to take found within the time limit

To also receive the best reward as a return value set need_details to True in searcher.search(...).

best_action, reward = searcher.search(initial_state=initial_state, need_details=True)
print(best_action)  # the best action to take found within the time limit
print(reward)  # the expected reward for the best action

Examples

You can find some examples using the MCTS here:

Collaborating

Feel free to raise a new issue for any new feature or bug you've spotted. Pull requests are also welcomed if you're interested in directly improving the project.

Coding Guidelines

Commit message should follow the Conventional Commits specification. This makes contributions easily comprehensible and enables us to automatically generate release notes.

Recommended tooling for developers:

Example commit message

fix: prevent racing of requests

Introduce a request id and a reference to latest request. Dismiss
incoming responses other than from latest request.

Remove timeouts which were used to mitigate the racing issue but are
obsolete now.

Reviewed-by: Z
Refs: #123

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

monte_carlo_tree_search-2.0.3-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file monte_carlo_tree_search-2.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for monte_carlo_tree_search-2.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2b682996a797947589c26437ed09d82a7d61dda1d15b6e8c6e01deede6a93cb2
MD5 2e31a7b9f0cba1ee1f772e947ccf024b
BLAKE2b-256 00e8c1148542255a5c1b95f8c11c6dde792aca3e34e85aed930704d1e6b7b9c4

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page