def test_general_random_actions(self): " Test that the step function is successful when random policies are given to both teams " env = gym.make(ENV_NAME) env.reset(policy_blue=policy.Random(), policy_red=policy.Random()) for step in range(1000): state, reward, done, info = env.step() if done: break
def test_general_num_agents_list(self): " Test that TypeError is raised when list value is entered for the number of blue or red agents " env = gym.make(ENV_NAME) with raises(TypeError): env.NUM_BLUE = [3,4,2] env.NUM_RED = 3 env.reset(policy_blue=policy.Random(), policy_red=policy.Random()) env.step()
def test_else_list_wrong_moves(self): " Test that SystemExit occurs when incorrect number of moves are entered for else statement " env.is_done = False env.CONTROL_ALL = False env.reset(policy_blue=policy.Random(), policy_red=policy.Random()) with raises(SystemExit) as sysex: env.step([0,1,4]) assert str(sysex.value) == 'ERROR: You entered wrong number of moves. There are 6 entities.'
def test_general_differing_number_of_agents(self): " Test that the step function is successful with different number of agents " env = gym.make(ENV_NAME) num_tests = 100 for instance in range(num_tests): env.NUM_BLUE = randint(0, 20) env.NUM_RED = randint(0, 20) env.reset(policy_blue=policy.Random(), policy_red=policy.Random()) env.step()
def test_general_equal_number_of_agents(self): " Test that the step function is successful with different number of agents " env = gym.make(ENV_NAME) max_agents = 20 # arbitrary value for num_agents in range(max_agents): env.NUM_BLUE = num_agents env.NUM_RED = num_agents env.reset(policy_blue=policy.Random(), policy_red=policy.Random()) env.step()
def test_else_list_invalid_input(self): " Test that when values that are greater than 4 or less than -5 are entered " " an IndexError is raised " env.is_done = False env.CONTROL_ALL = False env.reset(policy_blue=policy.Random(), policy_red=policy.Random()) with raises(IndexError) as err1: env.step([5,6,3,9,100,2]) with raises(IndexError) as err2: env.step([-4,-4,-6,-3,-2,-1]) assert str(err1.value) == 'list index out of range' assert str(err2.value) == 'list index out of range'
def use_this_policy(policyName=None): if policyName is None: heur_policy_list = [policy.Patrol(), policy.Roomba(), policy.Defense(), policy.Random(), policy.AStar()] heur_weight = [1,1,1,1,1] heur_weight = np.array(heur_weight) / sum(heur_weight) return np.random.choice(heur_policy_list, p=heur_weight) elif policyName == "Roomba": return policy.Roomba() elif policyName == "Patrol": return policy.Patrol() elif policyName == "Defense": return policy.Defense() elif policyName == "AStar": return policy.AStar() elif policyName == "Random": return policy.Random()
def test_general_different_number_of_agents(self): " Test that the step function is successful with different number of agents " env = gym.make(ENV_NAME) env.NUM_BLUE = 0 env.NUM_RED = 0 env.NUM_BLUE_UAV = 0 env.NUM_RED_UAV = 0 env.NUM_GRAY = 0 env.NUM_BLUE_UGV2 = 0 env.NUM_RED_UGV2 = 0 env.NUM_BLUE_UGV3 = 0 env.NUM_RED_UGV3 = 0 env.NUM_BLUE_UGV4 = 0 env.NUM_RED_UGV4 = 0 env.reset(policy_blue=policy.Random(), policy_red=policy.Random()) env.step()
def test_general_blue_action_specified(self): " Test that no errors occur when blue action is specified using env.action_space.sample() " env = gym.make(ENV_NAME) env.reset(policy_red=policy.Random()) for step in range(1000): action = env.action_space.sample() state, reward, done, info = env.step(action) if done: break
import time import gym import gym_cap import gym_cap.heuristic as policy # Initialize the environment env = gym.make("cap-v0") # Reset the environment and select the policies for each of the team observation = env.reset(map_size=20, config_path='demo/base_settings.ini', policy_blue=policy.Defense(), policy_red=policy.Random()) num_match = 100 render = False rscore = [] start_time = time.time() for n in range(num_match): done = False rewards = [] while not done: # Take a step and receive feedback observation, reward, done, info = env.step() rewards.append(reward) # Render and sleep (not needed for score analysis) if render: env.render() time.sleep(.05)
def test_policy_red_random(self): " Tests that random policy is appropriately assigned to the red team " " for the reset function " random_policy = policy.Random() env.reset(policy_red=random_policy) assert env._policy_red == random_policy
def test_else_none(self): " Test that no error occurs when entities_action is None because exception is excepted " env.is_done = False env.CONTROL_ALL = False env.reset(policy_blue=policy.Random(), policy_red=policy.Random()) env.step(None)
def test_else_list_right_moves(self): " Test that when 6 moves are entered for the blue team, the step function is a success " env.is_done = False env.CONTROL_ALL = False env.reset(policy_blue=policy.Random(), policy_red=policy.Random()) env.step([0,2,1,3,4,2])
def test_else_int(self): " Test that when an integer is given, no error occurs as long as too many moves have not been entered " env.is_done = False env.CONTROL_ALL = False env.reset(policy_blue=policy.Random(), policy_red=policy.Random()) env.step(3)