forked from Wei-1/Scala-Machine-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEpsilonGreedyTest.scala
48 lines (36 loc) · 1.31 KB
/
EpsilonGreedyTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Wei Chen - Epsilon Greedy Search Test
// 2020-03-08
import com.scalaml.general.MatrixFunc._
import com.scalaml.algorithm.EpsilonGreedy
import org.scalatest.funsuite.AnyFunSuite
class EpsilonGreedySuite extends AnyFunSuite {
val eg = new EpsilonGreedy()
def evaluation(arr: Array[Double]): Double = 1 / ((arr.head - 0.7).abs + 1)
val choices: Array[Array[Double]] = Array(
Array(0.7),
Array(0.8),
Array(1.0),
Array(0.5)
)
val epsilon: Double = 0.1
test("EpsilonGreedy Test : Initial") {
assert(eg.currentScores == null)
}
test("EpsilonGreedy Test : Search - Start") {
for (i <- 0 until 1000)
eg.search(evaluation, choices, null, epsilon)
assert(eg.currentScores.size == choices.size)
val best = eg.search(evaluation, choices, null, epsilon)
assert((best.head - 0.7).abs < 0.05)
}
test("EpsilonGreedy Test : Search - Continue") {
var scores: Array[Double] = Array(0, 0, 1 / 1.3, 0)
for (i <- 0 until 1000) {
eg.search(evaluation, choices, scores, epsilon)
scores = eg.currentScores
}
assert(eg.currentScores.size == scores.size)
val best = eg.search(evaluation, choices, scores, epsilon)
assert((best.head - 0.7).abs < 0.05)
}
}