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()
Metadata
Release files for PyMCT 1.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| PyMCT-1.3.0.tar.gz | 17.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| PyMCT-1.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 35.1 kB
Release files / PyMCT-1.3.0.tar.gz
| Download URL | PyMCT-1.3.0.tar.gz |
|---|---|
| Size | 17.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
be3d83ff9de5d60056d0f1fa75261ce038662059cf9824b6a9535fbb66af714c
|
|
BLAKE2b-256 checksum How to use checksums |
8394fc3b803c10bed8cab87edf16d22a75eb49d2a5cefb5b0d5cc7c40f5f41c6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.11.0
|
Release files / PyMCT-1.3.0-py3-none-any.whl
| Download URL | PyMCT-1.3.0-py3-none-any.whl |
|---|---|
| Size | 17.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1fe5a4cc0b4248cd51f3f9467f958123b67c80e1df37022f76edbf375d3f2462
|
|
BLAKE2b-256 checksum How to use checksums |
e7c854036c7e49b7391f71a231ac7f1ce92dd6a7e02cc5dd5f7b073825c2b180
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.11.0
|