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
|
"""Tests for the MonteCarlo algorithm."""
import unittest
from imago.engine.decisionAlgorithm import DecisionAlgorithm
from imago.gameLogic.gameState import GameState
from imago.engine.monteCarlo import MCTS
from imago.engine.monteCarlo import MCTSNode
TEST_BOARD_SIZE = 9
class TestMonteCarlo(unittest.TestCase):
"""Test MonteCarlo algorithm."""
def testPickMove(self):
"""Test picking a move."""
state = GameState(TEST_BOARD_SIZE)
tree = MCTS(state.lastMove)
#print(tree.pickMove().toString())
def testForceNextMove(self):
"""Test forcing next move."""
# Next move before expansion (no children nodes)
state = GameState(TEST_BOARD_SIZE)
tree = MCTS(state.lastMove)
self.assertEqual(set(), tree.root.children)
tree.forceNextMove((0, 1))
self.assertEqual(set(), tree.root.children)
# Next move after expansion (with children nodes)
tree.expansions = 2
tree.simulationsPerExpansion = 2
tree.pickMove()
self.assertEqual(tree.expansions, len(tree.root.children))
nextMoveCoords = list(tree.root.children)[0].move.coords
tree.forceNextMove(nextMoveCoords)
def testPass(self):
"""Test passing as next move."""
state = GameState(TEST_BOARD_SIZE)
tree = MCTS(state.lastMove)
self.assertFalse(tree.root.move.isPass)
tree.forceNextMove("pass")
self.assertTrue(tree.root.move.isPass)
def testClearBoard(self):
"""Test clearing board returns root to original and retains information."""
state = GameState(TEST_BOARD_SIZE)
tree = MCTS(state.lastMove)
firstMoveCoords = (0,0)
secondMoveCoords = (1,0)
thirdMoveCoords = (0,1)
tree.forceNextMove(firstMoveCoords)
tree.forceNextMove(secondMoveCoords)
tree.forceNextMove(thirdMoveCoords)
tree.clearBoard()
nextNode = list(tree.root.children)[0]
self.assertEqual(firstMoveCoords, nextNode.move.coords)
nextNode = list(nextNode.children)[0]
self.assertEqual(secondMoveCoords, nextNode.move.coords)
nextNode = list(nextNode.children)[0]
self.assertEqual(thirdMoveCoords, nextNode.move.coords)
#def testSimulation(self):
# """Test Monte Carlo simulation."""
# board = GameBoard(TEST_BOARD_SIZE, TEST_BOARD_SIZE)
# move = GameMove(board)
# node = MCTSNode(move, None)
# nMatches = 100
# scoreDiffHeur = 15
# node.simulation(nMatches, scoreDiffHeur)
if __name__ == '__main__':
unittest.main()
|