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
Release history Release notifications | RSS feed
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.2.1.tar.gz
(17.3 kB
view details)
Built Distribution
PyMCT-1.2.1-py3-none-any.whl
(17.7 kB
view details)
File details
Details for the file PyMCT-1.2.1.tar.gz
.
File metadata
- Download URL: PyMCT-1.2.1.tar.gz
- Upload date:
- Size: 17.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d09b01ca4fcf73a91d1cd69806741782b098ec802c48df05042f0da3c22b404 |
|
MD5 | efafb2e4da982c7e32c5ce346fc936ae |
|
BLAKE2b-256 | d3f489c858ed5bec5a59123466c6f2c4f6c19b8636bb2d16ae9dd82ca145df1c |
File details
Details for the file PyMCT-1.2.1-py3-none-any.whl
.
File metadata
- Download URL: PyMCT-1.2.1-py3-none-any.whl
- Upload date:
- Size: 17.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0bfe0c54f935d8a2fbaf5ece5b0b58d43a7f8e82ae0343e4ba95cf1c6e5baf78 |
|
MD5 | 7195aadfd65956330c63a27dc6dd621b |
|
BLAKE2b-256 | 730bc837c58701db4833ef5e777dcd6c0601b02aeca29524fc065d9b64b5bef2 |