Skip to content

Metrics performance fix #320

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 1 commit 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
12 changes: 10 additions & 2 deletions src/main/java/evaluation/listeners/MetricsGameListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class MetricsGameListener implements IGameListener {

// Destination directory for the reports
String destDir = "metrics/out/"; //by default
boolean firstReport;

public MetricsGameListener() {
}
Expand All @@ -61,6 +62,7 @@ public MetricsGameListener(IDataLogger.ReportDestination logTo, IDataLogger.Repo
reportDestinations = Collections.singletonList(logTo);
this.reportTypes = Arrays.asList(dataTypes);
this.metrics = new LinkedHashMap<>();
this.firstReport = true;
for (AbstractMetric m : metrics) {
m.setDataLogger(new DataTableSaw(m)); //todo this logger needs to be read from JSON
this.metrics.put(m.getName(), m);
Expand Down Expand Up @@ -128,7 +130,7 @@ public void report() {
// of redundant directories
if (!(reportTypes.size() == 1 && reportTypes.contains(RawDataPerEvent)))
for (AbstractMetric metric : metrics.values()) {
metric.report(destDir, reportTypes, reportDestinations);
metric.report(destDir, reportTypes, reportDestinations, !firstReport);
}

// We also create raw data files for groups of metrics responding to the same event
Expand All @@ -142,9 +144,15 @@ public void report() {
}
if (!eventMetrics.isEmpty()) {
IDataLogger dataLogger = new DataTableSaw(eventMetrics, event, eventToIndexingColumn(event));
dataLogger.getDefaultProcessor().processRawDataToFile(dataLogger, destDir);
dataLogger.getDefaultProcessor().processRawDataToFile(dataLogger, destDir, !firstReport);
}
}
//Clean the data. We don't want to keep this in memory; instead we append after every reporting.
for (AbstractMetric metric : metrics.values()) {
IDataLogger dataLogger = metric.getDataLogger();
dataLogger.flush();
}
firstReport = false;
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/main/java/evaluation/metrics/AbstractMetric.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ public boolean filterByEventTypeWhenReporting() {
* @param reportTypes - list of report types to produce
* @param reportDestinations - list of report destinations to produce
*/
public void report(String folderName, List<IDataLogger.ReportType> reportTypes, List<IDataLogger.ReportDestination> reportDestinations)
public void report(String folderName,
List<IDataLogger.ReportType> reportTypes,
List<IDataLogger.ReportDestination> reportDestinations,
boolean append)
{
//DataProcessor with compatibility assertion:
IDataProcessor dataProcessor = getDataProcessor();
Expand All @@ -217,7 +220,7 @@ public void report(String folderName, List<IDataLogger.ReportType> reportTypes,

if (reportType == IDataLogger.ReportType.RawData) {
if (reportDestination == IDataLogger.ReportDestination.ToFile || reportDestination == IDataLogger.ReportDestination.ToBoth) {
dataProcessor.processRawDataToFile(dataLogger, folderName);
dataProcessor.processRawDataToFile(dataLogger, folderName, append);
}
if (reportDestination == IDataLogger.ReportDestination.ToConsole || reportDestination == IDataLogger.ReportDestination.ToBoth) {
dataProcessor.processRawDataToConsole(dataLogger);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/evaluation/metrics/IDataLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ default void reset() {}
*/
IDataProcessor getDefaultProcessor();

void flush();

IDataLogger copy();
IDataLogger emptyCopy();
IDataLogger create();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/evaluation/metrics/IDataProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface IDataProcessor
* @param logger - logger that contains the raw data
* @param folderName - name of the folder to save the file to
*/
void processRawDataToFile(IDataLogger logger, String folderName);
void processRawDataToFile(IDataLogger logger, String folderName, boolean append);


/**
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/evaluation/metrics/TournamentMetric.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ public class TournamentMetric extends AbstractMetric {

AbstractMetric wrappedMetric;

private boolean firstReport;

public TournamentMetric(AbstractMetric metric) {
super(metric.getEventTypes());
this.wrappedMetric = metric;
this.firstReport = true;
}

/**
Expand Down Expand Up @@ -118,7 +121,7 @@ public void report(String folderName, List<IDataLogger.ReportType> reportTypes,

if (reportType == IDataLogger.ReportType.RawData) {
if (reportDestination == IDataLogger.ReportDestination.ToFile || reportDestination == IDataLogger.ReportDestination.ToBoth) {
dataProcessor.processRawDataToFile(logger, folder);
dataProcessor.processRawDataToFile(logger, folder, !firstReport);
}
if (reportDestination == IDataLogger.ReportDestination.ToConsole || reportDestination == IDataLogger.ReportDestination.ToBoth) {
dataProcessor.processRawDataToConsole(logger);
Expand All @@ -140,5 +143,6 @@ public void report(String folderName, List<IDataLogger.ReportType> reportTypes,
}
}
}
firstReport = false;
}
}
6 changes: 6 additions & 0 deletions src/main/java/evaluation/metrics/tablessaw/DataTableSaw.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public IDataProcessor getDefaultProcessor() {
return new TableSawDataProcessor();
}

@Override
public void flush() {
this.data = data.emptyCopy();
}

@Override
public IDataLogger copy() {
return new DataTableSaw(metric, data.copy());
Expand Down Expand Up @@ -297,4 +302,5 @@ record = true;
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import evaluation.summarisers.TAGNumericStatSummary;
import tech.tablesaw.api.*;
import tech.tablesaw.columns.Column;
import tech.tablesaw.io.csv.CsvWriteOptions;
import tech.tablesaw.plotly.Plot;
import tech.tablesaw.plotly.api.LinePlot;
import tech.tablesaw.plotly.components.*;
Expand All @@ -26,9 +27,23 @@ public class TableSawDataProcessor implements IDataProcessor {


@Override
public void processRawDataToFile(IDataLogger logger, String folderName) {
public void processRawDataToFile(IDataLogger logger, String folderName, boolean append) {
DataTableSaw dts = (DataTableSaw) logger;
dts.data.write().csv(folderName + "/" + dts.data.name() + ".csv");
String filename = folderName + "/" + dts.data.name() + ".csv";
if(!append) {
dts.data.write().csv(filename);
} else {
try {
File file = new File(filename);
boolean headerNeeded = !file.exists();
Writer w = new FileWriter(file, true);
CsvWriteOptions.Builder options = CsvWriteOptions.builder(w);
options.header(headerNeeded);
dts.data.write().csv(options.build());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/evaluation/TunableParametersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public void toJSONWithDefaults() {
assertEquals(67, json.get("maxTreeDepth"));
assertEquals(0.56, params.getParameterValue("rolloutPolicyParams.temperature"));
assertEquals(0.56, ((JSONObject) json.get("rolloutPolicyParams")).get("temperature"));
assertEquals(Math.sqrt(2), (Double) json.get("K"), 0.002);
assertEquals(1.0, (Double) json.get("K"), 0.002);
assertEquals(false, json.get("useMASTAsActionHeuristic"));
assertEquals("BUDGET_FM_CALLS", json.get("budgetType"));

Expand Down