def request(self,pattern,partial=None,require_new=False): if partial is None and len(self.partials)>0: partial=self.partials[0] self.busy=True if self.error: self.error=False self._request_count+=1 b=getattr(self.sch,'bound',None) pattern=Pattern(pattern,b,partial=partial) all=self.dm if require_new: all=[x for x in all if not self.finst.contains(x)] matches=[x for x in all if pattern.match(x) is not None] #for x in matches: print `x`,self.get_activation(x) for a in self.adaptors: a.matched(matches) if len(matches)==0: self.fail(self._request_count) else: maximum=None for chunk in matches: chunk.activation=self.get_activation(chunk) if partial is not None: chunk.activation+=chunk._partial if maximum is None or chunk.activation>maximum: maximum=chunk.activation if maximum<self.threshold: self.fail(self._request_count) else: best=[x for x in matches if x.activation==maximum] choice=self.random.choice(best) self.recall(choice,matches=matches,request_number=self._request_count)
def find_matching_chunks(self,pattern,threshold=None): bound=getattr(self.sch,'bound',None) pattern=Pattern(pattern,bound) matches=[x for x in self.dm if pattern.match(x) is not None] if threshold is not None: matches=[x for x in matches if self.get_activation(x)>=threshold] return matches
def request(self,pattern,partial=None,require_new=False): if partial is None and len(self.partials)>0: partial=self.partials[0] self.busy=True if self.error: self.error=False self._request_count+=1 b=getattr(self.sch,'bound',None) pattern=Pattern(pattern,b,partial=partial) all=self.dm if require_new: all=[x for x in all if not self.finst.contains(x)] matches=[x for x in all if pattern.match(x) is not None] #if self.log: # for x in matches: # self.log[`x`]=self.get_activation(x) for a in self.adaptors: a.matched(matches) if len(matches)==0: self.fail(self._request_count) else: maximum=None for chunk in matches: chunk.activation=self.get_activation(chunk) if partial is not None: chunk.activation+=chunk._partial if maximum is None or chunk.activation>maximum: maximum=chunk.activation if maximum<self.threshold: self.fail(self._request_count) else: best=[x for x in matches if x.activation==maximum] choice=self.random.choice(best) self.recall(choice,matches=matches,request_number=self._request_count)
def test_group(self): obj={'a':dict(a=1,b=None),'c':dict(d=2,e=1)} self.assertEqual(Pattern(dict(c='d:2 e:1')).match(obj),{}) self.assertEqual(Pattern(dict(a='a:?x',c='d:2 e:?x')).match(obj),{'x':'1'}) self.assertEqual(Pattern(dict(a='b:!?x',c='d:!?x e:?x')).match(obj),{'x':'1'}) self.assertEqual(Pattern(dict(a='a:!?x b:!?x',c='d:!?x e:?x')).match(obj),None)
def compress_shoulder(self, function_name, **kwargs): ''' This function compresses the shoulder (should be in ribs rotation direction) to help reduce agent width. :param function_name: :param kwargs: bone='shoulder.L' OR bone='shoulder.R' :return:applies the shoulder compression with set_rotation on Morse side ''' #Check the max rotation print("Compress Shoulder") if self.busy: return self.busy = True maxReached = False minReached = False kwargs.update(self.function_map[function_name][1]) #if kwargs['bone'] == 'shoulder.R': # kwargs['radians'] = kwargs['radians'] * 1 if kwargs['bone'] == 'shoulder.L': kwargs['radians'] = kwargs['radians'] * -1 minR, maxR = self._boneProperties[kwargs['bone']][kwargs['axis']] #print("MINR", minR) if Decimal(kwargs['radians']).quantize( Decimal('0.000'), rounding=ROUND_HALF_UP) >= Decimal(maxR).quantize( Decimal('0.000'), rounding=ROUND_HALF_UP): print("SET TO MAX") maxReached = True kwargs['radians'] = maxR if Decimal(kwargs['radians']).quantize( Decimal('0.000'), rounding=ROUND_HALF_UP) <= Decimal(minR).quantize( Decimal('0.000'), rounding=ROUND_HALF_UP): print("SET TO MIN") minReached = True kwargs['radians'] = minR print("RADIANS", kwargs['radians']) middleware.send(self.function_map[function_name][0], **kwargs) #middleware.send('rotate_torso',axis=axis, radians=radians) pattern = 'type:proprioception ' + 'bone:' + kwargs['bone'] matcher = Pattern(pattern) for obj in self._internalChunks: #if axis='0.0' if matcher.match(obj) != None: obj.rotation0 = kwargs['radians'] if kwargs['radians'] < 0: obj.rotation_direction = 'right' elif kwargs['radians'] > 0: obj.rotation_direction = 'left' if maxReached: obj.rotation0_quality = 'max' if minReached: obj.rotation0_quality = 'min' self.busy = False self.update_posture()
def request(self, pattern=''): print("REQUEST") if self.busy: return #for obj in self._internalChunks: # print("OBJ............") # for attr, value in obj.__dict__.items(): # print(attr,value) matcher = Pattern(pattern) print("Matcher", matcher) self.error = False r = [] for obj in self._internalChunks: #print("one",obj) if matcher.match(obj) != None: r.append(obj) self.busy = True d = self.delay if self.delay_sd is not None: d = max(0, self.random.gauss(d, self.delay_sd)) yield d print("YEILDED", d) self.busy = False if len(r) == 0: self._b1.clear() self.error = True else: obj = self.random.choice(r) self._b1.set(obj)
def request(self,pattern=''): print("REQUEST") if self.busy: return #for obj in self._internalChunks: # print("OBJ............") # for attr, value in obj.__dict__.items(): # print(attr,value) matcher = Pattern(pattern) print("Matcher",matcher) self.error=False r=[] for obj in self._internalChunks: #print("one",obj) if matcher.match(obj)!= None: r.append(obj) self.busy = True d = self.delay if self.delay_sd is not None: d=max(0,self.random.gauss(d,self.delay_sd)) yield d print("YEILDED", d) self.busy=False if len(r) == 0: self._b1.clear() self.error = True else: obj=self.random.choice(r) self._b1.set(obj)
def test_exact(self): obj={'self':Obj(a=1,b=2,c=3)} p=Pattern(dict(self='a:1 b:2 c:3')) self.assertEqual(p.match(obj),{}) obj['self'].c=2 self.assertEqual(p.match(obj),None) self.assertEqual(Pattern(dict(self='a:1 a:2')).match(obj),None) self.assertEqual(p.match({'self':None}),None) self.assertEqual(Pattern(dict(self=None)).match({'self':None}),{})
def compress_shoulder(self,function_name,**kwargs): ''' This function compresses the shoulder (should be in ribs rotation direction) to help reduce agent width. :param function_name: :param kwargs: bone='shoulder.L' OR bone='shoulder.R' :return:applies the shoulder compression with set_rotation on Morse side ''' #Check the max rotation print("Compress Shoulder") if self.busy: return self.busy = True maxReached = False minReached = False kwargs.update(self.function_map[function_name][1]) #if kwargs['bone'] == 'shoulder.R': # kwargs['radians'] = kwargs['radians'] * 1 if kwargs['bone'] == 'shoulder.L': kwargs['radians'] = kwargs['radians'] * -1 minR,maxR = self._boneProperties[kwargs['bone']][kwargs['axis']] #print("MINR", minR) if Decimal(kwargs['radians']).quantize(Decimal('0.000'),rounding=ROUND_HALF_UP) >= Decimal(maxR).quantize(Decimal('0.000'),rounding=ROUND_HALF_UP): print("SET TO MAX") maxReached=True kwargs['radians'] = maxR if Decimal(kwargs['radians']).quantize(Decimal('0.000'),rounding=ROUND_HALF_UP) <= Decimal(minR).quantize(Decimal('0.000'),rounding=ROUND_HALF_UP): print("SET TO MIN") minReached = True kwargs['radians'] = minR print("RADIANS",kwargs['radians']) middleware.send(self.function_map[function_name][0],**kwargs) #middleware.send('rotate_torso',axis=axis, radians=radians) pattern='type:proprioception ' + 'bone:' + kwargs['bone'] matcher=Pattern(pattern) for obj in self._internalChunks: #if axis='0.0' if matcher.match(obj)!=None: obj.rotation0=kwargs['radians'] if kwargs['radians'] < 0: obj.rotation_direction = 'right' elif kwargs['radians'] > 0: obj.rotation_direction = 'left' if maxReached: obj.rotation0_quality='max' if minReached: obj.rotation0_quality='min' self.busy=False self.update_posture()
def __init__(self, visual, location): ccm.Model.__init__(self) self._visual = visual self._location = location self.busy = False self.lastLocationPattern = Pattern('') self.tracking = None self.timeAppeared = {} self.visualOnsetSpan = 0.5 self.finst = Finst(self)
def context(self,pattern): pat=Pattern(pattern) chunks=[x for x in self.memory.dm if pat.match(x) is not None] for k,hist in self.histogram.items(): hist.clear() if len(chunks)==0: raise Exception('No chunks match salience pattern: "%s"'%pattern) dw=1.0/len(chunks) for c in chunks: for k,hist in self.histogram.items(): val=c.get(k,None) if val not in hist: hist[val]=dw else: hist[val]+=dw
def request(self,pattern=''): print("REQUEST") if self.busy: return matcher = Pattern(pattern) self.error=False r=[] for obj in self._internalChunks: print("one") if matcher.match(obj)!=None: print("Not None", obj)
def request(self, pattern=''): print("REQUEST") if self.busy: return matcher = Pattern(pattern) self.error = False r = [] for obj in self._internalChunks: print("one") if matcher.match(obj) != None: print("Not None", obj)
def context(self,pattern): pat=Pattern(pattern) chunks=[x for x in self.memory.dm if pat.match(x) is not None] for k,hist in list(self.histogram.items()): hist.clear() if len(chunks)==0: raise Exception('No chunks match salience pattern: "%s"'%pattern) dw=1.0/len(chunks) for c in chunks: for k,hist in list(self.histogram.items()): val=c.get(k,None) if val not in hist: hist[val]=dw else: hist[val]+=dw
def examine(self, pat): if isinstance(pat, str) and ':' not in pat and pat.count(' ') == 1: pat = 'x:%s y:%s' % tuple(pat.split(' ')) self.lastExamine = Pattern(pat, self.sch.bound) pat = self.lastExamine for obj in self.parent.parent.get_children(): if hasattr(obj, 'x') and hasattr(obj, 'y') and getattr( obj, 'visible', True): if pat.match(obj) is not None: #if self.isClose(obj.x,float(pat[0])) and self.isClose(obj.y,float(pat[1])): self.busy = True yield 0.085 self.busy = False if obj in self.parent.parent.get_children(): self.tracking = obj self.log._ = 'Vision sees %s' % Chunk(obj) self._visual.set(obj) self.finst.add(obj) else: self.tracking = None self.log._ = 'Vision sees nothing' self._visual.clear() break
def __init__(self, pre, post, keep, retrieve, pre_bound, post_bound): self.name = '%s-%s-%d' % (pre.name, post.name, id(self)) self.system = pre.system self.base_utility = 0 self.bound = None code1 = [] for k, v in list(pre_bound.items()): code1.append(' %s=%s' % (k, repr(v))) code1.append(' if True: # compiled from %s' % pre.name) added_line = False for line in pre.code.splitlines(): for k in keep: if line.strip().startswith(k): code1.append(' ' + line) added_line = True break if not added_line: code1.append(' pass') code1 = '\n'.join(code1) for k, v in list(pre_bound.items()): code1 = code1.replace('?' + k, v) code2 = [] for k, v in list(post_bound.items()): code2.append(' %s=%s' % (k, repr(v))) code2.append(' if True: # compiled from %s' % post.name) for line in post.code.splitlines(): if len(line.strip()) > 0: code2.append(' ' + line) code2 = '\n'.join(code2) for k, v in list(post_bound.items()): code2 = code2.replace('?' + k, v) self.code = 'if True:\n%s\n%s' % (code1, code2) self.func = compile(self.code, '<production-%s>' % self.name, 'exec') keys = list(pre.keys) patterns = {} for buf, pat in list(pre.pattern_specs.items()): for k, v in list(pre_bound.items()): pat = pat.replace('?' + k, v) patterns[buf] = pat for m in post.keys: if m == retrieve: pass elif m not in keys: keys.append(m) pat = post.pattern_specs[m] for k, v in list(post_bound.items()): pat = pat.replace('?' + k, v) patterns[buf] = pat self.keys = keys self.pattern_specs = patterns self.pattern = Pattern(patterns)
def attendTo(self, pattern, unattended=False, new=False): if isinstance(pattern, str) and ':' not in pattern and pattern.count(' ') == 1: pattern = 'x:%s y:%s' % tuple(pattern.split(' ')) self.lastLocationPattern = Pattern(pattern, self.sch.bound) r = [] for obj in self.parent.parent.get_children(): if new == True and not self.isNew(obj): continue if unattended == True and self.finst.isIn(obj): continue if hasattr(obj, 'x') and hasattr(obj, 'y') and getattr( obj, 'visible', True): if self.lastLocationPattern.match(obj) != None: r.append(obj) if len(r) > 0: obj = self.random.choice(r) self.log._ = 'Vision found obj at (%g,%g)' % (obj.x, obj.y) self._location.set('%g %g' % (obj.x, obj.y))
def request(self, pattern=''): if self.busy: return matcher = Pattern(pattern) self.error = False r = [] for obj in self.parent.parent.get_children(): if matcher.match(obj) != None: if not hasattr(obj, 'salience') and not hasattr( obj, 'visible'): continue if hasattr(obj, 'salience'): if self.random.random() > obj.salience: continue if hasattr(obj, 'visible'): if obj.visible == False: continue if hasattr(obj, 'value'): if obj.value == None: continue r.append(obj) self.busy = True d = self.delay if self.delay_sd is not None: d = max(0, self.random.gauss(d, self.delay_sd)) yield d self.busy = False if len(r) == 0: self._buffer.clear() self.error = True else: obj = self.random.choice(r) if obj not in self.parent.parent.get_children(): self._buffer.clear() self.error = True elif hasattr(obj, 'visible') and obj.visible == False: self._buffer.clear() self.error = True else: self._buffer.set(obj)
def request(self,pattern=''): if self.busy: return matcher=Pattern(pattern) self.error=False r=[] for obj in self.parent.parent.get_children(): if matcher.match(obj)!=None: print("Not None") if not hasattr(obj,'salience') and not hasattr(obj,'visible'): continue if hasattr(obj,'salience'): if self.random.random()>obj.salience: continue if hasattr(obj,'visible'): if obj.visible==False: continue if hasattr(obj,'value'): if obj.value==None: continue r.append(obj) self.busy=True d=self.delay if self.delay_sd is not None: d=max(0,self.random.gauss(d,self.delay_sd)) yield d self.busy=False if len(r)==0: self._buffer.clear() self.error=True else: obj=self.random.choice(r) if obj not in self.parent.parent.get_children(): self._buffer.clear() self.error=True elif hasattr(obj,'visible') and obj.visible==False: self._buffer.clear() self.error=True else: self._buffer.set(obj)
def rotate_torso(self,function_name,**kwargs): '''Rotate ribs on axis by radians''' #Check the max rotation print("ROTATE TORSO") if self.busy: return self.busy = True print("ROTATE TORSO2") maxReached = False minReached = False kwargs.update(self.function_map[function_name][1]) print("KW", kwargs) minR,maxR = self._boneProperties['part.torso'][kwargs['axis']] #print("MINR", minR) if kwargs['radians'] >= maxR: #print("SET TO MAX") maxReached=True kwargs['radians'] = maxR if kwargs['radians'] <= minR: #print("SET TO MIN") minReached = True kwargs['radians'] = minR #print("RADIANS",radians) middleware.send(self.function_map[function_name][0],**kwargs) #middleware.send('rotate_torso',axis=axis, radians=radians) pattern='type:proprioception bone:torso' matcher=Pattern(pattern) for obj in self._internalChunks: #if axis='0.0' if matcher.match(obj)!=None: obj.rotation0=kwargs['radians'] if kwargs['radians'] < 0: obj.rotation_direction = 'right' elif kwargs['radians'] > 0: obj.rotation_direction = 'left' if maxReached: obj.rotation0_quality='max' if minReached: obj.rotation0_quality='min' self.busy=False self.update_posture()
def rotate_torso(self, function_name, **kwargs): '''Rotate ribs on axis by radians''' #Check the max rotation print("ROTATE TORSO") if self.busy: return self.busy = True print("ROTATE TORSO2") maxReached = False minReached = False kwargs.update(self.function_map[function_name][1]) print("KW", kwargs) minR, maxR = self._boneProperties['part.torso'][kwargs['axis']] #print("MINR", minR) if kwargs['radians'] >= maxR: #print("SET TO MAX") maxReached = True kwargs['radians'] = maxR if kwargs['radians'] <= minR: #print("SET TO MIN") minReached = True kwargs['radians'] = minR #print("RADIANS",radians) middleware.send(self.function_map[function_name][0], **kwargs) #middleware.send('rotate_torso',axis=axis, radians=radians) pattern = 'type:proprioception bone:torso' matcher = Pattern(pattern) for obj in self._internalChunks: #if axis='0.0' if matcher.match(obj) != None: obj.rotation0 = kwargs['radians'] if kwargs['radians'] < 0: obj.rotation_direction = 'right' elif kwargs['radians'] > 0: obj.rotation_direction = 'left' if maxReached: obj.rotation0_quality = 'max' if minReached: obj.rotation0_quality = 'min' self.busy = False self.update_posture()
def __init__(self,visual,location): ccm.Model.__init__(self) self._visual=visual self._location=location self.busy=False self.lastLocationPattern=Pattern('') self.tracking=None self.timeAppeared={} self.visualOnsetSpan=0.5 self.finst=Finst(self)
def examine(self, pat): """ Check the visible object at a visual location. :param pat: specify the visual location. :return: """ # The argument "pat" (pattern) contains the information of a visual location. # If pat provides only the values of x-coordinate and y-coordinate, # we reorganize pat into 'x: x-coordinate y: y-coordinate'. if isinstance(pat, str) and ':' not in pat and pat.count(' ') == 1: pat = 'x:%s y:%s' % tuple(pat.split(' ')) # The x-coordinate, y-coordinate often are variables, e.g. "?x ?y". We use function Pattern() to map # the variables to the values stored in sch.bound. self.lastExamine = Pattern(pat, self.sch.bound) # Now we update the variable pat which has assigned valules. pat = self.lastExamine # In the following self is the vision module, self.parent is the agent which includes the vision module, # and self.parent.parent is the environment in which the agent is. In the for-loop, each object in the environment # is checked to see if they are visiable objects. for obj in self.parent.parent.get_children(): # First, let us check if an object is a visible object. We do so by checking if the object contains the attributes # "x", "y", and if the attribute "visible=True". if hasattr(obj, 'x') and hasattr(obj, 'y') and getattr( obj, 'visible', True): # Further, if the visual location pat matches the location of the visible object, if pat.match(obj) is not None: #(obsolete) if self.isClose(obj.x,float(pat[0])) and self.isClose(obj.y,float(pat[1])): # The vision module starts working; it is busy. self.busy = True # The default time required for a vision module request is set as 0.085s. yield 0.085 # The vision module finishes working; it is not busy. self.busy = False # In the following, self is the vision module, self.parent is the agent which includes the vision module, # and self.parent.parent is the environment in which the agent is. # If obj is still an object in the environment, if obj in self.parent.parent.get_children(): # The vision module is tracking this object. self.tracking = obj # We print the following information to the display. self.log._ = 'Vision sees %s' % Chunk(obj) # We put the object into the visual buffer. self._visual.set(obj) # We add the object into the list of finst (finger instantiation). self.finst.add(obj) # If obj is no longer an object in the environment, else: # There is nothing to track. self.tracking = None # We print the following information to the display. self.log._ = 'Vision sees nothing' # We clear the visual buffer. self._visual.clear() # Each time, only one object can be seen. When we find one, we break the for-loop. break
def test_exact(self): obj = {'self': Obj(a=1, b=2, c=3)} p = Pattern(dict(self='a:1 b:2 c:3')) self.assertEqual(p.match(obj), {}) obj['self'].c = 2 self.assertEqual(p.match(obj), None) self.assertEqual(Pattern(dict(self='a:1 a:2')).match(obj), None) self.assertEqual(p.match({'self': None}), None) self.assertEqual(Pattern(dict(self=None)).match({'self': None}), {})
def lower_arms(self, function_name, **kwargs): ''' This function lowers the arms completly. :param function_name: 'lower_arms' :param kwargs: {} :return: Moves the arms by so they are pointing downward :special note: The arms currently do not map properly to the desired arms for the model. Because the armature start somewhere near 45 (or -45) deg., when you lower the arms, you have to lower them to -45 (or 45) deg.,. On the ACT-R side, I'm calling this 0 deg. ''' print("LOWER ARMS", kwargs) if self.busy: return self.busy = True # minR,maxR = self._boneProperties['part.torso'][kwargs['axis']] # #print("MINR", minR) # if kwargs['radians'] > maxR: # #print("SET TO MAX") # maxReached=True # kwargs['radians'] = maxR # if kwargs['radians'] < minR: # #print("SET TO MIN") # minReached = True # kwargs['radians'] = minR patterns = [ 'type:proprioception bone:upper_arm.R', 'type:proprioception bone:upper_arm.L' ] for pattern in patterns: matcher = Pattern(pattern) for obj in self._internalChunks: #if axis='0.0' if matcher.match(obj) != None: obj.rotation0 = '0' obj.overall_quality = 'lowered' self.busy = False self.update_posture() middleware.send(self.function_map[function_name][0], **kwargs)
def lower_arms(self,function_name,**kwargs): ''' This function lowers the arms completly. :param function_name: 'lower_arms' :param kwargs: {} :return: Moves the arms by so they are pointing downward :special note: The arms currently do not map properly to the desired arms for the model. Because the armature start somewhere near 45 (or -45) deg., when you lower the arms, you have to lower them to -45 (or 45) deg.,. On the ACT-R side, I'm calling this 0 deg. ''' print("LOWER ARMS", kwargs) if self.busy: return self.busy = True # minR,maxR = self._boneProperties['part.torso'][kwargs['axis']] # #print("MINR", minR) # if kwargs['radians'] > maxR: # #print("SET TO MAX") # maxReached=True # kwargs['radians'] = maxR # if kwargs['radians'] < minR: # #print("SET TO MIN") # minReached = True # kwargs['radians'] = minR patterns = ['type:proprioception bone:upper_arm.R', 'type:proprioception bone:upper_arm.L'] for pattern in patterns: matcher=Pattern(pattern) for obj in self._internalChunks: #if axis='0.0' if matcher.match(obj)!=None: obj.rotation0='0' obj.overall_quality='lowered' self.busy=False self.update_posture() middleware.send(self.function_map[function_name][0],**kwargs)
def get_bounding_box(self): print("get_bounding_box") if self.busy: return self.busy = True self._boundingBox = [x * 1.00 for x in middleware.request('getBoundingBox', [])] pattern='type:proprioception feature:bounding_box' matcher=Pattern(pattern) objs = 0 for obj in self._internalChunks: if matcher.match(obj)!= None: objs+=1 obj.width=repr(self._boundingBox[0]) obj.depth=repr(self._boundingBox[1]) obj.height=repr(self._boundingBox[2]) if objs > 1: raise Exception("There shouldn't be more than one match...") print("get_bounding_box done.") self.busy = False self.update_posture()
def attendTo(self, pattern, unattended=False, new=False): self.lastLocationPattern = Pattern(pattern, self.sch.bound) r = [] for obj in self.parent.parent.get_children(): #if new==True and not self.isNew(obj): continue #if unattended==True and self.finst.isIn(obj): continue if hasattr(obj, 'x') and hasattr(obj, 'y') and getattr( obj, 'visible', True): if pattern.isMatch(obj) != None: r.append(obj) if len(r) > 0: obj = self.random.choice(r) self.log._ = 'Vision found obj at (%d,%d)' % (obj.x, obj.y) self._location.set('%d %d' % (obj.x, obj.y))
def get_bounding_box(self): print("get_bounding_box") if self.busy: return self.busy = True self._boundingBox = [ x * 1.00 for x in middleware.request('getBoundingBox', []) ] pattern = 'type:proprioception feature:bounding_box' matcher = Pattern(pattern) objs = 0 for obj in self._internalChunks: if matcher.match(obj) != None: objs += 1 obj.width = repr(self._boundingBox[0]) obj.depth = repr(self._boundingBox[1]) obj.height = repr(self._boundingBox[2]) if objs > 1: raise Exception("There shouldn't be more than one match...") print("get_bounding_box done.") self.busy = False self.update_posture()
def attendTo(self, pattern, unattended=False, new=False): # The argument "pattern" contains the information of a visual location. # If pat provides only the values of x-coordinate and y-coordinate, # we reorganize pat into 'x: x-coordinate y: y-coordinate'. if isinstance(pattern, str) and ':' not in pattern and pattern.count(' ') == 1: pattern = 'x:%s y:%s' % tuple(pattern.split(' ')) # The x-coordinate, y-coordinate often are variables, e.g. "?x ?y". We use function Pattern() to map # the variables to the values stored in sch.bound. self.lastLocationPattern = Pattern(pattern, self.sch.bound) r = [] # In the following self is the vision module, self.parent is the agent which includes the vision module, # and self.parent.parent is the environment in which the agent is. In the for-loop, each object in the environment # is checked to see if they are visiable objects. for obj in self.parent.parent.get_children(): # If the visual location is new, but the object is not new, we directly go to check the next object. if new == True and not self.isNew(obj): continue # If the location is unattended, but the object is in finst (finger instantiation) list, we directly go to check # the next object. if unattended == True and self.finst.isIn(obj): continue # Let us check if an object is a visible object. We do so by checking if the object contains the attributes # "x", "y", and if the attribute "visible=True". if hasattr(obj, 'x') and hasattr(obj, 'y') and getattr( obj, 'visible', True): # Further, if the visual location matches the location of the visible object, if self.lastLocationPattern.match(obj) != None: # We add this object to the list r. r.append(obj) # If we found more than one visible objects in the list r, if len(r) > 0: # We randomly chose one object from the list. obj = self.random.choice(r) # We print the following information to the display. self.log._ = 'Vision found obj at (%g,%g)' % (obj.x, obj.y) # We store the object's location into the visual location buffer. self._location.set('%g %g' % (obj.x, obj.y))
def attendTo(self,pattern,unattended=False,new=False): if isinstance(pattern, str) and ':' not in pattern and pattern.count(' ')==1: pattern='x:%s y:%s'%tuple(pattern.split(' ')) self.lastLocationPattern=Pattern(pattern,self.sch.bound) r=[] for obj in self.parent.parent.get_children(): if new==True and not self.isNew(obj): continue if unattended==True and self.finst.isIn(obj): continue if hasattr(obj,'x') and hasattr(obj,'y') and getattr(obj,'visible',True): if self.lastLocationPattern.match(obj)!=None: r.append(obj) if len(r)>0: obj=self.random.choice(r) self.log._='Vision found obj at (%g,%g)'%(obj.x,obj.y) self._location.set('%g %g'%(obj.x,obj.y))
def examine(self, pat): self.lastExamine = Pattern(pat, self.sch.bound) pat = self.lastExamine for obj in self.parent.parent.get_children(): if hasattr(obj, 'x') and hasattr(obj, 'y') and getattr( obj, 'visible', True): if True or pat.match(obj) is not None: #if self.isClose(obj.x,float(pat[0])) and self.isClose(obj.y,float(pat[1])): yield 0.085 if obj in self.parent.parent.get_children(): self.tracking = obj self.log._ = 'Vision sees %s' % Chunk(obj) self._visual.set(obj) self.finst.add(obj) else: self.tracking = None self.log._ = 'Vision sees nothing' self._visual.clear() break
def makePattern(text): return Pattern(dict(self=text))
class Vision(ccm.Model): def __init__(self, visual, location): ccm.Model.__init__(self) self._visual = visual self._location = location self.busy = False self.lastLocationPattern = Pattern('') self.tracking = None self.timeAppeared = {} self.visualOnsetSpan = 0.5 self.finst = Finst(self) def start(self): self.environmentUpdate() def isNew(self, object): if not getattr(object, 'visible', True): return time = self.timeAppeared.get(object, None) if time == None: self.timeAppeared[object] = self.now() return True return self.now() < time + self.visualOnsetSpan def environmentUpdate(self): while True: if self._location.isEmpty(): r = [] for o in self.parent.parent.get_children(): if not getattr(o, 'visible', True): if o in self.timeAppeared.keys(): del self.timeAppeared[o] continue if o not in self.timeAppeared: if hasattr(o, 'x') and hasattr(o, 'y'): #if self.lastLocationPattern.match(o)!=None: r.append(o) if len(r) > 0: obj = self.random.choice(r) self.log._ = 'Vision stuffed obj at (%g,%g)' % (obj.x, obj.y) self._location.set('%g %g' % (obj.x, obj.y)) #print 'checking tracking',self.tracking #if self.tracking!=None and (self.tracking not in self.parent.parent.get_children() or not getattr(self.tracking,'visible',True)): # print '...lost' # self.sch.add(self.lostTrack,delay=0.085) for o in self.parent.parent.get_children(): self.isNew(o) yield self.parent.parent.changes, self.lostTrack def lostTrack(self): if self.tracking is not None: self.tracking = None self.log._ = 'Object disappeared' self._visual.clear() #self._location.clear() def attendToUnattended(self, pattern=''): self.attendTo(pattern=pattern, unattended=True) def attendToNew(self, pattern=''): self.attendTo(pattern=pattern, new=True) def attendTo(self, pattern, unattended=False, new=False): if isinstance(pattern, str) and ':' not in pattern and pattern.count(' ') == 1: pattern = 'x:%s y:%s' % tuple(pattern.split(' ')) self.lastLocationPattern = Pattern(pattern, self.sch.bound) r = [] for obj in self.parent.parent.get_children(): if new == True and not self.isNew(obj): continue if unattended == True and self.finst.isIn(obj): continue if hasattr(obj, 'x') and hasattr(obj, 'y') and getattr( obj, 'visible', True): if self.lastLocationPattern.match(obj) != None: r.append(obj) if len(r) > 0: obj = self.random.choice(r) self.log._ = 'Vision found obj at (%g,%g)' % (obj.x, obj.y) self._location.set('%g %g' % (obj.x, obj.y)) def isClose(self, a, b): return abs(a - b) < 0.01 def examine(self, pat): if isinstance(pat, str) and ':' not in pat and pat.count(' ') == 1: pat = 'x:%s y:%s' % tuple(pat.split(' ')) self.lastExamine = Pattern(pat, self.sch.bound) pat = self.lastExamine for obj in self.parent.parent.get_children(): if hasattr(obj, 'x') and hasattr(obj, 'y') and getattr( obj, 'visible', True): if pat.match(obj) is not None: #if self.isClose(obj.x,float(pat[0])) and self.isClose(obj.y,float(pat[1])): self.busy = True yield 0.085 self.busy = False if obj in self.parent.parent.get_children(): self.tracking = obj self.log._ = 'Vision sees %s' % Chunk(obj) self._visual.set(obj) self.finst.add(obj) else: self.tracking = None self.log._ = 'Vision sees nothing' self._visual.clear() break
def update_posture(self): pattern1='type:proprioception bone:upper_arm.R'# overall_quality:lowered' pattern2='type:proprioception bone:upper_arm.L'# overall_quality:lowered' pattern3='type:proprioception bone:shoulder.R'# rotation0_quality:max' pattern4='type:proprioception bone:shoulder.L'# rotation0_quality:min' #probably should find those, instead of supplying them pattern5='type:proprioception bone:torso'# rotation_direction:right' matcher1=Pattern(pattern1) matcher2=Pattern(pattern2) matcher3=Pattern(pattern3) matcher4=Pattern(pattern4) matcher5=Pattern(pattern5) pattern = 'type:posture' matcher = Pattern(pattern) for obj in self._internalChunks: #if axis='0.0' if matcher.match(obj)!=None: obj.minimal_width = 'false' matches = [matcher1,matcher2,matcher3,matcher4,matcher5] objs = [] for m in matches: for obj in self._internalChunks: if m.match(obj) != None: objs.append(obj) try:#For posture, minimal width, yes if objs[0].overall_quality=='lowered' and \ objs[1].overall_quality=='lowered' and\ objs[2].rotation0_quality=='max' and\ objs[3].rotation0_quality=='min' and\ objs[4].rotation_direction == 'right': pattern = 'type:posture' matcher = Pattern(pattern) for obj in self._internalChunks: #if axis='0.0' if matcher.match(obj)!=None: obj.minimal_width = 'true' return except Exception: pass try:#For posture, minimal width, yes if objs[0].overall_quality=='lowered' and \ objs[1].overall_quality=='lowered' and\ objs[2].rotation0_quality=='min' and\ objs[3].rotation0_quality=='max' and\ objs[4].rotation_direction == 'left': pattern = 'type:posture' matcher = Pattern(pattern) for obj in self._internalChunks: #if axis='0.0' if matcher.match(obj)!=None: obj.minimal_width = 'true' return except Exception: pass
class Vision(ccm.Model): def __init__(self, visual, location): ccm.Model.__init__(self) self._visual = visual self._location = location self.busy = False self.lastLocationPattern = Pattern('') self.tracking = None self.timeAppeared = {} self.visualOnsetSpan = 0.5 self.finst = Finst(self) def start(self): self.environmentUpdate() def isNew(self, object): if not getattr(object, 'visible', True): return time = self.timeAppeared.get(object, None) if time == None: self.timeAppeared[object] = self.now() return True return self.now() < time + self.visualOnsetSpan def environmentUpdate(self): # First, create a while-loop for checking updates to the environment. while True: # If the visual location buffer is empty, if self._location.isEmpty(): # Create a list for storing new visible objects in the environment. r = [] # In the following self is the vision module, self.parent is the agent which includes the vision module, # and self.parent.parent is the environment in which the agent is. for o in self.parent.parent.get_children(): # If the object is not visible anymore, if not getattr(o, 'visible', True): # further if the object is in timeAppeared, then we delete the object from timeAppeared. # Note timeAppeared records the time when an object becomes visible in the environment. if o in self.timeAppeared.keys(): del self.timeAppeared[o] # Let us go to check the next object in the environment directly. continue # If the object is not in timeAppeared, if o not in self.timeAppeared: # further if the object has the location information, if hasattr(o, 'x') and hasattr(o, 'y'): #(obsolete) if self.lastLocationPattern.match(o)!=None: # Let add this object into the list r. r.append(o) # If there is new visible objects, i.e., the list is not empty, if len(r) > 0: # We randomly select one object. obj = self.random.choice(r) # We print the following information to the display. self.log._ = 'Vision stuffed obj at (%g,%g)' % (obj.x, obj.y) # We add the location of the new visible object to the visual location buffer. self._location.set('%g %g' % (obj.x, obj.y)) #(obsolete) print 'checking tracking',self.tracking #(obsolete) if self.tracking!=None and (self.tracking not in self.parent.parent.get_children() or not getattr(self.tracking,'visible',True)): #(obsolete) print '...lost' #(obsolete) self.sch.add(self.lostTrack,delay=0.085) # In the following self is the vision module, self.parent is the agent which includes the vision module, # and self.parent.parent is the environment in which the agent is. In the for-loop, each object in the environment # is checked to see if they are new visiable objects. for o in self.parent.parent.get_children(): self.isNew(o) # In the following self is the vision module, self.parent is the agent which includes the vision module, # and self.parent.parent is the environment in which the agent is. yield self.parent.parent.changes, self.lostTrack def lostTrack(self): """Indicate that the vision tracking is lost.""" # If the vision module is tracking an object, if self.tracking is not None: # The object slips away from our tracking. self.tracking = None # We print the information to the display. self.log._ = 'Object disappeared' # And we clear the visual buffer. self._visual.clear() #(obsolete) self._location.clear() def attendToUnattended(self, pattern=''): self.attendTo(pattern=pattern, unattended=True) def attendToNew(self, pattern=''): self.attendTo(pattern=pattern, new=True) def attendTo(self, pattern, unattended=False, new=False): # The argument "pattern" contains the information of a visual location. # If pat provides only the values of x-coordinate and y-coordinate, # we reorganize pat into 'x: x-coordinate y: y-coordinate'. if isinstance(pattern, str) and ':' not in pattern and pattern.count(' ') == 1: pattern = 'x:%s y:%s' % tuple(pattern.split(' ')) # The x-coordinate, y-coordinate often are variables, e.g. "?x ?y". We use function Pattern() to map # the variables to the values stored in sch.bound. self.lastLocationPattern = Pattern(pattern, self.sch.bound) r = [] # In the following self is the vision module, self.parent is the agent which includes the vision module, # and self.parent.parent is the environment in which the agent is. In the for-loop, each object in the environment # is checked to see if they are visiable objects. for obj in self.parent.parent.get_children(): # If the visual location is new, but the object is not new, we directly go to check the next object. if new == True and not self.isNew(obj): continue # If the location is unattended, but the object is in finst (finger instantiation) list, we directly go to check # the next object. if unattended == True and self.finst.isIn(obj): continue # Let us check if an object is a visible object. We do so by checking if the object contains the attributes # "x", "y", and if the attribute "visible=True". if hasattr(obj, 'x') and hasattr(obj, 'y') and getattr( obj, 'visible', True): # Further, if the visual location matches the location of the visible object, if self.lastLocationPattern.match(obj) != None: # We add this object to the list r. r.append(obj) # If we found more than one visible objects in the list r, if len(r) > 0: # We randomly chose one object from the list. obj = self.random.choice(r) # We print the following information to the display. self.log._ = 'Vision found obj at (%g,%g)' % (obj.x, obj.y) # We store the object's location into the visual location buffer. self._location.set('%g %g' % (obj.x, obj.y)) def isClose(self, a, b): return abs(a - b) < 0.01 def examine(self, pat): """ Check the visible object at a visual location. :param pat: specify the visual location. :return: """ # The argument "pat" (pattern) contains the information of a visual location. # If pat provides only the values of x-coordinate and y-coordinate, # we reorganize pat into 'x: x-coordinate y: y-coordinate'. if isinstance(pat, str) and ':' not in pat and pat.count(' ') == 1: pat = 'x:%s y:%s' % tuple(pat.split(' ')) # The x-coordinate, y-coordinate often are variables, e.g. "?x ?y". We use function Pattern() to map # the variables to the values stored in sch.bound. self.lastExamine = Pattern(pat, self.sch.bound) # Now we update the variable pat which has assigned valules. pat = self.lastExamine # In the following self is the vision module, self.parent is the agent which includes the vision module, # and self.parent.parent is the environment in which the agent is. In the for-loop, each object in the environment # is checked to see if they are visiable objects. for obj in self.parent.parent.get_children(): # First, let us check if an object is a visible object. We do so by checking if the object contains the attributes # "x", "y", and if the attribute "visible=True". if hasattr(obj, 'x') and hasattr(obj, 'y') and getattr( obj, 'visible', True): # Further, if the visual location pat matches the location of the visible object, if pat.match(obj) is not None: #(obsolete) if self.isClose(obj.x,float(pat[0])) and self.isClose(obj.y,float(pat[1])): # The vision module starts working; it is busy. self.busy = True # The default time required for a vision module request is set as 0.085s. yield 0.085 # The vision module finishes working; it is not busy. self.busy = False # In the following, self is the vision module, self.parent is the agent which includes the vision module, # and self.parent.parent is the environment in which the agent is. # If obj is still an object in the environment, if obj in self.parent.parent.get_children(): # The vision module is tracking this object. self.tracking = obj # We print the following information to the display. self.log._ = 'Vision sees %s' % Chunk(obj) # We put the object into the visual buffer. self._visual.set(obj) # We add the object into the list of finst (finger instantiation). self.finst.add(obj) # If obj is no longer an object in the environment, else: # There is nothing to track. self.tracking = None # We print the following information to the display. self.log._ = 'Vision sees nothing' # We clear the visual buffer. self._visual.clear() # Each time, only one object can be seen. When we find one, we break the for-loop. break
class Vision(ccm.Model): def __init__(self,visual,location): ccm.Model.__init__(self) self._visual=visual self._location=location self.busy=False self.lastLocationPattern=Pattern('') self.tracking=None self.timeAppeared={} self.visualOnsetSpan=0.5 self.finst=Finst(self) def start(self): self.environmentUpdate() def isNew(self,object): if not getattr(object,'visible',True): return time=self.timeAppeared.get(object,None) if time==None: self.timeAppeared[object]=self.now() return True return self.now()<time+self.visualOnsetSpan def environmentUpdate(self): while True: if self._location.isEmpty(): r=[] for o in self.parent.parent.get_children(): if not getattr(o,'visible',True): if o in self.timeAppeared.keys(): del self.timeAppeared[o] continue if o not in self.timeAppeared: if hasattr(o,'x') and hasattr(o,'y'): #if self.lastLocationPattern.match(o)!=None: r.append(o) if len(r)>0: obj=self.random.choice(r) self.log._='Vision stuffed obj at (%g,%g)'%(obj.x,obj.y) self._location.set('%g %g'%(obj.x,obj.y)) #print 'checking tracking',self.tracking #if self.tracking!=None and (self.tracking not in self.parent.parent.get_children() or not getattr(self.tracking,'visible',True)): # print '...lost' # self.sch.add(self.lostTrack,delay=0.085) for o in self.parent.parent.get_children(): self.isNew(o) yield self.parent.parent.changes,self.lostTrack def lostTrack(self): if self.tracking is not None: self.tracking=None self.log._='Object disappeared' self._visual.clear() #self._location.clear() def attendToUnattended(self,pattern=''): self.attendTo(pattern=pattern,unattended=True) def attendToNew(self,pattern=''): self.attendTo(pattern=pattern,new=True) def attendTo(self,pattern,unattended=False,new=False): if isinstance(pattern, str) and ':' not in pattern and pattern.count(' ')==1: pattern='x:%s y:%s'%tuple(pattern.split(' ')) self.lastLocationPattern=Pattern(pattern,self.sch.bound) r=[] for obj in self.parent.parent.get_children(): if new==True and not self.isNew(obj): continue if unattended==True and self.finst.isIn(obj): continue if hasattr(obj,'x') and hasattr(obj,'y') and getattr(obj,'visible',True): if self.lastLocationPattern.match(obj)!=None: r.append(obj) if len(r)>0: obj=self.random.choice(r) self.log._='Vision found obj at (%g,%g)'%(obj.x,obj.y) self._location.set('%g %g'%(obj.x,obj.y)) def isClose(self,a,b): return abs(a-b)<0.01 def examine(self,pat): if isinstance(pat, str) and ':' not in pat and pat.count(' ')==1: pat='x:%s y:%s'%tuple(pat.split(' ')) self.lastExamine=Pattern(pat,self.sch.bound) pat=self.lastExamine for obj in self.parent.parent.get_children(): if hasattr(obj,'x') and hasattr(obj,'y') and getattr(obj,'visible',True): if pat.match(obj) is not None: #if self.isClose(obj.x,float(pat[0])) and self.isClose(obj.y,float(pat[1])): self.busy=True yield 0.085 self.busy=False if obj in self.parent.parent.get_children(): self.tracking=obj self.log._='Vision sees %s'%Chunk(obj) self._visual.set(obj) self.finst.add(obj) else: self.tracking=None self.log._='Vision sees nothing' self._visual.clear() break
def update_posture(self): pattern1 = 'type:proprioception bone:upper_arm.R' # overall_quality:lowered' pattern2 = 'type:proprioception bone:upper_arm.L' # overall_quality:lowered' pattern3 = 'type:proprioception bone:shoulder.R' # rotation0_quality:max' pattern4 = 'type:proprioception bone:shoulder.L' # rotation0_quality:min' #probably should find those, instead of supplying them pattern5 = 'type:proprioception bone:torso' # rotation_direction:right' matcher1 = Pattern(pattern1) matcher2 = Pattern(pattern2) matcher3 = Pattern(pattern3) matcher4 = Pattern(pattern4) matcher5 = Pattern(pattern5) pattern = 'type:posture' matcher = Pattern(pattern) for obj in self._internalChunks: #if axis='0.0' if matcher.match(obj) != None: obj.minimal_width = 'false' matches = [matcher1, matcher2, matcher3, matcher4, matcher5] objs = [] for m in matches: for obj in self._internalChunks: if m.match(obj) != None: objs.append(obj) try: #For posture, minimal width, yes if objs[0].overall_quality=='lowered' and \ objs[1].overall_quality=='lowered' and\ objs[2].rotation0_quality=='max' and\ objs[3].rotation0_quality=='min' and\ objs[4].rotation_direction == 'right': pattern = 'type:posture' matcher = Pattern(pattern) for obj in self._internalChunks: #if axis='0.0' if matcher.match(obj) != None: obj.minimal_width = 'true' return except Exception: pass try: #For posture, minimal width, yes if objs[0].overall_quality=='lowered' and \ objs[1].overall_quality=='lowered' and\ objs[2].rotation0_quality=='min' and\ objs[3].rotation0_quality=='max' and\ objs[4].rotation_direction == 'left': pattern = 'type:posture' matcher = Pattern(pattern) for obj in self._internalChunks: #if axis='0.0' if matcher.match(obj) != None: obj.minimal_width = 'true' return except Exception: pass