def take_control(self): """ Always answers yes """ obs = yield action = rng.choice([Yes, No]) response = yield action p_valid = self._valid_detector.action_valid(action, first_sentence(response)) success = (p_valid > 0.5) self.record(success) self._eagerness = 0.
def get_double_action(self): if len(kg.player_location.entities) + len(kg.inventory.entities) <= 1: return None entity1 = None entity2 = None count = 0 while id(entity1) == id(entity2): if count == 100: return None # Failsafe else: count += 1 entity1 = self.get_random_entity() entity2 = self.get_random_entity() verb, prep = rng.choice(complex_verbs) return DoubleAction(verb, entity1, prep, entity2)
def extract_single_object_actions(self, entity): if id(entity) in self.cached_extractions: return self.cached_extractions[id(entity)] # Collect known actions for the given entity. noun = entity.name actions_to_try = [] unknown_actions_to_exclude = {} for affordable_attribute in self.affordable_attributes: est_prob = self.estimate_attribute_prob( noun, affordable_attribute.attribute_name) if est_prob >= affordable_attribute.known_action_extraction_threshold: for known_action in affordable_attribute.known_actions_to_try: actions_to_try.append((known_action(entity), est_prob)) for unknown_action in affordable_attribute.unknown_actions_to_exclude: unknown_actions_to_exclude[unknown_action] = True # Collect unknown actions for the given entity. unknown_actions_to_try = self.extract_unknown_actions_with_log_probs( noun) for a_lp in unknown_actions_to_try: if a_lp[0] not in self.action_prior_table: self.action_prior_table[a_lp[0]] = -1. # Needs human review. for a_lp in unknown_actions_to_try: prob = self.estimate_unknown_action_prob(a_lp[1]) prob *= max(0., self.action_prior_table[ a_lp[0]]) # Treat unreviewed actions (-1) as 0.0 if prob > self.unknown_action_extraction_threshold: action_text = a_lp[0] action_minus_the = action_text[:-4] if action_minus_the not in unknown_actions_to_exclude.keys(): if action_minus_the in self.unknown_actions_to_promote: target_action = rng.choice( self.unknown_actions_to_promote[action_minus_the]) action_object = target_action(entity) else: action_object = action.SingleAction( action_text, entity) actions_to_try.append((action_object, prob)) # Sort by descending probability of expected value of taking the action. actions_to_try.sort(key=lambda tup: tup[1], reverse=True) self.cached_extractions[id(entity)] = actions_to_try return actions_to_try
def get_single_object_action(self): entity = self.get_random_entity() if not entity: return None verb = rng.choice(single_object_verbs) return SingleAction(verb, entity)
def get_standalone_action(self): return StandaloneAction(rng.choice(standalone_verbs))
def get_random_entity(self): """ Returns a random entity from the location or inventory. """ if kg.player_location.entities or kg.inventory.entities: return rng.choice(kg.player_location.entities + kg.inventory.entities) return None