Skip to content

Commit

Permalink
ValidationResult: allow accumulating errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rmartinsanta committed Dec 4, 2024
1 parent 38048f8 commit b7019cb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- (New) Ablation test: run ablation test by default after autoconfig procedure.
- (New) Irace plots: generate an HTML report with the results of running the autoconfig procedure.
- (New) Two ParetoSet implementations are provided: a simple list based (should be correct, but slow) and a NDTree based implementation (should be fast, but may contain bugs).
- (New) ValidationResult: allow accumulating errors.
- (Breaking) Due to changes in how objectives are handled, ReferenceResult methods have been renamed for clarity.
- (Fix) Math.random, Collections.shuffle now blocked using AspectJ instead of reflection. --add-opens no longer necessary.
- (Fix) Add @JsonIgnore to instance in solution class already exists in getter, but serializer may be configured to use fields instead of methods.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

import es.urjc.etsii.grafo.exception.InvalidSolutionException;

import java.util.ArrayList;
import java.util.List;

/**
* Result of validating a solution
*/
public class ValidationResult {
private final boolean isValid;
private final String reasonFailed;
private final List<String> reasonFailed = new ArrayList<>();

/**
* Is the solution valid?
*
* @return true if the solution passed all validations, false if any failed
*/
public boolean isValid() {
return isValid;
return reasonFailed.isEmpty();
}

/**
Expand All @@ -24,17 +26,20 @@ public boolean isValid() {
* @return cause if validation failure
*/
public String getReasonFailed() {
return reasonFailed;
if(this.reasonFailed.isEmpty()){
throw new IllegalStateException("Validation passed, no reason to get cause");
}
if(this.reasonFailed.size() == 1){
return this.reasonFailed.getFirst();
}
return "Multiple failures: " + String.join("; ", this.reasonFailed);
}

private ValidationResult(boolean isValid, String reasonFailed) {
this.isValid = isValid;
this.reasonFailed = reasonFailed;
}
private ValidationResult() {}

public void throwIfFail(){
if(!this.isValid()){
throw new InvalidSolutionException(this.reasonFailed);
throw new InvalidSolutionException(getReasonFailed());
}
}

Expand All @@ -44,7 +49,7 @@ public void throwIfFail(){
* @return ValidationResult
*/
public static ValidationResult ok(){
return new ValidationResult(true, "");
return new ValidationResult();
}

/**
Expand All @@ -54,6 +59,14 @@ public static ValidationResult ok(){
* @return ValidationResult
*/
public static ValidationResult fail(String reason){
return new ValidationResult(false, reason);
var vr = new ValidationResult();
return vr.addFailure(reason);
}

public ValidationResult addFailure(String reason) {
if(reason != null && !reason.isBlank()){
this.reasonFailed.add(reason);
}
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ public class __RNAME__SolutionValidator extends SolutionValidator<__RNAME__Solut
public ValidationResult validate(__RNAME__Solution solution) {
// You should implement this method to check that the solution is valid, without using any kind of existing caches or scores.
// For example, you can recalculate solution score and check if it matches the score stored in the solution.

var validationResult = ValidationResult.ok();
// Example checks:

// if(solution.getAssignedElements() > 10){
// return ValidationResult.fail("Cannot have more than 10 assigned elements");
// validationResult.addFailure("Cannot have more than 10 assigned elements");
// }

// double recalculateScore = {......};
// if(solution.getScore() != recalculateScore){
// return ValidationResult.fail("Score mismatch, expected: " + recalculateScore + ", got: " + solution.getScore());
// validationResult.addFailure("Score mismatch, expected: " + recalculateScore + ", got: " + solution.getScore());
// }

// if(!solution.unassignedClients.isEmpty()){
// return ValidationResult.fail("Invalid solution, all clients should be assigned. Remaining clients: " + solution.unassignedClients);
// validationResult.addFailure("Invalid solution, all clients should be assigned. Remaining clients: " + solution.unassignedClients);
// }

return ValidationResult.ok();
return validationResult;
}
}

0 comments on commit b7019cb

Please sign in to comment.