def choose_move(self, backgammon): """ Chooses a random board from all possible boards. """ # get a list of all possible moves all_boards = BoardFactory.generate_all_boards(backgammon.current_player,\ backgammon.dice, backgammon.board) # pick a random move random_board = random.choice(all_boards) return random_board
def choose_move(self, backgammon): """ Function passes the list of all possible boards to the net and evaluates them. Training can be disabled by the Player class parameter - 'learning_mode'. In this case the net only predicts the chances of winning for a board. """ best_board = None # the value representing the expected contribution of a distinct # board to win the game expected_utility = -1.0 # next_out is the network output of the selected new board next_output = [] # get all possible boards from BoardFactory all_boards = BoardFactory.generate_all_boards(backgammon.current_player, \ backgammon.dice, \ backgammon.board) # loop over all boards for board in all_boards: board_vector = self.board_to_vector(board) output = self.neural_network.get_network_output(board_vector) # translate network output into an actual meaning for player # eg if output [0.1, 0.3], white odds of winning are lower # than black's odds utility = self.compute_utility(output) if utility > expected_utility: best_board = board expected_utility = utility next_output = output # learning_mode indicates whether the network propagates back errors # or only evaluates boards if self.learning_mode: current_input = self.board_to_vector(backgammon.board) # pass the current board (board before this move is about to happen) # to the network current_output = self.neural_network.get_network_output(current_input) self.neural_network.back_prop(current_output, next_output) return best_board