Example #1
0
def mcts(problem, iters, useSaved):
    """
    Takes a 2-player game and uses Monte Carlo Tree Search to calculate and return
    a strategy for the game.

    Adapted from
    https://www.analyticsvidhya.com/blog/2019/01/monte-carlo-tree-search-introduction-algorithm-deepmind-alphago/
    """

    # Settings
    # iters = 1000
    # useSaved = True

    if problem.isTerminal():
        return (0, problem.finalScore())

    savestate = problem.getState()

    if useSaved and problem.getState() in nodes:
        tree = nodes[problem.getState()]
    else:
        tree = StateNode(problem, None)

    tree.expand()

    for i in range(iters):
        leaf = expand(tree)
        score = rollout(problem, leaf.getState())
        backprop(leaf, score)

    problem.setState(savestate)
    return tree.nextMove()
Example #2
0
	def __init__( self, name, parent ):
		StateNode.__init__( self, name, parent, initialState = self.STATE_ACTIVE )
		self.log = Logger().get_instance( self.__class__.__name__ )

		self.authentication = AuthenticationAdapter( 'auth_adapter', self )
		self.authentication.addListener( self )
		self.authentication = None #FIXME:

		self.transactions = {}
		self.dialogs = {}
Example #3
0
	def __init__( self, name, parent ):
		StateNode.__init__( self, name, parent, initialState = System.STATE_STOPPED )
		self.log = Logger().get_instance( self.__class__.__name__ )

		self.config = Configuration( 'config', self )
		self.config.addListener( self )

		self.network = MessageAdapter( 'msg_net', self )
		self.network.addListener( self )

		self.resources = Resources( 'resources', self )
		self.resources.addListener( self )

		self.addListener( parent )
Example #4
0
def main():
    a = StateNode("You did action A!", AsciiImages.gameover())
    b = StateNode("You did action B!", "")
    c = StateNode("blah", "")
    c.setConnectingStatesAndTheirActions([a, b], ["do a", "do b"])

    textPrintSpeed = 0.05
    imagePrintSpeed = 0.1
    game = GameState(c, textPrintSpeed, imagePrintSpeed)
    while (not game.isOver()):
        game.printCurrentNode()
        actionNumber = game.getActionNumber(game.currentNode)
        game.changeStateDueToAction(actionNumber)
    game.printCurrentNode()
Example #5
0
	def __init__( self, name, parent = None ):
		StateNode.__init__( self, name, parent )
Example #6
0
	def __init__( self, name, parent ):
		StateNode.__init__( self, name, parent, initialState = self.STATE_ACTIVE )
		self.log = Logger().get_instance( self.__class__.__name__ )

		self.users = {}