Advertisement

Turn based, complete information game AI

Started by January 26, 2012 10:49 PM
13 comments, last by alvaro 12 years, 8 months ago
Well, i implemented my AI.

I just need to clarify some points, i needed to modify various part of the code provided at mcts.ai since it could lead to exceptions and other bad behaviours.

1) How are handled "final" nodes? With final i mean nodes that contains a win or a loss. The algorithm will try to expand them, but they can't be expanded as the simulated player can't do any move(since he lost and it is no more present on the board)


cur.expand();
TreeNode newNode = cur.select();
visited.add(newNode);
double value = rollOut(newNode);


become

cur.expand();
double value;
if(!cur.isLeaf()) //the node is not a final state, the player can do a move and is not dead
{
TreeNode newNode = cur.select();
EvaluationContext.getCurrentBoard().apply_move(newNode.getMove());
EvaluationContext.getCurrentBoard().nextPlayer();
visited.add(newNode);
value = rollOut(newNode);
}
else
{
value = EvaluationContext.getCurrentBoard().playerWins(EvaluationContext.getCurrentBoard().getCurrentPlayerID()) ? 1:0;
}


where

public void expand() {
children = new TreeNode[nActions];
for (int i=0; i<nActions; i++) {
children = new TreeNode();
}
}


become

public void expand() {
if(!isLeaf())
return;
List<Move> moves = EvaluationContext.getCurrentBoard().generate_moves();
if(moves.size() == 0)
return;
children = new TreeNode[moves.size()];
int i = 0;
for (Move move : moves) {
children[i++] = new TreeNode(move, EvaluationContext);
}
}


so each time it will continue to simulate them even if it is not needed..

2) I have my "real" board (the one on which the player plays and interact) and a board for each Ai simulating thread, and i keep this 2 in sync updating the real board with the ai moves and updating the ai ones with player moves. Each thread have also a board on which it plays simulations (since simulations change the board state). deepCopying the board or generating a new one from the real one (the implementations are different, ai board is by far more memory efficient) takes quite the same time. I'm facing sync problems (i blame myself for not commintting when all worked, i changed a few things and now i'm not no more able to sync it :( lots of debug time is waiting for me), i'm already planning to solve it anywais, but is it wrong to generate ai boards from real board instead of keeping them in sync?
I have some opinions about point 2. The absolute cleanest way to organize the relationship between the GUI and the engine is to define a text-based protocol that they will use to communicate. The GUI and the engine are run as separate processes which communicate through a pipe. This has many advantages, including the possibility of swapping one engine for another (if you make your protocol available to others), automated testing of the engine, you can easily adapt this scheme to allow people to play online...

That mechanism is very popular in chess, where most engines implement one of two protocols (UCI and whatever XBoard uses). You should read the description of UCI because it is well designed.

Even if you don't end up implementing this in two processes, I would still strongly encourage very clear separation of engine and GUI, with a well-defined interface between them.
Advertisement
I already have different threads working on the update loop of the engine and the ai simultions, and i communicate with moves(that contains a board independent description of the attacker, the defender, and the action to perform), so it is already similar to a communication protocol, but implementing a real and full protocol is far too much work and i don't think it is well suited for this situation (mobile phone game). Btw i already have a class that acts as an interface between the engine and the simulating threads(i start one for each core of the device). So you are saying that is better to keep the boards "indirectly" in sync (using the protocol to notify the moves of the ai and the player) instead of regenerating the board each time..

PS: interesting article about the communication protocol for game http://altdevblogaday.com/2012/01/23/writing-your-own-websocket-server/
Problem finally solved, everithing is working just fine!
It was hard to syncronize all the thread and the board!
Now a bit of code clean up and then multi player support is waiting for me!
Thanks for the help!
So, did you end up using Monte Carlo?

This topic is closed to new replies.

Advertisement