def test_mapping_validity(self): environment = Environment(0, writeToDB=False) agentx86 = AgentX86(environment, 1) big_to_tiny = agentx86.big_to_tiny for i in range(40): # recreate environment to reset update counts environment = Environment(0, writeToDB=False) agentx86 = AgentX86(environment, 1) # update the environments assuming that the action big_to_tiny[i] had been taken agentx86.X_env.update_envys(agentx86.player, agentx86.big_to_tiny[i]) # count of number of environments that should have updated count_true = len(big_to_tiny[i]) # counter which will hold the number of updated environment states count_update = 0 for e in environment.get_envys(): if len([v for v in e.envy_state if v != 0]) == 1: count_update += 1 if count_true != count_update: self.fail( "Environments incorrectly updated, expected to update %d, but updated %d. big_to_tiny[%d]" % (count_true, count_update, i)) # pass the test self.assertEqual(0, 0)
def test_bad_random_action_parameters(self): environment = Environment(0, writeToDB=False) agentx86 = AgentX86(environment, 1) bbs = [1] * 39 bbc = [1] * 39 self.assertRaises(Exception, agentx86.random_action, bbs, bbc)
def test_no_random_action(self): environment = Environment(0, writeToDB=False) agentx86 = AgentX86(environment, 1) bbs = [1] * 40 bbc = [0] * 40 self.assertRaises(Exception, agentx86.random_action, bbs, bbc)
def test_take_action_environment_update(self): environment = Environment(0, writeToDB=False) agentx86 = AgentX86(environment, 1) curr_env_count = agentx86.X_env.get_environment().count(0) agentx86.take_action() agentx86.take_action() new_env_count = agentx86.X_env.get_environment().count(0) self.assertEqual(curr_env_count - 2, new_env_count)
def test_take_action(self): try: environment = Environment(0, writeToDB=False) agentx86 = AgentX86(environment, 1) agentx86.take_action() except Exception: self.fail( "Taking an action caused the agent to create an exception")
def test_empty_value_dictionary(self): environment = Environment(0, writeToDB=False) agentx86 = AgentX86(environment, 1) flag = True for a in agentx86.get_agentlings(): value_dictionary = a.env.get_values(a.state_history) flag = flag and (value_dictionary == {}) self.assertEqual(flag, True)
def test_state_history_data_type(self): environment = Environment(0, writeToDB=False) agentx86 = AgentX86(environment, 1) agentx86.take_action() for a in agentx86.agentlings: try: a.update_state_history() except: self.fail("State history failed after a single move")
def test_both_action_choices(self): environment = Environment(0, writeToDB=False) agentx86 = AgentX86(environment, 1) bbs = [1] * 39 bbs.append(2) bbc = [0] * 39 bbc.append(1) self.assertEqual(agentx86.random_action(bbs, bbc), agentx86.find_action(bbs, bbc))
def train(self, epochs, map_num, verbose=False, writeToDB=False): if epochs < 1 or epochs > 10000: raise Exception("Epochs should be between 1 and 10,000 inclusive.") for i in range(epochs): print("Epoch: ",i) # Environment - creates the environment and creates all the states for the environment environment = Environment(environment_id=self.map_num, areas=self.areas, writeToDB=False) if verbose: print("Environment created") # Agent X86 - creates the agent for the environment agentX86P1 = AgentX86(environment,1) agentX86P2 = AgentX86(environment,2) game = Game(agentX86P1, agentX86P2, map_num) #print(environment, agentX86P1, agentX86P2, game) if verbose: print("Starting Game ", i) game.play_game(update_after_game = True, verbose = verbose, get_state_histories_for_p1=True) print("Training for %d epochs completed" % (epochs))
def test_update_state_history(self): environment = Environment(0, writeToDB=False) agentx86 = AgentX86(environment, 1) value_list = agentx86.update() self.assertEqual(value_list[0], {})
def test_agentling_many_states(self): environment = Environment(0, writeToDB=False) agentx86 = AgentX86(environment, 1) for i in range(100): agentx86.agentlings[0].update_state_history() self.assertEqual(100, len(agentx86.agentlings[0].state_history))
def test_agentling_state_history(self): environment = Environment(0, writeToDB=False) agentx86 = AgentX86(environment, 1) agentx86.agentlings[0].update_state_history() agentx86.agentlings[0].reset_state_history() self.assertEqual(0, len(agentx86.agentlings[0].state_history))
def test_state_build_agentlings(self): # set up the big agent and environment environment = Environment(0, writeToDB=False) agentx86 = AgentX86(environment, 1) self.assertEqual(len(agentx86.agentlings), 9)