exit from runSimulation when time has run out, attempt at #479

master
melvinzhang 2015-10-29 21:45:46 +08:00
parent 4ef38c9d65
commit 73bf205c9c
1 changed files with 13 additions and 8 deletions

View File

@ -182,12 +182,12 @@ public class MCTSAI implements MagicAI {
return startGame.map(RCHOICES.get(bestC)); return startGame.map(RCHOICES.get(bestC));
} }
private Runnable genSimulationTask(final MagicGame rootGame, final LinkedList<MCTSGameTree> path, final BlockingQueue<Runnable> queue) { private Runnable genSimulationTask(final MagicGame rootGame, final LinkedList<MCTSGameTree> path, final BlockingQueue<Runnable> queue, final long END_TIME) {
return new Runnable() { return new Runnable() {
@Override @Override
public void run() { public void run() {
// propagate result of random play up the path // propagate result of random play up the path
final double score = randomPlay(path.getLast(), rootGame); final double score = randomPlay(path.getLast(), rootGame, END_TIME);
queue.offer(genBackpropagationTask(score, path)); queue.offer(genBackpropagationTask(score, path));
} }
}; };
@ -250,7 +250,7 @@ public class MCTSAI implements MagicAI {
// submit random play to executor // submit random play to executor
if (running) { if (running) {
try { try {
executor.execute(genSimulationTask(rootGame, path, queue)); executor.execute(genSimulationTask(rootGame, path, queue, END_TIME));
} catch (RejectedExecutionException e) { } catch (RejectedExecutionException e) {
// occurs when trying to submit to a execute that has shutdown // occurs when trying to submit to a execute that has shutdown
return; return;
@ -422,7 +422,7 @@ public class MCTSAI implements MagicAI {
} }
//returns a reward in the range [0, 1] //returns a reward in the range [0, 1]
private double randomPlay(final MCTSGameTree node, final MagicGame game) { private double randomPlay(final MCTSGameTree node, final MagicGame game, final long END_TIME) {
//terminal node, no need for random play //terminal node, no need for random play
if (game.isFinished()) { if (game.isFinished()) {
if (game.getLosingPlayer() == game.getScorePlayer()) { if (game.getLosingPlayer() == game.getScorePlayer()) {
@ -437,7 +437,7 @@ public class MCTSAI implements MagicAI {
if (!CHEAT) { if (!CHEAT) {
game.showRandomizedHiddenCards(); game.showRandomizedHiddenCards();
} }
final int[] counts = runSimulation(game); final int[] counts = runSimulation(game, END_TIME);
//System.err.println("COUNTS:\t" + counts[0] + "\t" + counts[1]); //System.err.println("COUNTS:\t" + counts[0] + "\t" + counts[1]);
@ -452,7 +452,7 @@ public class MCTSAI implements MagicAI {
} }
} }
private int[] runSimulation(final MagicGame game) { private int[] runSimulation(final MagicGame game, final long END_TIME) {
int aiChoices = 0; int aiChoices = 0;
int oppChoices = 0; int oppChoices = 0;
@ -461,7 +461,10 @@ public class MCTSAI implements MagicAI {
game.setFastChoices(true); game.setFastChoices(true);
// simulate game until it is finished or reached MAX_CHOICES // simulate game until it is finished or reached MAX_CHOICES
while (game.advanceToNextEventWithChoice() && aiChoices < MAX_CHOICES && oppChoices < MAX_CHOICES) { while (game.advanceToNextEventWithChoice() &&
aiChoices < MAX_CHOICES &&
oppChoices < MAX_CHOICES &&
System.currentTimeMillis() < END_TIME) {
final MagicEvent event = game.getNextEvent(); final MagicEvent event = game.getNextEvent();
if (event.getPlayer() == game.getScorePlayer()) { if (event.getPlayer() == game.getScorePlayer()) {
@ -473,7 +476,9 @@ public class MCTSAI implements MagicAI {
//get simulation choice and execute //get simulation choice and execute
final Object[] choice = event.getSimulationChoiceResult(game); final Object[] choice = event.getSimulationChoiceResult(game);
assert choice != null : "ERROR! No choice found during MCTS sim"; assert choice != null : "ERROR! No choice found during MCTS sim";
game.executeNextEvent(choice); if (System.currentTimeMillis() < END_TIME) {
game.executeNextEvent(choice);
}
//terminate early if score > MIN_SCORE or score < -MIN_SCORE //terminate early if score > MIN_SCORE or score < -MIN_SCORE
if (game.getScore() < -MIN_SCORE) { if (game.getScore() < -MIN_SCORE) {