def testBallOneMoveDoesNotAffectBoard(self): pongGame = Pong() ballPosition = pongGame.ballPosition pongGame = pongGame.makeMove(1, NOTHING) np.testing.assert_array_almost_equal(ballPosition, pongGame.ballPosition)
def testPlayerBothZeroRewardIfGameGoing(self): pongGame = Pong() pongGame.makeMove(1, NOTHING) pongGame.makeMove(2, NOTHING) self.assertEqual(pongGame.getReward(1), 0) self.assertEqual(pongGame.getReward(2), 0)
def testCalculateFeaturesReturnsCorrectlySizedArray(self): pongGame = Pong() numberOfFeatures = 3 player = 1 action = NOTHING result = len(pongGame.getFeatures(player, action)) self.assertEqual(numberOfFeatures, result)
def testCalculateFeaturesReturnsNotNoneObjects(self): pongGame = Pong() player = 1 action = NOTHING result = pongGame.getFeatures(player, action) for i in result: self.assertIsNotNone(i)
def testGameEndedAfterWinningMove(self): ballPosition = np.array([99.5, 2]) ballVelocity = np.array([2, 0.1]) pongGame = Pong(ballVelocity, ballPosition) pongGame = pongGame.makeMove(1, NOTHING) pongGame = pongGame.makeMove(2, NOTHING) self.assertTrue(pongGame.gameEnded())
def testPlayerTwoNegativeRewardIfLost(self): ballPosition = np.array([99.5, 2]) ballVelocity = np.array([2, 0.1]) pongGame = Pong(ballVelocity, ballPosition) expectedReward = -100 pongGame = pongGame.makeMove(1, NOTHING) pongGame = pongGame.makeMove(2, NOTHING) self.assertEqual(expectedReward, pongGame.getReward(2))
def testUpdateBallIfInCorner(self): ballPosition = np.array([99.5, 49.5]) ballVelocity = np.array([2, 2]) pongGame = Pong(ballVelocity, ballPosition) pongGame.p2pos = 40 expectedVelocity = np.array([-2, -2]) result = updateBall(pongGame) np.testing.assert_array_almost_equal(expectedVelocity, result[1])
def testBallMovesAfterTwoMoves(self): pongGame = Pong() ballPos = pongGame.ballPosition ballVelocity = pongGame.ballVelocity pongGame = pongGame.makeMove(1, UP) pongGame = pongGame.makeMove(2, NOTHING) np.testing.assert_array_almost_equal( np.array( [ballPos[0] + ballVelocity[0], ballPos[1] + ballVelocity[1]]), pongGame.ballPosition)
def testUpdateBallBounceOnLeftPaddle(self): ballVelocity = np.array([-0.9, 0.1]) ballPosition = np.array([0.5, 25]) pongGame = Pong(ballVelocity, ballPosition) expectedVelocity = np.array([0.9, 0.1]) result = updateBall(pongGame) np.testing.assert_array_almost_equal(expectedVelocity, result[1])
def testUpdateBallIfBallBouncesOnTopWall(self): ballVelocity = np.array([0.5, -0.8]) ballPosition = np.array([50, 2]) pongGame = Pong(ballVelocity, ballPosition) expectedVelocity = np.array([0.5, 0.8]) result = updateBall(pongGame) np.testing.assert_array_almost_equal(expectedVelocity, result[1])
numRepeatGames = 100 if game == BATTLESHIP: boardSize = 10 ships = [2, 3, 3, 4, 5] gameFunc = lambda: Battleship(boardSize, ships) if game == HEXAGRID: width = 13 height = 9 gameFunc = lambda: HexaGrid(width, height) if game == PONG: width = 100 height = 50 gameFunc = lambda: Pong(width, height) agent1 = QFunctionSGD(1, gameFunc().getNumFeatures(), batchSize=100, gamma=0.9, decay=0.95, alpha=0.001, minWeight=0, maxWeight=0) agent2 = QFunctionSGD(2, gameFunc().getNumFeatures(), batchSize=100, gamma=0.9, decay=0.95, alpha=0.001,
def testGameNotEndedAfterMoves(self): pongGame = Pong() pongGame.makeMove(1, NOTHING) pongGame.makeMove(2, NOTHING) self.assertFalse(pongGame.gameEnded())
def testPlayersMultipleMovesThrowsValueError(self): pongGame = Pong() pongGame.makeMove(1, UP) with self.assertRaises(ValueError): pongGame.makeMove(1, UP)