Skip to main content

A simple implement for MCTS algorithm.

Project description

PyMCT

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

Install

pip install --upgrade PyMCT

Example

This is a example for searching a randomly generated tree, each transcation to a new state is given with random reward.

Import moudles:

from random import randint
from PyMCT.MCT import MCTNode, MCTS, State

create the test case:

class Test:
    _root:MCTNode
    _MCTS:MCTS
    
    def __init__(self, root_state:int, c:int=2, max_iter:int=10):       
        root_state = State(root_state)
        self._root = MCTNode(state=root_state)
        #Set the serach with max iteration and max tree heights
        self._MCTS = MCTS(self.root,c=c,max_iter=max_iter, max_height=2, debug=True)
        
    #Always return a random reward. Note that the function must take one MCTNode as argument, and return a value.
    def reward_func(slef, node:MCTNode):
        return randint(0, 10)
    
    #Randomly expand the tree with new node. Note that the function must take one MCTNode as argument, and return a list of new states.
    def discover_func(self, node:MCTNode):
        new_states = list()
        for i in range(randint(1,10)):
            new_states.append(State(i))
        return new_states

    def run(self):
        #Pass in the reward and discover function, start the algorithm!
        self.MCTS.iterate(self.reward_func, self.discover_func)
        
        #Find the optimal path.
        self.MCTS.find_optimal_path()
        
        #Display the tree, Note that if no tag is given to MCTNode, a random tag will be generated and display here.
        self.MCTS.render_tree()
        
        #Print the oprimal path. This is the list of MCTNodes.
        print(self.MCTS.optimal_path)
    
    @property
    def root(self):
        return self._root
    
    @property
    def MCTS(self):
        return self._MCTS

run the test:

if __name__ == '__main__':
    test = Test(0)
    test.run()

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.1.4.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

PyMCT-1.1.4-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for PyMCT-1.1.4.tar.gz
Algorithm Hash digest
SHA256 db576649387171699117d6cdb39bc55c179979aa9790a3b87734ea063fe4d708
MD5 92b78120a74553b00570253c83e4c5dc
BLAKE2b-256 4b82403746a3f99647a76954484181ef904aaa3efbf6b4a5aed0215f99ac6a47

See more details on using hashes here.

File details

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

File metadata

  • Download URL: PyMCT-1.1.4-py3-none-any.whl
  • Upload date:
  • Size: 17.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.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a6843b43f95d6214e519609da7144fdeac5d111df2ad97c153f3be1ef1dbbdaa
MD5 1bde92234538417e7764997228a58af5
BLAKE2b-256 6f6e3f7ddf6c4e85e6ad3edf272d81e5172b14abf13ef5f312206c2e21090e38

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