def test_get_recent_entities(self): self.memory = AgentMemory() joe_memid = PlayerNode.create( self.memory, Player(10, "joe", Pos(1, 0, 1), Look(0, 0))) players = self.memory.get_recent_entities(memtype="Player") assert len(players) == 1 jane_memid = PlayerNode.create( self.memory, Player(11, "jane", Pos(-1, 0, 1), Look(0, 0))) players = self.memory.get_recent_entities(memtype="Player") assert len(players) == 2
def test_player_apis(self): self.memory = LocoAgentMemory() player_list = [ Player(20, "xyz", Pos(1, 1, 1), Look(1, 1)), Player(10, "abc", Pos(0, 0, 3), Look(0, 0)), ] # test update_other_players self.memory.update_other_players(player_list) assert self.memory.get_player_by_name("xyz").pos == (1.0, 1.0, 1.0) assert self.memory.get_player_by_eid(10).name == "abc"
def setUp(self): super().setUp() memory = self.agent.memory PlayerNode.create(memory, Player(10, "agent10", Pos(1, 63, 1), Look(0, 0))) PlayerNode.create(memory, Player(11, "agent11", Pos(-2, 63, 1), Look(0, 0))) PlayerNode.create(memory, Player(12, "agent12", Pos(-4, 63, -3), Look(0, 0))) self.set_looking_at([1, 63, 1])
def test_triggers(self): self.memory = AgentMemory(agent_time=self.time) joe_memid = PlayerNode.create( self.memory, Player(10, "joe", Pos(1, 0, 1), Look(0, 0))) joe_tag_memid = self.memory.tag(joe_memid, "joe") jane_memid = PlayerNode.create( self.memory, Player(11, "jane", Pos(-1, 0, 1), Look(0, 0))) _, joe_mems = self.memory.basic_search( "SELECT MEMORY FROM ReferenceObject WHERE name=joe") _, jane_mems = self.memory.basic_search( "SELECT MEMORY FROM ReferenceObject WHERE name=jane") assert len(joe_mems) == 1 assert len(jane_mems) == 1 _, joe_mems_from_tag = self.memory.basic_search( "SELECT MEMORY FROM ReferenceObject WHERE has_tag=joe") _, jane_mems_from_tag = self.memory.basic_search( "SELECT MEMORY FROM ReferenceObject WHERE has_tag=jane") assert len(joe_mems_from_tag) == 1 assert len(jane_mems_from_tag) == 0 self.time.add_tick() brother_of_memid = self.memory.add_triple(subj=joe_memid, pred_text="brother_of", obj=jane_memid) sister_of_memid = self.memory.add_triple(subj=jane_memid, pred_text="sister_of", obj=joe_memid) triples = self.memory.get_triples(subj=jane_memid, pred_text="sister_of") assert len(triples) == 1 self.time.add_tick() self.memory.db_write("UPDATE ReferenceObjects SET x=? WHERE uuid=?", 2, joe_memid) cmd = "SELECT updated_time FROM Memories WHERE uuid=?" joe_t = self.memory._db_read(cmd, joe_memid)[0][0] jane_t = self.memory._db_read(cmd, jane_memid)[0][0] assert joe_t == 2 assert jane_t == 0 self.memory.forget(joe_memid) triples = self.memory.get_triples(subj=jane_memid, pred_text="sister_of") assert len(triples) == 0
def __init__(self, opts, name="Locobot"): self.backend = opts.backend super(LocobotAgent, self).__init__(opts) logging.info("LocobotAgent.__init__ started") self.agent_type = "locobot" self.opts = opts self.entityId = 0 self.no_default_behavior = opts.no_default_behavior self.last_chat_time = -1000000000000 self.name = name self.player = Player(100, name, Pos(0, 0, 0), Look(0, 0)) self.pos = Pos(0, 0, 0) self.uncaught_error_count = 0 self.last_task_memid = None self.point_targets = [] self.init_event_handlers() # list of (prob, default function) pairs if self.backend == 'habitat': self.visible_defaults = [(1.0, default_behaviors.explore)] elif self.backend == 'hellorobot': self.visible_defaults = [] else: raise RuntimeError("Unknown backend specified {}" % (self.backend, )) self.interaction_logger = InteractionLogger() if os.path.exists("annotation_data/rgb"): shutil.rmtree("annotation_data/rgb") if os.path.exists("annotation_data/seg"): shutil.rmtree("annotation_data/seg")
def test_get_node_from_memid(self): self.memory = AgentMemory() joe_memid = PlayerNode.create( self.memory, Player(10, "joe", Pos(1, 0, 1), Look(0, 0))) assert self.memory.get_node_from_memid(joe_memid) == "Player" loc_memid = LocationNode.create(self.memory, (0, 0, 0)) assert self.memory.get_node_from_memid(loc_memid) == "Location"
def test_place_field(self): memory = AgentMemory() PF = memory.place_field joe_x = 1 joe_z = 2 joe_loc = (joe_x, 0, joe_z) jane_loc = (-1, 0, 1) joe_memid = PlayerNode.create( memory, Player(10, "joe", Pos(*joe_loc), Look(0, 0))) jane_memid = PlayerNode.create( memory, Player(11, "jane", Pos(*jane_loc), Look(0, 0))) wall_locs = [{"pos": (-i, 0, 4)} for i in range(5)] changes = [{ "pos": joe_loc, "memid": joe_memid }, { "pos": jane_loc, "memid": jane_memid }] changes.extend(wall_locs) PF.update_map(changes) assert PF.maps[0]["map"].sum() == 7 jl = PF.memid2locs[joe_memid] assert len(jl) == 1 recovered_pos = tuple( int(i) for i in PF.map2real(*PF.idx2ijh(list(jl.keys())[0]))) assert recovered_pos == (joe_x, joe_z) assert len(PF.memid2locs["NULL"]) == 5 changes = [{"pos": (-1, 0, 4), "is_delete": True}] PF.update_map(changes) assert len(PF.memid2locs["NULL"]) == 4 new_jane_x = -5 new_jane_z = 5 changes = [{ "pos": (new_jane_x, 0, new_jane_z), "memid": jane_memid, "is_move": True }] PF.update_map(changes) jl = PF.memid2locs[jane_memid] assert len(jl) == 1 recovered_pos = tuple( int(i) for i in PF.map2real(*PF.idx2ijh(list(jl.keys())[0]))) assert recovered_pos == (new_jane_x, new_jane_z) assert PF.maps[0]["map"].sum() == 6
def test_linear_extent_search(self): self.memory = AgentMemory() PlayerNode.create( self.memory, Player(0, "self", Pos(-1, 0, -1), Look(0, 0)), memid=self.memory.self_memid, ) joe_eid = 10 joe_memid = PlayerNode.create( self.memory, Player(joe_eid, "joe", Pos(1, 0, 1), Look(0, 0))) jane_memid = PlayerNode.create( self.memory, Player(11, "jane", Pos(-10, 0, 3), Look(0, 0))) jules_memid = PlayerNode.create( self.memory, Player(12, "jules", Pos(-1, 0, 2), Look(0, 0))) # FIXME shouldn't need to do this, interpreter should back off to something else # if there is no AttentionNode to use in filter_by_sublocation AttentionNode.create(self.memory, [1, 0, 1], joe_eid) self.interpreter = Interpreter("joe", "NULL", self.memory) self.interpreter.subinterpret["attribute"] = AttributeInterpreter() self.interpreter.subinterpret[ "specify_locations"] = dummy_specify_locations l = all_test_commands.LINEAR_EXTENTS["distance from joe"] l["frame"] = "joe" a = interpret_linear_extent(self.interpreter, "joe", l) m = MemorySearcher() comparator = { "input_left": { "value_extractor": { "attribute": a } }, "input_right": { "value_extractor": 5 }, "comparison_type": "GREATER_THAN", } query_dict = { "output": "MEMORY", "memory_type": "ReferenceObject", "where_clause": { "AND": [comparator] }, } memids, _ = m.search(self.memory, query=query_dict)
def test_get_entity_by_id(self): self.memory = MCAgentMemory() mob_memid = MobNode.create(self.memory, Mob(10, 65, Pos(1, 2, 3), Look(0, 0))) player_memid = PlayerNode.create( self.memory, Player(20, "xyz", Pos(1, 1, 1), Look(1, 1))) # Test get_entity_by_eid with entityId assert self.memory.get_entity_by_eid(10).pos == (1.0, 2.0, 3.0) assert self.memory.get_entity_by_eid(20).pos == (1.0, 1.0, 1.0)
def test_player_apis_memory(self): self.memory = AgentMemory() joe_memid = PlayerNode.create( self.memory, Player(10, "joey", Pos(1, 0, 1), Look(0, 0))) self.memory.tag(joe_memid, "basketball_player") ann_memid = PlayerNode.create( self.memory, Player(20, "ann", Pos(1, 0, 4), Look(0, 0))) self.memory.tag(ann_memid, "swimmer") # Test get_player_by_eid assert self.memory.get_player_by_eid(10).name == "joey" # Test get_player_by_name assert self.memory.get_player_by_name("joey").eid == 10 # Test get_players_tagged # Test get_player_by_id assert self.memory.get_player_by_id(ann_memid).name == "ann"
def test_untag(self): self.memory = AgentMemory() player_memid = PlayerNode.create( self.memory, Player(10, "rachel", Pos(1, 0, 1), Look(0, 0))) self.memory.tag(subj_memid=player_memid, tag_text="girl") self.memory.tag(subj_memid=player_memid, tag_text="plays_football") assert len(self.memory.get_triples(subj=player_memid, obj_text="girl")) == 1 self.memory.untag(subj_memid=player_memid, tag_text="girl") assert len(self.memory.get_triples(subj=player_memid, obj_text="girl")) == 0
def test_memids_and_tags(self): self.memory = AgentMemory() player_memid = PlayerNode.create( self.memory, Player(10, "rache", Pos(1, 0, 1), Look(0, 0))) self.memory.tag(subj_memid=player_memid, tag_text="girl") self.memory.tag(subj_memid=player_memid, tag_text="plays_football") # test get_memids_by_tag self.memory.get_memids_by_tag(tag="girl") assert len(self.memory.get_memids_by_tag(tag="girl")) == 1 assert self.memory.get_memids_by_tag(tag="girl")[0] == player_memid # test_get_tags_by_memid assert "girl" in self.memory.get_tags_by_memid(player_memid) # test get_triples assert len(self.memory.get_triples(subj=player_memid, obj_text="girl")) == 1
def test_get_mem_by_id(self): self.memory = AgentMemory() kavya_memid = PlayerNode.create( self.memory, Player(10, "kavya", Pos(1, 0, 1), Look(0, 0))) assert self.memory.get_mem_by_id(kavya_memid).NODE_TYPE == "Player"
def test_sql_form(self): self.memory = AgentMemory() # FIXME? should this be in memory init? # FIXME!! in agents use SelfNode instead of PlayerNode self_memid = SelfNode.create(self.memory, Player(1, "robot", Pos(0, 0, 0), Look(0, 0)), memid=self.memory.self_memid) rachel_memid = PlayerNode.create( self.memory, Player(10, "rachel", Pos(1, 0, 1), Look(0, 0))) self.memory.tag(subj_memid=rachel_memid, tag_text="girl") self.memory.tag(subj_memid=rachel_memid, tag_text="plays_football") robert_memid = PlayerNode.create( self.memory, Player(11, "robert", Pos(4, 0, 5), Look(0, 0))) self.memory.tag(subj_memid=robert_memid, tag_text="boy") self.memory.tag(subj_memid=robert_memid, tag_text="plays_football") sam_memid = PlayerNode.create( self.memory, Player(12, "sam", Pos(-2, 0, 5), Look(0, 0))) self.memory.tag(subj_memid=sam_memid, tag_text="girl") self.memory.tag(subj_memid=sam_memid, tag_text="plays_volleyball") # test NOT m = MemorySearcher() query = "SELECT MEMORY FROM ReferenceObject WHERE (NOT has_tag=girl)" memids, _ = m.search(self.memory, query=query) assert robert_memid in memids assert sam_memid not in memids assert rachel_memid not in memids # test OR m = MemorySearcher() query = "SELECT MEMORY FROM ReferenceObject WHERE ((has_tag=plays_volleyball) OR (NOT has_tag=girl))" memids, _ = m.search(self.memory, query=query) assert robert_memid in memids assert sam_memid in memids assert rachel_memid not in memids # test that text form and dict form return same records query_dict = { "output": "MEMORY", "memory_type": "ReferenceObject", "where_clause": { "OR": [ { "pred_text": "has_tag", "obj_text": "plays_volleyball" }, { "NOT": [{ "pred_text": "has_tag", "obj_text": "girl" }] }, ] }, } memids_d, _ = m.search(self.memory, query=query_dict) assert set(memids_d) == set(memids) # test table property with tag m = MemorySearcher() query = "SELECT MEMORY FROM ReferenceObject WHERE ((has_tag=plays_volleyball) AND (x<0))" memids, _ = m.search(self.memory, query=query) assert robert_memid not in memids assert sam_memid in memids assert rachel_memid not in memids query = "SELECT (x, y) FROM ReferenceObject WHERE ((has_tag=plays_volleyball) AND (x<0))" memids, vals = m.search(self.memory, query=query) assert abs(vals[0][0] + 2.0) < 0.01 assert abs(vals[0][1]) < 0.01 triple_memid = TripleNode.create(self.memory, subj=sam_memid, pred_text="mother_of", obj=robert_memid) query = "SELECT MEMORY FROM ReferenceObject WHERE <<#{}, mother_of, ?>>".format( sam_memid) memids, _ = m.search(self.memory, query=query) assert robert_memid in memids assert len(memids) == 1 # test FROM works query = "SELECT MEMORY FROM Triple WHERE create_time > -100" memids, _ = m.search(self.memory, query=query) assert all( [type(self.memory.get_mem_by_id(m)) is TripleNode for m in memids]) assert triple_memid in memids assert robert_memid not in memids