def __initialize_agent__(self, msg): """Initialize an agent. Set the agent id, it's allies and enemies, the respective game number to 0 and it's agents. Log the initalized agent, set a reply_msg as a simple acknowledgment message and send it to the server. Args: msg: A message of comm.REQUEST_INIT_MSG type. """ agent_id = msg.agent_id ally_ids = self.__get_allies__(agent_id) enemy_ids = self.__get_enemies__(agent_id) if agent_id in self.agents: del self.agents[agent_id] self.game_number[agent_id] = 0 self.agents[agent_id] = self.agent_classes[agent_id](agent_id, ally_ids, enemy_ids) log('Initialized {} #{}'.format(self.agent_teams[agent_id], agent_id)) reply_msg = comm.AckMessage() self.server.send(reply_msg)
def __set_agent_pm__(self, msg): """Set the probability map back to the agents.""" self.ghostId.append(msg.agent_id) self.probability_map.append(msg.pm) pacman = self.__get_enemies__(msg.agent_id) # print("Mapa recebido do agente {}".format(msg.agent_id)) # print(msg.pm) ident = msg.agent_id if len(self.probability_map) == len(self.__get_allies__(ident))+1: width = self.probability_map[0].width height = self.probability_map[0].height walls = self.probability_map[0].walls sumOfValues = 0.0 newPM = Map(width, height, walls) # Populate new matrix for x in range(height): for y in range(width): for probMap in self.probability_map: if not probMap._is_wall((x, y)): newPM[x][y] = log1p(newPM[x][y]) + log1p(probMap[x][y]) if not newPM._is_wall((x, y)): # print newPM[x][y] sumOfValues = sumOfValues + newPM[x][y] # Normalize it for x in range(height): for y in range(width): if not newPM._is_wall((x, y)): newPM[x][y] = newPM[x][y]/sumOfValues # print("Novo mapa de probabilidade: ") # print(newPM) for agent in self.ghostId: self.game_states[agent].agent_maps[pacman[0]] = newPM # print("Mapa de probabilidade do agente {}".format(agent)) # print self.game_states[agent].agent_maps[pacman[0]] self.ghostId = [] self.probability_map = [] self.server.send(comm.AckMessage()) else: self.server.send(comm.AckMessage())
def __share_learn__(self, msg): """Set the agent new goal. Args: msg: A message of type GOAL_MSG """ if (self.game_states[msg.agent_id].iteration % 5) == 0: self.ghostId.append(msg.agent_id) ps = self.agents[msg.agent_id].learning.previous_state self.learnTripples.append((msg.agent_id, ps, msg.state, msg.previous_behavior, msg.reward)) num = len(self.__get_allies__(msg.agent_id)) if len(self.learnTripples) == num: for agent in self.ghostId: for tripple in self.learnTripples: if agent != tripple[0]: ps = tripple[1] state = tripple[2] pb = tripple[3] reward = tripple[4] # print "\nAgent receiving info: {}".format(agent) # print "Information Recived from agent: {}" # .format(tripple[0]) # print "Previous State: {}" # .format(tripple[1].get_position()) # print "State: {}" # .format(tripple[2].get_position()) # print "Behavior: {}".format(tripple[3]) # print "Reward: {}".format(tripple[4]) self.agents[agent].learning.learnFromOther(state, ps, pb, reward) self.ghostId = [] self.learnTripples = [] self.server.send(comm.AckMessage()) else: self.server.send(comm.AckMessage()) else: self.server.send(comm.AckMessage())
def __set_agent_policy__(self, msg): """Set an agent policy. Set the policy for the msg agent id, and sand to the server a simple acknowledgment message. Args: msg: A message of type comm.POLICY_MSG. """ self.agents[msg.agent_id].set_policy(msg.policy) self.server.send(comm.AckMessage())
def __register_agent__(self, msg): """Register an agent. Set the agent classes and team, log the registered agent, set a reply_msg as a simple acknowledgment message and send it to the server. Args: msg: A message of comm.REQUEST_REGISTER_MSG """ self.agent_classes[msg.agent_id] = msg.agent_class self.agent_teams[msg.agent_id] = msg.agent_team log('Registered {} #{} ({})'.format(msg.agent_team, msg.agent_id, msg.agent_class.__name__)) reply_msg = comm.AckMessage() self.server.send(reply_msg)
def __set_mse__(self, msg): """...""" pacman = self.__get_enemies__(msg.agent_id) pacman_pos = self.realPositions # print pacman_pos pMap = self.game_states[msg.agent_id].agent_maps[pacman[0]] # print pMap maxValue = 0 maxValueX = 0 maxValueY = 0 width = pMap.width height = pMap.height for x in range(height): for y in range(width): if not pMap._is_wall((x, y)): if pMap[x][y] > maxValue: maxValue = pMap[x][y] maxValueX = x maxValueY = y coord = (maxValueX, maxValueY) distance = (abs(maxValueX - pacman_pos[0]) + abs(maxValueY - pacman_pos[1])) ** 2 self.numInstancesArray[msg.agent_id-1] += 1 self.instanceErrorsArray[msg.agent_id-1] += distance # print("Agente: {}".format(msg.agent_id-1)) # print("\nNumero instancia: {}" # .format(self.numInstancesArray[msg.agent_id-1])) # print("Posicao pacman: {}".format(pacman_pos)) # print("Posicao estimada: {}".format(coord)) # print("Erro instancia: {}".format(distance)) # print("Erro: {}".format(self.instanceErrorsArray[msg.agent_id-1])) self.server.send(comm.AckMessage())
def __start_game_for_agent__(self, msg): """Start Game for an Agent. Call __get_allies__ and __get_enemies__, initialize a Game State for a agent_id from message. Send a acknowledgment message to the server. Log the Start Game for agent number message. Args: msg: A message of type comm.REQUEST_GAME_START_MSG. """ ally_ids = self.__get_allies__(msg.agent_id) enemy_ids = self.__get_enemies__(msg.agent_id) eater = (self.agent_teams[msg.agent_id] == 'pacman') if (self.agent_teams[msg.agent_id] != 'pacman'): self.numInstancesArray.append(0) self.instanceErrorsArray.append(0) if msg.agent_id in self.game_states: del self.game_states[msg.agent_id] iteration = self.game_number[msg.agent_id] self.game_states[msg.agent_id] = GameState(width=msg.map_width, height=msg.map_height, walls=[], agent_id=msg.agent_id, ally_ids=ally_ids, enemy_ids=enemy_ids, eater=eater, iteration=iteration) reply_msg = comm.AckMessage() self.server.send(reply_msg) log('Start game for {} #{}'.format(self.agent_teams[msg.agent_id], msg.agent_id))
def __set_agent_pm_mse__(self, msg): """Set the probability map back to the agents.""" self.ghostId.append(msg.agent_id) self.probability_map.append(msg.pm) maxValue = 0 maxValueX = 0 maxValueY = 0 pacman = self.__get_enemies__(msg.agent_id) # print("Mapa recebido do agente {}".format(msg.agent_id)) # print(msg.pm) ident = msg.agent_id if len(self.probability_map) == len(self.__get_allies__(ident)) + 1: width = self.probability_map[0].width height = self.probability_map[0].height walls = self.probability_map[0].walls sumOfValues = 0.0 newPM = Map(width, height, walls) # Populate new matrix for x in range(height): for y in range(width): for probMap in self.probability_map: if not probMap._is_wall((x, y)): newPM[x][y] = newPM[x][y] + probMap[x][y] if not newPM._is_wall((x, y)): sumOfValues = sumOfValues + newPM[x][y] # Normalize it for x in range(height): for y in range(width): if not newPM._is_wall((x, y)): newPM[x][y] = newPM[x][y] / sumOfValues if newPM[x][y] > maxValue: maxValue = newPM[x][y] maxValueX = x maxValueY = y # print(">>>>>>>>>>>>>>>>>>>>>>>>") # print("Novo mapa de probabilidade: ") # print(newPM) # Get the max value self.numInstances += 1 pacman_pos = self.realPositions print("\nPacman Position: {}".format(pacman_pos)) print("Pacman Estimate Position: {}".format( (maxValueX, maxValueY))) print("Previous instanceError: {}".format(self.instanceError)) self.instanceError += (abs(maxValueX - pacman_pos[0]) + abs(maxValueY - pacman_pos[1])) print("New Instance Error: {}".format( abs(maxValueX - pacman_pos[0]) + abs(maxValueY - pacman_pos[1]))) print ">>>>>>>>>>>>>>>>>>>>>>>>" for agent in self.ghostId: self.game_states[agent].agent_maps[pacman[0]] = newPM # print("Mapa de probabilidade do agente {}".format(agent)) # print self.game_states[agent].agent_maps[pacman[0]] self.ghostId = [] self.probability_map = [] self.server.send(comm.AckMessage()) else: self.server.send(comm.AckMessage())