Skip to content

Commit 3ffb49b

Browse files
zepfredtriceo
authored andcommitted
fix: address scale equal to NaN
1 parent ec49da1 commit 3ffb49b

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

core/src/main/java/ai/timefold/solver/core/api/solver/ProblemSizeStatistics.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public String approximateProblemScaleAsFormattedString() {
3939
}
4040

4141
String approximateProblemScaleAsFormattedString(Locale locale) {
42+
if (Double.isNaN(approximateProblemSizeLog) || Double.isInfinite(approximateProblemSizeLog)) {
43+
return "0";
44+
}
45+
4246
if (approximateProblemSizeLog < 10) { // log_10(10_000_000_000) = 10
4347
return "%s".formatted(format(Math.pow(10d, approximateProblemSizeLog), BASIC_FORMATTER, locale));
4448
}

core/src/main/java/ai/timefold/solver/core/impl/domain/solution/descriptor/SolutionDescriptor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,11 @@ public double getProblemScale(ScoreDirector<Solution_> scoreDirector, Solution_
10951095
result += MathUtils.getPossibleArrangementsScaledApproximateLog(MathUtils.LOG_PRECISION, logBase,
10961096
totalListMovableValueCount, possibleTargetsForListValue);
10971097
}
1098-
return (result / (double) MathUtils.LOG_PRECISION) / MathUtils.getLogInBase(logBase, 10d);
1098+
var scale = (result / (double) MathUtils.LOG_PRECISION) / MathUtils.getLogInBase(logBase, 10d);
1099+
if (Double.isNaN(scale) || Double.isInfinite(scale)) {
1100+
return 0;
1101+
}
1102+
return scale;
10991103
}
11001104

11011105
public ProblemSizeStatistics getProblemSizeStatistics(ScoreDirector<Solution_> scoreDirector, Solution_ solution) {

core/src/test/java/ai/timefold/solver/core/api/solver/ProblemSizeStatisticsTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,25 @@ void formatApproximateProblemScale() {
6767
statistics = getProblemSizeStatistics(321_123_456_789L);
6868
assertThat(statistics.approximateProblemScaleAsFormattedString())
6969
.isEqualTo("3.211235 × 10^11");
70+
71+
// scale = -infinity
72+
statistics = new ProblemSizeStatistics(0L, 0L, 0L, Double.NEGATIVE_INFINITY);
73+
assertThat(statistics.approximateProblemScaleAsFormattedString())
74+
.isEqualTo("0");
75+
76+
// scale = +infinity
77+
statistics = new ProblemSizeStatistics(0L, 0L, 0L, Double.POSITIVE_INFINITY);
78+
assertThat(statistics.approximateProblemScaleAsFormattedString())
79+
.isEqualTo("0");
80+
81+
// scale = NaN
82+
statistics = new ProblemSizeStatistics(0L, 0L, 0L, Double.NaN);
83+
assertThat(statistics.approximateProblemScaleAsFormattedString())
84+
.isEqualTo("0");
85+
86+
// scale = 0
87+
statistics = new ProblemSizeStatistics(0L, 0L, 0L, 0);
88+
assertThat(statistics.approximateProblemScaleAsFormattedString())
89+
.isEqualTo("1");
7090
}
7191
}

core/src/test/java/ai/timefold/solver/core/impl/domain/solution/descriptor/SolutionDescriptorTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,23 @@ void problemScaleBasic() {
409409
});
410410
}
411411

412+
@Test
413+
void emptyProblemScale() {
414+
int valueCount = 27;
415+
int entityCount = 27;
416+
SolutionDescriptor<TestdataSolution> solutionDescriptor = TestdataSolution.buildSolutionDescriptor();
417+
TestdataSolution solution = TestdataSolution.generateSolution(valueCount, entityCount);
418+
solution.getValueList().clear();
419+
assertSoftly(softly -> {
420+
softly.assertThat(solutionDescriptor.getGenuineEntityCount(solution)).isEqualTo(entityCount);
421+
softly.assertThat(solutionDescriptor.getGenuineVariableCount(solution)).isEqualTo(entityCount);
422+
softly.assertThat(solutionDescriptor.getMaximumValueRangeSize(solution)).isEqualTo(0);
423+
softly.assertThat(solutionDescriptor.getApproximateValueCount(solution)).isEqualTo(0);
424+
softly.assertThat(solutionDescriptor.getProblemScale(null, solution))
425+
.isEqualTo(0);
426+
});
427+
}
428+
412429
@Test
413430
void problemScaleMultipleValueRanges() {
414431
var solutionDescriptor = TestdataValueRangeSolution.buildSolutionDescriptor();

0 commit comments

Comments
 (0)