DenseLance / mcts-simple

mcts-simple is a Python3 library that allows reinforcement learning problems to be solved easily with its implementations of Monte Carlo Tree Search.
MIT License
18 stars 1 forks source link

AssertionError #3

Closed vincentvason closed 1 year ago

vincentvason commented 1 year ago

Hello, Thank you for your library which I used as a part of my project now. However, I encounter an error after run and it is AssertionError in a library. Did you know what is kind of issue? and what is a solution to solve this issue? Thank you.

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[5], line 4
      1 game = SpellBot()
      3 tree = UCT(game, allow_transpositions = True, training = True)
----> 4 tree.self_play(iterations = 500)

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\mcts_simple\mcts\mcts.py:119, in MCTS.self_play(self, iterations)
    117 desc = "Training" if self.training is True else "Evaluating"
    118 for _ in tqdm(range(iterations), desc = desc):
--> 119     self.step()

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\mcts_simple\mcts\mcts.py:100, in MCTS.step(self)
     98 def step(self) -> None:
     99     if self.training is True:
--> 100         self.backpropagation(self.simulation(self.expansion(self.selection(self.root))))
    101     else:
    102         node = self.root

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\mcts_simple\mcts\uct.py:43, in UCT.expansion(self, path)
     42 def expansion(self, path: List[UCTNode]) -> List[UCTNode]:
---> 43     return super().expansion(path)

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\mcts_simple\mcts\mcts.py:71, in MCTS.expansion(self, path)
     68     expanded_game.take_action(action)
     69     path[-1].add_child(expanded_game.current_player(), str(expanded_game.get_state()), action)
---> 71 assert len(path[-1].children) > 0
     73 path[-1].is_expanded = True
     74 action = path[-1].choose_random_action()

AssertionError: 
DenseLance commented 1 year ago

This is a good question! The line assert len(path[-1].children) > 0, or any assertion line for that matter in _mctssimple, is a guide that tells you that this criteria has to be met in order for the module to run as intended. The AssertionError in this case tells you that both the selection and expansion phases were unable to find an action to take in the particular state you are currently in (i.e. game.possible_actions() = []).

I will need more information regarding your code, which might be the most likely reason why this problem has surfaced. Just note that the development and testing of this module was mainly done on Python 3.7. 🗡️

vincentvason commented 1 year ago

Thank you. I understand now and it solved. I integrated your library to a Trading Card Games (e.g. Hearthstone) which player has option to skip their turns when their resource is ran out. It seems that I need to included do nothing to possible action.

DenseLance commented 1 year ago

Yep that's right, doing nothing is also considered an action in the case of Monte Carlo simulations.