Skip to content

Commit 85921ca

Browse files
authored
Make the peek method behaviour in the path selector deterministic #598 (#600)
1 parent 45b7165 commit 85921ca

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

utbot-framework/src/main/kotlin/org/utbot/engine/selectors/nurs/NonUniformRandomSearch.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ abstract class NonUniformRandomSearch(
5858

5959
private val randomGen: Random? = seed?.let { Random(seed) }
6060

61+
// We use this value to avoid non-deterministic behaviour of the
62+
// peek method. Without it, we might have different states for
63+
// a sequence of the `peek` calls.
64+
// Now we remember it at the first `peek` call and use it
65+
// until a first `poll` call. The first call should reset it back to null.
66+
private var lastTakenRandomValue: Double? = null
67+
6168
override fun update() {
6269
executionQueue.updateAll()
6370
}
@@ -75,15 +82,22 @@ abstract class NonUniformRandomSearch(
7582
* with probability executionState.asWeight.weight / sumWeights
7683
*/
7784
override fun peekImpl(): ExecutionState? {
78-
val rand = (randomGen?.nextDouble() ?: 1.0) * sumWeights
85+
if (lastTakenRandomValue == null) {
86+
lastTakenRandomValue = randomGen?.nextDouble()
87+
}
88+
89+
val rand = (lastTakenRandomValue ?: 1.0) * sumWeights
7990

8091
return executionQueue.findLeftest(rand)?.first
8192
}
8293

8394
override fun removeImpl(state: ExecutionState): Boolean = executionQueue.remove(state)
8495

8596
override fun pollImpl(): ExecutionState? =
86-
peekImpl()?.also { remove(it) }
97+
peekImpl()?.also {
98+
remove(it)
99+
lastTakenRandomValue = null
100+
}
87101

88102
override fun isEmpty() =
89103
executionQueue.isEmpty()

0 commit comments

Comments
 (0)