/** * Monte-Carlo simulation for playout. * * @param node Node to start at * @param timeout Time limit * @return Scores for all players */ private int[] playout(Node node, long timeout) { if (node instanceof TerminalNode) { return ((TerminalNode) node).goal; } MachineState state = ((NonTerminalNode) node).state; while (System.currentTimeMillis() < timeout) { if (theMachine.isTerminal(state)) { List<Integer> s; try { s = theMachine.getGoals(state); } catch (GoalDefinitionException e) { System.err.println("Could not compute goal values."); return null; } int[] scores = new int[s.size()]; for (int i = 0; i < scores.length; i++) { scores[i] = s.get(i); } return scores; } try { state = theMachine.getNextState(state, theMachine.getRandomJointMove(state)); } catch (TransitionDefinitionException e) { System.err.println("Could not perform state update."); return null; } catch (MoveDefinitionException e) { System.err.println("Could not compute legal moves."); return null; } } return null; }
@Override public void run() { try { if (match.getAnalysisClock() >= 0) { sendAnalyzeRequests(); } notifyObservers(new ServerNewMatchEvent(stateMachine.getRoles(), currentState)); notifyObservers(new ServerTimeEvent(match.getStartClock() * 1000)); sendStartRequests(); appendErrorsToMatchDescription(); while (!stateMachine.isTerminal(currentState)) { publishWhenNecessary(); saveWhenNecessary(); notifyObservers(new ServerNewGameStateEvent(currentState)); notifyObservers(new ServerTimeEvent(match.getPlayClock() * 1000)); notifyObservers(new ServerMatchUpdatedEvent(match, spectatorServerKey, saveToFilename)); previousMoves = sendPlayRequests(); notifyObservers(new ServerNewMovesEvent(previousMoves)); currentState = stateMachine.getNextState(currentState, previousMoves); match.appendMoves2(previousMoves); match.appendState(currentState.getContents()); appendErrorsToMatchDescription(); } match.markCompleted(stateMachine.getGoals(currentState)); publishWhenNecessary(); saveWhenNecessary(); notifyObservers(new ServerNewGameStateEvent(currentState)); notifyObservers(new ServerCompletedMatchEvent(getGoals())); notifyObservers(new ServerMatchUpdatedEvent(match, spectatorServerKey, saveToFilename)); sendStopRequests(previousMoves); } catch (InterruptedException ie) { if (match.isAborted()) { return; } else { ie.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } }
@Override public void stateMachineMetaGame(long timeout) throws TransitionDefinitionException, MoveDefinitionException, GoalDefinitionException { System.out.println("Metagame. Preparing to test the state machine"); StateMachine stateMachine = getStateMachine(); ProverStateMachine psm = (ProverStateMachine) stateMachine; List gdlDescription = psm.gdlDescription; // The only line you have to adapt in this file StateMachine stateMachineX = new CachedStateMachine(new PropNetStateMachine()); stateMachineX.initialize(gdlDescription); MachineState rootState = stateMachine.getInitialState(); MachineState rootStateX = stateMachineX.getInitialState(); if (!compare(rootState, rootStateX)) { System.out.println("Initial states are different"); System.out.println(rootState); System.out.println(rootStateX); return; } long finishBy = timeout - 1000; int nbExpansion = 0; boolean abort = false; while (System.currentTimeMillis() < finishBy && !abort) { MachineState state = rootState; while (true) { boolean isTerminal = stateMachine.isTerminal(state); boolean isTerminalX = stateMachineX.isTerminal(state); if (!compare(isTerminal, isTerminalX)) { System.out.println("DISCREPANCY between isTerminal values"); System.out.println("State : " + state); abort = true; break; } if (isTerminal) { List goal = stateMachine.getGoals(state); List goalX = stateMachineX.getGoals(state); if (!compare(goal, goalX)) { System.out.println("DISCREPANCY between goal values"); System.out.println(goal); System.out.println(goalX); abort = true; break; } break; } for (Role role : stateMachine.getRoles()) { List moves = stateMachine.getLegalMoves(state, role); List movesX = stateMachineX.getLegalMoves(state, role); if (!compare(moves, movesX, role)) { System.out.println("DISCREPANCY between legal moves for role " + role); System.out.println(moves); System.out.println(movesX); abort = true; break; } } List jointMove = stateMachine.getRandomJointMove(state); MachineState nextState = stateMachine.getNextState(state, jointMove); MachineState nextStateX = stateMachineX.getNextState(state, jointMove); if (!compare(nextState, nextStateX)) { System.out.println("DISCREPANCY between next states"); System.out.println("Previous state : " + state); System.out.println("Joint move : " + jointMove); System.out.println("New state : " + nextState); System.out.println("New stateX : " + nextStateX); abort = true; break; } state = nextState; nbExpansion++; } } System.out.println("Metagaming finished"); System.out.println("Nb expansion : " + nbExpansion); }