[AI] Improvements on repeating actions

This commit is contained in:
magenoxx 2012-07-29 08:56:35 +04:00
parent 50ffb96a3f
commit 2200fb8572
3 changed files with 28 additions and 15 deletions

View file

@ -89,6 +89,7 @@ public class ComputerPlayer6 extends ComputerPlayer<ComputerPlayer6> implements
private static final String FILE_WITH_INSTRUCTIONS = "config/ai.please.cast.this.txt"; private static final String FILE_WITH_INSTRUCTIONS = "config/ai.please.cast.this.txt";
private List<String> suggested = new ArrayList<String>(); private List<String> suggested = new ArrayList<String>();
protected Set<String> actionCache;
private static final List<TreeOptimizer> optimizers = new ArrayList<TreeOptimizer>(); private static final List<TreeOptimizer> optimizers = new ArrayList<TreeOptimizer>();
@ -103,6 +104,7 @@ public class ComputerPlayer6 extends ComputerPlayer<ComputerPlayer6> implements
maxThink = skill * 300; maxThink = skill * 300;
maxNodes = Config2.maxNodes; maxNodes = Config2.maxNodes;
getSuggestedActions(); getSuggestedActions();
this.actionCache = new HashSet<String>();
} }
public ComputerPlayer6(final ComputerPlayer6 player) { public ComputerPlayer6(final ComputerPlayer6 player) {
@ -114,6 +116,7 @@ public class ComputerPlayer6 extends ComputerPlayer<ComputerPlayer6> implements
this.actions.addAll(player.actions); this.actions.addAll(player.actions);
this.targets.addAll(player.targets); this.targets.addAll(player.targets);
this.choices.addAll(player.choices); this.choices.addAll(player.choices);
this.actionCache = player.actionCache;
} }
@Override @Override
@ -512,6 +515,7 @@ public class ComputerPlayer6 extends ComputerPlayer<ComputerPlayer6> implements
sim.getPlayerList().getNext(); sim.getPlayerList().getNext();
} }
SimulationNode2 newNode = new SimulationNode2(node, sim, action, depth, currentPlayer.getId()); SimulationNode2 newNode = new SimulationNode2(node, sim, action, depth, currentPlayer.getId());
int testVal = GameStateEvaluator2.evaluate(currentPlayer.getId(), sim);
logger.debug("simulating -- node #:" + SimulationNode2.getCount() + " actions:" + action); logger.debug("simulating -- node #:" + SimulationNode2.getCount() + " actions:" + action);
sim.checkStateAndTriggered(); sim.checkStateAndTriggered();
int val = addActions(newNode, depth-1, alpha, beta); int val = addActions(newNode, depth-1, alpha, beta);
@ -1243,15 +1247,25 @@ public class ComputerPlayer6 extends ComputerPlayer<ComputerPlayer6> implements
private boolean checkForRepeatedAction(Game sim, SimulationNode2 node, Ability action, UUID playerId) { private boolean checkForRepeatedAction(Game sim, SimulationNode2 node, Ability action, UUID playerId) {
if (action instanceof PassAbility) if (action instanceof PassAbility)
return false; return false;
int val = GameStateEvaluator2.evaluate(playerId, sim); int newVal = GameStateEvaluator2.evaluate(playerId, sim);
SimulationNode2 test = node.getParent(); SimulationNode2 test = node.getParent();
while (test != null && !test.getPlayerId().equals(playerId)) { while (test != null) {
test = test.getParent(); if (test.getPlayerId().equals(playerId)) {
} if (test.getAbilities() != null && test.getAbilities().size() == 1) {
if (test != null && test.getAbilities() != null && test.getAbilities().size() == 1) { if (action.toString().equals(test.getAbilities().get(0).toString())) {
if (action.toString().equals(test.getAbilities().get(0).toString()) && GameStateEvaluator2.evaluate(playerId, test.getGame()) == val) { if (test.getParent() != null) {
return true; Game prevGame = node.getGame();
if (prevGame != null) {
int oldVal = GameStateEvaluator2.evaluate(playerId, prevGame);
if (oldVal >= newVal) {
return true;
}
}
}
}
}
} }
test = test.getParent();
} }
return false; return false;
} }

View file

@ -39,9 +39,7 @@ import mage.game.turn.*;
import mage.players.Player; import mage.players.Player;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import java.util.LinkedList; import java.util.*;
import java.util.List;
import java.util.UUID;
/** /**
* *
@ -129,6 +127,7 @@ public class ComputerPlayer7 extends ComputerPlayer6 implements Player {
return false; return false;
case END_TURN: case END_TURN:
case CLEANUP: case CLEANUP:
actionCache.clear();
pass(); pass();
return false; return false;
} }
@ -148,11 +147,11 @@ public class ComputerPlayer7 extends ComputerPlayer6 implements Player {
root = root.children.get(0); root = root.children.get(0);
int bestScore = root.getScore(); int bestScore = root.getScore();
//if (bestScore > currentScore || allowBadMoves) { //if (bestScore > currentScore || allowBadMoves) {
actions = new LinkedList<Ability>(root.abilities); actions = new LinkedList<Ability>(root.abilities);
combat = root.combat; combat = root.combat;
//} else { for (Ability ability : actions) {
//System.out.println("[" + game.getPlayer(playerId).getName() + "][pre] Action: not better score"); actionCache.add(ability.getRule() + "_" + ability.getSourceId());
//} }
} else { } else {
System.out.println("[" + game.getPlayer(playerId).getName() + "][pre] Action: skip"); System.out.println("[" + game.getPlayer(playerId).getName() + "][pre] Action: skip");
} }