Escape from Recursion with a Time Limit
I'm writing a program to play checkers where each move is timed. I am
using alpha-beta pruning to find the best move. I can only go so deep in
the decision tree because:
The decision tree is huge for checkers
There is a time limit I have to decide the move
Is there a way to escape from the call stack instantly when I have run out
of time, even when there are lots of frames on the stack? I thought about
throwing an exception, but that seemed like a poor decision.
This is what I am doing now, but it is not fast enough
public Move play(Board board) {
ArrayList<Board> actions = board.findPossibleMoves();
if (actions.size() == 0) {
return new Board();
}
int depth = 3;
// Each move has 1 second (1000ms)
TimeLimit tl = new TimeLimit(1000);
double alpha = Double.NEGATIVE_INFINITY;
double beta = Double.POSITIVE_INFINITY;
Board bestSoFar = actions.get(0);
double v = Double.NEGATIVE_INFINITY;
for (Board b : actions) {
double result = minValue(b, depth, alpha, beta, tl);
if (result >= v) {
bestSoFar = b;
v = result;
}
if (v >= beta) return b;
alpha = Math.max(alpha, v);
}
return bestSoFar;
}
private double maxValue(Board board, int depth, double alpha, double beta,
TimeLimit tl) {
if (tl.isTimeUp())
return score(board);
...
}
private double minValue(Board board, int depth, double alpha, double beta,
TimeLimit tl) {
if (tl.isTimeUp())
return score(board);
...
}
No comments:
Post a Comment