Beispiel #1
0
    def parse_response(self, response):
        here = gv.kg.player_location
        success = False
        for line in response.splitlines():
            line = line.strip()
            if ':' in line:
                success = True
                # Example - small mailbox: It's securly anchored.
                entity_name, resp = [w.strip() for w in line.split(':', 1)]
                short_name = entity_name.split(' ')[-1]
                if here.has_entity_with_name(entity_name):
                    entity = here.get_entity_by_name(entity_name)
                elif kg.inventory.has_entity_with_name(entity_name):
                    entity = kg.inventory.get_entity_by_name(entity_name)
                else:
                    # Create the entity at the current location
                    entity = Entity(entity_name, here)
                    entity.add_name(short_name)
                    here.add_entity(entity)

                take_action = gv.Take(entity)
                p_valid = take_action.validate(resp)
                dbg("[Take] p={:.2f} {} --> {}".format(p_valid, entity_name,
                                                       resp))
                entity.add_action_record(take_action, p_valid, resp)
                if p_valid > 0.5:
                    take_action.apply()
        self.record(success)
Beispiel #2
0
    def take_action(self, observation):
        if self.env:
            # Add true locations to the .log file.
            loc = self.env.get_player_location()
            if loc and hasattr(loc, 'num') and hasattr(
                    loc, 'name') and loc.num and loc.name:
                dbg("[TRUE_LOC] {} \"{}\"".format(loc.num, loc.name))

            # Output a snapshot of the kg.
            # with open(os.path.join(self.kgs_dir_path, str(self.step_num) + '.kng'), 'w') as f:
            #     f.write(str(self.knowledge_graph)+'\n\n')
            # self.step_num += 1

        observation = observation.strip()
        if self.first_step:
            dbg("[NAIL] {}".format(observation))
            self.first_step = False
            return 'look'  # Do a look to get rid of intro text

        if not kg.player_location:
            loc = Location(observation)
            kg.add_location(loc)
            kg.player_location = loc
            kg._init_loc = loc

        self.consume_event_stream()

        if not self.active_module:
            self.elect_new_active_module()

        next_action = self.generate_next_action(observation)
        return next_action
Beispiel #3
0
    def take_control(self):
        obs = yield

        # Failsafe checks
        if self._eagerness == 0.:  # Should never happen anyway.
            self.get_eagerness()  # But if it does, try finding a best action.
            if self._eagerness == 0.:
                return  # If no good action can be found, simply return without yielding.

        action = self.best_action
        self.best_action = None
        self._eagerness = 0.

        response = yield action
        p_valid = action.validate(response)
        if p_valid is None:
            p_valid = self._valid_detector.action_valid(
                action, first_sentence(response))
        if isinstance(action, SingleAction):
            action.entity.add_action_record(action, p_valid, response)
        elif isinstance(action, DoubleAction):
            action.entity1.add_action_record(action, p_valid, response)
        success = (p_valid > 0.5)
        self.record(success)
        if success:
            action.apply()
        dbg("[INT]({}) p={:.2f} {} --> {}".format("val" if success else "inv",
                                                  p_valid, action, response))

        if ('RESTART' in response and 'RESTORE' in response
                and 'QUIT' in response) or ('You have died' in response):
            if action not in self.actions_that_caused_death:
                self.actions_that_caused_death[
                    action] = True  # Remember actions that cause death.
Beispiel #4
0
 def take_control(self):
     """
     1) Detect candidate Entities from current location.
     2) Examine entities to get detailed descriptions
     3) Extract nested entities from detailed descriptions
     """
     obs = yield
     curr_loc = kg.player_location
     undescribed_entities = self.get_descriptionless_entities()
     if undescribed_entities:
         entity = undescribed_entities[0]
         action = gv.Examine(entity.name)
         response = yield action
         entity.description = response
         p_valid = self._valid_detector.action_valid(action, response)
         dbg("[EXM] p={:.2f} {} --> {}".format(p_valid, action,
                                               clean(response)))
         curr_loc.add_action_record(action, 1., response)
     else:
         entity_name = self._to_examine[curr_loc].pop()
         action = gv.Examine(entity_name)
         response = yield action
         p_valid = self._valid_detector.action_valid(
             action, first_sentence(response))
         success = (p_valid > self._validation_threshold)
         self.record(success)
         dbg("[EXM]({}) p={:.2f} {} --> {}".format(
             "val" if success else "inv", p_valid, action, clean(response)))
         curr_loc.add_action_record(action, p_valid, response)
         if success:
             entity = curr_loc.get_entity_by_description(response)
             if entity is None:
                 entity = Entity(entity_name,
                                 curr_loc,
                                 description=response)
                 # TODO: incorrect for entities discovered inside other entities
                 curr_loc.add_entity(entity)
             else:
                 dbg("[EXM](val) Discovered alternate name "\
                     "\'{}\' for \'{}\'".format(entity_name, entity.name))
                 entity.add_name(entity_name)
         if success:
             entity = curr_loc.get_entity_by_description(response)
             inv_entity = kg.inventory.get_entity_by_description(response)
             if entity is None and inv_entity is None:
                 entity = Entity(entity_name,
                                 curr_loc,
                                 description=response)
                 # TODO: incorrect for entities discovered inside other entities
                 curr_loc.add_entity(entity)
             else:
                 if entity:
                     dbg("[EXM](val) Discovered alternate name " \
                         "\'{}\' for \'{}\'".format(entity_name, entity.name))
                     entity.add_name(entity_name)
                 if inv_entity:
                     dbg("[EXM](val) Discovered alternate name " \
                         "\'{}\' for inventory item \'{}\'".format(entity_name, inv_entity.name))
                     inv_entity.add_name(entity_name)
Beispiel #5
0
 def take_control(self):
     """ Performs the previously extracted action """
     obs = yield
     dbg("[RESTART] Restarting Game")
     action = StandaloneAction("IEEECIG-ADVENT-RESTART-COMMAND")
     response = yield action
     kg.reset()
     self._eagerness = 0.
Beispiel #6
0
 def take_control(self):
     """ Performs the previously extracted action """
     obs = yield
     response = yield self.act_to_do
     dbg("[YouHaveTo] {} --> {}".format(self.act_to_do, response))
     p_valid = self._valid_detector.action_valid(self.act_to_do,
                                                 first_sentence(response))
     success = (p_valid > 0.5)
     self.record(success)
     self._eagerness = 0.
Beispiel #7
0
 def process_event(self, event):
     """ Process an event from the event stream. """
     location, message = self.get_event_info(event)
     if location not in self._to_examine:
         self._to_examine[location] = []
     if not message:
         return
     candidate_entities = self.detect_entities(message)
     dbg("[EXM](detect) {} --> {}".format(clean(message),
                                          candidate_entities))
     self.filter(candidate_entities)
Beispiel #8
0
 def elect_new_active_module(self):
     """ Selects the most eager module to take control. """
     most_eager = 0.
     for module in self.modules:
         eagerness = module.get_eagerness()
         if eagerness >= most_eager:
             self.active_module = module
             most_eager = eagerness
     dbg("[NAIL](elect): {} Eagerness: {}"\
         .format(type(self.active_module).__name__, most_eager))
     self.action_generator = self.active_module.take_control()
     self.action_generator.send(None)
Beispiel #9
0
 def observe(self, obs, action, score, new_obs, terminal):
     """ Observe will be used for learning from rewards. """
     p_valid = self._valid_detector.action_valid(action, new_obs)
     dbg("[VALID] p={:.3f} {}".format(p_valid, clean(new_obs)))
     if kg.player_location:
         dbg("[EAGERNESS] {}".format(' '.join(
             [str(module.get_eagerness()) for module in self.modules[:5]])))
     event_stream.push(
         NewTransitionEvent(obs, action, score, new_obs, terminal))
     action_recognized(action, new_obs)  # Update the unrecognized words
     if terminal:
         kg.reset()
Beispiel #10
0
def action_recognized(action, response):
    """
    Returns True if the action was recognized based on the response.
    Returns False if the action is not recognized and appends it to
    the list of unrecognized_words.

    """
    unrecognized_word = get_unrecognized(action, response)
    if unrecognized_word:
        if unrecognized_word not in gv.kg._unrecognized_words:
            gv.dbg("[UTIL] Added unrecognized word \"{}\"".format(unrecognized_word))
            gv.kg._unrecognized_words.append(unrecognized_word)
        return False
    return True
Beispiel #11
0
 def take_control(self):
     obs = yield
     action = self.get_action()
     while action is None or not action.recognized():
         action = self.get_action()
     response = yield action
     p_valid = self._valid_detector.action_valid(action,
                                                 first_sentence(response))
     if isinstance(action, StandaloneAction):
         kg.player_location.add_action_record(action, p_valid, response)
     elif isinstance(action, SingleAction):
         action.entity.add_action_record(action, p_valid, response)
     elif isinstance(action, DoubleAction):
         action.entity1.add_action_record(action, p_valid, response)
     success = (p_valid > 0.5)
     self.record(success)
     dbg("[IDLER]({}) p={:.2f} {} --> {}".format(
         "val" if success else "inv", p_valid, action, response))
Beispiel #12
0
 def __init__(self, seed, env, rom_name, output_subdir='.'):
     self.setup_logging(rom_name, output_subdir)
     rng.seed(seed)
     dbg("RandomSeed: {}".format(seed))
     self.knowledge_graph = gv.kg
     self.knowledge_graph.__init__()  # Re-initialize KnowledgeGraph
     gv.event_stream.clear()
     self.modules = [
         Examiner(True),
         Hoarder(True),
         Navigator(True),
         Interactor(True),
         Idler(True),
         YesNo(True),
         YouHaveTo(True),
         Darkness(True)
     ]
     self.active_module = None
     self.action_generator = None
     self.first_step = True
     self._valid_detector = LearnedValidDetector()
     if env and rom_name:
         self.env = env
         self.step_num = 0
Beispiel #13
0
 def push(self, event):
     gv.dbg("[LOG]({}) {}".format(type(event).__name__, event.message))
     self._stream.append(event)