-
Notifications
You must be signed in to change notification settings - Fork 76
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
base: master
Are you sure you want to change the base?
Changes from 7 commits
6bff4e6
d7ee875
2909cc1
0233c8d
9f4e619
e0e31ec
b42f4b4
b6ed36d
1ebe068
580568f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()) { | ||
|
@@ -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 | ||
|
@@ -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, | ||
|
@@ -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) { | ||
|
@@ -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) { | ||
|
@@ -483,7 +491,6 @@ public final void run() { | |
firstEnd = false; | ||
} | ||
} | ||
|
||
if (debug) System.out.println("Exiting synchronized block in Game"); | ||
} | ||
} | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is essentially the same method as the |
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -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); | ||
} | ||
|
@@ -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; | ||
|
@@ -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++) { | ||
|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
There was a problem hiding this comment.
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.