Skip to main content

A simple implement for MCTS algorithm.

Project description

PyMCT

This is a simple implement for Manto Carlo Tree Search algorithm.

Example

from cmath import inf
from random import randint, random
from PyMCT.MCT import MCTNode, MCTS
from typing import Union, List
from anytree import RenderTree

create a implement of MCTS class that has unique reward function, expand function and terminate condition.

class testMCTS(MCTS):

    def __init__(self, root: MCTNode, c: Union[int, float], max_iter: int, debug=False) -> None:
        super().__init__(root, c, max_iter, debug)

    def get_reward(self, node: MCTNode):
        node._reward = node.state

    def expand(self, node: MCTNode) -> List[MCTNode]:
        continue_chance = random()
        if continue_chance <1:
            for i in range(1,randint(1,5)):
                new_node = MCTNode(state=(self.root.depth+1)*i,parent=node)
        return node.children

    def terminate(self) -> bool:
        for node in self.root.descendants:
            if node.uct == inf:
                return False
        if self.root.height >= 3:
            return True
        else:
            return False

pass the root

test_mcts = testMCTS(root=root, c=2, debug=True, max_iter=100)

start the seacrh

test_mcts.iterate()

render the tree

print(RenderTree(test_mcts.root))

print the optimal path

test_mcts.find_optimal_path()
print(test_mcts.optimal_path)

Project details


Download files

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

Source Distribution

PyMCT-1.0.2.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

PyMCT-1.0.2-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file PyMCT-1.0.2.tar.gz.

File metadata

  • Download URL: PyMCT-1.0.2.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for PyMCT-1.0.2.tar.gz
Algorithm Hash digest
SHA256 99e3c5f81ed1c0b30ffde353c0120e4713ba0071d38e00731b956e6964029287
MD5 d90916a221207f2d7e518eaaabbabed9
BLAKE2b-256 001dfcbc1c0bdf9e8aca98c78bf20b22bc5bea8a41b6b8d2a19ea9623e082b5f

See more details on using hashes here.

File details

Details for the file PyMCT-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: PyMCT-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for PyMCT-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c7763927ebecdc4d23c92e7319d6f7d1f526132d6b6d9820b8c4afb7b48ced15
MD5 157972f598432165d83fc0292e99db6c
BLAKE2b-256 97bed44e534fa6575aa918158329849d7aac6b4c6559bf875d7d6c6feeabd80d

See more details on using hashes here.

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