def isInitialState(self): """Returns True if this is the first play of the game, i.e., all playedCards entries are empty. :returns: True if it's the first play of the game, False otherwise. """ return all(cards.empty(played) for played in self.playedCards)
def simulation(self, node): # if agent's hand is empty return score if cards.empty(node.hands[self.idx]): return node.score / node.visits else: count = 0 for i in range(node.numPlayers): if cards.empty(node.hands[i]): count += 1 if count == node.numPlayers - 1: return (node.numPlayers + 1)**-1 agents = [dummyAgent.DummyAgent for i in xrange(node.numPlayers)] gm = game.Game(agents, node.hands, node.playedCards, node.whosTurn, node.topCard, node.lastPlayed, node.finished) results = gm.playGame() return ((results.index(self.idx) + 1)**-1) / node.visits
def __init__(self, playedCards, whosTurn, hands, idx, lastMove, depth, topCard=None, lastPlayed=None, finished=[], parent=None, score=0.): self.lastMove = lastMove self.depth = depth self.visits = 1. self.score = score self.children = [] self.parent = parent self.hands = hands self.idx = idx self.terminal = cards.empty(hands[self.idx]) self.turn = (self.idx == whosTurn) super(mctsNode, self).__init__(playedCards, whosTurn, topCard, lastPlayed, finished)
def simulation(self, node): # if agent's hand is empty return score if cards.empty(node.hands[self.idx]): return node.score / node.visits else: count = 0 for i in range(node.numPlayers): if cards.empty(node.hands[i]): count += 1 if count == node.numPlayers - 1: return (node.numPlayers + 1) ** -1 agents = [dummyAgent.DummyAgent for i in xrange(node.numPlayers)] gm = game.Game(agents, node.hands, node.playedCards, node.whosTurn, node.topCard, node.lastPlayed, node.finished) results = gm.playGame() return ((results.index(self.idx) + 1) ** -1) / node.visits
def __init__(self, playedCards, whosTurn, hands, idx, lastMove, depth, topCard=None, lastPlayed=None, finished=[], parent = None, score=0.): self.lastMove = lastMove self.depth = depth self.visits = 1. self.score = score self.children = [] self.parent = parent self.hands = hands self.idx = idx self.terminal = cards.empty(hands[self.idx]) self.turn = (self.idx == whosTurn) super(mctsNode, self).__init__(playedCards, whosTurn, topCard, lastPlayed, finished)
def selection(self, root): numDone = len(root.finished) if root.children == []: # player to play has no cards, but game isn't finished if (not root.turn) and cards.empty(root.hands[root.whosTurn]) and root.numPlayers > numDone: root.addChild(agent.PASS) return self.selection(root.children[0]) elif (root.turn and root.terminal) or root.isFinalState(): return root else: curr_state = state.State(root.playedCards, root.whosTurn, root.topCard, root.lastPlayed, root.finished) testagent = agent.Agent(root.whosTurn, root.hands[root.whosTurn]) actions = testagent.getAllActions(curr_state) root.addAllChildren(actions) selected_child = random.choice(root.children) return selected_child else: return self.selection(self.bestChild(root.children))
def selection(self, root): numDone = len(root.finished) if root.children == []: # player to play has no cards, but game isn't finished if (not root.turn) and cards.empty( root.hands[root.whosTurn]) and root.numPlayers > numDone: root.addChild(agent.PASS) return self.selection(root.children[0]) elif (root.turn and root.terminal) or root.isFinalState(): return root else: curr_state = state.State(root.playedCards, root.whosTurn, root.topCard, root.lastPlayed, root.finished) testagent = agent.Agent(root.whosTurn, root.hands[root.whosTurn]) actions = testagent.getAllActions(curr_state) root.addAllChildren(actions) selected_child = random.choice(root.children) return selected_child else: return self.selection(self.bestChild(root.children))