public class ProbabilityMoveSelectorTest {
@Test
public void randomSelection() {
MoveSelector childMoveSelector = SelectorTestUtils.mockMoveSelector(DummyMove.class,
new DummyMove("e1"), new DummyMove("e2"), new DummyMove("e3"), new DummyMove("e4"));
SelectionProbabilityWeightFactory<DummyMove> probabilityWeightFactory = new SelectionProbabilityWeightFactory<DummyMove>() {
public double createProbabilityWeight(ScoreDirector scoreDirector, DummyMove move) {
if (move.getCode().equals("e1")) {
return 1000.0;
} else if (move.getCode().equals("e2")) {
return 200.0;
} else if (move.getCode().equals("e3")) {
return 30.0;
} else if (move.getCode().equals("e4")) {
return 4.0;
} else {
throw new IllegalStateException("Unknown move (" + move + ").");
}
}
};
MoveSelector moveSelector = new ProbabilityMoveSelector(childMoveSelector, SelectionCacheType.STEP,
probabilityWeightFactory);
Random workingRandom = mock(Random.class);
when(workingRandom.nextDouble()).thenReturn(1222.0 / 1234.0, 111.0 / 1234.0, 0.0, 1230.0 / 1234.0, 1199.0 / 1234.0);
DefaultSolverScope solverScope = mock(DefaultSolverScope.class);
when(solverScope.getWorkingRandom()).thenReturn(workingRandom);
moveSelector.solvingStarted(solverScope);
AbstractPhaseScope phaseScopeA = mock(AbstractPhaseScope.class);
when(phaseScopeA.getSolverScope()).thenReturn(solverScope);
when(phaseScopeA.getWorkingRandom()).thenReturn(workingRandom);
moveSelector.phaseStarted(phaseScopeA);
AbstractStepScope stepScopeA1 = mock(AbstractStepScope.class);
when(stepScopeA1.getPhaseScope()).thenReturn(phaseScopeA);
when(stepScopeA1.getWorkingRandom()).thenReturn(workingRandom);
moveSelector.stepStarted(stepScopeA1);
assertCodesOfNeverEndingMoveSelector(moveSelector, 4L, "e3", "e1", "e1", "e4", "e2");
moveSelector.stepEnded(stepScopeA1);
moveSelector.phaseEnded(phaseScopeA);
moveSelector.solvingEnded(solverScope);
verifyPhaseLifecycle(childMoveSelector, 1, 1, 1);
verify(childMoveSelector, times(1)).iterator();
}