Skip to content

Commit

Permalink
fix: address scale equal to NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
zepfred authored and triceo committed Aug 9, 2024
1 parent ec49da1 commit 3ffb49b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public String approximateProblemScaleAsFormattedString() {
}

String approximateProblemScaleAsFormattedString(Locale locale) {
if (Double.isNaN(approximateProblemSizeLog) || Double.isInfinite(approximateProblemSizeLog)) {
return "0";
}

if (approximateProblemSizeLog < 10) { // log_10(10_000_000_000) = 10
return "%s".formatted(format(Math.pow(10d, approximateProblemSizeLog), BASIC_FORMATTER, locale));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,11 @@ public double getProblemScale(ScoreDirector<Solution_> scoreDirector, Solution_
result += MathUtils.getPossibleArrangementsScaledApproximateLog(MathUtils.LOG_PRECISION, logBase,
totalListMovableValueCount, possibleTargetsForListValue);
}
return (result / (double) MathUtils.LOG_PRECISION) / MathUtils.getLogInBase(logBase, 10d);
var scale = (result / (double) MathUtils.LOG_PRECISION) / MathUtils.getLogInBase(logBase, 10d);
if (Double.isNaN(scale) || Double.isInfinite(scale)) {
return 0;
}
return scale;
}

public ProblemSizeStatistics getProblemSizeStatistics(ScoreDirector<Solution_> scoreDirector, Solution_ solution) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,25 @@ void formatApproximateProblemScale() {
statistics = getProblemSizeStatistics(321_123_456_789L);
assertThat(statistics.approximateProblemScaleAsFormattedString())
.isEqualTo("3.211235 × 10^11");

// scale = -infinity
statistics = new ProblemSizeStatistics(0L, 0L, 0L, Double.NEGATIVE_INFINITY);
assertThat(statistics.approximateProblemScaleAsFormattedString())
.isEqualTo("0");

// scale = +infinity
statistics = new ProblemSizeStatistics(0L, 0L, 0L, Double.POSITIVE_INFINITY);
assertThat(statistics.approximateProblemScaleAsFormattedString())
.isEqualTo("0");

// scale = NaN
statistics = new ProblemSizeStatistics(0L, 0L, 0L, Double.NaN);
assertThat(statistics.approximateProblemScaleAsFormattedString())
.isEqualTo("0");

// scale = 0
statistics = new ProblemSizeStatistics(0L, 0L, 0L, 0);
assertThat(statistics.approximateProblemScaleAsFormattedString())
.isEqualTo("1");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,23 @@ void problemScaleBasic() {
});
}

@Test
void emptyProblemScale() {
int valueCount = 27;
int entityCount = 27;
SolutionDescriptor<TestdataSolution> solutionDescriptor = TestdataSolution.buildSolutionDescriptor();
TestdataSolution solution = TestdataSolution.generateSolution(valueCount, entityCount);
solution.getValueList().clear();
assertSoftly(softly -> {
softly.assertThat(solutionDescriptor.getGenuineEntityCount(solution)).isEqualTo(entityCount);
softly.assertThat(solutionDescriptor.getGenuineVariableCount(solution)).isEqualTo(entityCount);
softly.assertThat(solutionDescriptor.getMaximumValueRangeSize(solution)).isEqualTo(0);
softly.assertThat(solutionDescriptor.getApproximateValueCount(solution)).isEqualTo(0);
softly.assertThat(solutionDescriptor.getProblemScale(null, solution))
.isEqualTo(0);
});
}

@Test
void problemScaleMultipleValueRanges() {
var solutionDescriptor = TestdataValueRangeSolution.buildSolutionDescriptor();
Expand Down

0 comments on commit 3ffb49b

Please sign in to comment.