Example #1
0
 def __init__(self,
              color="black",
              time_limit=5,
              gui=None,
              headless=False,
              strategy=OthelloHeuristic.DEFAULT_STRATEGY):
     super(ComputerPlayer, self).__init__(color, time_limit, gui, headless)
     heuristic = OthelloHeuristic(strategy)
     self.ai = GameArtificialIntelligence(heuristic.evaluate)
Example #2
0
class ComputerPlayer(Player):

    name = "ComputerPlayer"

    def __init__(self,
                 color="black",
                 time_limit=5,
                 gui=None,
                 headless=False,
                 strategy=OthelloHeuristic.DEFAULT_STRATEGY):
        super(ComputerPlayer, self).__init__(color, time_limit, gui, headless)
        heuristic = OthelloHeuristic(strategy)
        self.ai = GameArtificialIntelligence(heuristic.evaluate)

    # Is Random For Now
    def get_move(self):
        import datetime
        import sys
        other_color = BLACK
        if self.color == BLACK:
            other_color = WHITE
        start = datetime.datetime.now()
        move = self.ai.move_search(self.current_board, self.time_limit,
                                   self.color, other_color)
        delta = datetime.datetime.now() - start
        # print >> sys.stderr, "Time taken:", delta
        self.apply_move(move)
        return self.current_board
Example #3
0
    def __init__(self, color="black", time_limit=5, gui=None, headless=False, epochs=5, batch_size=100):
        super(DeepLearningPlayer, self).__init__(color, time_limit, gui, headless)
        self.model = Net()
        self.ai = GameArtificialIntelligence(self.evaluate_board);

        if torch.cuda.is_available():
            self.model.cuda(0)
            print "CUDA activated"

        # print(self.model)

        try:
            self.model = DataHandler.load_weights(self.name)
        except Exception:
            if epochs != 0:
                self.train_model(epochs=epochs, batch_size=batch_size)
Example #4
0
class ComputerPlayer(Player):

    def __init__(self, color="black", time_limit=5, gui=None):
        super(ComputerPlayer, self).__init__(color, time_limit, gui)
        heuristic = OthelloHeuristic()
        self.ai = GameArtificialIntelligence(heuristic.evaluate)

    # Is Random For Now
    def get_move(self):
        import datetime
        import sys
        other_color = BLACK
        if self.color == BLACK:
            other_color = WHITE
        start = datetime.datetime.now()
        move = self.ai.move_search(self.current_board, self.time_limit, self.color, other_color)
        delta = datetime.datetime.now() - start
        print >> sys.stderr, "Time taken:", delta
        self.apply_move(move)
        return self.current_board
Example #5
0
 def __init__(self, color="black", time_limit=5, gui=None):
     super(ComputerPlayer, self).__init__(color, time_limit, gui)
     heuristic = OthelloHeuristic()
     self.ai = GameArtificialIntelligence(heuristic.evaluate)
Example #6
0
class DeepLearningPlayer(Player):

    name = "DeepLearningPlayer"

    def __init__(self, color="black", time_limit=5, gui=None, headless=False, epochs=5, batch_size=100):
        super(DeepLearningPlayer, self).__init__(color, time_limit, gui, headless)
        self.model = Net()
        self.ai = GameArtificialIntelligence(self.evaluate_board);

        if torch.cuda.is_available():
            self.model.cuda(0)
            print "CUDA activated"

        # print(self.model)

        try:
            self.model = DataHandler.load_weights(self.name)
        except Exception:
            if epochs != 0:
                self.train_model(epochs=epochs, batch_size=batch_size)

    def train_model(self, epochs=10, batch_size=100, continue_training=False):
        losses = self.model.train_model(epochs=epochs, batch_size=batch_size, continue_training=continue_training)
        DataHandler.store_weights(player_name=self.name, model=self.model)
        return losses

    def train_model_on_curriculum(self, epochs_per_stage=1, final_epoch=30, continue_training=False):
        final_epoch = min(final_epoch, 30)
        losses = self.model.train_model_on_curriculum(epochs_per_stage=epochs_per_stage, final_epoch=final_epoch, continue_training=continue_training)
        DataHandler.store_weights(player_name=self.name + "_curriculum", model=self.model)
        return losses

    def evaluate_board(self, board, color, other_player):
        sample = FloatTensor([[board.get_representation(color)]])

        if torch.cuda.is_available():
            sample = sample.cuda(0)
        sample = Variable(sample)

        return self.model(sample)

    def get_move(self):
        # return self.get_move_alpha_beta()
        moves = self.current_board.get_valid_moves(self.color)

        # predict value for each possible move
        predictions = [(self.__predict_move__(move), move) for move in moves]

        # print "Chose move with prediction [%s]" % max(predictions)[0]
        self.apply_move(max(predictions)[1])
        return self.current_board

    def get_move_alpha_beta(self):
        move = self.ai.move_search(self.current_board, self.time_limit, self.color, (self.color % 2) + 1)
        self.apply_move(move)
        return self.current_board

    def __predict_move__(self, move):
        board = deepcopy(self.current_board)
        board.apply_move(move, self.color)

        sample = FloatTensor([[board.get_representation(self.color)]])
        if torch.cuda.is_available():
            sample = sample.cuda(0)
        sample = Variable(sample)

        return self.model(sample)