def test_get_players_sorted(): """Run get_players with sorted flag to true.""" # Set up variables ba1 = BaseAgent() ba2 = BaseAgent() cfe = CoinFlipEngine() lad = BaseLadder(game=cfe) lad.match_func = mock_match_func # Add players to the ladder lad.add_player(ba1) lad.add_player(ba2) # Run the game lad.run_game() # Check that the results are sorted in ascending elo players = lad.get_players(sort=True) player1 = players[0] player2 = players[1] assert player1.elo > player2.elo assert (player1.num_wins == 1 and player2.num_wins == 0) assert (player1.num_losses == 0 and player2.num_losses == 1)
def test_match_error(): """Ensure RuntimeError is thrown when no players available to match.""" lad = BaseLadder() ba1 = BaseAgent(id_in="Ba1") lad.match_func = mock_match_func # Error thrown when no players available try: lad.match_players() assert False except RuntimeError: pass # Error thrown when only one player available lad.add_player(ba1) try: lad.match_players() assert False except RuntimeError: pass # Originally enough players, but not enough afterwards for _ in range(2): lad.add_player(BaseAgent()) try: lad.match_players() # 3 players in pool lad.match_players() # Only 1 player in pool assert False except RuntimeError: pass
def test_floor(): """Tests that ratings do not go below 1000.""" player1 = BaseAgent() player2 = BaseAgent() player1.elo = elo(player1, player2, 0) assert player1.elo == 1000
def test_run_game(): """Test run_game functions properly.""" # Set up variables ba1 = BaseAgent() ba2 = BaseAgent() cfe = CoinFlipEngine() lad = WeightedLadder(game=cfe) # Add players to the ladder lad.add_player(ba1) lad.add_player(ba2) # Run the game lad.run_game() # Check that the ladder updated properly players = lad.get_players() player1 = players[0] player2 = players[1] # Only one elo value changes assert ((player1.elo > 1000 and player2.elo == 1000) or (player1.elo == 1000 and player2.elo > 1000)) # Someone won the game assert ((player1.num_wins == 0 and player2.num_wins == 1) or (player1.num_wins == 1 and player2.num_wins == 0)) # Someone lost the game assert ((player1.num_losses == 0 and player2.num_losses == 1) or (player1.num_losses == 1 and player2.num_losses == 0))
def test_make_move(): """Tests that make_move throws an error.""" ba1 = BaseAgent() try: ba1.make_move() assert False except NotImplementedError: return
def test_add(): """Basic test for ladder add_player method.""" lad = BaseLadder() ba1 = BaseAgent() ba2 = BaseAgent() lad.add_player(ba1) lad.add_player(ba2) assert len(lad.player_pool) == 2
def __init__(self): """ Init this agent :param size_x: battlefield horizontal size :param size_y: battlefield vertical size :param detector_num: detector quantity of this side :param fighter_num: fighter quantity of this side """ BaseAgent.__init__(self) self.obs_ind = 'maddpg'
def test_run(): """Run single iteration of game.""" player1 = BaseAgent() player2 = BaseAgent() cfe_win = CoinFlipEngine(prob_win=1) outcome = cfe_win.run(player1, player2) assert outcome == 1 cfe_loss = CoinFlipEngine(prob_win=0) outcome = cfe_loss.run(player1, player2) assert outcome == 0
def __init__(self): """ Init this agent :param size_x: battlefield horizontal size :param size_y: battlefield vertical size :param detector_num: detector quantity of this side :param fighter_num: fighter quantity of this side """ BaseAgent.__init__(self) self.obs_ind = 'simple' if not os.path.exists('model/simple/model.pkl'): print('Error: agent simple model data not exist!') exit(1) self.fighter_model = dqn.RLFighter(ACTION_NUM)
def __init__(self): """ Init this agent :param size_x: battlefield horizontal size :param size_y: battlefield vertical size :param detector_num: detector quantity of this side :param fighter_num: fighter quantity of this side """ BaseAgent.__init__(self) self.obs_ind = 'maddpg6' if not os.path.exists('model/{}/{}0.pkl'.format(MODEL_PATH, MODEL_NAME)): logger.info('Error: agent maddpg6 model data not exist!') exit(1) self.maddpg = MADDPG(FIGHTER_NUM, OBS_NUM, ACTION_NUM)
def __init__(self): """ Init this agent :param size_x: battlefield horizontal size :param size_y: battlefield vertical size :param detector_num: detector quantity of this side :param fighter_num: fighter quantity of this side """ BaseAgent.__init__(self) self.obs_ind = 'airc_homo_rule' self.last_random_action_set = {} self.to_explore_set = {} self.explore_destination_set = {} self.rule_set = 'attack'
def test_match_basic(): """Test that match functions properly.""" # Set up variables lad = WeightedLadder() ba1 = BaseAgent() ba2 = BaseAgent() # Add the players to the ladder lad.add_player(ba1) lad.add_player(ba2) # Generate a match (should be ba1 and ba2) _ = lad.match_players() # Assert that players get removed from ladder assert not lad.player_pool assert lad.num_turns == 1
def test_init(): """Test the variables are set properly.""" ba1 = BaseAgent(id_in="doot", type="Pew") assert ba1.elo == 1000 assert ba1.id == "doot" assert ba1.num_wins == 0 assert ba1.num_losses == 0 assert ba1.type == "Pew"
def test_match_func(): """Test the match_func to make sure it works.""" # Set up variables lad = WeightedLadder() ba1 = BaseAgent(id_in="Ba1") ba2 = BaseAgent(id_in="Ba2") # Make the elo score higher ba1.elo = 1500 ba2.elo = 1400 # Add the higher ranked players lad.add_player(ba1) lad.add_player(ba2) # Add the rest of the agents (ranked lower) for i in range(3, 11): lad.add_player(BaseAgent(id_in="Ba{}".format(i))) # Try matching the higher ranked players together match1, match2 = lad.match_players() while (match1.id is not ba1.id) and (match1.id is not ba2.id): match1, match2 = lad.match_players() # Higher elo players got matched together assert (match2.id == ba1.id or match2.id == ba2.id)
def __init__(self): """ Init this agent :param size_x: battlefield horizontal size :param size_y: battlefield vertical size :param detector_num: detector quantity of this side :param fighter_num: fighter quantity of this side """ BaseAgent.__init__(self) # self.preposs = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], # [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] self.obs_ind = OBS_IND_NAME if not os.path.exists('model/{}/{}.pkl'.format(MODEL_PATH_AGENT, MODEL_NAME_AGENT)): logger.info('Error: agent maddpg4 model data not exist!') exit(1) self.fighter_model = dqn.RLFighter(OBS_NUM, ACTION_NUM) self.step = 0
def __init__(self): """ Init this agent :param size_x: battlefield horizontal size :param size_y: battlefield vertical size :param detector_num: detector quantity of this side :param fighter_num: fighter quantity of this side """ BaseAgent.__init__(self) # self.preposs = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], # [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] self.obs_ind = 'dqn_jy' if not os.path.exists('model/dqn_jy/model_000015200.pkl'): print( 'class Agent(BaseAgent) Error: agent test1 model data not exist!' ) exit(1) self.fighter_model = dqn.RLFighter(ACTION_NUM)
def test_available_players(): """Test that available players picks out players not in games.""" lad = BaseLadder() ba1 = BaseAgent() ba2 = BaseAgent() ba3 = BaseAgent() lad.match_func = mock_match_func # No players are in games, so all available lad.add_player(ba1) lad.add_player(ba2) lad.add_player(ba3) assert len(lad.available_players) == 3 # One player is taken _, _ = lad.match_players() assert len(lad.available_players) == 1 assert lad.available_players[0] in [(ba2, 0), (ba3, 0), (ba1, 0)]
def test_drop(): """Test that ratings drop when appropriate.""" player1 = BaseAgent() player2 = BaseAgent() player1.elo = 1619 player2.elo = 1609 player1.elo = elo(player1, player2, 0) assert player1.elo == 1602
def test_increase(): """Tests that ratings increase when appropriate.""" player1 = BaseAgent() player2 = BaseAgent() player1.elo = 1619 player2.elo = 1609 player2.elo = elo(player2, player1, 1) assert player2.elo == 1625
def test_duplicate_add(): """Test that same player cannot exist twice on ladder.""" lad = BaseLadder() ba1 = BaseAgent() lad.add_player(ba1) lad.num_turns = 50 lad.add_player(ba1) assert lad.player_pool[0][1] == 50 assert len(lad.player_pool) == 1
def test_selection_size(): """Test that selection size works properly.""" # Initialization lad = WeightedLadder() assert lad.selection_size == 1 # Populate ladder base_player = BaseAgent() base_player.elo = 1030 for ind in range(15): new_player = BaseAgent() new_player.elo = 1000 + ind lad.add_player(new_player) # Only getting pool of one player lad.selection_size = 1 assert len(lad.get_candidate_matches(base_player)) == 1 assert lad.get_candidate_matches(base_player)[0][0].elo == 1014 # Pool of normal size, get five best players lad.selection_size = 5 assert len(lad.get_candidate_matches(base_player)) == 5 max_elo = 1014 for pair in lad.get_candidate_matches(base_player): assert pair[0].elo == max_elo max_elo -= 1 # Pool of unreasonable size, just get max number of players lad.selection_size = 5000 assert len(lad.get_candidate_matches(base_player)) == 15
def test_match_basic(): """Test that match functions properly.""" # Set up variables lad = BaseLadder() ba1 = BaseAgent() ba2 = BaseAgent() # Use fake match function lad.match_func = mock_match_func # Add the players to the ladder lad.add_player(ba1) lad.add_player(ba2) # Generate a match (should be ba1 and ba2) _ = lad.match_players() # Assert that players get removed from player pool assert not lad.available_players assert lad.num_turns == 1 for player, _ in lad.player_pool: assert player.in_game
def test_no_duplicates(): """Test that same player cannot exist twice on ladder.""" lad = BaseLadder() ba1 = BaseAgent() lad.add_player(ba1) try: lad.add_player(ba1) except ValueError: # Ladder throws a ValueError if a duplicate player exists # We want to be here return assert False
def test_win_loss(): """Test calculation of win/loss ratio.""" ba1 = BaseAgent() # Wins with no losses has undefined w/l ratio ba1.num_wins = 50 assert ba1.win_loss_ratio() is None # Losses makes the calculation valid ba1.num_losses = 10 assert ba1.win_loss_ratio() == 5 # num_wins + num_losses = total_games assert ba1.total_games() == 60
from agent.base_agent import BaseAgent import envs import gym if __name__ == '__main__': env = gym.make('2048-v0') agent = BaseAgent(env) train_log = agent.train() test_result = agent.test()
def add_agents(self): """Add agents to the ladder.""" for _ in range(self.num_players): player = BaseAgent() self.ladder.add_player(player)
def run_agent(agent: BaseAgent, reward: dict, obs: dict): action, event_type = agent.step(reward, obs) reward = play_ground.act(action, event_type) # reward after an action return reward