Exemple #1
0
 def choose_move(self, state: CatanState):
     self.scores_by_player = state.get_scores_by_player_indexed()
     next_moves = state.get_next_moves()
     if len(next_moves) <= 1:
         return next_moves[0]
     if state.is_initialisation_phase():
         best_move, best_score = None, 0
         for move in next_moves:
             state.make_move(move)
             score = self.initialization_phase_heuaristic(state)
             state.unmake_move(move)
             if score > best_score:
                 best_move = move
                 best_score = score
         return best_move
     mcts = MCTS(MCTSNode(state), next_moves, self.exploration_param)
     mcts.do_n_rollouts(self.iterations)
     return mcts.choose().move
Exemple #2
0
    def winning_heuristic(self, state: CatanState):
        """
        our heuristic for winning the game! it is composed of 3 different heuristics.
        :param state: the state of the game.
        :return: a heuristic score for this state.
        """
        self.scores_by_player = state.get_scores_by_player_indexed()
        my_score = self.scores_by_player[self.get_id()]
        if my_score >= 10:
            return inf
        max_score = max(self.scores_by_player)
        if max_score >= 10:
            return -inf
        if state.is_initialisation_phase():
            return self.heuristic_initialisation_phase(state)
        if self.in_first_phase():
            return self.heuristic_first_phase(state,
                                              self.winner_first_phase_weights)

        return self.heuristic_final_phase(state,
                                          self.winner_last_phase_weights)
 def default_heuristic(self, state: CatanState):
     if state.is_initialisation_phase():
         return self._random_choice([i for i in range(10)])
     # as discussed with Shaul, this isn't zero-sum heuristic, but a max-gain approach where only own player's
     # value is is taken in account
     return float(state.get_scores_by_player()[self])