Skip to content

Commit

Permalink
Fixes uninitialized variable count averaging without floating point a…
Browse files Browse the repository at this point in the history
…rithmetic.

Round up, more is worse.
  • Loading branch information
oskopek committed Sep 1, 2015
1 parent e2627d3 commit d0be46e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -268,7 +268,12 @@ public String getTotalScoreWithUninitializedPrefix() {
} }


public String getAverageScoreWithUninitializedPrefix() { public String getAverageScoreWithUninitializedPrefix() {
return ScoreUtils.getScoreWithUninitializedPrefix(Math.round(Math.ceil(totalUninitializedVariableCount / (double) getSingleBenchmarkResultList().size())), averageScore); int dividend = getTotalUninitializedVariableCount();
int divisor = getSingleBenchmarkResultList().size();
if (divisor == 0) {
throw new IllegalStateException("Cannot calculate average score of 0 single benchmarks.");
}
return ScoreUtils.getScoreWithUninitializedPrefix((dividend / divisor) + (dividend % divisor != 0 ? 1 : 0), getAverageScore());
} }


public EnvironmentMode getEnvironmentMode() { public EnvironmentMode getEnvironmentMode() {
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2015 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.optaplanner.benchmark.impl.result;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.Test;
import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

public class SolverBenchmarkResultTest {

@Test
public void testGetAverageScoreWithUninitializedPrefix() throws Exception {
SolverBenchmarkResult solverBenchmarkResult = spy(new SolverBenchmarkResult(null));
SingleBenchmarkResult singleBenchmarkResult1 = mock(SingleBenchmarkResult.class);
SingleBenchmarkResult singleBenchmarkResult2 = mock(SingleBenchmarkResult.class);
when(solverBenchmarkResult.getSingleBenchmarkResultList()).thenReturn(Arrays.asList(singleBenchmarkResult1, singleBenchmarkResult2));
when(solverBenchmarkResult.getAverageScore()).thenReturn(HardSoftScore.valueOf(-10, -100));
when(solverBenchmarkResult.getTotalUninitializedVariableCount()).thenReturn(0);
assertEquals("-10hard/-100soft", solverBenchmarkResult.getAverageScoreWithUninitializedPrefix());
when(solverBenchmarkResult.getTotalUninitializedVariableCount()).thenReturn(2);
assertEquals("1uninitialized/-10hard/-100soft", solverBenchmarkResult.getAverageScoreWithUninitializedPrefix());
when(solverBenchmarkResult.getTotalUninitializedVariableCount()).thenReturn(3);
assertEquals("2uninitialized/-10hard/-100soft", solverBenchmarkResult.getAverageScoreWithUninitializedPrefix());
when(solverBenchmarkResult.getTotalUninitializedVariableCount()).thenReturn(1);
assertEquals("1uninitialized/-10hard/-100soft", solverBenchmarkResult.getAverageScoreWithUninitializedPrefix());
}

@Test(expected = IllegalStateException.class)
public void testZeroDivisorGetAverageScoreWithUninitializedPrefix() throws Exception {
SolverBenchmarkResult solverBenchmarkResult = spy(new SolverBenchmarkResult(null));
when(solverBenchmarkResult.getSingleBenchmarkResultList()).thenReturn(Collections.<SingleBenchmarkResult>emptyList());
when(solverBenchmarkResult.getAverageScore()).thenReturn(HardSoftScore.valueOf(-10, -100));
when(solverBenchmarkResult.getTotalUninitializedVariableCount()).thenReturn(0);
solverBenchmarkResult.getAverageScoreWithUninitializedPrefix();
}
}
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static double calculateTimeGradient(Number[] totalDiffNumbers, Number[] s
} }


// TODO remove me (and all occurences) once https://issues.jboss.org/browse/PLANNER-405 is fixed // TODO remove me (and all occurences) once https://issues.jboss.org/browse/PLANNER-405 is fixed
public static String getScoreWithUninitializedPrefix(long uninitializedVariableCount, Score score) { public static String getScoreWithUninitializedPrefix(int uninitializedVariableCount, Score score) {
if (score == null) { if (score == null) {
return null; // return null, not String "null" so we can use a default value return null; // return null, not String "null" so we can use a default value
} }
Expand Down

0 comments on commit d0be46e

Please sign in to comment.