def __init__(self, config, environment, choice="prop", llb=False, explo="babbling", n_explo_points=0, choose_children_mode='competence', choose_children_local=True): Observable.__init__(self) self.config = config self.environment = environment self.choice = choice self.llb = llb self.explo = explo self.n_explo_points = n_explo_points self.choose_children_mode = choose_children_mode self.ccm_local = choose_children_local self.conf = self.config.agent self.expl_dims = self.config.agent.m_dims self.inf_dims = self.config.agent.s_dims self.t = 1 self.modules = {} self.chosen_modules = {} self.mid_control = '' self.last_space_children_choices = {} self.hierarchy = Hierarchy() # Build Hierarchy for motor_space in self.config.m_spaces.values(): self.hierarchy.add_motor_space(motor_space) for mid in self.config.modules.keys(): # Build modules self.init_module(mid) #[set.add(self.modules[mid].controled_vars, cvar) for cmid in self.config.modules[mid]['children'] if self.hierarchy.is_mod(cmid) for cvar in self.modules[cmid].controled_vars] for mid in self.modules.keys(): self.last_space_children_choices[mid] = Queue.Queue()
def clusterSelected(self, event=None): popup = Toplevel(self) hierarchy = None if (self.displayRepresentatives): hierarchy = Hierarchy(representatives=map( lambda s: self.findHierarchy(s).hierarchy, self.selected)) else: reps = [] for tag in self.selected: reps += self.findHierarchy(tag).hierarchy.representatives hierarchy = Hierarchy(representatives=reps) frame = GraphFrame(popup, hierarchy) frame.pack(fill=BOTH, expand=1)
def __init__(self, groupSize, dirs): self.groupSize = groupSize self.dir = dirs if not os.path.exists(self.dir): os.mkdir(self.dir) self.VP2AS = defaultdict(set) self.VP2path = defaultdict(set) self.fullVP = set() self.partialVP = set() self.VPGroup = list() self.fileNum = -1 self.tier = Hierarchy('asrel.txt')
def to_tree(self, root_id, priority_list=[]): """ Use a BFS from the root to decide the level of each node in the graph. Associate the children to their parents according to a priority_list. Input: dag - a directer acyclic graph, format should be a dictionary whose keys are all the node IDs and values are lists of theirs children root - the ID of the node that should be the root of the tree """ if root_id not in self: raise KeyError('The chosen root is not in the GODag %s:' % root_id) tree = Hierarchy() tree.create_node(self[root_id].name, root_id, parent=None) parents = [root_id] level = 0 while parents: logging.info("Tree level %02d, %d nodes" % (level, len(parents))) children = set() child_to_parent_list = {} for parent in parents: for child in [c.id for c in self[parent].children]: if tree.get_node(child) is not None: continue children.add(child) child_to_parent_list.setdefault(child, []).append(parent) # associate each child with the parent with the highest priority # in the priority list for child, parent_list in child_to_parent_list.iteritems(): parent = parent_list[ 0] # by default, choose the first parent in the list # if one of the IDs in the priority list is a parent, use that # one instead for go_id in priority_list: if go_id in parent_list: parent = go_id break # we have to get rid of all colons in the names of the # tree because Paver treats them as delimiters between # display name and systematic name child_name = self[child].name.replace(':', ';') tree.create_node(child_name, child, parent) parents = list(children) level += 1 return tree
def to_shallow_tree(self, root_id): """ Use a BFS from the root to decide the level of each node in the graph. For each child, keep only one of the edges leading to it so it will have a single parent, from the previous level. Choose these edges in a way which will keep the tree balanced (probably not optimally though). This will assure that only one of the shortest paths from the root to every node is kept. Input: dag - a directer acyclic graph, format should be a dictionary whose keys are all the node IDs and values are lists of theirs children root - the ID of the node that should be the root of the tree """ if root_id not in self: raise KeyError('The chosen root is not in the GODag %s:' % root_id) tree = Hierarchy() tree.create_node(self[root_id].name, root_id, parent=None) parents = [root_id] while parents: children = set() child_to_parent_list = {} for parent in parents: for child in [c.id for c in self[parent].children]: if tree.get_node(child) is not None: continue children.add(child) child_to_parent_list.setdefault(child, []).append(parent) # the following list will contain pairs, counting the number of # children that each parent has. child_counts = [[0, p] for p in parents] for child, parent_list in child_to_parent_list.iteritems(): # give this child to the parent with the least children for i, (counter, parent) in enumerate(child_counts): if parent in parent_list: break # we have to get rid of all colons in the names of the # tree because Paver treats them as delimiters between # display name and systematic name child_name = self[child].name.replace(':', ';') tree.create_node(child_name, child, parent) child_counts[i][0] += 1 child_counts.sort() return tree
def to_tree(self, root_id, priority_list=[]): """ Use a BFS from the root to decide the level of each node in the graph. Associate the children to their parents according to a priority_list. Input: dag - a directer acyclic graph, format should be a dictionary whose keys are all the node IDs and values are lists of theirs children root - the ID of the node that should be the root of the tree """ if root_id not in self: raise KeyError('The chosen root is not in the GODag %s:' % root_id) tree = Hierarchy() tree.create_node(self[root_id].name, root_id, parent=None) parents = [root_id] level = 0 while parents: logging.info("Tree level %02d, %d nodes" % (level, len(parents))) children = set() child_to_parent_list = {} for parent in parents: for child in [c.id for c in self[parent].children]: if tree.get_node(child) is not None: continue children.add(child) child_to_parent_list.setdefault(child, []).append(parent) # associate each child with the parent with the highest priority # in the priority list for child, parent_list in child_to_parent_list.iteritems(): parent = parent_list[0] # by default, choose the first parent in the list # if one of the IDs in the priority list is a parent, use that # one instead for go_id in priority_list: if go_id in parent_list: parent = go_id break # we have to get rid of all colons in the names of the # tree because Paver treats them as delimiters between # display name and systematic name child_name = self[child].name.replace(':', ';') tree.create_node(child_name, child, parent) parents = list(children) level += 1 return tree
def __init__(self, name="NoName", mintime=0., maxtime=0.): """ Creates a new Transcription instance. @param name: (str) the name of the transcription @param mintime in seconds @param maxtime in seconds """ super(Transcription, self).__init__() self.__name = u'NoName' self.__mintime = mintime self.__maxtime = maxtime self.__tiers = [] # a list of Tier() instances self.__media = [] # a list of Media() instances self.__ctrlvocab = [] # a list of CtrlVocab() instances self._hierarchy = Hierarchy() self.SetName(name)
def main(args): if(len(args) != 3): print "Usage: clusterFrame.py C clustering.pkl" print " C is the cluster in clustering.pkl to display" sys.exit(0) C = int(args[1]) path = args[2] print "Loading" clustering = utils.load_obj(path) #clustering = doc.get_docs_nested(driver.get_data_dir("very_small")) hierarchy = Hierarchy.createHierarchy(clustering) print "Starting GUI" root = Tk() frame = GraphFrame(root, hierarchy) frame.pack(fill=BOTH,expand=1) root.mainloop()
def __pb_classificate_click(self): self.__remove_chart_from_layout() self.__init_chart() try: self.__amount_of_objects = int(self.ui.le_amount_of_objects.text()) except ValueError: pass # self.__distances = [[0.0, 6.0, 5.0], [6.0, 0.0, 2.0], [5.0, 2.0, 0.0]] self.__distances = self.__generate_random_distances( self.__amount_of_objects) print(self.__distances) hierarchy = Hierarchy(self.__distances, self.__amount_of_objects) hierarchy.find_groups() hierarchy.draw(self.__chart) self.__chart.createDefaultAxes() self.__update_form()
def main(): d = Diagnosis() c = Category() l = Level() q = Question() e = Emergency_followup() hist = Historik() value_list = [3, 4, 5, 4, 5, 6, 6, 5, 4] #answer = q.get_question("sex.txt", 1) #print(answer) #cat_list = c.get_category_questions("sex.txt") #print(cat_list) #answer = l.get_level_category(3) #print(answer) h = Hierarchy() #answer = h.get_hierarchy_categories() #print(answer) I = Interface() #answer = I.get_questions_all() #print(answer) #answer = I.get_question_akut() #print(answer) #answer = d.setup_diagnosis() #answer = d.read_diagnosis() #answer = d.write_diagnosis("this is question nr 3", "A diagnos") #answer = d.get_diagnosis("this is question nr 3") #answer = e.setup_followup() #answer = e.change_followup("How have your general health been today?", "Go for a run") #answer = e.read_followup("How have your general health been today?") #answer = e.new_followup() #answer = I.interface_questions_akut() #answer = I.interface_daily_questions() #answer = hist.set_historik_total([1, 2 , 3, 6]) #answer = hist.get_historik_all_total() answer = l.level_count() print(answer)
def main(input_data, basement, num_layers, multiplier, num_patches, features): if os.path.isfile(input_data): data = load_file(input_data) elif os.path.isdir(input_data): data = load_folder(input_data) else: raise Exception('{} is neither file nor directory'.format(input_data)) if features: try: features = [int(f) for f in features.split(',')] except: raise Exception('Bad features: {}. Expecting a bunch of integers separated by comma'.format(features)) if len(features) != num_layers: raise Exception('{} features specified, but total number of layers is {}'.format(len(features), num_layers)) H = Hierarchy(data) for i in xrange(num_layers): mult = 0 if i == 0 else multiplier f = features[i] if features else 100 H.add_layer(basement * (multiplier ** i), f, num_patches, mult) H.learn() H.visualize_layer(i)
def main(): parser = argparse.ArgumentParser(__file__, description='CSV to Block Diagram Converter') parser.add_argument('--path', '-p', help='Enter file path to csv directory', required=True) parser.add_argument('--dot', '-d', help='Generate dot files', required=False, type=bool) parser.add_argument('--clips', '-c', help='Generate CLIPS facts', required=False, type=bool) args = parser.parse_args() csv_dir_path = args.path read_csv = ReadCSV() hier = Hierarchy() for (directory, _, files) in walk(csv_dir_path): for f in files: try: if len(f.split('.')) == 2 and f.split('.')[1] == 'csv': csv_name, csv_obj = read_csv.read_csv(directory, f) hier.collect_first_row(csv_name, csv_obj) except NameError as e: print e.message hierarchy = hier.sorted_graph hier.construct_graph() hier.bfs_edges() levels = hier.levels for (directory, _, files) in walk(csv_dir_path): for f in files: try: if len(f.split('.')) == 2 and f.split('.')[1] == 'csv': read_csv.read_csv_contents(directory, f) except NameError as e: print e.message knol = read_csv.knol_all layout = BlockDiagramLayout(levels, knol, hierarchy) layout.draw_layout() layout.write_dot() dot_out_dir = layout.dot_out_dir block_out_dir = layout.block_out_dir # write dot for individual blocks if args.dot: gen_dot = GenerateDot(dot_out_dir, block_out_dir) print '-' * 100 print 'GENERATE DOT FILE(S)' print '-' * 100 block_map = {} for i, block in enumerate(knol.keys()): print i+1, '=', block.replace('.csv', '') block_map[i+1] = block print choice = raw_input('Choose block(s) for dot file generation (space separated): ') gen_dot.generate_dot(choice, block_map, knol) # generate CLIPS facts for individual blocks if args.clips: gen_clips = GenerateCLIPS() print '-' * 100 print 'GENERATE CLIPS FACTS' print '-' * 100 block_map = {} for i, block in enumerate(knol.keys()): print i + 1, '=', block.replace('.csv', '') block_map[i + 1] = block print choice = raw_input('Choose block(s) for CLIPS facts generation (space separated): ') gen_clips.generate_clips_facts(choice, block_map, knol)
parser.add_argument("--supervised_data_dir", default=None) parser.add_argument("--results_folder", default=None) args = parser.parse_args() val_file = os.path.join(p.data_dir, 'val.data') if not args.supervised else os.path.join(args.supervised_data_dir, 'supervised.data') if args.email: email_sender = EmailSender(smtp_server=p.smtp_server, port=p.port, sender_email=p.sender_email, sender_password=args.sender_password, receiver_email=p.receiver_email, subject="%s: testing %s model" % (socket.gethostname(), args.model_type)) email_sender.send_email("Starting to test %s model." % args.model_type) else: email_sender = None if args.supervised: hierarchy_file = os.path.join(args.supervised_data_dir, 'hierarchy.pkl') if not os.path.exists(hierarchy_file): hierarchy_file = os.path.join(args.checkpoint_folder, 'hierarchy.pkl') else: hierarchy_file = os.path.join(args.checkpoint_folder, 'hierarchy.pkl') hierarchy = Hierarchy.from_dict(read_pickle(hierarchy_file)) try: main(args.model_type, val_file, args.checkpoint_folder, hierarchy, supervised=args.supervised, device=args.device, email_sender=email_sender, results_folder=args.results_folder, noload=args.noload) # nprocs = 2 # main_distributed = distributed_wrapper(main, nprocs) # main_distributed(args.model_type, val_file, args.checkpoint_folder, device=args.device) except Exception as e: if email_sender is not None: email_sender("Got an exception:\n%s" % e) raise e
class Transcription( MetaObject ): """ @authors: Brigitte Bigi @contact: [email protected] @license: GPL, v3 @summary: Generic representation of an annotated file. Transcriptions in SPPAS are represented with: - meta data: a serie of tuple key/value - a name (used as Id) - tiers - a hierarchy - controlled vocabularies (yet not used) Inter-tier relations are managed by establishing alignment or association links between 2 tiers: - alignment: annotations of a tier A (child) have only Time instances included in those of annotations of tier B (parent); - association: annotations of a tier A have exactly Time instances included in those of annotations of tier B. >>> transcription = Transcription() >>> formertier = transcription.NewTier("parent") >>> lattertier = transcription.NewTier("child") >>> transcription.GetHierarchy().addLink("TimeAlignment", formertier, lattertier) """ def __init__(self, name="NoName", mintime=0., maxtime=0.): """ Creates a new Transcription instance. @param name: (str) the name of the transcription @param mintime in seconds @param maxtime in seconds """ super(Transcription, self).__init__() self.__name = u'NoName' self.__mintime = mintime self.__maxtime = maxtime self.__tiers = [] # a list of Tier() instances self.__media = [] # a list of Media() instances self.__ctrlvocab = [] # a list of CtrlVocab() instances self._hierarchy = Hierarchy() self.SetName(name) # ------------------------------------------------------------------------ def GetHierarchy(self): """ Return the Hierarchy instance. """ return self._hierarchy # ------------------------------------------------------------------------ def GetName(self): """ Return the string of the name of the transcription. """ return self.__name # ------------------------------------------------------------------------ def SetName(self, name): """ Set a new name (the name is also used as Id, to identify a Transcription) @param name (str) @raise UnicodeDecodeError """ name = ' '.join(name.split()) if isinstance(name, unicode): self.__name = name else: try: self.__name = name.decode("utf-8") except UnicodeDecodeError as e: raise e # ------------------------------------------------------------------------ def Set(self, tiers, name='NoName'): """ Set a transcription. @param tiers: Transcription or list of Tier instances. @raise TypeError: """ if isinstance(tiers, Transcription): self.metadata = tiers.metadata self._hierarchy = tiers._hierarchy self.__media = tiers.__media self.__ctrlvocab = tiers.__ctrlvocab self.__name = tiers.__name self.__mintime = tiers.__mintime self.__maxtime = tiers.__maxtime self.__tiers = tiers.__tiers if all(isinstance(tier, Tier) for tier in tiers) is False: raise TypeError("Transcription or List of Tier instances argument required, not %r" % tiers) self.__tiers = [tier for tier in tiers] self.__media = set( [tier.GetMedia() for tier in tiers] ) self.__ctrlvocab = set( [tier.GetCtrlVocab() for tier in tiers] ) self.__name = name # ------------------------------------------------------------------------ def Copy(self): """ Return a copy of the transcription. """ return copy.deepcopy(self) # ------------------------------------------------------------------------ def SetSherableProperties(self, other): """ Set some of the properties of other to self: media, ctrlvocab. """ if not isinstance(other, Transcription): raise TypeError("Can not set properties. Expected Transcription instance, got %s."%type(other)) self.SetMedia( other.GetMedia() ) self.SetCtrlVocab( other.GetCtrlVocab() ) # ------------------------------------------------------------------------ # Media # ------------------------------------------------------------------------ def GetMedia(self): return self.__media def GetMediaFromId(self, idm): idt = idm.strip() for m in self.__media: if m.id == idt: return m return None def AddMedia(self, newmedia): if not isinstance(newmedia, Media): raise TypeError("Can not add media in Transcription. Expected Media instance, got %s."%type(newmedia)) ids = [ m.id for m in self.__media ] if newmedia.id in ids: raise ValueError('A media is already defined with the same identifier %s'%newmedia.id) self.__media.append( newmedia ) def RemoveMedia(self, oldmedia): if not isinstance(oldmedia, Media) or not oldmedia in self.__media: raise TypeError("Can not remove media of Transcription.") self.__media.remove( oldmedia ) for tier in self.__tiers: if tier.GetMedia() == oldmedia: tier.SetMedia(None) def SetMedia(self, media): self.__media = [] for m in media: self.AddMedia( m ) # ------------------------------------------------------------------------ # Controlled vocabularies # ------------------------------------------------------------------------ def GetCtrlVocab(self): return self.__ctrlvocab def GetCtrlVocabFromId(self, idt): idt = idt.strip() for c in self.__ctrlvocab: if c.id == idt: return c return None def AddCtrlVocab(self, ctrlvocab): if not isinstance(ctrlvocab, CtrlVocab): raise TypeError("Unknown controlled vocabulary error in transcription.py.") self.__ctrlvocab.append(ctrlvocab) def RemoveCtrlVocab(self, ctrlvocab): if not isinstance(ctrlvocab, CtrlVocab): raise TypeError("Unknown controlled vocabulary error in transcription.py.") self.__ctrlvocab.remove(ctrlvocab) for tier in self.__tiers: if tier.GetCtrlVocab() == ctrlvocab: tier.SetCtrlVocab(None) # ----------------------------------------------------------------------- # Tiers # ------------------------------------------------------------------------ def GetMinTime(self): """ Return the minimum time value. """ return self.__mintime # ------------------------------------------------------------------------ def SetMinTime(self, mintime): """ Set the minimum time value. """ self.__mintime = mintime # ------------------------------------------------------------------------ def GetMaxTime(self): """ Return the maximum time value. """ # In theory, maxtime represents the duration of the annotated media. # Then, it can not be lesser than the end of the last annotation... # But... none of the annotation formats indicate such duration! # So, we do what we can with what we have. if self.GetEnd() > self.__maxtime: self.__maxtime = self.GetEnd() return self.__maxtime # ------------------------------------------------------------------------ def SetMaxTime(self, maxtime): """ Set the maximum time value. """ self.__maxtime = maxtime if self.__maxtime < self.GetEnd(): raise Exception( 'Impossible to fix a max time value ' 'lesser than the end of annotations.') # ------------------------------------------------------------------------ def GetSize(self): """ Return the number of tiers in the transcription. """ return len(self.__tiers) # ------------------------------------------------------------------------ def NewTier(self, name="Empty"): """ Add a new empty tier at the end of the transcription. @param name: (str) the name of the tier to create @return: newly created empty tier """ tier = Tier(name) tier.SetTranscription(self) self.__rename(tier) self.__tiers.append(tier) return tier # ------------------------------------------------------------------------ def Add(self, tier, index=None): """ Add a new tier at a given index. Index must be lower than the transcription size. By default, the tier is appended at the end of the list. @param tier (Tier) the tier to add to the transcription @param index (int) the position in the list of tiers @raise IndexError @return: Tier index """ tier.SetTranscription(self) self.__rename(tier) if tier.GetEnd() > self.__maxtime: self.__maxtime = tier.GetEndValue() if tier.GetBegin() < self.__mintime: self.__mintime = tier.GetBeginValue() if index is not None: if index >= len(self.__tiers) or index < 0: raise IndexError self.__tiers.insert(index, tier) else: self.__tiers.append(tier) index = len(self.__tiers)-1 # TODO: GET ITS MEDIA AND SET TO THE TRANSCRIPTION??? # TODO: IDEM WITH CTRLVOCAB....???? return index # ------------------------------------------------------------------------ def Append(self, tier): """ Append a tier in the transcription. @param tier (Tier) the tier to add to the transcription @return Tier index (int) """ tier.SetTranscription(self) self.__rename(tier) self.__tiers.append(tier) if tier.GetEnd() > self.__maxtime: self.__maxtime = tier.GetEndValue() if tier.GetBegin() < self.__mintime: self.__mintime = tier.GetBeginValue() # TODO: GET ITS MEDIA AND SET TO THE TRANSCRIPTION??? # TODO: IDEM WITH CTRLVOCAB....??? return len(self.__tiers)-1 # ------------------------------------------------------------------------ def Pop(self, index=-1): """ Pop a tier of the transcription. @param index: the index of the tier to pop. Default is the last one. @raise IndexError @return: Return the removed tier """ if self.IsEmpty(): raise IndexError("Pop from empty transcription.") try: self._hierarchy.removeTier(self.__tiers[index]) return self.__tiers.pop(index) except IndexError as e: raise e # ------------------------------------------------------------------------ def Remove(self, index): """ Remove a tier of the transcription. @param index: the index of the tier to remove. @raise IndexError: """ if index >= len(self.__tiers) or index < 0: raise IndexError self._hierarchy.removeTier(self.__tiers[index]) del self.__tiers[index] # ------------------------------------------------------------------------ def GetIndex(self, name, case_sensitive=True): """ Get the index of a tier from its name. @param name: (str) EXACT name of the tier @param case_sensitive: (bool) @return: Return the tier index or -1 """ t = self.Find(name, case_sensitive) for i, tier in enumerate(self.__tiers): if t is tier: return i return -1 # ------------------------------------------------------------------------ def Find(self, name, case_sensitive=True): """ Find a tier from its name. @param name: (str) EXACT name of the tier @param case_sensitive: (bool) @return: Return the tier or None """ for tier in self: if case_sensitive: if tier.GetName() == name.strip(): return tier else: if tier.GetName().lower() == name.strip().lower(): return tier return None # ------------------------------------------------------------------------ def GetBeginValue(self): """ Return the smaller begin time value of all tiers. Return 0 if the transcription contains no tiers. @return: Float value, or 0 if the transcription contains no tiers. """ if self.IsEmpty(): return 0. return min(tier.GetBeginValue() for tier in self.__tiers) @deprecated def GetBegin(self): return self.GetBeginValue() # ------------------------------------------------------------------------ def GetEndValue(self): """ Return the higher end time value of all tiers. Return 0 if the transcription contains no tiers. @return: Float value, or 0 if the transcription contains no tiers. """ if self.IsEmpty(): return 0. return max(tier.GetEndValue() for tier in self.__tiers) @deprecated def GetEnd(self): return self.GetEndValue() # ------------------------------------------------------------------------ def IsEmpty(self): """ Ask the transcription to be empty or not. A transcription is empty if it does not contain tiers. @return: bool """ return len(self.__tiers) == 0 # ------------------------------------------------------------------------ # Input/Output # ------------------------------------------------------------------------ def read(self): name = self.__class__.__name__ raise NotImplementedError("%s does not support read()." % name) # ------------------------------------------------------------------------ def write(self): name = self.__class__.__name__ raise NotImplementedError("%s does not support write()." % name) # ------------------------------------------------------------------------ # Private # ------------------------------------------------------------------------ def __rename(self, tier): name = tier.GetName() i = 2 while self.Find(name) is not None: name = u"%s-%d" % (tier.GetName(), i) i += 1 tier.SetName(name) # ------------------------------------------------------------------------ # Overloads # ------------------------------------------------------------------------ def __len__(self): return len(self.__tiers) # ------------------------------------------------------------------------ def __iter__(self): for x in self.__tiers: yield x # ------------------------------------------------------------------------ def __getitem__(self, i): return self.__tiers[i]
class Supervisor(Observable): def __init__(self, config, environment, choice="prop", llb=False, explo="babbling", n_explo_points=0, choose_children_mode='competence', choose_children_local=True): Observable.__init__(self) self.config = config self.environment = environment self.choice = choice self.llb = llb self.explo = explo self.n_explo_points = n_explo_points self.choose_children_mode = choose_children_mode self.ccm_local = choose_children_local self.conf = self.config.agent self.expl_dims = self.config.agent.m_dims self.inf_dims = self.config.agent.s_dims self.t = 1 self.modules = {} self.chosen_modules = {} self.chosen_spaces = {} for s_space in self.config.s_spaces: self.chosen_spaces[s_space] = 0 self.mid_control = '' self.last_space_children_choices = {} self.credit_tool_move = {} self.credit_hand_move = {} self.hierarchy = Hierarchy() # Build Hierarchy for motor_space in self.config.m_spaces.values(): self.hierarchy.add_motor_space(motor_space) for mid in self.config.modules.keys(): # Build modules self.init_module(mid) #[set.add(self.modules[mid].controled_vars, cvar) for cmid in self.config.modules[mid]['children'] if self.hierarchy.is_mod(cmid) for cvar in self.modules[cmid].controled_vars] for mid in self.modules.keys(): self.last_space_children_choices[mid] = Queue.Queue() self.credit_tool_move[mid] = 0 self.credit_hand_move[mid] = 0 def init_module(self, mid): self.modules[mid] = Module(self.config, mid) self.chosen_modules[mid] = 0 self.hierarchy.add_module(mid) # for space in self.config.modules[mid]['m_list']: # self.hierarchy.add_motor_space(space) self.hierarchy.add_sensori_space(self.config.modules[mid]['s']) for space in self.config.modules[mid]['m_list']: self.hierarchy.add_edge_space_module(mid, space) self.hierarchy.add_edge_module_space(mid, self.config.modules[mid]['s']) def choose_interesting_space(self, mode='softmax'): s_spaces = self.config.s_spaces interests = {} for s_space in s_spaces.keys(): if s_space == "s_h": interests[s_space] = self.modules["mod1"].interest() elif s_space == "s_t1": interests[s_space] = self.modules["mod2"].interest() # elif s_space == "s_t2": # interests[s_space] = self.modules["mod3"].interest() elif s_space == "s_o": interests[s_space] = np.sum([self.modules[mid].interest() for mid in ["mod3", "mod4"]]) self.emit('interests', [self.t, interests]) if mode == 'random': s_space = np.random.choice(self.interests.keys()) elif mode == 'greedy': eps = 0.2 if np.random.random() < eps: s_space = np.random.choice(self.interests.keys()) else: s_space = max(interests, key=interests.get) elif mode == 'softmax': temperature = 0.1 w = interests.values() s_space = s_spaces.keys()[softmax_choice(w, temperature)] elif mode == 'prop': w = interests.values() s_space = s_spaces.keys()[prop_choice(w, eps=0.2)] if self.t % 200 == 1: print print 'iterations', self.t - 1 print "competences", np.array([self.modules[mid].competence() for mid in self.modules.keys()]) print "progresses", np.array([self.modules[mid].interest_model.current_progress for mid in self.modules.keys()]) print "interests", np.array([self.modules[mid].interest() for mid in self.modules.keys()]) print "sm db n points", [len(self.modules[mid].sensorimotor_model.model.imodel.fmodel.dataset) for mid in self.modules.keys()] print "im db n points", [len(self.modules[mid].interest_model.data_xc) for mid in self.modules.keys()] print self.chosen_modules print "made tool moved object", self.credit_tool_move print "made hand moved object", self.credit_hand_move self.chosen_spaces[s_space] = self.chosen_spaces[s_space] + 1 return s_space # def choose_babbling_module(self, auto_create=False, progress_threshold=1e-2, mode='softmax', weight_by_level=False): # interests = {} # for mid in self.modules.keys(): # interests[mid] = self.modules[mid].interest() # self.emit('interest_' + mid, [self.t, interests[mid]]) # self.emit('competence_' + mid, [self.t, self.modules[mid].competence()]) # max_progress = max(interests.values()) # # # self.emit('babbling_module', "mod2") # # return "mod2" # # #print "max_progress", max_progress # if not auto_create or max_progress > progress_threshold: # if mode == 'random': # mid = np.random.choice(self.modules.keys()) # elif mode == 'greedy': # eps = 0.1 # if np.random.random() < eps: # mid = np.random.choice(self.modules.keys()) # else: # mid = max(interests, key=interests.get) # elif mode == 'softmax': # temperature = 0.1 # mids = interests.keys() # w = interests.values() # #print "progresses", w # #print "competences", [mod.competence() for mod in self.modules.values()] # if weight_by_level: # levels = self.hierarchy.module_levels() # for i in range(len(mids)): # f = 2.0 # w[i] = w[i] * np.power(f, max(levels.values()) - levels[mids[i]]) # #print w # mid = mids[softmax_choice(w, temperature)] # # elif mode == 'prop': # mids = interests.keys() # w = interests.values() # if self.t % 200 == 1: # # print # # print "influence ", self.influence # print # print 'iterations', self.t - 1 # print "competences", np.array([self.modules[mid].competence() for mid in self.modules.keys()]) # if sum(w) > 0: # print "interests", np.array(w) # # print "interesting objects:", int(((w[3] + w[6]) / sum(w))*100), "%" # # print "cumulated:", int((self.chosen_modules["mod4"] + self.chosen_modules["mod7"]) / float(sum(self.chosen_modules.values())) * 100), "%" # print "sm db n points", [len(self.modules[mid].sensorimotor_model.model.imodel.fmodel.dataset) for mid in self.modules.keys()] # print "im db n points", [len(self.modules[mid].interest_model.data_xc) for mid in self.modules.keys()] # print self.chosen_modules # #print self.babbling_module # # if weight_by_level: # levels = self.hierarchy.module_levels() # for i in range(len(mids)): # f = 10.0 # w[i] = w[i] * np.power(f, max(levels.values()) - levels[mids[i]]) # #print w # mid = mids[prop_choice(w, eps=0.1)] # # self.chosen_modules[mid] = self.chosen_modules[mid] + 1 # #print self.chosen_modules # self.emit('babbling_module', mid) # return mid # else: # return self.create_module() def fast_forward(self, log, forward_im=False): #ms_list = [] for m,s in zip(log.logs['motor'], log.logs['sensori']): ms = np.append(m,s) self.update_sensorimotor_models(ms) #ms_list += [ms] for mid, mod in self.modules.iteritems(): mod.fast_forward_models(log, ms_list=None, from_log_mod=mid, forward_im=forward_im) def eval_mode(self): self.sm_modes = {} for mod in self.modules.values(): self.sm_modes[mod.mid] = mod.sensorimotor_model.mode mod.sensorimotor_model.mode = 'exploit' def learning_mode(self): for mod in self.modules.values(): mod.sensorimotor_model.mode = self.sm_modes[mod.mid] def check_bounds_dmp(self, m_ag): return bounds_min_max(m_ag, self.conf.m_mins, self.conf.m_maxs) def motor_babbling(self): return rand_bounds(self.conf.m_bounds)[0] def motor_primitive(self, m): return m def rest_params(self): return self.environment.rest_params() def sensory_primitive(self, s): return s def get_eval_dims(self, s): return self.set_ms(s = s)[self.config.eval_dims] def set_ms(self, m=None, s=None): ms = zeros(self.conf.ndims) if m is not None: ms[self.conf.m_dims] = m if s is not None: ms[self.conf.s_dims] = s return ms def set_ms_seq(self, m=None, s=None): ms = zeros(self.conf.ndims) if m is not None: ms[self.conf.m_dims] = m if s is not None: ms[self.conf.s_dims] = s return [ms] def get_m(self, ms): return ms[self.conf.m_dims] def get_s(self, ms): return ms[self.conf.s_dims] def update_sensorimotor_models(self, ms): #print 'ms', ms for mid in ["mod1", "mod2"]: self.modules[mid].update_sm(self.modules[mid].get_m(ms), self.modules[mid].get_s(ms)) #print 'ms2', ms obj_end_pos_y = ms[10] + ms[-1] tool1_moved = (abs(ms[-6] - ms[-4]) > 0.0001) #tool2_moved = (abs(ms[-5] - ms[-3]) > 0.0001) tool1_touched_obj = tool1_moved and (abs(ms[-4] - obj_end_pos_y) < 0.0001) #tool2_touched_obj = tool2_moved and (abs(ms[-3] - obj_end_pos_y) < 0.0001) obj_moved = abs(ms[-1]) > 0.0001 obj_moved_with_hand = obj_moved and (not tool1_touched_obj)# and (not tool2_touched_obj) if tool1_touched_obj: if not self.mid_control == '': self.credit_tool_move[self.mid_control] += 1 if obj_moved_with_hand: if not self.mid_control == '': self.credit_hand_move[self.mid_control] += 1 if tool1_touched_obj or (tool1_moved and not obj_moved_with_hand): self.modules["mod4"].update_sm(self.modules["mod4"].get_m(ms), self.modules["mod4"].get_s(ms)) #print ms #print "tool1 moved" return "mod4" # elif tool2_touched_obj or (tool2_moved and not obj_moved_with_hand): # self.modules["mod6"].update_sm(self.modules["mod6"].get_m(ms), self.modules["mod6"].get_s(ms)) # #print ms # #print "tool2 moved" # return "mod6" else: self.modules["mod3"].update_sm(self.modules["mod3"].get_m(ms), self.modules["mod3"].get_s(ms)) #print "no tool moved" return "mod3" if obj_moved else None def choose_space_child(self, s_space, s, mode="competence", local="local", k=1): """ Choose the children of space s_space among modules that have the good sensori spaces, maximizing competence. """ try: possible_mids = self.hierarchy.space_children(s_space) except KeyError: print "s_space not found in hierarchy" return None if len(possible_mids) == 1: mid = possible_mids[0] return mid eps = 0.05 if mode == "competence": if local: # for mid in ["mod2", "mod5", 'mod6']: # dists, idxs = self.modules[mid].sensorimotor_model.model.imodel.fmodel.dataset.nn_y(s, k=1) # print mid, dists, idxs, self.modules[mid].sensorimotor_model.model.imodel.fmodel.dataset.get_xy(idxs[0]), y, s competences = [self.modules[pmid].competence_reached(s) for pmid in possible_mids] # print "sm db n points", [len(self.modules[mid].sensorimotor_model.model.imodel.fmodel.dataset) for mid in self.modules.keys()] else: competences = [self.modules[pmid].competence() for pmid in possible_mids] #print "choose space child", competences if k == 1: mid = possible_mids[greedy(competences, eps)] #print "chosen mid", mid return mid else: mid = possible_mids[greedy(competences)] return [(1. - (eps/2.) if pmid == mid else eps/2.) for pmid in possible_mids] if mode == "competence_prop": if local: # for mid in ["mod2", "mod5", 'mod6']: # dists, idxs = self.modules[mid].sensorimotor_model.model.imodel.fmodel.dataset.nn_y(s, k=1) # print mid, dists, idxs, self.modules[mid].sensorimotor_model.model.imodel.fmodel.dataset.get_xy(idxs[0]), y, s competences = [self.modules[pmid].competence_reached(s) for pmid in possible_mids] #print "choose space child", competences # print "sm db n points", [len(self.modules[mid].sensorimotor_model.model.imodel.fmodel.dataset) for mid in self.modules.keys()] else: competences = [self.modules[pmid].competence() for pmid in possible_mids] if k == 1: mid = possible_mids[prop_choice(competences, eps)] return mid else: rectified = 1. / np.array(competences) probas = rectified / np.sum(rectified) return ((1. - eps) * probas + eps/2.) elif mode == "interest": if local=="local": interests = [self.modules[pmid].interest_pt(s) for pmid in possible_mids] else: interests = [self.modules[pmid].interest() for pmid in possible_mids] #print "choose space child", interests if k == 1: mid = possible_mids[greedy(interests, eps=eps)] #print "chosen mid", mid return mid else: mid = possible_mids[greedy(interests)] return [(1. - (eps/2.) if pmid == mid else eps/2.) for pmid in possible_mids] elif mode == "interest_prop": if local=="local": interests = [self.modules[pmid].interest_pt(s) for pmid in possible_mids] else: interests = [self.modules[pmid].interest() for pmid in possible_mids] #print "choose space child", interests if k == 1: mid = possible_mids[prop_choice(interests, eps=eps)] #print "chosen mid", mid return mid else: return ((1. - eps) * (np.array(interests) / np.sum(interests)) + eps/2.) elif mode == "random": mid = np.random.choice(possible_mids) self.chosen_modules[mid] = self.chosen_modules[mid] + 1 return mid def get_mid_children(self, mid, m, mode="competence", local="local"): children = [] i = 0 for space in self.config.modules[mid]['m_list']: if self.hierarchy.is_motor_space(space): children.append(space) else: s = m[i:i + len(space)] # TO TEST i = i + len(space) children.append(self.choose_space_child(space, s, mode, local)) self.last_space_children_choices[mid].put(children) self.emit('chidren_choice_' + mid, [self.t, children]) #print "Choice of children of mid", mid, children return children def produce_module(self, mid, babbling=True, s=None, s_dims=None, allow_explore=False, explore=None, n_explo_points=0, context_ms=None): mod = self.modules[mid] #print "produce module ", mid, babbling, s, allow_explore, n_explo_points if self.explo == "all": mod.sensorimotor_model.mode = 'explore' elif self.explo == "babbling" and babbling: mod.sensorimotor_model.mode = 'explore' elif self.explo == "motor": expl = True m_list = self.hierarchy.module_children(mid) for m_space in m_list: if not m_space in self.hierarchy.motor_spaces: expl = False if expl: mod.sensorimotor_model.mode = 'explore' else: mod.sensorimotor_model.mode = 'exploit' elif allow_explore: mod.sensorimotor_model.mode = 'explore' else: mod.sensorimotor_model.mode = 'exploit' if explore is not None: mod.sensorimotor_model.mode = 'explore' if explore else 'exploit' if babbling: m_deps = mod.produce(context_ms=context_ms) #print "Produce babbling", mid, "s =", mod.s, "m=", m_deps else: m_deps = mod.inverse(s, s_dims=s_dims) #print "Produce not babbling", mid, "m =", m_deps mod.sensorimotor_model.mode = 'exploit' #print m_deps lower_explore = not (hasattr(mod.sensorimotor_model, "context_was_new") and mod.sensorimotor_model.context_was_new) if not lower_explore: explore = False #print "produce module", mid, "lower_explore", lower_explore, "explore", explore if self.choose_children_mode == 'interest_babbling': if babbling: ccm = "interest" else: ccm = "competence" else: ccm = self.choose_children_mode children = self.get_mid_children(mid, m_deps, mode=ccm, local=self.ccm_local) deps_actions = [] i = 0 for dep in children: if self.hierarchy.is_module(dep): m_dep = m_deps[i:i+len(self.config.modules[dep]['s'])] i = i + len(self.config.modules[dep]['s']) #self.modules[dep].top_down_points.put(m_dep) deps_actions.append(self.produce_module(dep, babbling=False, s=m_dep, allow_explore=False, explore=explore)) else: m_dep = m_deps[i:i+len(dep)] i = i + len(dep) #print "Action prim mod", mid, "m_dims", dep, "m_deps", m_dep deps_actions.append(Action(m_dep, m_dims=dep)) #print "Action mod", mid, "operator", self.config.modules[mid]['operator'], "m_deps", m_deps, 'actions=',deps_actions return Action(m_deps, operator=self.config.modules[mid]['operator'], actions=deps_actions) def produce(self, context_ms=None): for mid in self.modules.keys(): self.last_space_children_choices[mid] = Queue.Queue() # mid = self.choose_babbling_module(mode=self.choice, weight_by_level=self.llb) # self.mid_control = mid # action = self.produce_module(mid, n_explo_points=self.n_explo_points, context_ms=context_ms) # self.action = action # #print "Action", action.n_iterations, "mid", mid # #self.action.print_action() # m_seq = action.get_m_seq(len(self.conf.m_dims)) s_space = self.choose_interesting_space(mode=self.choice) #print "chosen s_space", s_space if s_space == "s_o": s = - np.array([0.] + context_ms) else: s = rand_bounds(np.array([self.conf.mins[self.config.s_spaces[s_space]], self.conf.maxs[self.config.s_spaces[s_space]]]))[0] #print "m_seq", m_seq self.m_seq = self.inverse(s_space, s, babbling=True, context=context_ms) #print "Produce ", self.t self.t = self.t + 1 return self.m_seq def inverse(self, s_space, s, babbling=False, context=None, explore=None): if s_space == "s_o": s = np.array(list(context) + list(s)) else: s = np.array(s) ccm = self.choose_children_mode mid = self.choose_space_child(self.config.s_spaces[s_space], s, mode=ccm, local=self.ccm_local) self.chosen_modules[mid] = self.chosen_modules[mid] + 1 #print "chosen mid", mid if babbling: self.mid_control = mid #print self.mid_control self.modules[mid].s = s else: self.mid_control = None action = self.produce_module(mid, babbling=False, s=s, explore=explore) self.m_seq = action.get_m_seq(len(self.conf.m_dims)) return self.m_seq def perceive(self, s_seq_, context=None, higher_module_perceive=True): s_seq = self.sensory_primitive(s_seq_) self.ms_seq = [] for m, s in zip(self.m_seq, s_seq): ms = self.set_ms(m, s) self.ms_seq.append(ms) # self.emit('agentM', m) # self.emit('agentS', s) last_ms = self.ms_seq[-1] # if abs(ms[-1]) > 0.01: # print self.t, "ms", last_ms mid = self.update_sensorimotor_models(last_ms) #print "mid_control", self.mid_control if self.mid_control is not None and (mid == self.mid_control or mid is None or self.mid_control in ["mod1", "mod2"]): self.modules[self.mid_control].update_im(self.modules[self.mid_control].get_m(last_ms), self.modules[self.mid_control].get_s(last_ms)) #print "mid control upd" def subscribe_topics_mids(self, topics, observer): for topic in topics: for mid in self.modules.keys(): self.subscribe(topic + '_' + mid, observer) def subscribe_topics_mods(self, topics, observer): for topic in topics: for mod in self.modules.values(): mod.subscribe(topic + '_' + mod.mid, observer)
class Supervisor(Observable): def __init__( self, config, environment, choice="prop", llb=False, explo="babbling", n_explo_points=0, choose_children_mode="competence", choose_children_local=True, allow_split_mod1=False, ): Observable.__init__(self) self.config = config self.log = None self.environment = environment self.choice = choice self.llb = llb self.explo = explo self.n_explo_points = n_explo_points self.choose_children_mode = choose_children_mode self.ccm_local = choose_children_local self.allow_split_mod1 = allow_split_mod1 self.conf = self.config.agent self.expl_dims = self.config.agent.m_dims self.inf_dims = self.config.agent.s_dims self.t = 1 self.modules = {} self.chosen_modules = {} self.mid_control = "" self.last_space_children_choices = {} self.split_mod1_maxlen = 10 self.previous_ms = deque([None] * self.split_mod1_maxlen, maxlen=self.split_mod1_maxlen) self.hierarchy = Hierarchy() # Build Hierarchy for motor_space in self.config.m_spaces.values(): self.hierarchy.add_motor_space(motor_space) for mid in self.config.modules.keys(): # Build modules self.init_module(mid) # [set.add(self.modules[mid].controled_vars, cvar) for cmid in self.config.modules[mid]['children'] if self.hierarchy.is_mod(cmid) for cvar in self.modules[cmid].controled_vars] for mid in self.modules.keys(): self.last_space_children_choices[mid] = Queue.Queue() def init_module(self, mid): self.modules[mid] = Module(self.config, mid) self.chosen_modules[mid] = 0 self.hierarchy.add_module(mid) self.hierarchy.add_sensori_space(self.config.modules[mid]["s"]) for space in self.config.modules[mid]["m_list"]: self.hierarchy.add_edge_space_module(mid, space) self.hierarchy.add_edge_module_space(mid, self.config.modules[mid]["s"]) def sensory_changed_mod1(self, threshold=0.1): print self.previous_ms std = np.std(self.previous_ms, axis=0) print std[self.config.modules["mod1"]["s"]] return [s_dim for s_dim in self.config.modules["mod1"]["s"] if abs(ms1[s_dim] - ms2[s_dim]) > threshold] def split_mod1(self, s_dims): print "SPLITING" print "iteration", self.t print "dimensions", s_dims old = self.modules["mod1"] conf = self.config.modules["mod1"].copy() new_conf = conf.copy() conf["s"] = sorted(list(set(conf["s"]) - set(s_dims))) new_conf["s"] = s_dims self.config.modules["mod1"] = conf n = len(self.config.modules.keys()) + 1 new_mid = "mod" + str(n) self.config.modules[new_mid] = new_conf self.init_module("mod1") self.init_module(new_mid) m_list = old.sensorimotor_model.model.imodel.fmodel.dataset.data[0] s_list = self.log.logs["agentS"] ms_list = np.array(zip(m_list, s_list)) self.modules["mod1"].sensorimotor_model.update_batch(ms_list[:, conf["m"]], ms_list[:, conf["s"]]) self.modules[new_mid].sensorimotor_model.update_batch(ms_list[:, new_conf["m"]], ms_list[:, new_conf["s"]]) self.modules["mod1"].interest_model.current_interest = old.interest_model.current_interest self.modules[new_mid].interest_model.current_interest = old.interest_model.current_interest def choose_babbling_module(self, auto_create=False, progress_threshold=1e-2, mode="softmax", weight_by_level=False): interests = {} for mid in self.modules.keys(): interests[mid] = self.modules[mid].interest() self.emit("interests", [self.t, interests]) if mode == "random": mid = np.random.choice(self.modules.keys()) elif mode == "greedy": eps = 0.1 if np.random.random() < eps: mid = np.random.choice(self.modules.keys()) else: mid = max(interests, key=interests.get) elif mode == "softmax": temperature = 0.1 mids = interests.keys() w = interests.values() if weight_by_level: levels = self.hierarchy.module_levels() for i in range(len(mids)): f = 2.0 w[i] = w[i] * np.power(f, max(levels.values()) - levels[mids[i]]) # print w mid = mids[softmax_choice(w, temperature)] elif mode == "prop": mids = interests.keys() w = interests.values() # if self.t % 100 == 0: # print # print 'iteration', self.t # print "competences", np.array([self.modules[mid].competence() for mid in self.modules.keys()]) # if sum(w) > 0: # print "interests", np.array(w) # print "interesting objects:", int(((w[3] + w[6]) / sum(w))*100), "%" # print "cumulated:", int((self.chosen_modules["mod4"] + self.chosen_modules["mod7"]) / float(sum(self.chosen_modules.values())) * 100), "%" # print self.chosen_modules # print "competences", [mod.competence() for mod in self.modules.values()] if weight_by_level: levels = self.hierarchy.module_levels() for i in range(len(mids)): f = 10.0 w[i] = w[i] * np.power(f, max(levels.values()) - levels[mids[i]]) # print w mid = mids[prop_choice(w, eps=0.1)] # print # print # print self.chosen_modules # print "chosen module:", mid elif mode == "prop-min": mids = interests.keys() w = interests.values() # if self.t % 100 == 0: # print "interests", np.array(w), 'iteration', self.t, "competences", np.array([self.modules[mid].competence() for mid in self.modules.keys()]), int(((w[3] + w[4]-2*min(w)) / sum(np.array(w)-min(w)))*100), "%" mid = mids[prop_choice(np.array(w) - min(w), eps=0.1)] self.chosen_modules[mid] = self.chosen_modules[mid] + 1 # print self.chosen_modules self.emit("babbling_module", mid) return mid def fast_forward(self, log, forward_im=False): ms_list = [] for m, s in zip(log.logs["agentM"], log.logs["agentS"]): ms = np.append(m, s) ms_list += [ms] ms_array = np.array(ms_list) for mid, mod in self.modules.iteritems(): mod.fast_forward_models(log, ms_array, mid, forward_im=forward_im) def eval_mode(self): self.sm_modes = {} for mod in self.modules.values(): self.sm_modes[mod.mid] = mod.sensorimotor_model.mode mod.sensorimotor_model.mode = "exploit" def learning_mode(self): for mod in self.modules.values(): mod.sensorimotor_model.mode = self.sm_modes[mod.mid] def check_bounds_dmp(self, m_ag): return bounds_min_max(m_ag, self.conf.m_mins, self.conf.m_maxs) def motor_babbling(self): return rand_bounds(self.conf.m_bounds)[0] def motor_primitive(self, m): return m def rest_params(self): return self.environment.rest_params() def sensory_primitive(self, s): return s def get_eval_dims(self, s): return self.set_ms(s=s)[self.config.eval_dims] def set_ms(self, m=None, s=None): ms = zeros(self.conf.ndims) if m is not None: ms[self.conf.m_dims] = m if s is not None: ms[self.conf.s_dims] = s return ms def set_ms_seq(self, m=None, s=None): ms = zeros(self.conf.ndims) if m is not None: ms[self.conf.m_dims] = m if s is not None: ms[self.conf.s_dims] = s return [ms] def get_m(self, ms): return ms[self.conf.m_dims] def get_s(self, ms): return ms[self.conf.s_dims] def update_sensorimotor_models(self, m, s): ms = self.set_ms(m, s) # self.emit('agentM', m) self.emit("agentS", s) for mod in self.modules.values(): mod.update_sm(mod.get_m(ms), mod.get_s(ms)) def choose_space_child(self, s_space, s, mode="competence", local="local"): """ Choose the children of space s_space among modules that have the good sensori spaces, maximizing competence. """ try: possible_mids = self.hierarchy.space_children(s_space) except KeyError: return None if len(possible_mids) == 1: return possible_mids[0] y = self.set_ms(s=s)[s_space] if mode == "competence": if local: competences = [ -self.modules[pmid].sensorimotor_model.model.imodel.fmodel.dataset.nn_y(y, k=1)[0][0] for pmid in possible_mids ] else: competences = [self.modules[pmid].competence() for pmid in possible_mids] return possible_mids[np.array(competences).argmax()] elif mode == "interest_greedy": eps = 0.1 if np.random.random() < eps: return np.random.choice(possible_mids) else: if local == "local": interests = [self.modules[pmid].interest_pt(y) for pmid in possible_mids] else: interests = [self.modules[pmid].interest() for pmid in possible_mids] return possible_mids[np.array(interests).argmax()] elif mode == "interest_prop": eps = 0.1 if np.random.random() < eps: return np.random.choice(possible_mids) else: if local == "local": interests = [self.modules[pmid].interest_pt(y) for pmid in possible_mids] else: interests = [self.modules[pmid].interest() for pmid in possible_mids] return possible_mids[prop_choice(interests, eps=0.1)] elif mode == "random": mid = np.random.choice(possible_mids) return mid def get_mid_children(self, mid, m, mode="competence", local="local"): children = [] i = 0 for space in self.config.modules[mid]["m_list"]: if self.hierarchy.is_motor_space(space): children.append(space) else: s = m[i : i + len(space)] # TO TEST i = i + len(space) children.append(self.choose_space_child(space, s, mode, local)) self.last_space_children_choices[mid].put(children) # self.emit('chidren_choice_' + mid, [self.t, children]) # print "Choice of children of mid", mid, children return children def produce_module(self, mid, babbling=True, s=None, s_dims=None, allow_explore=False, n_explo_points=0): mod = self.modules[mid] # print "produce module ", mid, babbling, s, allow_explore, n_explo_points if self.explo == "all": mod.sensorimotor_model.mode = "explore" elif self.explo == "babbling" and babbling: mod.sensorimotor_model.mode = "explore" elif self.explo == "motor": expl = True m_list = self.hierarchy.module_children(mid) for m_space in m_list: if not m_space in self.hierarchy.motor_spaces: expl = False if expl: mod.sensorimotor_model.mode = "explore" else: mod.sensorimotor_model.mode = "exploit" elif allow_explore: mod.sensorimotor_model.mode = "explore" else: mod.sensorimotor_model.mode = "exploit" if babbling: m_deps = mod.produce() # print "Produce babbling", mid, "s =", mod.s, "m=", m_deps else: m_deps = mod.inverse(s, s_dims=s_dims) if hasattr(mod.im, "add_top_down_goal"): mod.im.add_top_down_goal(s) # also add td goal to other modules with same s space ? # print "Produce not babbling", mid, "m =", m_deps mod.sensorimotor_model.mode = "exploit" # print m_deps if n_explo_points > 0: for _ in range(n_explo_points): # print "explo point", mod.s action = self.produce_module(mid, babbling=False, s=mod.s, allow_explore=True, n_explo_points=0) m_seq = action.get_m_seq(len(self.conf.m_dims)) self.t = self.t + 1 s_seq = self.environment.update(m_seq, log=False) for m, s in zip(m_seq, s_seq): self.update_sensorimotor_models(m, s) m_deps = mod.inverse(mod.s) if self.choose_children_mode == "interest_babbling": if babbling: ccm = "interest" else: ccm = "competence" else: ccm = self.choose_children_mode children = self.get_mid_children(mid, m_deps, mode=ccm, local=self.ccm_local) deps_actions = [] i = 0 for dep in children: if self.hierarchy.is_module(dep): m_dep = m_deps[i : i + len(self.config.modules[dep]["s"])] i = i + len(self.config.modules[dep]["s"]) # self.modules[dep].top_down_points.put(m_dep) deps_actions.append(self.produce_module(dep, babbling=False, s=m_dep, allow_explore=False)) else: m_dep = m_deps[i : i + len(dep)] i = i + len(dep) # print "Action prim mod", mid, "m_dims", dep, "m_deps", m_dep deps_actions.append(Action(m_dep, m_dims=dep)) # print "Action mod", mid, "operator", self.config.modules[mid]['operator'], "m_deps", m_deps, 'actions=',deps_actions return Action(m_deps, operator=self.config.modules[mid]["operator"], actions=deps_actions) def produce(self): for mid in self.modules.keys(): self.last_space_children_choices[mid] = Queue.Queue() mid = self.choose_babbling_module(mode=self.choice, weight_by_level=self.llb) self.mid_control = mid action = self.produce_module(mid, n_explo_points=self.n_explo_points) self.action = action m_seq = action.get_m_seq(len(self.conf.m_dims)) self.m_seq = m_seq self.chosen_modules[mid] = self.chosen_modules[mid] + 1 # print self.chosen_modules self.emit("module_to_credit", self.modules[mid].last_module_to_credit) self.t = self.t + 1 return m_seq def perceive_module(self, mid, action, ms_seq): m_deps = [] i = 0 children = self.last_space_children_choices[mid].get() for idx, n_it, dep in zip(range(action.n_actions), action.n_iterations_actions, children): if self.hierarchy.is_module(dep): m_dep = self.perceive_module(dep, action.actions[idx], ms_seq[i : i + n_it]) m_deps.append(m_dep) if action.operator == "seq": i = i + n_it else: m_deps.append(ms_seq[i][dep]) if action.operator == "seq": i = i + n_it m_deps = [item for m_dep in m_deps for item in m_dep] s = ms_seq[-1][self.config.modules[mid]["s"]] self.modules[mid].perceive(m_deps, s, has_control=(mid == self.mid_control)) return s def perceive(self, s_seq_, higher_module_perceive=True): s_seq = self.sensory_primitive(s_seq_) self.ms_seq = [] for m, s in zip(self.m_seq, s_seq): ms = self.set_ms(m, s) self.ms_seq.append(ms) self.emit("agentM", m) self.emit("agentS", s) last_ms = self.ms_seq[-1] for mid in self.modules.keys(): m_deps = self.modules[mid].get_m(last_ms) s = self.modules[mid].get_s(last_ms) self.modules[mid].perceive(m_deps, s, has_control=mid == self.mid_control) if self.allow_split_mod1 and self.t > 2 + self.split_mod1_maxlen: split_dims = self.sensory_changed_mod1() if len(split_dims) == 0 or len(split_dims) == len(self.config.modules["mod1"]["s"]): pass else: self.split_mod1(split_dims) self.previous_ms.append(last_ms) self.emit("babbling_interest", self.modules[self.mid_control].last_interest) def subscribe_topics_mod(self, topics, observer): for topic in topics: for mid in self.modules.keys(): self.subscribe(topic + "_" + mid, observer) def subscribe_mod(self, observer): for mod in self.modules.values(): mod.subscribe("choice" + "_" + mod.mid, observer) mod.subscribe("progress" + "_" + mod.mid, observer) mod.subscribe("inference" + "_" + mod.mid, observer) mod.subscribe("perception" + "_" + mod.mid, observer) mod.subscribe("im_update" + "_" + mod.mid, observer)
def print_s3_contents_boto3(connection, bucket): #for bucket in connection.buckets.all(): for key in bucket.objects.all(): print(key.key) if __name__ == '__main__': boto3_connection = boto3.resource('s3') s3_client = boto3.client('s3') bucket = boto3_connection.Bucket('econ-demog') bucket_name = 'econ-demog' quote_page = 'https://www2.census.gov/acs/downloads/Core_Tables/rural_statistics_area/2007/Wyoming/' quote_list = Hierarchy(quote_page) #key = boto.s3.key.Key(bucket,file_object) #filepath = '/home/david/PublicData/acs_files/' # for i in quote_list.files: file_name = i[i.rfind('/') + 1:] folder = file_name[:-4] filepath = '/home/david/PublicData/acs_files/' + folder + '/' if not os.path.exists(filepath): os.makedirs(filepath) quote_list.load_zip(i, filepath) acs_files = [f for f in listdir(filepath) if isfile(join(filepath, f))] #k = bucket.new_key(folder)
class Supervisor(Observable): def __init__(self, config, environment, choice="prop", llb=False, explo="babbling", n_explo_points=0, choose_children_mode='competence', choose_children_local=True): Observable.__init__(self) self.config = config self.environment = environment self.choice = choice self.llb = llb self.explo = explo self.n_explo_points = n_explo_points self.choose_children_mode = choose_children_mode self.ccm_local = choose_children_local self.conf = self.config.agent self.expl_dims = self.config.agent.m_dims self.inf_dims = self.config.agent.s_dims self.t = 1 self.modules = {} self.chosen_modules = {} self.mid_control = '' self.last_space_children_choices = {} self.hierarchy = Hierarchy() # Build Hierarchy for motor_space in self.config.m_spaces.values(): self.hierarchy.add_motor_space(motor_space) for mid in self.config.modules.keys(): # Build modules self.init_module(mid) #[set.add(self.modules[mid].controled_vars, cvar) for cmid in self.config.modules[mid]['children'] if self.hierarchy.is_mod(cmid) for cvar in self.modules[cmid].controled_vars] for mid in self.modules.keys(): self.last_space_children_choices[mid] = Queue.Queue() def init_module(self, mid): self.modules[mid] = Module(self.config, mid) self.chosen_modules[mid] = 0 self.hierarchy.add_module(mid) # for space in self.config.modules[mid]['m_list']: # self.hierarchy.add_motor_space(space) self.hierarchy.add_sensori_space(self.config.modules[mid]['s']) for space in self.config.modules[mid]['m_list']: self.hierarchy.add_edge_space_module(mid, space) self.hierarchy.add_edge_module_space(mid, self.config.modules[mid]['s']) # for space in self.config.modules[mid]['m_list']: # if self.hierarchy.is_motor_space(space): # for m_dim in space: # set.add(self.modules[mid].controled_vars, m_dim) # else: # for cvar in self.modules[dep].controled_vars: # set.add(self.modules[mid].controled_vars, cvar) # #[set.add(self.modules[mid].controled_vars, cvar) for cmid in self.config.modules[mid]['children'] for cvar in self.modules[cmid].controled_vars] # print "Controled vars", mid, self.modules[mid].controled_vars # def _new_mid(self): # return "mod" + str(len(self.modules.keys()) + 1) # # def _random_operator(self): # return random.choice(self.config.operators) # # def _s_par_constrained(self, possible_s, m): # """ # Return a random flatten subset of the set of possible s spaces, # taking into account the constraint of parallel operator to not have bounded variables in s. # """ # return [s_space for s_space, s_dims in possible_s.items() if (len([m_dim for m_dim in m if m_dim in s_dims]) == 0)]# s_space that do not contain an item of m # # def _random_s(self, possible_s): # if len(possible_s) > 0: # s_size = random.randint(1, len(possible_s)) # s = random.choice(self._combinations(possible_s, s_size)) # return list(s) # else: # return None # # def _combinations(self, l, k=2): # return list(itertools.combinations(l, k)) # def _constraints_m(self, possible_m_comb):#TODO make hierarchy compute controled_vars # #print possible_m_comb, self.modules[possible_m_comb[0][0]].controled_vars # _possible_m_comb = [] # for comb in possible_m_comb: # if self.hierarchy.is_mod(comb[0]): # m1 = self.modules[comb[0]].controled_vars # else: # m1 = set(self.config.m_spaces[comb[0]]) # if self.hierarchy.is_mod(comb[1]): # m2 = self.modules[comb[1]].controled_vars # else: # m2 = set(self.config.m_spaces[comb[1]]) # if set.isdisjoint(m1, m2): # _possible_m_comb.append(comb) # #print "possible_m_comb", possible_m_comb, "_possible_m_comb", _possible_m_comb # return _possible_m_comb # # def _random_par(self): # possible_deps = self._constraints_m(self._combinations(self.modules.keys() + self.config.m_spaces.keys())) # if len(possible_deps) == 0: # return [] # else: # return random.choice(possible_deps) # # def _random_seq(self): # return random.choice(self._combinations(self.modules.keys() + self.config.m_spaces.keys())) # # def _process_mid_m_deps(self, deps): # m_spaces = [] # for d in deps: # if self.hierarchy.is_mod(d): # m_spaces.append(self.config.modules[d]['s']) # else: # m_spaces.append(self.config.m_spaces[d]) # #print "_process_mid_m_deps m:", m, list(set(tuple([item for sublist in m for item in sublist]))) # m = [item for sublist in m_spaces for item in sublist] # flatten # return m # # def _random_connexions(self): # op = self._random_operator() # if op == "par": # deps = self._random_par() # elif op == "seq": # deps = self._random_seq() # #print "_random_connexions deps", deps # else: # raise NotImplementedError # if deps == []:# if _random_par failed # return self._random_connexions() # possible infinite loop if really no possible new module ? # else: # m = self._process_mid_m_deps(deps) # #print "m", m, "possible_s", possible_s, "s_size", s_size, "s", s # if op == "par": # possible_s_name = self._s_par_constrained(self.config.s_spaces, m) # else: # possible_s_name = self.config.s_spaces.keys() # #print "_random_connexions possible_s=", possible_s_name # s = self._random_s(possible_s_name) # #print "_random_connexions s=", s # s = [item for s_space in s for item in self.config.s_spaces[s_space]] # if s is not None: # print op, deps, m, s # return op, deps, m, s # else: # return self._random_connexions() # possible infinite loop if really no possible new module ? def create_module(self): mid = self._new_mid() op, mod_deps, m, s = self._random_connexions() #print "deps:", mod_deps, "m", m, "s:", s mconfig = dict(m = m, s = s, operator = op, babbling_name = 'goal', sm_name = 'NSLWLR', im_name = 'tree', from_log = None, children = mod_deps, motor_babbling_n_iter=10) print mid, mconfig self.config.modules[mid] = mconfig self.init_module(mid) return mid def choose_babbling_module(self, auto_create=False, progress_threshold=1e-2, mode='softmax', weight_by_level=False): interests = {} for mid in self.modules.keys(): interests[mid] = self.modules[mid].interest() self.emit('interest_' + mid, [self.t, interests[mid]]) self.emit('competence_' + mid, [self.t, self.modules[mid].competence()]) max_progress = max(interests.values()) # self.emit('babbling_module', "mod2") # return "mod2" #print "max_progress", max_progress if not auto_create or max_progress > progress_threshold: if mode == 'random': mid = np.random.choice(self.modules.keys()) elif mode == 'greedy': eps = 0.1 if np.random.random() < eps: mid = np.random.choice(self.modules.keys()) else: mid = max(interests, key=interests.get) elif mode == 'softmax': temperature = 0.1 mids = interests.keys() w = interests.values() #print "progresses", w #print "competences", [mod.competence() for mod in self.modules.values()] if weight_by_level: levels = self.hierarchy.module_levels() for i in range(len(mids)): f = 2.0 w[i] = w[i] * np.power(f, max(levels.values()) - levels[mids[i]]) #print w mid = mids[softmax_choice(w, temperature)] elif mode == 'prop': mids = interests.keys() w = interests.values() #print "progresses", w #print "competences", [mod.competence() for mod in self.modules.values()] if weight_by_level: levels = self.hierarchy.module_levels() for i in range(len(mids)): f = 10.0 w[i] = w[i] * np.power(f, max(levels.values()) - levels[mids[i]]) #print w mid = mids[prop_choice(w, eps=0.1)] self.chosen_modules[mid] = self.chosen_modules[mid] + 1 #print self.chosen_modules self.emit('babbling_module', mid) return mid else: return self.create_module() def fast_forward(self, log, forward_im=False): ms_list = [] for m,s in zip(log.logs['agentM'], log.logs['agentS']): ms = np.append(m,s) ms_list += [ms] ms_array = np.array(ms_list) for mid, mod in self.modules.iteritems(): mod.fast_forward_models(log, ms_array, mid, forward_im=forward_im) def eval_mode(self): self.sm_modes = {} for mod in self.modules.values(): self.sm_modes[mod.mid] = mod.sensorimotor_model.mode mod.sensorimotor_model.mode = 'exploit' def learning_mode(self): for mod in self.modules.values(): mod.sensorimotor_model.mode = self.sm_modes[mod.mid] def check_bounds_dmp(self, m_ag): return bounds_min_max(m_ag, self.conf.m_mins, self.conf.m_maxs) def motor_babbling(self): return rand_bounds(self.conf.m_bounds)[0] def motor_primitive(self, m): return m def rest_params(self): return self.environment.rest_params() def sensory_primitive(self, s): return s def get_eval_dims(self, s): return self.set_ms(s = s)[self.config.eval_dims] def set_ms(self, m=None, s=None): ms = zeros(self.conf.ndims) if m is not None: ms[self.conf.m_dims] = m if s is not None: ms[self.conf.s_dims] = s return ms def set_ms_seq(self, m=None, s=None): ms = zeros(self.conf.ndims) if m is not None: ms[self.conf.m_dims] = m if s is not None: ms[self.conf.s_dims] = s return [ms] def get_m(self, ms): return ms[self.conf.m_dims] def get_s(self, ms): return ms[self.conf.s_dims] def update_sensorimotor_models(self, m, s): ms = self.set_ms(m, s) self.emit('agentM', m) self.emit('agentS', s) for mod in self.modules.values(): mod.update_sm(mod.get_m(ms), mod.get_s(ms)) def choose_space_child(self, s_space, s, mode="competence", local="local"): """ Choose the children of space s_space among modules that have the good sensori spaces, maximizing competence. """ try: possible_mids = self.hierarchy.space_children(s_space) except KeyError: return None if len(possible_mids) == 1: return possible_mids[0] y = self.set_ms(s=s)[s_space] if mode == "competence": if local: competences = [- self.modules[pmid].sensorimotor_model.model.imodel.fmodel.dataset.nn_y(y, k=1)[0][0] for pmid in possible_mids] else: competences = [self.modules[pmid].competence() for pmid in possible_mids] return possible_mids[np.array(competences).argmax()] elif mode == "interest_greedy": eps = 0.1 if np.random.random() < eps: return np.random.choice(possible_mids) else: if local=="local": interests = [self.modules[pmid].interest_pt(y) for pmid in possible_mids] else: interests = [self.modules[pmid].interest() for pmid in possible_mids] return possible_mids[np.array(interests).argmax()] elif mode == "interest_prop": eps = 0.1 if np.random.random() < eps: return np.random.choice(possible_mids) else: if local=="local": interests = [self.modules[pmid].interest_pt(y) for pmid in possible_mids] else: interests = [self.modules[pmid].interest() for pmid in possible_mids] return possible_mids[prop_choice(interests, eps=0.1)] elif mode == "random": mid = np.random.choice(possible_mids) return mid def get_mid_children(self, mid, m, mode="competence", local="local"): children = [] i = 0 for space in self.config.modules[mid]['m_list']: if self.hierarchy.is_motor_space(space): children.append(space) else: s = m[i:i + len(space)] # TO TEST i = i + len(space) children.append(self.choose_space_child(space, s, mode, local)) self.last_space_children_choices[mid].put(children) self.emit('chidren_choice_' + mid, [self.t, children]) #print "Choice of children of mid", mid, children return children def produce_module(self, mid, babbling=True, s=None, s_dims=None, allow_explore=False, n_explo_points=0): mod = self.modules[mid] #print "produce module ", mid, babbling, s, allow_explore, n_explo_points if self.explo == "all": mod.sensorimotor_model.mode = 'explore' elif self.explo == "babbling" and babbling: mod.sensorimotor_model.mode = 'explore' elif self.explo == "motor": expl = True m_list = self.hierarchy.module_children(mid) for m_space in m_list: if not m_space in self.hierarchy.motor_spaces: expl = False if expl: mod.sensorimotor_model.mode = 'explore' else: mod.sensorimotor_model.mode = 'exploit' elif allow_explore: mod.sensorimotor_model.mode = 'explore' else: mod.sensorimotor_model.mode = 'exploit' if babbling: m_deps = mod.produce() #print "Produce babbling", mid, "s =", mod.s, "m=", m_deps else: m_deps = mod.inverse(s, s_dims=s_dims) if hasattr(mod.im, "add_top_down_goal"): mod.im.add_top_down_goal(s) # also add td goal to other modules with same s space ? #print "Produce not babbling", mid, "m =", m_deps mod.sensorimotor_model.mode = 'exploit' #print m_deps if n_explo_points > 0: for _ in range(n_explo_points): #print "explo point", mod.s action = self.produce_module(mid, babbling=False, s=mod.s, allow_explore=True, n_explo_points=0) m_seq = action.get_m_seq(len(self.conf.m_dims)) self.t = self.t + 1 s_seq = self.environment.update(m_seq, log=False) for m, s in zip(m_seq, s_seq): self.update_sensorimotor_models(m,s) m_deps = mod.inverse(mod.s) if self.choose_children_mode == 'interest_babbling': if babbling: ccm = "interest" else: ccm = "competence" else: ccm = self.choose_children_mode children = self.get_mid_children(mid, m_deps, mode=ccm, local=self.ccm_local) deps_actions = [] i = 0 for dep in children: if self.hierarchy.is_module(dep): m_dep = m_deps[i:i+len(self.config.modules[dep]['s'])] i = i + len(self.config.modules[dep]['s']) #self.modules[dep].top_down_points.put(m_dep) deps_actions.append(self.produce_module(dep, babbling=False, s=m_dep, allow_explore=False)) else: m_dep = m_deps[i:i+len(dep)] i = i + len(dep) #print "Action prim mod", mid, "m_dims", dep, "m_deps", m_dep deps_actions.append(Action(m_dep, m_dims=dep)) #print "Action mod", mid, "operator", self.config.modules[mid]['operator'], "m_deps", m_deps, 'actions=',deps_actions return Action(m_deps, operator=self.config.modules[mid]['operator'], actions=deps_actions) # # elif self.config.learning['training_mode'] == 'par': # # if self.config.learning['par']['par_mode'] == 'exploring': # exploring_mode = self.config.learning['par']['exploring']['exploring_mode'] # n_points = self.config.learning['par']['exploring'][exploring_mode]['n_points'] # # if exploring_mode == 'random': # for _ in range(n_points-1): # for child in mod.mconf['children']: # self.modules[child].sensorimotor_model.mode = 'explore' # self.m = self.modules[child].set_m(self.m, # self.modules[child].inverse(self.set_ms(s=s_child), # pref = 'explore_')) # # s_env = self.environment.update(self.m, log=False) # s = self.sensory_primitive(s_env) # self.update_sensorimotor_models(self.m,s) # # for child in mod.mconf['children']: # self.modules[child].sensorimotor_model.mode = 'exploit' # self.m = self.modules[child].set_m(self.m, # self.modules[child].inverse(self.get_ms(s=s_child))) # # elif exploring_mode == 'cma': # for child in mod.mconf['children']: # self.modules[child].sensorimotor_model.mode = 'exploit' # cma_conf = self.config.learning['par']['exploring'][exploring_mode] # self.wrap_explore_cma_fmin(self.modules[child], s_child) # _, ids = self.modules[child].sensorimotor_model.dataset.nn_y(s_child, k=1) # x0 = self.modules[child].sensorimotor_model.dataset.get_x(ids[0]) # bounds = [self.conf.m_mins,self.conf.m_maxs] # #print 'bounds',bounds # cma.fmin(self.cma_objective_function, # x0=x0, # sigma0=cma_conf['sigma0'], # options={"verb_log": 0, # 'verbose':-9, # 'bounds':bounds, # 'popsize':cma_conf['popsize'], # 'maxfevals':n_points-1}) # # self.modules[child].sensorimotor_model.mode = 'exploit' # self.m = self.modules[child].inverse(self.get_ms(s=s_child)) # else: # raise NotImplementedError # else: # raise NotImplementedError # else: # raise NotImplementedError # movement = self.motor_primitive(self.m) # return movement # def wrap_explore_cma_fmin(self, mod, s_goal): # # def f(m): # m_env = self.motor_primitive(m) # s_env = self.environment.update(m_env, log=False) # s = self.sensory_primitive(s_env) # self.update_sensorimotor_models(self.m,s) # smod = mod.get_s(self.set_ms(self.m,s)) # error = linalg.norm(s_goal - smod) # #print "CMA function evaluation. m=", m, "s=", s, "s_goal=", s_goal, "error=", error # return error # # self.cma_objective_function = f def produce(self): for mid in self.modules.keys(): self.last_space_children_choices[mid] = Queue.Queue() mid = self.choose_babbling_module(mode=self.choice, weight_by_level=self.llb) self.mid_control = mid action = self.produce_module(mid, n_explo_points=self.n_explo_points) self.action = action #print "Action", action.n_iterations, "mid", mid #self.action.print_action() m_seq = action.get_m_seq(len(self.conf.m_dims)) #print "m_seq", m_seq self.m_seq = m_seq #print "Produce ", self.t self.t = self.t + 1 return m_seq def perceive_module(self, mid, action, ms_seq): m_deps = [] i = 0 children = self.last_space_children_choices[mid].get() # print "perceive0", mid, children # print "perceive0.5", action.n_iterations_actions, action.operator for idx, n_it, dep in zip(range(action.n_actions), action.n_iterations_actions, children): if self.hierarchy.is_module(dep): m_dep = self.perceive_module(dep, action.actions[idx], ms_seq[i:i+n_it]) m_deps.append(m_dep) if action.operator == "seq": i = i + n_it else: #print "Perceive1 module", mid, ms_seq, i, dep m_deps.append(ms_seq[i][dep]) if action.operator == "seq": i = i + n_it m_deps = [item for m_dep in m_deps for item in m_dep] s = ms_seq[-1][self.config.modules[mid]['s']] #print "Perceive2 module", mid, m_deps, s, (mid==self.mid_control)) self.modules[mid].perceive(m_deps, s, has_control= (mid==self.mid_control)) return s def perceive(self, s_seq_, higher_module_perceive=True): s_seq = self.sensory_primitive(s_seq_) self.ms_seq = [] for m, s in zip(self.m_seq, s_seq): ms = self.set_ms(m, s) self.ms_seq.append(ms) self.emit('agentM', m) self.emit('agentS', s) if s[-2] > 0: # One of the boxes print "m", m print "s", s self.perceive_module(self.mid_control, self.action, self.ms_seq) last_ms = self.ms_seq[-1] if higher_module_perceive: for mid_parent in self.hierarchy.parents_with_operator(self.mid_control, ["par", None]): m_deps = self.modules[mid_parent].get_m(last_ms) s = self.modules[mid_parent].get_s(last_ms) #print "perceive higher module", mid_parent, m_deps, s self.modules[mid_parent].perceive(m_deps, s, has_control= False) def subscribe_topics_mod(self, topics, observer): for topic in topics: for mid in self.modules.keys(): self.subscribe(topic + '_' + mid, observer) def subscribe_mod(self, observer): for mod in self.modules.values(): mod.subscribe('choice' + '_' + mod.mid, observer) mod.subscribe('progress' + '_' + mod.mid, observer) mod.subscribe('inference' + '_' + mod.mid, observer) mod.subscribe('perception' + '_' + mod.mid, observer) mod.subscribe('im_update' + '_' + mod.mid, observer)
#Read in dataset into annotations and vocabulary for filename in listdir(getcwd() + '/datasets/' + dataset): with open(Path(getcwd() + '/datasets/' + dataset + '/' + filename), encoding='utf-8') as input_file: lines = input_file.readlines() assert len(lines) == 1 annotations[filename] = lines[0].lower().strip().split(" ") for tag in annotations[filename]: if tag not in vocabulary: vocabulary.append(tag) #Run smict, the first part of the algorithm subsumption_axioms, root_tag = smict(annotations, vocabulary, alpha) #Initialize hierarchy from subsumption axioms hierarchy = Hierarchy(subsumption_axioms, root_tag) #Perform subject clustering, the second part of our algorithm hierarchy = subject_clustering(hierarchy, annotations) #Prune the tree of empty clusters, the final part of our algorithm hierarchy.prune(hierarchy.get_root()) #Write pretty printed hierarchy and json summary of hierarchy with Path('smich_cluster_hierarchy_' + dataset).open( 'w', encoding="utf-8") as output_file: write_hierarchy(output_file, hierarchy.get_root()) with open('smich_cluster_hierarchy_json_' + dataset, 'w') as json_file: dump(generate_json_hierarchy(hierarchy), json_file)