Minimax algorithm with memoization?
Asked Answered
A

1

6

I am trying to implement a connect four AI using the minimax algorithm in javascript. Currently, it is very slow. Other than alpha-beta pruning which I will implement, I was wondering if it is worth it to hash game states to

  1. their heuristic evaluations
  2. the next best move

I can immediately see why 2 would be useful since there are many ways to get to the same game state but I am wondering if I also have to hash the current depth to make this work. For example, if I reached this state with a depth of 3 (so only say 4 more moves to look ahead) vs a depth of 2 with 5 moves to look ahead, I might arrive at a different answer. Doesn't this mean I should take the depth into account with the hash?

My second question is whether hashing boards to their evaluation is worth it. It takes me O(n) time to build my hash, and O(n) time to evaluate a board (though it's really more like O(2 or 3n)). Are game states usually hashed to their evaluations, or is this overkill? Thanks for any help

Amphiprostyle answered 7/6, 2016 at 3:59 Comment(1)
Isn't the depth inherent in the board position? If there are 10 pieces on the board, you are five moves deep.Rolf
B
2

Whenever you hash a value of a state (using heuristics), you need to have information about the depth at which this state was evaluated. This is because there is a big difference between the value is 0.1 at depth 1 and the value is 0.1 at depth 20. In the first case we barely investigated the space, so we are pretty unsure what happens. In the second case we have already done huge amount of work, so we kind of know what are we talking about.

The thing is that for some games we do not know what the depth is for a position. For example chess. But in connect 4, looking at a position you know what is the depth.

enter image description here enter image description here

For the connect 4 the depth here is 14 (only 14 circles have been put). So you do not need to store the depth.

As for whether you actually have to hash the state or re-evaluate it. Clearly a position in this game can be reached through many game-paths, so you kind of expect the hash to be helpful. Important question is the trade-off of creating/looking at a hash and how intensive your evaluation function is. If it looks like it does a lot of work - hash it and benchmark.

One last suggestion. You mentioned alpha-beta which is more helpful than hashing at your stage (and not that hard to implement). You can go further and implement move ordering for your alpha-beta. If I were you, I would do it, and only after that I would implement hashing.

Bijection answered 7/6, 2016 at 4:26 Comment(3)
Awesome. I did not realize I already inherently had the depthAmphiprostyle
Is it actually necessary to know the depth? From any particular state, you have the same possible next moves, no matter how you got there.Whoa
@Whoa no, knowing depth is not necessary, but rather an improvement of the version without depth. I already explained why is this an improvement in the beginning of my answer.Bijection

© 2022 - 2024 — McMap. All rights reserved.