|
| 1 | +// Wei Chen - Upper Confidence Bound Test |
| 2 | +// 2020-03-08 |
| 3 | + |
| 4 | +import com.scalaml.general.MatrixFunc._ |
| 5 | +import com.scalaml.algorithm.UpperConfidenceBound |
| 6 | +import org.scalatest.funsuite.AnyFunSuite |
| 7 | + |
| 8 | +class UpperConfidenceBoundSuite extends AnyFunSuite { |
| 9 | + |
| 10 | + val ucb = new UpperConfidenceBound() |
| 11 | + |
| 12 | + |
| 13 | + def evaluation(arr: Array[Double]): Double = 1 / ((arr.head - 0.7).abs + 1) |
| 14 | + |
| 15 | + val choices: Array[Array[Double]] = Array( |
| 16 | + Array(0.7), |
| 17 | + Array(0.8), |
| 18 | + Array(1.0), |
| 19 | + Array(0.5) |
| 20 | + ) |
| 21 | + val c: Double = 1 |
| 22 | + |
| 23 | + test("UpperConfidenceBound Test : Initial") { |
| 24 | + assert(ucb.currentStats == null) |
| 25 | + } |
| 26 | + |
| 27 | + test("UpperConfidenceBound Test : Search - Start") { |
| 28 | + for (i <- 0 until 100) |
| 29 | + ucb.search(evaluation, choices, null, c) |
| 30 | + assert(ucb.currentStats.size == choices.size) |
| 31 | + |
| 32 | + val best = ucb.search(evaluation, choices, null, c) |
| 33 | + assert((best.head - 0.7).abs < 0.05) |
| 34 | + } |
| 35 | + |
| 36 | + test("UpperConfidenceBound Test : Search - Continue") { |
| 37 | + var stats: Array[(Double, Int)] = Array( |
| 38 | + (0, 0), |
| 39 | + (0, 0), |
| 40 | + (1 / 1.3, 1), |
| 41 | + (0, 0) |
| 42 | + ) |
| 43 | + for (i <- 0 until 100) { |
| 44 | + ucb.search(evaluation, choices, stats, c) |
| 45 | + stats = ucb.currentStats |
| 46 | + } |
| 47 | + assert(ucb.currentStats.size == stats.size) |
| 48 | + |
| 49 | + val best = ucb.search(evaluation, choices, stats, c) |
| 50 | + assert((best.head - 0.7).abs < 0.05) |
| 51 | + } |
| 52 | + |
| 53 | +} |
0 commit comments