Package: imparaai-montecarlo
A Python3 library for running aMonte Carlo tree search, either traditionally by drilling down to end game states or with expert policies as might be provided by a neural network.Version:1.3.1Monte Carlo tree search basicsThe Monte Carlo tree search (MCTS) algorithm can help with making a decision from a number of options. It avoids exploring every possible option by randomly sampling a small number of pathways and picking the move with the highest probability of victory. This is commonly applied to games like chess or go where it's useful to know what move should come next if you want to win the game.MCTS works by expanding the search tree to figure out which moves (or child/subsequent states) are likely to produce a positive result if chosen. While time is available, the algorithm continues to explore the tree, always slightly favoring the direction that has either proven to be fruitful or is less explored. When no time is left, the most explored direction is chosen.The search tree expansion can be done in two different ways:Traditional: At least one random rollout to a game's end state (e.g. win, loss, tie) for each move under evaluation so the algorithm can make a choice.Expert policy (i.e. neural network): Instead of expensively rolling all the way out to a game's end state ask an expert (a neural network for example) which move is most likely to produce a positive outcome.For a deeper dive into the topic, check outthis article.This libraryAs the user of this library, you only have to provide:A function that finds the direct children of each search tree node (called thechild_finder)A function for evaluating nodes for end state outcomes (called thenode_evaluator)
--(Not necessary with neural network)UsageCreate a new Monte Carlo tree:fromchessimportGamefrommontecarlo.nodeimportNodefrommontecarlo.montecarloimportMonteCarlochess_game=Game()montecarlo=MonteCarlo(Node(chess_game))The root node describes your current game state. This state will be used by you later in thechild_finderand thenode_evaluator.For the sake of demonstration, we will assume you have a genericGamelibrary that can tell you what moves are possible and allows you to perform those moves to change the game's state.Traditional Monte CarloAdd achild_finderand anode_evaluator:defchild_finder(node):formoveinnode.state.get_possible_moves():child=Node(deepcopy(node.state))#or however you want to construct the child's statechild.state.move(move)#or however your library worksnode.add_child(child)defnode_evaluator(self,node):ifnode.state.won():return1elifnode.state.lost():return-1montecarlo.child_finder=child_findermontecarlo.node_evaluator=node_evaluatorThechild_findershould add any child nodes to the parent node passed into the function, if there are any. If there are none, the parent should be in an end state, so thenode_evaluatorshould return a value between-1and1.Expert policy (AI)If you have an expert policy that you can apply to the children as they're being generated, the library will recognize that it doesn't need to make the costly drill down to an end state. If your neural net produces both an expert policy value for the children and a win value for the parent node, you can skip declaring thenode_evaluatoraltogether.defchild_finder(self,node):win_value,expert_policy_values=neural_network.predict(node.state)formoveinnode.state.get_possible_moves():child=Node(deepcopy(node.state))child.state.move(move)child.player_number=child.state.whose_turn()child.policy_value=get_child_policy_value(child,expert_policy_values)#should return a probability value between 0 and 1node.add_child(child)node.update_win_value(win_value)montecarlo.child_finder=child_finderSimulate and make a choiceRun the simulations:montecarlo.simulate(50)#number of expansions to run. higher is typically more accurate at the cost of processing timeOnce the simulations have run you can ask the instance to make a choice:chosen_child_node=montecarlo.make_choice()chosen_child_node.state.do_something()After you've chosen a new root node, you can override it on themontecarloinstance and do more simulations from the new position in the tree.montecarlo.root_node=montecarlo.make_choice()If you're training a neural network, you may want to make a more exploratory choice for the first N moves of a game:montecarlo.root_node=montecarlo.make_exploratory_choice()This won't provide a purely random choice, rather it will be random with a bias favoring the more explored pathways.Turn-based environmentsIf you are modeling a turn-based environment (e.g. a two player board game), set theplayer_numberon each node so the selection process can invert child win values:node=Node(state)node.player_number=1It doesn't matter what this number is (you can use 1 and 2 or 5 and 6), only that it is consistent with other nodes.Tweaking the discovery factorWhen building a new child node, you can change the rate at which discovery is preferred:node=Node(state)node.discovery_factor=0.2#0.35 by default, can be between 0 and 1The closer this number is to 1, the more discovery will be favored over demonstrated value in later simulations.