-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathEpsilonGreedy.scala
More file actions
27 lines (24 loc) · 813 Bytes
/
EpsilonGreedy.scala
File metadata and controls
27 lines (24 loc) · 813 Bytes
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
// Wei Chen - Epsilon Greedy Search
// 2020-03-08
package com.scalaml.algorithm
class EpsilonGreedy {
var currentScores: Array[Double] = null
def search(
evaluation: Array[Double] => Double,
choices: Array[Array[Double]],
scores: Array[Double] = null,
epsilon: Double = 0.1
): Array[Double] = {
val size = choices.size
if (scores != null)
currentScores = scores
if (currentScores == null)
currentScores = Array.fill[Double](size)(Double.MinValue)
if (math.random < epsilon) {
val randSelect = (math.random * size).toInt
val value = evaluation(choices(randSelect))
currentScores(randSelect) = value
}
choices(currentScores.indexOf(currentScores.max))
}
}