multi-threaded monte carlo AI

This commit is contained in:
BetaSteward 2011-12-01 14:51:56 -05:00
parent e8ed56c80f
commit 84035ea849
3 changed files with 93 additions and 66 deletions

View file

@ -27,15 +27,21 @@
*/
package mage.player.ai;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.logging.Level;
import mage.Constants.RangeOfInfluence;
import mage.abilities.Ability;
import mage.abilities.ActivatedAbility;
import mage.game.Game;
import mage.game.combat.Combat;
import mage.game.combat.CombatGroup;
import mage.game.permanent.Permanent;
import mage.player.ai.MCTSPlayer.NextAction;
import mage.players.Player;
import org.apache.log4j.Logger;
@ -48,11 +54,15 @@ public class ComputerPlayerMCTS extends ComputerPlayer<ComputerPlayerMCTS> imple
protected transient MCTSNode root;
protected int thinkTime;
private final static transient Logger logger = Logger.getLogger(ComputerPlayerMCTS.class);
private ExecutorService pool;
private int cores;
public ComputerPlayerMCTS(String name, RangeOfInfluence range, int skill) {
super(name, range);
human = false;
thinkTime = skill;
cores = Runtime.getRuntime().availableProcessors();
pool = Executors.newFixedThreadPool(cores);
}
protected ComputerPlayerMCTS(UUID id) {
@ -70,32 +80,32 @@ public class ComputerPlayerMCTS extends ComputerPlayer<ComputerPlayerMCTS> imple
@Override
public void priority(Game game) {
getNextAction(game);
getNextAction(game, NextAction.PRIORITY);
Ability ability = root.getAction();
if (ability == null)
logger.fatal("null ability");
activateAbility((ActivatedAbility)ability, game);
}
protected void calculateActions(Game game) {
protected void calculateActions(Game game, NextAction action) {
if (root == null) {
Game sim = createMCTSGame(game);
MCTSPlayer player = (MCTSPlayer) sim.getPlayer(playerId);
player.setNextAction(MCTSPlayer.NextAction.PRIORITY);
player.setNextAction(action);
root = new MCTSNode(sim);
}
applyMCTS();
applyMCTS(game, action);
root = root.bestChild();
root.emancipate();
}
protected void getNextAction(Game game) {
protected void getNextAction(Game game, NextAction nextAction) {
if (root != null) {
root = root.getMatchingState(game.getState().getValue().hashCode());
root = root.getMatchingState(game.getState().getValue().hashCode(), nextAction);
if (root != null)
root.emancipate();
}
calculateActions(game);
calculateActions(game, nextAction);
}
// @Override
@ -171,10 +181,11 @@ public class ComputerPlayerMCTS extends ComputerPlayer<ComputerPlayerMCTS> imple
@Override
public void selectAttackers(Game game) {
Game sim = createMCTSGame(game);
MCTSPlayer player = (MCTSPlayer) sim.getPlayer(playerId);
player.setNextAction(MCTSPlayer.NextAction.SELECT_ATTACKERS);
root = new MCTSNode(sim);
applyMCTS();
getNextAction(sim, NextAction.SELECT_ATTACKERS);
// MCTSPlayer player = (MCTSPlayer) sim.getPlayer(playerId);
// player.setNextAction(MCTSPlayer.NextAction.SELECT_ATTACKERS);
// root = new MCTSNode(sim);
// applyMCTS();
Combat combat = root.bestChild().getCombat();
UUID opponentId = game.getCombat().getDefenders().iterator().next();
for (UUID attackerId: combat.getAttackers()) {
@ -185,10 +196,11 @@ public class ComputerPlayerMCTS extends ComputerPlayer<ComputerPlayerMCTS> imple
@Override
public void selectBlockers(Game game) {
Game sim = createMCTSGame(game);
MCTSPlayer player = (MCTSPlayer) sim.getPlayer(playerId);
player.setNextAction(MCTSPlayer.NextAction.SELECT_BLOCKERS);
root = new MCTSNode(sim);
applyMCTS();
getNextAction(sim, NextAction.SELECT_BLOCKERS);
// MCTSPlayer player = (MCTSPlayer) sim.getPlayer(playerId);
// player.setNextAction(MCTSPlayer.NextAction.SELECT_BLOCKERS);
// root = new MCTSNode(sim);
// applyMCTS();
Combat combat = root.bestChild().getCombat();
List<CombatGroup> groups = game.getCombat().getGroups();
for (int i = 0; i < groups.size(); i++) {
@ -235,51 +247,34 @@ public class ComputerPlayerMCTS extends ComputerPlayer<ComputerPlayerMCTS> imple
// throw new UnsupportedOperationException("Not supported yet.");
// }
protected void applyMCTS() {
protected void applyMCTS(final Game game, final NextAction action) {
long startTime = System.nanoTime();
long endTime = startTime + (thinkTime * 1000000000l);
MCTSNode current;
if (root.getNumChildren() == 1)
//there is only one possible action
return;
logger.info("applyMCTS - Thinking for " + (endTime - startTime)/1000000000.0 + "s");
while (true) {
long currentTime = System.nanoTime();
logger.info("Remaining time: " + (endTime - currentTime)/1000000000.0 + "s");
if (currentTime > endTime)
break;
current = root;
// Selection
while (!current.isLeaf()) {
current = current.select();
}
int result;
if (!current.isTerminal()) {
// Expansion
current.expand();
if (current == root && current.getNumChildren() == 1)
//there is only one possible action
return;
// Simulation
current = current.select();
result = current.simulate(this.playerId);
}
else {
result = current.isWinner(this.playerId)?1:0;
}
// Backpropagation
current.backpropagate(result);
List<MCTSExecutor> tasks = new ArrayList<MCTSExecutor>();
for (int i = 0; i < cores; i++) {
Game sim = createMCTSGame(game);
MCTSPlayer player = (MCTSPlayer) sim.getPlayer(playerId);
player.setNextAction(action);
MCTSExecutor exec = new MCTSExecutor(sim, playerId, thinkTime);
tasks.add(exec);
}
try {
pool.invokeAll(tasks);
} catch (InterruptedException ex) {
logger.warn("applyMCTS interrupted");
}
for (MCTSExecutor task: tasks) {
root.merge(task.getRoot());
}
logger.info("Created " + root.getNodeCount() + " nodes");
return;
}
/**
* Copies game and replaces all players in copy with mcts players
* Shuffles each players library so that there is no knowledge of its order