Skip to content

Commit

Permalink
Optional instanceName and Score in SolutionGeneratedEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
rmartinsanta committed Aug 30, 2024
1 parent 7dc130f commit 341594b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public class SolutionGeneratedEvent<S extends Solution<S,I>, I extends Instance>
private final String instanceName;
private final String algorithmName;
private final MetricsStorage metrics;
private final boolean success;
private final String iteration;
private final String instancePath;
private final double score;
private final long executionTime;
private final long timeToBest;
Expand All @@ -39,18 +41,25 @@ public class SolutionGeneratedEvent<S extends Solution<S,I>, I extends Instance>
* @param timeToBest time needed ot reach the best solution. timeToBest = totalTime - timeSinceLastModification
* @param metrics both framework calculated and user defined metrics
*/
public SolutionGeneratedEvent(boolean success, String iteration, S solution, String experimentName, Algorithm<S, I> algorithm, long executionTime, long timeToBest, MetricsStorage metrics) {
public SolutionGeneratedEvent(boolean success, String iteration, String instancePath, S solution, String experimentName, Algorithm<S, I> algorithm, long executionTime, long timeToBest, MetricsStorage metrics) {
super();
this.success = success;
this.iteration = iteration;
this.score = solution.getScore();
this.instanceName = solution.getInstance().getId();
this.instancePath = instancePath;
this.solution = new SoftReference<>(solution);
this.experimentName = experimentName;
this.algorithm = algorithm;
this.executionTime = executionTime;
this.timeToBest = timeToBest;
this.algorithmName = algorithm.getName();
this.metrics = metrics;
if(solution != null){
this.score = solution.getScore();
this.instanceName = solution.getInstance().getId();
} else {
this.score = Double.NaN;
this.instanceName = "Unknown";
}
}

/**
Expand Down Expand Up @@ -142,4 +151,12 @@ public Optional<S> getSolution() {
public MetricsStorage getMetrics() {
return metrics;
}

/**
* Was the solution generated successfully?
* @return true if the solution was generated successfully, false otherwise
*/
public boolean isSuccess() {
return success;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ protected void processWorkUnitResult(WorkUnitResult<S, I> r, ProgressBar pb) {
if(r.success()){
io.exportSolution(r, SolutionExportFrequency.ALL);
}
var solutionGenerated = new SolutionGeneratedEvent<>(r.success(), r.iteration(), r.solution(), r.experimentName(), r.algorithm(), r.executionTime(), r.timeToTarget(), r.metrics());

var solutionGenerated = new SolutionGeneratedEvent<>(r.success(), r.iteration(), r.instancePath(), r.solution(), r.experimentName(), r.algorithm(), r.executionTime(), r.timeToTarget(), r.metrics());
EventPublisher.getInstance().publishEvent(solutionGenerated);
if (log.isDebugEnabled()) {
log.debug(String.format("\t%s.\tT(s): %.3f \tTTB(s): %.3f \t%s", r.iteration(), nanosToSecs(r.executionTime()), nanosToSecs(r.timeToTarget()), r.solution()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class TestHelperFactory {
public static SolutionGeneratedEvent<TestSolution, TestInstance> solutionGenerated(String instanceName, String expName, String algName, int iter, double score, long time, long ttb){
var solution = new TestSolution(new TestInstance(instanceName), score);
var algorithm = new TestAlgorithm(algName);
return new SolutionGeneratedEvent<>(true, String.valueOf(iter), solution, expName, algorithm, time, ttb, new MetricsStorage());
return new SolutionGeneratedEvent<>(true, String.valueOf(iter), instanceName, solution, expName, algorithm, time, ttb, new MetricsStorage());
}

// public static SolutionGeneratedEvent<TestSolution, TestInstance> solutionGenerated(String instanceName, String expName, String algName, int iter, double score, long time, long ttb, Map<String, TreeSet<TimeValue>> properties){
Expand Down

0 comments on commit 341594b

Please sign in to comment.