Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement multithreading for NTBEA (ParameterSearch) and RoundRobinTournament (RunGames) #299

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions json/players/gameSpecific/TicTacToe.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"budgetType": "BUDGET_TIME",
"rolloutLength": 30,
"opponentTreePolicy": "OneTree",
"MASTGamma": 0,
"MASTGamma": 0.0,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not relevant to this PR per se, except that MASTGamma and K are both of type double, meaning this json file was causing errors. I encountered this during testing of the multithreading, so fixed it here.

"heuristic": {
"class": "players.heuristics.WinOnlyHeuristic"
},
"K": 1,
"K": 1.0,
"exploreEpsilon": 0.1,
"treePolicy": "UCB",
"MAST": "Both",
Expand Down
136 changes: 118 additions & 18 deletions src/main/java/core/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,19 @@ public final void reset(List<AbstractPlayer> players) {
* @param newRandomSeed - random seed is updated in the game parameters object and used throughout the game.
*/
public final void reset(List<AbstractPlayer> players, long newRandomSeed) {
reset(gameState, forwardModel, players, newRandomSeed);
}

/**
* Resets the game. Sets up the game state to the initial state as described by game rules, assigns players
* and their IDs, and initialises all players.
*
* @param gameState - game state to apply the reset to
* @param forwardModel - the forward model to use for resetting the game state
* @param players - new players for the game
* @param newRandomSeed - random seed is updated in the game parameters object and used throughout the game.
*/
public final void reset(AbstractGameState gameState, AbstractForwardModel forwardModel, List<AbstractPlayer> players, long newRandomSeed) {
gameState.reset(newRandomSeed);
forwardModel.abstractSetup(gameState);
if (players.size() == gameState.getNPlayers()) {
Expand Down Expand Up @@ -412,18 +425,15 @@ public void resetStats() {
}

/**
* Runs the game,
* Runs the game, with synchronisation facilities for GUI and terminal players; not to be used for
* (possibly multithreaded) ParameterSearch and RunGames instances
*/
public final void run() {

listeners.forEach(l -> l.onEvent(Event.createEvent(Event.GameEvent.ABOUT_TO_START, gameState)));

boolean firstEnd = true;

while (gameState.isNotTerminal() && !stop) {

synchronized (this) {

// Now synchronized with possible intervention from the GUI
// This is only relevant if the game has been paused...so should not affect
// performance in non-GUI situations
Expand All @@ -443,7 +453,6 @@ public final void run() {
// as the JVM hoists pause and isHumanToMove() ouside the while loop on the basis that
// they cannot be changed in this thread....


/*
* The Game is responsible for tracking the players and the current game state
* It is important that the Game never passes the main AbstractGameState to the individual players,
Expand All @@ -455,8 +464,9 @@ public final void run() {
*/

// Get player to ask for actions next (This horrendous line is for backwards compatibility).
boolean reacting = (gameState instanceof AbstractGameStateWithTurnOrder && ((AbstractGameStateWithTurnOrder) gameState).getTurnOrder() instanceof ReactiveTurnOrder
&& ((ReactiveTurnOrder) ((AbstractGameStateWithTurnOrder) gameState).getTurnOrder()).getReactivePlayers().size() > 0);
boolean reacting = gameState instanceof AbstractGameStateWithTurnOrder &&
((AbstractGameStateWithTurnOrder) gameState).getTurnOrder() instanceof ReactiveTurnOrder &&
!((ReactiveTurnOrder) ((AbstractGameStateWithTurnOrder) gameState).getTurnOrder()).getReactivePlayers().isEmpty();

// Check if this is the same player as last, count number of actions per turn
if (!reacting) {
Expand All @@ -470,10 +480,8 @@ public final void run() {
}

if (gameState.isNotTerminal()) {

if (debug) System.out.printf("Invoking oneAction from Game for player %d%n", activePlayer);
oneAction();

} else {
if (firstEnd) {
if (gameState.coreGameParameters.verbose) {
Expand All @@ -483,7 +491,6 @@ public final void run() {
firstEnd = false;
}
}

if (debug) System.out.println("Exiting synchronized block in Game");
}
}
Expand All @@ -495,13 +502,93 @@ public final void run() {
}
}

/**
* Runs an instance of the game, with the possibility to run fully parallel from any other game instances.
* Not to be used when there is a human player, only useful when parallelization needs to be possible.
* @return The final gameState object after finishing the game run(s)
*/
public AbstractGameState runInstance(LinkedList<AbstractPlayer> players, int seed, boolean randomGameParameters) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is essentially the same method as the run() method, except that it facilitates parallel execution. The reason this is a separate method, and not integrated into run() is because this function requires as few synchronized parts, in order to speed up execution, whereas run() requires a large synchronized block of code to facilitate GUI interaction such as pausing.
This meant I unfortunately needed to duplicate some of the code, although I removed all of the unnecessary pause/stop parts of the code. However, this also meant that most of the multithreading-specific code (such as increased number of scoped variables, instead of using class variables) is also limited to this runInstance method.

AbstractGameState gameState = this.gameState.copy(); // our own copy of the gameState, to play games with
AbstractForwardModel forwardModel = this.forwardModel.copy(); // our own copy of the forwardModel, to avoid concurrency issues
reset(gameState, forwardModel, players, seed); // reset gameState before playing

synchronized (this) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on my testing, events posted to the listeners need to be executed sequentially. I did not do much research to the effect of these events, but based on my test runs, just adding a synchronized block to each of the event activations made everything work correctly.

listeners.forEach(l -> l.onEvent(Event.createEvent(Event.GameEvent.ABOUT_TO_START, gameState)));
}
if (randomGameParameters) {
gameState.getGameParameters().randomize();
System.out.println("Game parameters: " + gameState.getGameParameters());
}

int lastPlayer = -1; // initialise with no last player, since we are starting a new game
int nActionsPerTurn = 1; // keep track within this scope to avoid parallel processes to modify this game's stats

boolean firstEnd = true;
// System.out.println("Running game: "+matchUpPlayers);
while (gameState.isNotTerminal()) {
int activePlayer = gameState.getCurrentPlayer();

AbstractPlayer currentPlayer = players.get(activePlayer);
/*
* The Game is responsible for tracking the players and the current game state
* It is important that the Game never passes the main AbstractGameState to the individual players,
* but instead always uses copy(playerId) to both:
* i) shuffle any hidden data they cannot see
* ii) ensure that any changes the player makes to the game state do not affect the genuine game state
*
* Players should never have access to the Game, or the main AbstractGameState, or to each other!
*/

// Get player to ask for actions next (This horrendous line is for backwards compatibility).
boolean reacting = gameState instanceof AbstractGameStateWithTurnOrder &&
((AbstractGameStateWithTurnOrder) gameState).getTurnOrder() instanceof ReactiveTurnOrder &&
!((ReactiveTurnOrder) ((AbstractGameStateWithTurnOrder) gameState).getTurnOrder()).getReactivePlayers().isEmpty();

// Check if this is the same player as last, count number of actions per turn
if (!reacting) {
if (currentPlayer != null && activePlayer == lastPlayer) {
nActionsPerTurn++;
} else {
nActionsPerTurnSum += nActionsPerTurn; // atomic
nActionsPerTurn = 1;
nActionsPerTurnCount++; // atomic
}
}

if (gameState.isNotTerminal()) {
if (debug) System.out.printf("Invoking oneAction from Game for player %d%n", activePlayer);
// keep track of last player within this scope, since parallel processes may modify this.lastPlayer
lastPlayer = gameState.getCurrentPlayer();
oneAction(gameState, forwardModel, players);
} else {
if (firstEnd) {
if (gameState.coreGameParameters.verbose) {
System.out.println("Ended");
}
terminate(gameState, forwardModel);
firstEnd = false;
}
}
if (debug) System.out.println("Exiting synchronized block in Game");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept this debug message in this version of the method, for consistency with debug output. If desired I could change this to something else, or remove it.

}
if (firstEnd) {
if (gameState.coreGameParameters.verbose) {
System.out.println("Ended");
}
terminate(gameState, forwardModel);
}
return gameState;
}

public final boolean isHumanToMove() {
int activePlayer = gameState.getCurrentPlayer();
return this.getPlayers().get(activePlayer) instanceof HumanGUIPlayer;
}

public final AbstractAction oneAction() {

return oneAction(gameState, forwardModel, players);
}
public final AbstractAction oneAction(AbstractGameState gameState, AbstractForwardModel forwardModel, List<AbstractPlayer> players) {
// we pause before each action is taken if running with a delay (e.g. for video recording with random players)
if (turnPause > 0)
synchronized (this) {
Expand Down Expand Up @@ -589,8 +676,9 @@ public final AbstractAction oneAction() {
}
// We publish an ACTION_CHOSEN message before we implement the action, so that observers can record the state that led to the decision
AbstractAction finalAction = action;
listeners.forEach(l -> l.onEvent(Event.createEvent(Event.GameEvent.ACTION_CHOSEN, gameState, finalAction, activePlayer)));

synchronized (this) {
listeners.forEach(l -> l.onEvent(Event.createEvent(Event.GameEvent.ACTION_CHOSEN, gameState, finalAction, activePlayer)));
}
} else {
currentPlayer.registerUpdatedObservation(observation);
}
Expand Down Expand Up @@ -621,7 +709,9 @@ public final AbstractAction oneAction() {
// We publish an ACTION_TAKEN message once the action is taken so that observers can record the result of the action
// (such as the next player)
AbstractAction finalAction1 = action;
listeners.forEach(l -> l.onEvent(Event.createEvent(Event.GameEvent.ACTION_TAKEN, gameState, finalAction1.copy(), activePlayer)));
synchronized (this) {
listeners.forEach(l -> l.onEvent(Event.createEvent(Event.GameEvent.ACTION_TAKEN, gameState, finalAction1.copy(), activePlayer)));
}

if (debug) System.out.printf("Finishing oneAction for player %s%n", activePlayer);
return action;
Expand All @@ -631,14 +721,25 @@ public final AbstractAction oneAction() {
* Called at the end of game loop execution, when the game is over.
*/
private void terminate() {
terminate(gameState, forwardModel);
}

/**
* Called at the end of game loop execution, when the game is over, given some gameState
* @param gameState The game state to handle termination for.
* @param forwardModel The forward model to handle termination with.
*/
private void terminate(AbstractGameState gameState, AbstractForwardModel forwardModel) {
// Print last state
if (gameState instanceof IPrintable && gameState.coreGameParameters.verbose) {
((IPrintable) gameState).printToConsole();
}

// Perform any end of game computations as required by the game
forwardModel.endGame(gameState);
listeners.forEach(l -> l.onEvent(Event.createEvent(Event.GameEvent.GAME_OVER, gameState)));
synchronized (this) {
listeners.forEach(l -> l.onEvent(Event.createEvent(Event.GameEvent.GAME_OVER, gameState)));
}
if (gameState.coreGameParameters.recordEventHistory) {
gameState.recordHistory(Event.GameEvent.GAME_OVER.name());
for (int i = 0; i < gameState.getNPlayers(); i++) {
Expand Down Expand Up @@ -836,8 +937,7 @@ public static void main(String[] args) {

/* Set up players for the game */
ArrayList<AbstractPlayer> players = new ArrayList<>();
players.add(new RandomPlayer());
players.add(new RandomPlayer());
players.add(new BasicMCTSPlayer());
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope the listed players did not have any big significance to the master branch. If they are relevant, I'll just restore this to the Random+Random+MCTS state it used to be in.

players.add(new BasicMCTSPlayer());

// RMHCParams params = new RMHCParams();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/evaluation/RunArg.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public enum RunArg {
nPlayers("The number of players in each game. Overrides playerRange.",
-1,
new Usage[]{Usage.ParameterSearch, Usage.RunGames}),
nThreads("The number of threads that can be spawned in order to evaluate games.\n" +
"\t For tournaments (including tournaments performed after ParameterSearch), the individual matachup evaluations are parallelized;" +
"\t For ParameterSearch itself, the repeats are parallelized; for this part, fewer threads than specified may be allocated.",
1,
new Usage[]{Usage.ParameterSearch, Usage.RunGames}),
neighbourhood("The size of neighbourhood to look at in NTBEA. Default is min(50, |searchSpace|/100) ",
50,
new Usage[]{Usage.ParameterSearch}),
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/evaluation/optimisation/MultiNTBEA.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,8 @@ private static int manhattan(int[] x, int[] y) {
return retValue;
}

@Override
public NTBEA copy() {
return new MultiNTBEA(params, game, nPlayers);
}
}
Loading