package tsp.pacman; import java.util.HashMap; public class PacmanMCTS implements Pacman { private MCTSNode tree; private HashMap stateToNode; private double C; private int maxTimeMilli; private int maxDepth; public PacmanMCTS(int maxTimeMilli, int maxDepth, double C){ this.C = C; this.maxTimeMilli = maxTimeMilli; this.maxDepth = maxDepth; stateToNode = new HashMap<>(); } @Override public Position chooseAction(PacmanState state) { // Mécanisme de mémorisation des simulations précédentes. if (tree == null) { tree = new MCTSNode(state); stateToNode.put(state, tree); } long startTime = System.currentTimeMillis(); MCTSNode currentNode = stateToNode.get(state); tree = currentNode; if (currentNode == null){ tree = new MCTSNode(state); stateToNode.put(state, tree); currentNode = stateToNode.get(state); } tree.deleteParent(); stateToNode = new HashMap<>(); stateToNode.put(state, tree); int counter = 0; while(System.currentTimeMillis() - startTime < maxTimeMilli){ counter += 1; MCTSNode leaf = currentNode.selectLeaf(C); leaf.generateSons(); for (MCTSNode node: leaf.getSons()){ this.stateToNode.put(node.getState(), node); } leaf.selectSon(C).runSimulation(maxDepth); } System.out.println("Number of nodes explored: " + counter); //tree.printTree(); return currentNode.selectSonFinal().getState().findPacman(); } }