forked from Wei-1/Scala-Machine-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpperConfidenceBoundTest.scala
53 lines (41 loc) · 1.4 KB
/
UpperConfidenceBoundTest.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
49
50
51
52
53
// Wei Chen - Upper Confidence Bound Test
// 2020-03-08
import com.scalaml.general.MatrixFunc._
import com.scalaml.algorithm.UpperConfidenceBound
import org.scalatest.funsuite.AnyFunSuite
class UpperConfidenceBoundSuite extends AnyFunSuite {
val ucb = new UpperConfidenceBound()
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 c: Double = 1
test("UpperConfidenceBound Test : Initial") {
assert(ucb.currentStats == null)
}
test("UpperConfidenceBound Test : Search - Start") {
for (i <- 0 until 100)
ucb.search(evaluation, choices, null, c)
assert(ucb.currentStats.size == choices.size)
val best = ucb.search(evaluation, choices, null, c)
assert((best.head - 0.7).abs < 0.05)
}
test("UpperConfidenceBound Test : Search - Continue") {
var stats: Array[(Double, Int)] = Array(
(0, 0),
(0, 0),
(1 / 1.3, 1),
(0, 0)
)
for (i <- 0 until 100) {
ucb.search(evaluation, choices, stats, c)
stats = ucb.currentStats
}
assert(ucb.currentStats.size == stats.size)
val best = ucb.search(evaluation, choices, stats, c)
assert((best.head - 0.7).abs < 0.05)
}
}