def get_slot_from_sub_goal(self, slot): if not self.sub_goal_exists(): return None sub_goal = self.next_sub_goal() if sub_goal['name'] in ('Copy_file', 'Move_file', 'Search_file', 'Open_file'): if slot == 'file_name': return sub_goal['file'] if slot == 'parent_directory' or slot == 'origin': if 'parent_directory' in sub_goal: return sub_goal['parent_directory'] if 'origin' in sub_goal: return FileTreeSimulator.last_dir_in_path( sub_goal['origin']) if slot == 'dest' and 'dest' in sub_goal: return FileTreeSimulator.last_dir_in_path(sub_goal['dest']) elif sub_goal['name'] == 'Rename_file': if slot == 'old_name': return sub_goal['old_name'] if slot == 'new_name': return sub_goal['new_name'] if slot == 'parent_directory': return sub_goal['parent_directory'] elif sub_goal['name'] == 'Change_directory': if slot == 'directory': return FileTreeSimulator.last_dir_in_path(sub_goal['dirs'][0]) return None
def generate_sub_goal_intent(self): sub_goal = self.next_sub_goal() pF = 0.5 if sub_goal['name'] == 'Change_directory': assert self.state['current_directory'] != sub_goal['dirs'][ -1], 'sub goal already reached' dirs = sub_goal['dirs'] index = int(random.uniform(0, len(dirs) - 0.01)) directory = dirs[index].split('/')[-1] return self.create_change_dir_desire(directory) elif sub_goal['name'] == 'Search_file': if random.random() < pF: return { 'intent': self.u_request, 'slot': 'directory', 'file_name': sub_goal['file'] } return {'intent': self.u_request, 'slot': 'directory'} elif sub_goal['name'] == 'Open_file': action = {'intent': self.Open_file_desire} if random.random() < pF: action['file_name'] = sub_goal['file'] if random.random() < pF: action['parent_directory'] = sub_goal['parent_directory'] return action elif sub_goal['name'] == 'Rename_file': action = {'intent': self.Rename_file_desire} if random.random() < pF: action['old_name'] = sub_goal['old_name'] if random.random() < pF: action['new_name'] = sub_goal['new_name'] if random.random() < pF: action['parent_directory'] = sub_goal['parent_directory'] return action elif sub_goal['name'] == 'Move_file' or sub_goal['name'] == 'Copy_file': pO, pD, pF = 0.2, 0.4, 0.8 action = { 'intent': (self.Move_file_desire if sub_goal['name'] == 'Move_file' else self.Copy_file_desire) } if random.uniform(0, 1) < pO: action['origin'] = FileTreeSimulator.last_dir_in_path( sub_goal['origin']) if random.uniform(0, 1) < pD: action['dest'] = FileTreeSimulator.last_dir_in_path( sub_goal['dest']) if random.uniform(0, 1) < pF: action['file_name'] = sub_goal['file'] return action return None
def create_tree_sim(self, root_inf=None): if root_inf is None: root = self.root sim = FileTreeSimulator([], name=self.name_by_node[root]) else: root, sim = root_inf if root not in self.children: return sim for n in self.children[root]: if n not in self.file_exists: continue t = 0 if self.file_type[n] == fbrowser.Directory else 1 _, m = sim.add_file(self.name_by_node[n], t) if not t: self.create_tree_sim((n, m['tree_sim'])) return sim
def add_known_files_to_graph(self, tree_sim=None): """ adds root directory for now :return: root's node and root's name """ if tree_sim is None: tree_sim = FileTreeSimulator() root = self.add_root_file(tree_sim.name) self.add_tree_to_graph(tree_sim, root)
def _ask_sub_goal(self, agent_action): if not self.sub_goal_exists(): return None asked_action = agent_action['action'] sub_goal = self.next_sub_goal() if asked_action['intent'] == 'Change_directory' and sub_goal['name'] == 'Change_directory' \ and asked_action['new_directory'] in sub_goal['dirs']: return {'intent': self.confirm} if asked_action['intent'] in ( 'Move_file', 'Copy_file') and sub_goal['name'] == asked_action['intent']: keys = ['origin', 'dest'] asked_action['file'] = asked_action['file_name'] response = {'intent': self.confirm} for key in keys: if not FileTreeSimulator.equal_paths(sub_goal[key], asked_action[key]): return {'intent': self.deny} if asked_action['file'] != sub_goal['file']: return {'intent': self.deny} return response if asked_action['intent'] == 'Open_file' and sub_goal[ 'name'] == asked_action['intent']: p_dir = FileTreeSimulator.last_dir_in_path(asked_action['path']) if p_dir == sub_goal['parent_directory'] and asked_action[ 'file_name'] == sub_goal['file']: return {'intent': self.confirm} else: return {'intent': self.deny} if sub_goal['name'] == 'Rename_file' and asked_action[ 'intent'] == sub_goal['name']: p_dir = FileTreeSimulator.last_dir_in_path(asked_action['path']) asked_action['parent_directory'] = p_dir keys = ['old_name', 'new_name', 'parent_directory'] response = {'intent': self.confirm} for key in keys: if asked_action[key] != sub_goal[key]: return {'intent': self.deny} return response return None
def reset(self, first_action): tree = FileTreeSimulator.read_existing_dirs(directory=self.directory) data = {'current_tree_sim': tree, 'tree_sim': tree} self.dqn_agent.eps = 0 self.dqn_agent.reset(first_action, data)
def update_sub_goals(self, agent_action): for sub_goal in self.goal['sub_goal']: if sub_goal['name'] == 'Change_directory': last_dir = sub_goal['dirs'][-1] current_dir = self.state['current_directory'] if current_dir[-1] == '/': current_dir = current_dir[:-1] if last_dir[-1] == '/': last_dir = last_dir[:-1] if current_dir == last_dir: self.goal['sub_goal'].remove(sub_goal) self.subgoal_reward = len(sub_goal['dirs']) elif current_dir in sub_goal['dirs']: self.subgoal_reward = sub_goal['dirs'].index( current_dir) + 1 del sub_goal['dirs'][:self.subgoal_reward] if sub_goal['name'] == 'Search_file': if agent_action[ 'intent'] == 'inform' and 'paths' in agent_action: self.subgoal_reward = 0 paths = agent_action['paths'] r = self.state['current_file_tree'].lookup_file_name( sub_goal['file']) self.subgoal_reward = -2 if r is not None: f, m = r for path in paths: if FileTreeSimulator.equal_paths( m['tree_sim'].path(True), path): self.subgoal_reward = 2 break self.goal['sub_goal'].remove(sub_goal) if sub_goal['name'] == 'Rename_file': if agent_action['intent'] == 'Rename_file': p_dir = FileTreeSimulator.last_dir_in_path( agent_action['path']) agent_action['parent_directory'] = p_dir keys = ['old_name', 'new_name', 'parent_directory'] self.subgoal_reward = 2 for key in keys: if agent_action[key] != sub_goal[key]: self.subgoal_reward = -3 self.goal['sub_goal'].remove(sub_goal) if sub_goal['name'] == 'Open_file': if agent_action['intent'] == sub_goal['name']: p_dir = FileTreeSimulator.last_dir_in_path( agent_action['path']) if p_dir != sub_goal['parent_directory'] or agent_action[ 'file_name'] != sub_goal['file']: self.subgoal_reward = -3 else: self.subgoal_reward = 2 self.goal['sub_goal'].remove(sub_goal) if sub_goal['name'] == 'Move_file' or sub_goal[ 'name'] == 'Copy_file': if agent_action['intent'] == sub_goal['name']: keys = ['origin', 'dest'] agent_action['file'] = agent_action['file_name'] self.subgoal_reward = 2 for key in keys: if not FileTreeSimulator.equal_paths( sub_goal[key], agent_action[key]): self.subgoal_reward = -3 break if agent_action['file'] != sub_goal['file']: self.subgoal_reward = -3 self.goal['sub_goal'].remove(sub_goal) break