def findBestUCBNode(self, node: Node): parentVisitCount = node.getNodeMCTSInformation().getVisitCount() bestscore = 0.0 bestchildren = [] child: Node for child in node.get_children(): score = self.ucbValue(parentVisitCount, child.getNodeMCTSInformation().getWinScore(), node.getNodeMCTSInformation().getVisitCount()) if score > bestscore: bestchildren = [child] bestscore = score elif score == bestscore: bestchildren.append(child) if len(bestchildren) == 0: logging.warning("No best child found") return random.choice(bestchildren)
def simulate_round(self, node: Node): # Random walk round = get_round_from_player_round(node.getNodeMCTSInformation().getRound(), node.getNodeMCTSInformation().getRound().hands) round.action_play_card(node.getNodeMCTSInformation().getCard()) cards = round.nr_played_cards random_player = MyPlayer() while cards < 36: player_round = PlayerRoundCheating() player_round.set_from_round(round) card_action = random_player.play_card(player_round) round.action_play_card(card_action) cards += 1 myPoints = round.points_team_0 pointsEnemy = round.points_team_1 maxPoints = myPoints + pointsEnemy if myPoints > pointsEnemy: return (myPoints - 0) / (maxPoints - 0) else: return 0
def __init__(self) -> None: self._rootNode = Node()
def expand_node(self, node: Node, round: PlayerRoundCheating): valid_cards = np.flatnonzero(round.get_valid_cards()) for card in valid_cards: newNode = Node() newNode.set_parent(node) newNode.getNodeMCTSInformation().setRound(round) newNode.getNodeMCTSInformation().setPlayerNr(node.getNodeMCTSInformation().getRound().player) newNode.getNodeMCTSInformation().setCard(card) node.addChild(newNode)