def test_global_params(self): fm = FARGModel(seed=1) self.assertEqual(fm.seed, 1) self.assertTrue(isinstance(fm.activation_g, ActivationGraph)) fm = FARGModel(aprop=dict(alpha=0.92)) self.assertEqual(fm.activation_g.propagator.alpha, 0.92)
def paint(self, fm: FARGModel, source: CellRef, dest: CellRef, builder: Union[Agent, None] = None, **ignored): #print('CPAINT', self) # TODO throw if any members/args are missing if builder is None: builder = self s0: SeqState = source.contents if s0 is None: return try: taken_avails, remaining_avails = s0.take_avails(self.operands) except ValuesNotAvail as exc: # TODO builder=self even if builder overridden by caller? #print('CNOT') fm.build(Blocked(taggee=self, reason=exc), builder=self) fm.downboost(self) return result = self.operator.call(*taken_avails) new_avails = tuple(remaining_avails) + (result, ) delta = ArithDelta(tuple(taken_avails), result, self.operator) s1 = SeqState(new_avails, delta) return fm.paint_value(dest, s1, builder=builder)
def update_support(self, fm: FARGModel): for consume in fm.search_ws( (Consume, CellRef, GettingCloser(taggee=None, target=self.target)), max_n=20): fm.set_support_edge(self, consume, weight=self.promisingness_of(fm, consume))
def consult_slipnet(self, fm: FARGModel): source = self.canvas.last_nonblank() #avails = self.canvas[self.addr].avails avails = source.contents.avails activations_in = {} for avail in avails: activations_in[Before(avail)] = 1.0 activations_in[After(self.target)] = 0.1 if all(avail > self.target for avail in avails): activations_in[Increase()] = 10.0 exclude = Exclude(fm.neighbors(self)) #source = CellRef(self.canvas, self.addr) agents = fm.pulse_slipnet( # GLOBAL constants in next line activations_in, k=20, type=Agent, num_get=1, filter=exclude) # TODO We need a way to get an Agent from the slipnet that means # something like "Just add what you have." #print('WANT got from slipnet:', [str(a) for a in agents]) #DIAG for agent in agents: if isinstance(agent, Consume): #HACK agent = replace(agent, source=source) fm.build(agent, builder=self, init_a=0.1)
def look(self, fm: FARGModel): # TODO Exclude nodes already tagged GettingCloser found = first(fm.search_ws(ImCell)) #print('GettingCloser.Tagger FOUND', found) if found: weight = GettingCloser.calc_weight(found, self.target) if weight > 0.001: # TODO Indicate the Want node? The target? # TODO Indicate builder tag = fm.build( GettingCloser(taggee=found, target=self.target, weight=weight)) fm.add_mut_support(tag, found)
def go( self, fm: FARGModel, **overrides #source: Union[CellRef, None]=None, #dest: Union[CellRef, None]=None, #builder: Union[Agent, None]=None ) -> CellRef: if overrides.get('source', None) is None: overrides['source'] = self.source if overrides.get('dest', None) is None: overrides['dest'] = overrides['source'].imaginary_next_cellref() result = self.paint(fm, **overrides) if result is not None: #fm.boost(self) fm.build(GoIsDone(taggee=self)) return result
def promisingness_of(self, fm: FARGModel, elem: Elem) -> float: # TODO Scouts, probably constructed from the slipnet, should search # for what's promising and apply tags. It shouldn't be hard-coded # in the Want class. if isinstance(elem, Consume): result = 0.0 if fm.is_blocked(elem): result += 0.1 else: result += 0.2 if fm.can_act(elem): result += 0.2 return result elif isinstance(elem, CellRef): #return 2.0 if elem.contents == self.target else 0.0 return 20.0 if has_avail_value(elem.contents, self.target) else 0.0 elif isinstance(elem, GettingCloser): # TODO Promising, not GettingCloser #print('WANTGC', elem) #DIAG return 10 * elem.weight else: return 0.0
def go(self, fm: FARGModel): # TODO Don't build these if they're already built fm.build(Detector(self.target, action=RaiseException(SolvedNumble)), builder=self) fm.build(GettingCloser.Tagger(target=self.target), builder=self) self.consult_slipnet(fm) self.update_support(fm) fm.sleep(self)
def test_hardcoded_pons_asinorum(self): fm = FARGModel() ca = fm.build(SeqCanvas([SeqState((4, 5, 6), None)])) wa = fm.build(Want(15, canvas=ca, addr=0)) wa.go(fm) # Builds Consume objects and Detector for co in fm.ws_query(Consume, builder=wa): co.go(fm) bl = fm.ws_query1(Blocked) bl.go(fm) d9 = fm.ws_query1(Detector, target=9) d9.go(fm) co1 = fm.ws_query1(Consume, operands=(4, 5)) co2 = fm.ws_query1(Consume, operands=(9, 6)) aseq0 = fm.build( AgentSeq((co1, co2), initial_kwargs=tupdict(source=CellRef(ca, 0), dest=CellRef(ca, 1)))) aseq0.go(fm) #This should not complain aseq = fm.ws_query1(AgentSeq) aseq.act(fm) d15 = fm.ws_query1(Detector, target=15) try: d15.go(fm) self.fail('Detector did not detect solution.') except SolvedNumble as exc: self.assertEqual(str(exc), '4 + 5 = 9; 9 + 6 = 15')
def rebuild_with_blanks(self, fm: FARGModel, avail_operands: Tuple[Value], unavail_operands: Tuple[Value]): print('REBU', self, avail_operands, unavail_operands) fm.build(replace(self, operands=avail_operands), builder=self)
def on_build(self, fm: FARGModel): fm.build(self.source, builder=self) fm.build(self.dest, builder=self, edge_weight=1.0)
def can_go(self, fm: FARGModel): return not fm.is_blocked(self)
def on_build(self, fm: FARGModel): fm.add_mut_support(self, self.canvas)