forked from Wei-1/Scala-Machine-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMCTSTest.scala
102 lines (84 loc) · 3.17 KB
/
MCTSTest.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Wei Chen - Monte Carlo Tree Search Test
// 2017-08-08
import com.scalaml.general.MatrixFunc._
import com.scalaml.algorithm.MCTS
import org.scalatest.funsuite.AnyFunSuite
class MCTSSuite extends AnyFunSuite {
{
val mcts = new MCTS
def simulation(state: Array[Double]): Double = {
val statei = state.zipWithIndex
val myi = statei.maxBy(_._1)._2
val eni = statei.minBy(_._1)._2
if (myi == eni) -1.0
else 1.0
}
def actions(state: Array[Double]): Array[Array[Double]] = {
val statei = state.zipWithIndex
val myi = statei.maxBy(_._1)._2
val eni = statei.minBy(_._1)._2
val newstate = Array(0.0, 0.0, 0.0, 0.0)
if (myi == eni) {
Array(newstate)
} else {
val mymoves = Array((myi + 3) % 4, myi, (myi + 1) % 4)
val enmoves = Array((eni + 3) % 4, eni, (eni + 1) % 4)
mymoves.map { i =>
if (enmoves.contains(i)) {
newstate.clone
} else {
val fei = enmoves.minBy(ei => (i - ei).abs % 3)
val ns = newstate.clone
ns(i) = 1.0
ns(fei) = -1.0
ns
}
}
}
}
test("MCTS Test : Search Test 1") {
val state = Array(-1.0, 1.0, 0.0, 0.0)
val iter: Int = 4
val node = mcts.search(simulation, actions, state, iter)
assert(arrayequal(node.init, Array(0.0, -1.0, 1.0, 0.0)) || arrayequal(node.init, Array(0.0, 0.0, 1.0, -1.0)))
assert(node.score == 1.0)
}
test("MCTS Test : Search Test 2") {
val state = Array(-1.0, 0.0, 1.0, 0.0)
val iter: Int = 2
val node = mcts.search(simulation, actions, state, iter)
assert(arrayequal(node.init, Array(0.0, -1.0, 1.0, 0.0)) || arrayequal(node.init, Array(0.0, 0.0, 1.0, -1.0)))
assert(node.score == 1.0)
}
}
{
val mcts = new MCTS
def simulation(state: Array[Double]): Double = {
val statei = state.zipWithIndex
val i = statei.maxBy(_._1)._2
i match {
case 0 => 1
case 1 => 2
case 2 => 3
case 3 => 0
}
}
def actions(state: Array[Double]): Array[Array[Double]] = {
val statei = state.zipWithIndex
val i = statei.maxBy(_._1)._2
i match {
case 0 => Array(Array(0, 1, 0, 0), Array(0, 0, 1, 0))
case 1 => Array(Array(0, 1, 0, 0))
case 2 => Array(Array(0, 0, 0, 1))
case 3 => Array(Array(0, 0, 0, 1))
}
}
test("MCTS Test : Search Test 3") {
val state = Array(1.0, 0.0, 0.0, 0.0)
val iter: Int = 4
val node = mcts.search(simulation, actions, state, iter)
assert(arrayequal(node.init, Array(0.0, 1.0, 0.0, 0.0)))
assert(node.score == 2.0)
}
}
}