java connect 4 AI algorithm not very bright
Do I just need a heuristic in the utility function? Any pointers for a decent heuristic?
Quote: Original post by jerichauNot necessarily. And even if it did, it wouldn't necessarily optimize out the string building.
Doesn't the JVM optimize out the Util.debug calls when there is an if(false) in it?
Quote: Do I just need a heuristic in the utility function? Any pointers for a decent heuristic?
The best heuristic is a stupid one. That is, don't go overboard trying to catch special cases in your heuristic function. Instead, do something simple (like counting consecutive chains of 3 pieces, or rows with your piece on top) and let minimax worry about the intelligence part. The important thing is that your heuristic be very very fast.
Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play
"Reducing the world to mathematical equations!"
I'm going to try to keep the search tree in memory and expand it as needed, because it's repeating the search from scratch each time right now.
Quote: Original post by jerichau
Unfortunately it does. Anything more than 10 seconds is unacceptable.
Ten seconds? 7^4 is 2401. 7^5 is 16807. That's 0.6 milliseconds per terminal state. If your algorithm is really taking that long, you're doing something really REALLY wrong.
EDIT: You're also allocating iterators per state expansion, which is a no-no. Don't allocate things. Also, if max_value returns both the maximum value and the choice which generated that, your code will be much simpler.
Quote: Original post by SneftelQuote: Do I just need a heuristic in the utility function? Any pointers for a decent heuristic?
The best heuristic is a stupid one. That is, don't go overboard trying to catch special cases in your heuristic function. Instead, do something simple (like counting consecutive chains of 3 pieces, or rows with your piece on top) and let minimax worry about the intelligence part. The important thing is that your heuristic be very very fast.
Actually, that's not true. Well, it's true for some games, but not for connect 4. Let's define a "threat" as a group of 4 squares in a line with 3 of them being disks of the same color and the other one being empty. A threat will be considered "even" if its empty square is on the top row, or an even number of rows below, and odd otherwise. A good evaluation function will value odd threats much more than even threats. You also have to be careful about not considering something a threat if its empty square is immediately above the empty square in an enemy threat. There's a few more things to consider that can be learned by analyzing the endgame mathematically. An evaluation function that knows about these things will result in a much stronger player than one that doesn't, even if it takes more time.
About only reaching depth 4, you should really be able to get a lot more. Using alpha-beta with good ordering heuristics will nearly double the depth you can reach in a given amount of time. Using transposition tables will also give you a nice boost. Programming in C++ I bet you can reach depth 14 or so in 10 seconds. Programming in Java and being careful about not doing expensive operations, you may drop to depth 12. Anything below that probably means you are doing something wrong.
Don't clone the board. Just add a way to undo moves. The minimax function can perform a move, call itself recursively, and then undo the move. No more pointless copying of the entire board. You can also get rid of the Node class.
Don't search the entire board for a victory. You only need to check the winning conditions that involve the piece that was just placed.
Quote: Original post by Vorpy
Don't search the entire board for a victory. You only need to check the winning conditions that involve the piece that was just placed.
Are you talking about the win condition on the actual play or the testing for win conditions for each node of planning? Obviously they are both using the same algorithm and need to be streamlined. If you do this one thing, you can cut your search time in half, probably.
Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play
"Reducing the world to mathematical equations!"