def test_nn_runner_run_batch_agent(benchmark): #Run batch against randomly initialized opponents agent = Agent(base_net_file=None) opponent = Agent(base_net_file=None) game_runner = GameRunner(opponent=opponent) nnrunner = NNRunner(agent,game_runner) benchmark(nnrunner.run_batch,1)
def test_nn_runner_run_batch_random(benchmark): #Run batch against random opponent agent = Agent(base_net_file=None) opponent = RandomAgent() game_runner = GameRunner(opponent=opponent) nnrunner = NNRunner(agent,game_runner) benchmark(nnrunner.run_batch,1)
def test_nn_runner_train_1_1(benchmark): #Benchmark test for training with one agent=Agent() opponent=Agent() game_runner=GameRunner(opponent=opponent) nnrunner = NNRunner(agent,game_runner) benchmark.pedantic(nnrunner.train,kwargs={"batch_size":1,"batches":1},rounds=10)
def test_nn_runner_run_episode(): agent=Agent() game_runner=GameRunner() nnrunner = NNRunner(agent,game_runner) rewards, values, log_probs, entropy_term = nnrunner.run_episode() assert len(rewards) == len(values) == len(log_probs) == len(entropy_term) assert len(rewards) > 0
def test_nn_runner_run_batch(): agent=Agent() game_runner=GameRunner() nnrunner = NNRunner(agent,game_runner) #Test that training with different batch sizes are possible nnrunner.run_batch(episodes=1) nnrunner.run_batch(episodes=2) #Test that training works with a random agent opponent=RandomAgent() game_runner=GameRunner(opponent=opponent) nnrunner = NNRunner(agent,game_runner) nnrunner.run_batch(episodes=2) #Test that training works with a default agent opponent=Agent() game_runner=GameRunner(opponent=opponent) nnrunner = NNRunner(agent,game_runner) nnrunner.run_batch(episodes=2)
def test_init(): gamerunner = GameRunner() #Move counter should be set to 0 assert gamerunner.move_counter == 0 #Player should start with 0 score assert gamerunner.player_score == 0 #Game board should be set game_board = gamerunner.game.game_board_displays for i in range(5): assert np.sum(game_board[i])==4 game_center = gamerunner.game.game_board_center assert np.array_equal(game_center,np.array([0,0,0,0,0,1]))
def test_init(): agent=Agent() game_runner=GameRunner() nnrunner = NNRunner(agent,game_runner)
def test_get_state(): gamerunner = GameRunner() state_flat = gamerunner.get_state() assert np.sum(state_flat) == 4*5 + 1 assert np.size(state_flat) == 5*5 + 6 + 5*5*2 + 5*5*2 + 2 + 2 + 1
def test_step(): #Use seed 1 to get a state equal to the test resource "first round" random.seed(1) gamerunner = GameRunner() reward, end_of_game = gamerunner.step(nn_serialize(1,0,2)) #The turn should have gone back to the original player assert gamerunner.game.current_player == 1 #For this seed, game should not have ended after one step assert not end_of_game #Score on the original board should not have been counted assert np.array_equal(gamerunner.game.score,np.zeros(2)) #Load game_end_of_round_2 to see that game_board gets reset and player gets back to one gamerunner = GameRunner() gamerunner.game.import_JSON(script_dir+"/resources/game_end_of_round_2.json") reward, end_of_game = gamerunner.step(nn_serialize(0,4,1)) assert gamerunner.game.current_player == 1 #For this scenario, game should have ended after one step assert not end_of_game #Load game_end_of_round_2 to see that the game ends if the right steps are made gamerunner = GameRunner() gamerunner.game.import_JSON(script_dir+"/resources/game_end_of_round_2.json") gamerunner.player_score=49-32 random.seed(1) reward, end_of_game = gamerunner.step(nn_serialize(0,0,1)) assert end_of_game #Check that player score is same as score difference assert gamerunner.player_score==gamerunner.game.score[0]-gamerunner.game.score[1] #Load game_end_of_round_3 to see that game_board gets reset gamerunner = GameRunner() gamerunner.game.import_JSON(script_dir+"/resources/game_end_of_round_3.json") gamerunner.player_score=49-32 reward, end_of_game = gamerunner.step(nn_serialize(0,3,0)) #For this scenario, game should not have ended after one step assert not end_of_game #With this scenario, reward should be -6 assert reward == -6 #Since board is reset, the player_score should be equal to the game score difference assert gamerunner.player_score==gamerunner.game.score[0]-gamerunner.game.score[1] #Game board should be set game_board = gamerunner.game.game_board_displays for i in range(5): assert np.sum(game_board[i])==4 game_center = gamerunner.game.game_board_center assert np.array_equal(game_center,np.array([0,0,0,0,0,1])) assert gamerunner.game.current_player == 1