-
Notifications
You must be signed in to change notification settings - Fork 1
refactor: problem bound structure #143
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
base: develop
Are you sure you want to change the base?
Changes from all commits
23dd65e
ba9d219
0a4921f
f686629
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 |
|---|---|---|
| @@ -1,11 +1,68 @@ | ||
| package edu.kit.provideq.toolbox.api; | ||
|
|
||
| import edu.kit.provideq.toolbox.BoundWithInfo; | ||
| import edu.kit.provideq.toolbox.Solution; | ||
|
|
||
| public record ComparisonDto(float comparison, BoundWithInfo bound, Solution<?> solution) { | ||
|
Contributor
Author
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. It would be nice if you included an JavaDoc for this class |
||
| /** | ||
| * A DTO for a comparison between a bound and a solution. | ||
| */ | ||
| public class ComparisonDto { | ||
| private BoundDto bound; | ||
| private float comparison; | ||
|
|
||
| /** | ||
| * Creates a new comparison DTO out of a comparison value and a bound. | ||
| * | ||
| * @param comparison the comparison value, e.g., the ratio of the solution to the bound | ||
| * @param bound a bound | ||
| */ | ||
| public ComparisonDto(float comparison, BoundWithInfo bound) { | ||
| this.comparison = comparison; | ||
| this.bound = new BoundDto(bound); | ||
| } | ||
|
|
||
| public ComparisonDto() { | ||
| this.comparison = -1; | ||
| this.bound = null; | ||
| } | ||
|
|
||
|
|
||
| @Override public String toString() { | ||
| return "Comparison{comparison=%f, bound=%s, solution=%s}" | ||
| .formatted(comparison, bound, solution); | ||
| return "Comparison{bound=%s, comparison=%f}" | ||
| .formatted(bound, comparison); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the bound of the comparison as a BoundDto. | ||
| * | ||
| * @return the bound of the comparison, e.g., the best known solution or the optimal solution | ||
| */ | ||
| public BoundDto getBound() { | ||
| return bound; | ||
| } | ||
|
|
||
| public void setBound(BoundDto bound) { | ||
| this.bound = bound; | ||
| } | ||
|
|
||
| public void setBound(BoundWithInfo bound) { | ||
| this.bound = new BoundDto(bound); | ||
| } | ||
|
|
||
| public boolean hasBound() { | ||
| return bound != null; | ||
| } | ||
|
|
||
| public void setComparison(float comparison) { | ||
| this.comparison = comparison; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the comparison value of the comparison. | ||
| * | ||
| * @return the comparison value, e.g., the ratio of the solution to the bound. | ||
| */ | ||
| public float getComparison() { | ||
| return comparison; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
|
|
||
| import edu.kit.provideq.toolbox.BoundWithInfo; | ||
| import edu.kit.provideq.toolbox.Solution; | ||
| import edu.kit.provideq.toolbox.api.BoundDto; | ||
| import edu.kit.provideq.toolbox.api.ComparisonDto; | ||
| import edu.kit.provideq.toolbox.meta.setting.SolverSetting; | ||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
|
|
@@ -10,6 +12,7 @@ | |
| import java.util.Set; | ||
| import java.util.UUID; | ||
| import java.util.function.Consumer; | ||
| import java.util.regex.Pattern; | ||
| import java.util.stream.Collectors; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
|
|
@@ -30,7 +33,7 @@ public class Problem<InputT, ResultT> { | |
|
|
||
| private InputT input; | ||
| private Solution<ResultT> solution; | ||
| private BoundWithInfo bound; | ||
| private final ComparisonDto boundWithComparison = new ComparisonDto(); | ||
| private ProblemState state; | ||
| private ProblemSolver<InputT, ResultT> solver; | ||
| private List<SolverSetting> solverSettings; | ||
|
|
@@ -84,6 +87,10 @@ public Mono<Solution<ResultT>> solve() { | |
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Estimates a bound for the problem's solution. Uses the estimator provided by the problem type. | ||
| * Also sets the execution time of the estimation in the boundWithComparison object. | ||
| */ | ||
| public void estimateBound() { | ||
| if (this.input == null) { | ||
| throw new IllegalStateException("Cannot estimate value without input!"); | ||
|
|
@@ -93,6 +100,7 @@ public void estimateBound() { | |
| if (optionalEstimator.isEmpty()) { | ||
| throw new IllegalStateException("Cannot estimate value without an estimator!"); | ||
| } | ||
|
|
||
| var estimator = optionalEstimator.get(); | ||
|
|
||
| long start = System.currentTimeMillis(); | ||
|
|
@@ -101,7 +109,28 @@ public void estimateBound() { | |
| long finish = System.currentTimeMillis(); | ||
| var executionTime = finish - start; | ||
|
|
||
| this.bound = new BoundWithInfo(estimatedBound, executionTime); | ||
| this.boundWithComparison.setBound(new BoundWithInfo(estimatedBound, executionTime)); | ||
| } | ||
|
|
||
| /** | ||
| * Compares the current solution with the bound according to the bound type. | ||
| */ | ||
| public void compareBound() { | ||
| if (this.solution == null) { | ||
|
Contributor
Author
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. Can we outsource this check to BoundWithInfo or ComparisonDto? It would be nice if we would have an indicator by a method call from the class which signalizes that the bound may not be set - isBoundSet() for instance? |
||
| throw new IllegalStateException("Cannot compare bound without solution!"); | ||
| } | ||
| if (!this.boundWithComparison.hasBound()) { | ||
| throw new IllegalStateException("Cannot compare bound without bound!"); | ||
| } | ||
|
|
||
| var bound = this.boundWithComparison.getBound(); | ||
| var solutionData = this.solution.getSolutionData(); | ||
|
|
||
| float solutionValue = getSolutionValue(solutionData); | ||
|
|
||
| var comparison = bound.boundType().compare(bound.bound(), solutionValue); | ||
|
|
||
| this.boundWithComparison.setComparison(comparison); | ||
| } | ||
|
|
||
| public UUID getId() { | ||
|
|
@@ -231,7 +260,23 @@ public String toString() { | |
| + '}'; | ||
| } | ||
|
|
||
| public Optional<BoundWithInfo> getBound() { | ||
| return Optional.ofNullable(bound); | ||
| public Optional<BoundDto> getBound() { | ||
| return Optional.ofNullable(boundWithComparison.getBound()); | ||
| } | ||
|
|
||
| public Optional<ComparisonDto> getBoundWithComparison() { | ||
| return Optional.of(boundWithComparison); | ||
| } | ||
|
|
||
| private float getSolutionValue(ResultT solutionData) { | ||
| var pattern = Pattern.compile(this.type.getSolutionPattern()); | ||
| var solutionMatcher = pattern.matcher(solutionData.toString()); | ||
| float solutionValue; | ||
| if (solutionMatcher.find()) { | ||
| solutionValue = Float.parseFloat(solutionMatcher.group(1)); | ||
| } else { | ||
| throw new IllegalStateException("Solution does not match the expected pattern!"); | ||
| } | ||
| return solutionValue; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.