-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
98 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/scala/algorithm/optimization/UpperConfidenceBound.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Wei Chen - Upper Confidence Bound | ||
// 2020-03-08 | ||
|
||
package com.scalaml.algorithm | ||
|
||
class UpperConfidenceBound { | ||
var currentStats: Array[(Double, Int)] = null | ||
|
||
def select(c: Double): Int = { | ||
val n = currentStats.count(_._2 > 0) | ||
val currentScores = currentStats.map { case (m, kn) => | ||
m + c * math.sqrt(math.log(n + 1) / (kn + 1e-12)) | ||
} | ||
currentScores.indexOf(currentScores.max) | ||
} | ||
|
||
def add(i: Int, value: Double) { | ||
val (currentValue, currentCount) = currentStats(i) | ||
val newValue = (currentValue * currentCount + value) / (currentCount + 1) | ||
currentStats(i) = (newValue, currentCount + 1) | ||
} | ||
|
||
def search( | ||
evaluation: Array[Double] => Double, | ||
choices: Array[Array[Double]], | ||
scores: Array[(Double, Int)] = null, | ||
c: Double = 1 | ||
): Array[Double] = { | ||
val size = choices.size | ||
if (scores != null) | ||
currentStats = scores | ||
if (currentStats == null) | ||
currentStats = Array.fill[(Double, Int)](size)((0, 0)) | ||
val currentSelect = select(c) | ||
val value = evaluation(choices(currentSelect)) | ||
add(currentSelect, value) | ||
choices(currentStats.indexOf(currentStats.maxBy(_._1))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/test/scala/algorithm/optimization/UpperConfidenceBoundTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) | ||
} | ||
|
||
} |