0

I've just finished coding the connect 4 game on python and added the minimax algorithm to it but (I might be exaggerating only a bit) it took a million years. So, I added Alpha-Beta pruning to it. However, it is still loading and looking for the best move as I am writing this post 10 minutes later.

Could any help and try optimizing it more. Thanks in advance.

 def minimax(self, arr, a, b, depth, maximising):
    result = self.checkWin(arr)
    if result:
        d = {'R': +1, 'B': -1, 'Tie': 0}
        return d[result]
    if maximising is True:
        bestScore = -math.inf
        for i, j in self.getPossible(self.board):
            if arr[i][j] == '':
                arr[i][j] = 'R'
                score = self.minimax(arr, a, b, depth - 1, False)
                arr[i][j] = ''
                bestScore = max((score, bestScore))
                a = max((a, score))
                if b <= a:
                    break
        return bestScore
    else:
        bestScore = math.inf
        for i, j in self.getPossible(self.board):
            if arr[i][j] == '':
                arr[i][j] = 'B'
                score = self.minimax(arr, a, b, depth - 1, True)
                arr[i][j] = ''
                bestScore = min((score, bestScore))
                b = min((b, score))
                if b <= a:
                    break
        return bestScore

This is the evaluating function and i think i go all the way to the end of the minimax tree if i am not supposed to, then could someone tell me what value i should assign to non-terminal states of the tree.

  • How deep is your search? What does your evaluation function look like? – Thomas May 28 '22 at 14:58
  • What is "the connect 4 game"? – ИванКарамазов May 28 '22 at 14:59
  • @ИванКарамазов https://en.wikipedia.org/wiki/Connect_Four – Jérôme Richard May 28 '22 at 15:15
  • @Thomas is this good enough – surya narayanan May 28 '22 at 15:56
  • If you want to avoid searching all the way to the leaf nodes of the tree, you will need to come up with some heuristic evaluation function that can assign a value to a non-terminal board position. This is probably the most difficult part of developing a computer player for such a game! A quick web search turns up [this article](https://roadtolarissa.com/connect-4-ai-how-it-works/) for example. And also [this one](http://blog.gamesolver.org/) for a perfect player. – Thomas May 28 '22 at 16:31

1 Answers1

0

There are in general 3 things you can do to speed up your AI:

  1. Make it faster to find all possible moves in a given position.
  2. Make it faster to evaluate a given position.
  3. Make the minimax algorithm faster by doing clever cut offs.

First of all you need to find your bottleneck. How much time is spent finding all moves and how much is spent evaluating the position?

As of point 3 you have already implemented alpha-beta which makes it faster and also giving you the same result as without alpha-beta.

Another thing you can try is transposition tables. To simplify, you will in the table store information about the position as well as the evaluation for the position. So if you encounter the same position in the tree again you don't have to search the node and evaluate it, you just take the value from the transposition table.

You could look at chessprogramming.org for inspiration on what other pruning techniques you could apply for connect 4.

eligolf
  • 1,682
  • 1
  • 6
  • 22