class Human(HumanAgentBrain): ''' Human that can also handle slowdown. Currently not really implemented, we take the parameter but ignore it. ''' def __init__(self, slowdown: int): super().__init__() self.map_state = None self.agents = None def filter_observations(self, state): if self.map_state is None: self.map_state = MapState(state) self.agents = state['World']['team_members'] # Updating the map with visible blocks self.map_state.update_map(None, state) # handle messages for message in self.received_messages: self._handle_message(state, message) # for testing # self.log("current blocks: " + str(self.map_state.blocks)) # self.log("goal blocks:" + str(self.map_state.drop_zone)) # self.log("matching blocks:" + str(self.map_state.get_matching_blocks_within_range(self.map.get_agent_location(state)))) # self.log("self: " + str(self.map_state.get_agent_location(state, None))) # for agent in state.get_agents(): # self.log(str(agent['obj_id']) + ": " + str(self.map_state.get_agent_location(state, agent['obj_id']))) # self.log("wanted colors: " + str(self.map_state._get_goal_colour_set())) # self.log("wanted shape: " + str(self.map_state._get_goal_shape_set())) # self.log("rooms: " + str(self.map_state.rooms)) # self.log("filter: " + str(self.map_state.filter_blocks_within_range(2, self.map_state.get_agent_location(state)))) # self.log("carried blocks: " + str(self.map_state.carried_blocks)) # self.log("agents: " + str(list(map(lambda x: {'block': x['is_carrying'], 'agent': x['obj_id']}, state.get_agents())))) # finally, send all messages stored in the mapstate Queue # SHOULD BE IN DECIDE_ON_BW4T_ACTION BUT HERE FOR DEBUGGING for message in self.map_state.get_message_queue(): self.send_message(message) return state # Why need to returning state def decide_on_bw4t_action(self, state: State): return super().decide_on_bw4t_action(state) def _handle_message(self, state, message): if type(message) is dict: # self.log("handling message " + str(message)) self.map_state.update_map(message, state) def _broadcast(self, message): self.send_message(message) def log(self, message): print(self.agent_id + ":", message)
class Team42Agent(BW4TBrain): ''' This agent should be dumb but reliable ''' def __init__(self, settings: Dict[str, object]): super().__init__(settings) # self._moves = [MoveNorth.__name__, MoveEast.__name__, MoveSouth.__name__, MoveWest.__name__] self.settings = settings self.strategy: Team42Strategy = None self.map_state: MapState = None self.agents = None self._door_range = 1 self.agent_state: agst.Team42AgentState = None self.holding = [] def initialize(self): super().initialize() self.strategy = Team42Strategy.get_brain_strategy(self.settings, self) # self.map_state = None # self.agents = None self._door_range = 1 # self.agent_state: Team42AgentState = None # self.change_state( self.change_state( self.strategy.initial_state(Navigator(self.agent_id, self.action_set), StateTracker(self.agent_id))) # agst.WalkingState(self.strategy, Navigator(self.agent_id, self.action_set), StateTracker(self.agent_id))) # self.holding = [] def change_state(self, newState: agst.Team42AgentState): self.agent_state = newState self.agent_state.set_agent(self) def grab_block(self, block): self.holding.append(block) self.holding.sort(key=lambda b: b[0]) # for testing, makes the normal agent deliver in reverse order # if "normal" in self.agent_id: # self.holding.sort(key=lambda b: b[0], reverse=True) def drop_block(self, block): for i, b in enumerate(self.holding): if b[1] == block[1]: self.holding.pop(i) return def is_holding_blocks(self): return len(self.agent_properties['is_carrying']) > 0 def is_max_capacity(self): return len(self.agent_properties['is_carrying']) > 2 def get_highest_priority_block(self): return self.holding[0] def filter_bw4t_observations(self, state) -> State: if self.map_state is None: self.map_state = MapState(state) self.agents = state['World']['team_members'] # Updating the map with visible blocks self.map_state.update_map(None, state) # handle messages # self.log("received: " + str(len(self.received_messages)) + " messages") for message in self.received_messages: if message['type'] == 'Hello': continue if message['agent_id'] != self.map_state.agent_id: self._handle_message(message) self.received_messages.clear() # print(self.map_state.blocks) # for testing # self.log(self.agent_id + " current blocks: " + str(self.map_state.blocks)) # self.log("goal blocks:" + str(self.map.drop_zone)) # self.log("matching blocks:" + str(self.map.get_matching_blocks())) return state def decide_on_bw4t_action(self, state: State): # self.log("carrying: " + str(self.map_state.carried_blocks)) action = self.agent_state.process(self.map_state, state) # finally, send all messages stored in the mapstate Queue for message in self.map_state.get_message_queue(): # self.log("sending message " + str(message)) self.send_message(message) return action # def _nearbyDoors(self, state: State): # # copy from humanagent # # Get all doors from the perceived objects # objects = list(state.keys()) # doors = [obj for obj in objects if 'is_open' in state[obj]] # doors_in_range = [] # for object_id in doors: # # Select range as just enough to grab that object # dist = int(np.ceil(np.linalg.norm( # np.array(state[object_id]['location']) - np.array( # state[self.agent_id]['location'])))) # if dist <= self._door_range: # doors_in_range.append(object_id) # return doors_in_range def _handle_message(self, message): if type(message) is dict and message['type'] != None: self.map_state.update_map(message, None) def _broadcast(self, type, data): content = { 'agent_id': self.agent_id, 'type': type, 'blocks': data } # global, so also to itself self.send_message(Message(content=content, from_id=self.agent_id, to_id=None)) # def is_json(self, string): # try: # json_object = json.loads(string) # except ValueError as e: # return False # return True def log(self, message): print(self.agent_id + ":", message)