Skip to content

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

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

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Contributor 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
291 changes: 119 additions & 172 deletions src/main/java/core/Game.java

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/main/java/evaluation/RunArg.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,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
8 changes: 4 additions & 4 deletions src/main/java/evaluation/listeners/ActionFeatureListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public String[] names() {


@Override
public double[] extractFeatureVector(AbstractAction action, AbstractGameState state, int perspectivePlayer) {
protected double[] extractFeatureVector(AbstractAction action, AbstractGameState state, int perspectivePlayer) {
// We put phi in first, and then psi
double[] retValue = new double[psiFn.names().length + phiFn.names().length];
double[] phi = cachedPhi == null ?
Expand All @@ -63,13 +63,13 @@ public double[] extractFeatureVector(AbstractAction action, AbstractGameState st
return retValue;
}

protected void processStateWithTargets(AbstractGameState state, AbstractAction action, Map<String, Map<AbstractAction, Double>> targets) {
protected synchronized void processStateWithTargets(AbstractGameState state, AbstractAction action, Map<String, Map<AbstractAction, Double>> targets) {
actionValues = targets;
processState(state, action);
}

@Override
public void processState(AbstractGameState state, AbstractAction action) {
public synchronized void processState(AbstractGameState state, AbstractAction action) {
// we override this from FeatureListener, because we want to record the feature vector for each action
if (action == null) return; // we do not record data for the GAME_OVER event
cachedPhi = null;
Expand Down Expand Up @@ -109,7 +109,7 @@ private Map<String, Double> getActionScores(AbstractAction action) {


@Override
public String injectAgentAttributes(String raw) {
public synchronized String injectAgentAttributes(String raw) {
return raw.replaceAll(Pattern.quote("*PSI*"), psiFn.getClass().getCanonicalName())
.replaceAll(Pattern.quote("*PHI*"), phiFn != null ? phiFn.getClass().getCanonicalName() : "NONE");
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/evaluation/listeners/FeatureListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void setLogger(IStatisticLogger logger) {
}

@Override
public void onEvent(Event event) {
public synchronized void onEvent(Event event) {

if (event.type == frequency && frequency != Event.GameEvent.GAME_OVER) {
// if GAME_OVER, then we cover this a few lines down
Expand All @@ -55,7 +55,7 @@ public void onEvent(Event event) {
}

@Override
public boolean setOutputDirectory(String... nestedDirectories) {
public synchronized boolean setOutputDirectory(String... nestedDirectories) {

if (logger instanceof FileStatsLogger fileLogger) {
fileLogger.setOutPutDirectory(nestedDirectories);
Expand Down Expand Up @@ -112,18 +112,18 @@ public void report() {
}

@Override
public void setGame(Game game) {
public synchronized void setGame(Game game) {
this.game = game;
}

@Override
public Game getGame() {
public synchronized Game getGame() {
return game;
}

public abstract String[] names();

public abstract double[] extractFeatureVector(AbstractAction action, AbstractGameState state, int perspectivePlayer);
protected abstract double[] extractFeatureVector(AbstractAction action, AbstractGameState state, int perspectivePlayer);


/**
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/evaluation/listeners/MetricsGameListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public MetricsGameListener(IDataLogger.ReportDestination logTo, IDataLogger.Repo
* @param event Event has information about its type and data fields for game, state, action and player.
* It's not guaranteed that the data fields are different to null, so a check is necessary.
*/
public void onEvent(Event event) {
public synchronized void onEvent(Event event) {
if (!eventsOfInterest.contains(event.type))
return;

Expand All @@ -94,7 +94,7 @@ public void onEvent(Event event) {
}

@Override
public boolean setOutputDirectory(String... nestedDirectories) {
public synchronized boolean setOutputDirectory(String... nestedDirectories) {

boolean success = true;

Expand Down Expand Up @@ -165,22 +165,22 @@ private String eventToIndexingColumn(IGameEvent e) {
}

/* Getters, setters */
public final void setGame(Game game) {
public synchronized final void setGame(Game game) {
this.game = game;
}

public final Game getGame() {
public synchronized final Game getGame() {
return game;
}

public void reset() {
public synchronized void reset() {
for (AbstractMetric metric : metrics.values()) {
metric.reset();
}
}

@Override
public void init(Game game, int nPlayersPerGame, Set<String> playerNames) {
public synchronized void init(Game game, int nPlayersPerGame, Set<String> playerNames) {
this.game = game;

for (AbstractMetric metric : metrics.values()) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/evaluation/listeners/StateFeatureListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public String[] names() {
}

@Override
public double[] extractFeatureVector(AbstractAction action, AbstractGameState state, int perspectivePlayer) {
protected double[] extractFeatureVector(AbstractAction action, AbstractGameState state, int perspectivePlayer) {
return phiFn.featureVector(state, perspectivePlayer);
}

@Override
public String injectAgentAttributes(String raw) {
public synchronized String injectAgentAttributes(String raw) {
return raw.replaceAll(Pattern.quote("*PHI*"), phiFn.getClass().getCanonicalName());
}
}
15 changes: 11 additions & 4 deletions src/main/java/evaluation/optimisation/GameEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import core.Game;
import core.interfaces.IGameHeuristic;
import core.interfaces.IStateHeuristic;
import core.interfaces.IStatisticLogger;
import evaluation.listeners.IGameListener;
import evodef.SearchSpace;
import evodef.SolutionEvaluator;
Expand Down Expand Up @@ -95,9 +94,17 @@ public double evaluate(double[] doubles) {
*/
@Override
public double evaluate(int[] settings) {
if (debug)
System.out.printf("Starting evaluation %d of %s at %tT%n", nEvals,
Arrays.toString(settings), System.currentTimeMillis());
if (debug) {
HashMap<String, Object> chosenConfigs = new HashMap<>();
for (int i = 0; i < searchSpace.nDims(); i++) {
int finalI = i;
chosenConfigs.put(searchSpace.name(i), IntStream.range(0, searchSpace.nValues(i))
.mapToObj(j -> searchSpace.value(finalI, j))
.toList().get(settings[i]));
}
System.out.printf("%d Starting evaluation %d of %s at %tT%n", this.hashCode(), nEvals,
chosenConfigs, System.currentTimeMillis());
}
Object configuredThing = searchSpace.getAgent(settings);
boolean tuningPlayer = configuredThing instanceof AbstractPlayer;
boolean tuningGame = configuredThing instanceof Game;
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