From 65ac3a6b050dcb88688cdc2654b1ed6693e9a160 Mon Sep 17 00:00:00 2001 From: InigoGutierrez Date: Mon, 12 Jun 2023 19:43:40 +0200 Subject: Submitted version. --- tests/test_gameBoard.py | 28 +++++++++++++++++++ tests/test_gameState.py | 2 +- tests/test_imagoIO.py | 9 ++++-- tests/test_monteCarlo.py | 2 +- tests/test_neuralNetwork.py | 68 +++++++++++++++++++++++++++++++++++++++++++++ tests/test_sgf.py | 27 ++++++++++++++++++ 6 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 tests/test_sgf.py (limited to 'tests') diff --git a/tests/test_gameBoard.py b/tests/test_gameBoard.py index 8a7b127..c7808ac 100644 --- a/tests/test_gameBoard.py +++ b/tests/test_gameBoard.py @@ -114,5 +114,33 @@ class TestGameBoard(unittest.TestCase): board.board[9][0] = Player.WHITE self.assertEqual((9, 21), board.score()) + def testToString(self): + """Test formatting of the board as a string.""" + + board = GameBoard(9, 9) + self.assertEqual(' A B C D E F G H J \n\ +9 · · · · · · · · · \n\ +8 · · · · · · · · · \n\ +7 · · · · · · · · · \n\ +6 · · · · · · · · · \n\ +5 · · · · · · · · · \n\ +4 · · · · · · · · · \n\ +3 · · · · · · · · · \n\ +2 · · · · · · · · · \n\ +1 · · · · · · · · · ', board.toString()) + + board.moveAndCapture(2, 6, Player.BLACK) + board.moveAndCapture(5, 4, Player.WHITE) + self.assertEqual(' A B C D E F G H J \n\ +9 · · · · · · · · · \n\ +8 · · · · · · · · · \n\ +7 · · · · · · B · · \n\ +6 · · · · · · · · · \n\ +5 · · · · · · · · · \n\ +4 · · · · W · · · · \n\ +3 · · · · · · · · · \n\ +2 · · · · · · · · · \n\ +1 · · · · · · · · · ', board.toString()) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_gameState.py b/tests/test_gameState.py index 638e269..1c6b997 100644 --- a/tests/test_gameState.py +++ b/tests/test_gameState.py @@ -73,7 +73,7 @@ class TestGameState(unittest.TestCase): ]) - self.assertFalse(state.playMove(-1, -1)) + self.assertRaises(Exception, state.playMove, -1, -1) self.assertEqual(state.getBoard().getBoard(), [ [Player.EMPTY, Player.EMPTY, Player.EMPTY], diff --git a/tests/test_imagoIO.py b/tests/test_imagoIO.py index 4f2d7bd..499fdb2 100644 --- a/tests/test_imagoIO.py +++ b/tests/test_imagoIO.py @@ -81,7 +81,8 @@ class TestImagoIO(unittest.TestCase): '= \n\n' + '= \n\n' + '= \n\n' + - '? unknown command\n\n', + '? unknown command\n\n' + + '= \n\n', value ) @@ -112,7 +113,8 @@ class TestImagoIO(unittest.TestCase): self.assertEqual( '= ' + commandsString + - '\n\n\n', + '\n\n' + + '= \n\n', value ) @@ -140,7 +142,8 @@ class TestImagoIO(unittest.TestCase): self.assertEqual( '? Wrong number of arguments\n' + '? Usage: fixed_handicap \n\n' + - '= A1 A2\n\n', + '= A1 A2\n\n' + + '= \n\n', value ) diff --git a/tests/test_monteCarlo.py b/tests/test_monteCarlo.py index 496c073..9d1fcfc 100644 --- a/tests/test_monteCarlo.py +++ b/tests/test_monteCarlo.py @@ -66,7 +66,7 @@ class TestMonteCarlo(unittest.TestCase): self.assertEqual(thirdMoveCoords, nextNode.move.coords) #def testSimulation(self): - # """Test calculation of group liberties.""" + # """Test Monte Carlo simulation.""" # board = GameBoard(TEST_BOARD_SIZE, TEST_BOARD_SIZE) # move = GameMove(board) # node = MCTSNode(move, None) diff --git a/tests/test_neuralNetwork.py b/tests/test_neuralNetwork.py index dfcbd7a..42ba4a1 100644 --- a/tests/test_neuralNetwork.py +++ b/tests/test_neuralNetwork.py @@ -1,8 +1,16 @@ """Tests for neural network module.""" +import os +import shutil import unittest +from imago.data.enums import DecisionAlgorithms +from imago.sgfParser.sgf import loadGameTree +from imago.gameLogic.gameState import GameState from imago.engine.keras.neuralNetwork import NeuralNetwork +from imago.engine.keras.denseNeuralNetwork import DenseNeuralNetwork +from imago.engine.keras.convNeuralNetwork import ConvNeuralNetwork +from imago.engine.keras.keras import Keras class TestNeuralNetwork(unittest.TestCase): """Test neural network module.""" @@ -13,3 +21,63 @@ class TestNeuralNetwork(unittest.TestCase): self.assertRaises(NotImplementedError, NeuralNetwork, "non/existing/file") + + def testNetworks(self): + """Test creation of initial model for dense neural network""" + + testModel = 'testModel' + testModelPlot = 'testModelPlot' + + games = [] + for file in [ + '../collections/minigo/matches/1.sgf', + '../collections/minigo/matches/2.sgf', + '../collections/minigo/matches/3.sgf' + ]: + games.append(loadGameTree(file)) + matches = [game.getMainLineOfPlay() for game in games] + + nn = DenseNeuralNetwork(modelPath=testModel, boardSize=9) + nn.trainModel(matches, epochs=1, verbose=0) + + game = GameState(9) + nn.pickMove(game.lastMove, game.getCurrentPlayer()) + + nn.saveModel(testModel) + self.assertTrue(os.path.isdir(testModel)) + shutil.rmtree(testModel, ignore_errors=True) + + nn.saveModel() + self.assertTrue(os.path.isdir(testModel)) + nn = DenseNeuralNetwork(modelPath=testModel, boardSize=9) + + nn.saveModelPlot(testModelPlot) + self.assertTrue(os.path.isfile(testModelPlot)) + + shutil.rmtree(testModel, ignore_errors=True) + os.remove(testModelPlot) + + nn = ConvNeuralNetwork(testModel, boardSize=9) + + def testKeras(self): + """Test keras model loading.""" + + gameState = GameState(9) + move = gameState.lastMove + + keras = Keras(move) + keras.forceNextMove("pass") + + keras = Keras(move, DecisionAlgorithms.DENSE) + keras.forceNextMove((3,3)) + + keras = Keras(move, DecisionAlgorithms.CONV) + self.assertRaises(RuntimeError, keras.forceNextMove, "wrongmove") + pickedCoords = keras.pickMove() + self.assertTrue(len(pickedCoords) == 2 or pickedCoords == "pass") + + self.assertRaises(RuntimeError, Keras, move, DecisionAlgorithms.MONTECARLO) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_sgf.py b/tests/test_sgf.py new file mode 100644 index 0000000..1266429 --- /dev/null +++ b/tests/test_sgf.py @@ -0,0 +1,27 @@ +"""Tests for processing of SGF files.""" + +import unittest + +from imago.sgfParser.sgfyacc import parser + +TESTING_SGF = 'tests/testingSGF.sgf' + +class TestSGF(unittest.TestCase): + """Test processing SGF files.""" + + def testToGameTree(self): + """Test converting file to GameTree""" + + file = open(TESTING_SGF, "r") + text = file.read() + file.close() + + astNode = parser.parse(text) + + astNode.toGameMoveTree() + + astNode.toString() + + +if __name__ == '__main__': + unittest.main() -- cgit v1.2.1