def test_litpainter_antipathy(self): fm = FARGModel() self.assertLess(fm.mutual_antipathy_weight, 0.0, 'mutual_antipathy_weight must be negative') ca = fm.build(SeqCanvas([SeqState((4, 5, 6), None)])) cr = CellRef(ca, 1) # pointer to 2nd cell on canvas state1 = SeqState((9, 6), ArithDelta((4, 5), 9, plus)) lp1 = fm.build(LitPainter(cr, state1)) state2 = SeqState((10, 5), ArithDelta((4, 6), 10, plus)) lp2 = fm.build(LitPainter(cr, state2)) self.assertTrue(fm.has_antipathy_to(lp1, lp2)) self.assertFalse(fm.has_antipathy_to(lp1, lp1)) self.assertEqual(fm.ae_weight(lp1, lp2), fm.mutual_antipathy_weight) self.assertTrue(fm.can_go(lp1)) self.assertTrue(fm.can_go(lp2)) self.assertFalse(cr.has_value()) # When lp1 paints, lp2 should be locked out fm.run(lp1) self.assertEqual(cr.value, state1) self.assertTrue(cr.has_value()) self.assertFalse(fm.can_go(lp2))
def run(bricks: Sequence[int], target: int, seed: int = 1, num_slipnet_iterations: Optional[int] = None, paint_threshold: float = 0.1) -> None: global fm, ca, cr0, cr1, cr2, cr3, wa lenable(Agent, Codelet, Fizzle) #, log_pulse) fm = FARGModel(slipnet=Slipnet(Graph.augment(eqn_graph, desnaggers_graph)), seed=seed, num_slipnet_iterations=num_slipnet_iterations, paint_threshold=paint_threshold) ca = fm.build(StepCanvas([Step(tuple(bricks))])) cr0 = CellRef(ca, 0) cr1 = CellRef(ca, 1) cr2 = CellRef(ca, 2) cr3 = CellRef(ca, 3) wa = fm.build( Want(startcell=cr0, target=target, on_success=RaiseException(SolvedPuzzle))) # try: fm.do_timestep(num=40) # 20 # except Exception as exc: # print(exc) pr(fm, extra=True)
def test_next_cellref(self) -> None: ca = StepCanvas([Step([4, 5, 6])]) cr0 = CellRef(ca, 0) cr1 = CellRef(ca, 1) self.assertEqual(ca.next_addr(0), 1) self.assertEqual(ca.next_cellref(cr0), cr1) self.assertEqual(cr0.next_cellref(), cr1)
def test_consume(self) -> None: fm = FARGModel() ca = fm.build(self.pons_start_canvas()) cr0 = fm.build(CellRef(ca, 0)) cr1 = fm.build(CellRef(ca, 1)) codelet = Consume(operator=plus, operands=(4, 5), source=cr0) sources = fm.run_codelet(codelet) self.assertEqual(fm.look_up_by_name('result', sources), self.step1)
def test_sleep_codelet(self) -> None: fm = FARGModel() ca = fm.build(self.pons_start_canvas()) cr0 = fm.build(CellRef(ca, 0)) cr1 = fm.build(CellRef(ca, 1)) lp = fm.build(LitPainter(dest=cr1, value=self.step1)) self.assertFalse(fm.is_sleeping(lp)) fm.run_codelet(Sleep(agent=lp, sleep_duration=10)) self.assertTrue(fm.is_sleeping(lp))
def test_gettingcloser(self): fm = TestFM(seed=1) ca = fm.build(SeqCanvas([SeqState((4, 5, 6), None)])) cr0 = CellRef(ca, 0) cr1 = CellRef(ca, 1) co = fm.build(Consume(plus, (6, 5), source=cr0, dest=cr1)) fm.run(co) lp = fm.the(LitPainter) self.assertGreater(GettingCloser.calc_weight(lp, 15), 0.1) tagger = fm.build(GettingCloser.Tagger(15)) tagger.look(fm) self.assertTrue(fm.is_tagged(lp, GettingCloser))
def test_avail_detector(self): fm = FARGModel() ca = fm.build(SeqCanvas([SeqState((4, 5, 6), None)])) cr0 = fm.build(CellRef(ca, 0)) cr1 = fm.build(CellRef(ca, 1)) # TODO The Detector should only look within ca det = fm.build(AvailDetector(9, CellRef, RaiseException(TestFoundIt))) det.look(fm) fm.paint(cr1, SeqState((6, 9), ArithDelta((4, 5), 9, plus))) with self.assertRaises(TestFoundIt): for _ in range(40): det.look(fm)
def test_paint_codelet(self) -> None: fm = FARGModel() ca = fm.build(self.pons_start_canvas()) cr1 = fm.build(CellRef(ca, 1)) fm.run_codelet(Paint(cr1, self.step1)) self.assertEqual(ca[1], self.step1)
def test_variant_maker(self) -> None: fm = FARGModel() ca = fm.build(self.pons_start_canvas()) cr0 = fm.build(CellRef(ca, 0)) ag1 = fm.build(Consumer( operator=plus, operands=(4, 4), source=cr0 )) ag2 = fm.build(VariantMakerFromAvails( agent=ag1, cellref=cr0, avails=(4, None), unavails=(None, 4) )) # The VariantMakerFromAvails should make a new Consumer, one whose # operands include 4 and another real avail, i.e. 5 or 6. #lenable(Agent, Codelet, Fizzle) #pr(fm) fm.run_agent(ag2) new_consumer = first(fm.nodes((Consumer, Exclude(ag1)))) #ldisable_all() self.assertTrue( match_wo_none(new_consumer, Consumer.make(plus, (4, 5))) or match_wo_none(new_consumer, Consumer.make(plus, (4, 6))) )
def test_basics(self): fm = FARGModel() # Did it initialize a random-number seed? self.assertIsInstance(fm.seed, int) # Build something ca = fm.build(SeqCanvas([SeqState((4, 5, 6), None)])) # Is it there? eiws = fm.get_eiws(ca) self.assertEqual(eiws.elem, ca) # Can we find it by exact match? self.assertEqual(fm.the(ca), ca) # Can we find it by searching for its class? self.assertEqual(fm.the(Canvas), ca) # Does it have initial activation? self.assertEqual(fm.a(ca), 1.0) # TODO Check builder and behalf_of # TODO Log the BUILT? cr = fm.build(CellRef(ca, 1)) # pointer to 2nd cell on canvas # TODO auto-support between CellRef and Canvas? (as in next line) #self.assertEqual(fm.ae_weight(cr, ca), fm.mutual_support_weight) delta = ArithDelta((4, 5), 9, plus) state1 = SeqState((9, 6), delta) fm.paint(cr, state1) # TODO paint_on()? self.assertEqual(ca[1], state1)
def test_query_slipnet_for_delegate(self) -> None: graph = Graph.with_features( Consumer.make_table(range(1, 20), range(1, 20), [plus, minus, times])) fm = FARGModel(slipnet=Slipnet(graph)) ca = fm.build(self.pons_start_canvas()) cr0 = fm.build(CellRef(ca, 0)) # succeed codelet = QuerySlipnetForDelegate(qargs=(QBeforeFromAvails(), QAfter(15), SearchFor(Consumer))) #t0 = process_time() fm.run_codelet_and_follow_ups(codelet, {'source': cr0}) #print('RUN1', process_time() - t0) delegate = fm.the(Consumer) self.assertIsNotNone(delegate) # fail codelet = QuerySlipnetForDelegate(qargs=(QBeforeFromAvails(), QAfter(15), SearchFor(DummyAgent))) #t0 = process_time() with self.assertRaises(NoResultFromSlipnet): fm.run_codelet_and_follow_ups(codelet, {'source': cr0})
def test_dead_end_extender(self) -> None: fm = FARGModel() ca = fm.build(self.pons_start_canvas()) cr0 = CellRef(ca, 0) cr1 = CellRef(ca, 1) cr2 = CellRef(ca, 2) wa = fm.build(Want(startcell=cr0, target=15)) fm.run_agent(wa) det = fm.the(DeadEndDetector) # Let's Consume and LitPainter our way to a dead end co1 = fm.build(Consumer( operator=times, operands=(6, 5), source=cr0 )) fm.run_agent(co1) lp1 = fm.the(LitPainter, es=fm.built_by(co1)) fm.run_agent(lp1) co2 = fm.build(Consumer( operator=times, operands=(4, 30), source=cr1 )) fm.run_agent(co2) lp2 = fm.the(LitPainter, es=fm.built_by(co2)) fm.run_agent(lp2) """ fm.paint(cr1, self.step1) fm.paint(cr2, self.step2bad) fm.run_detector(det) dead_end: Any = fm.the(DeadEnd) assert dead_end is not None self.assertIsNotNone(dead_end) self.assertEqual(dead_end.for_goal, wa) self.assertEqual(fm.builder_of(dead_end), det) """ fm.run_detector(det) dead_end: Any = fm.has_tag(cr2, DeadEnd) assert dead_end fm.add_tag(lp2, dead_end) self.assertCountEqual( fm.taggees_of(dead_end), [co2, lp2, cr2] ) self.assertEqual(fm.builder_of(dead_end), det)
def run( # type: ignore[override] self, fm: FARGModel, startcell: CellRef # start searching from here ) -> CodeletResults: result = startcell.last_painted_cellref() with logging(self, cellref=result): return {'startcell': result}
def test_snag_tag_and_no_result_from_slipnet(self) -> None: fm = FARGModel() ca = fm.build(self.pons_start_canvas()) cr0 = CellRef(ca, 0) cr1 = CellRef(ca, 1) ag = fm.build(Consumer( operator=plus, operands=(3, 5), # 3 is not avail, so this Consumer must fail source=cr0, #dest=cr1 )) fm.run_agent(ag) snag_tag = fm.the(Fizzle) self.assertIsNotNone(snag_tag) self.assertEqual(fm.builder_of(snag_tag), ag) self.assertEqual(fm.agent_state(ag), Snag)
def test_before_from_avails(self) -> None: fm = FARGModel() ca = fm.build(self.pons_start_canvas()) cr0 = CellRef(ca, 0) qarg = QBeforeFromAvails(source=cr0) self.assertCountEqual(as_iter(qarg.get_items( fm, [])), [Before(4), Before(5), Before(6)])
def test_dead_end_detector_links_to_want(self) -> None: fm = FARGModel() ca = fm.build(self.pons_start_canvas()) cr0 = CellRef(ca, 0) cr1 = CellRef(ca, 1) cr2 = CellRef(ca, 2) wa = fm.build(Want(startcell=cr0, target=15)) fm.run_agent(wa) det = fm.the(DeadEndDetector) assert det is not None fm.paint(cr1, self.step1) fm.paint(cr2, self.step2bad) fm.run_detector(det) dead_end: Any = fm.the(DeadEnd) self.assertIsNotNone(dead_end) self.assertEqual(dead_end.for_goal, wa) self.assertEqual(fm.builder_of(dead_end), det)
def test_want_w_forced(self): fm = TestFM() ca = fm.build(SeqCanvas([SeqState((4, 5, 6), None)])) cr0 = fm.build(CellRef(ca, 0)) cr1 = cr0.next_cellref() wa = fm.build(Want(target=15, startcell=cr0, sk=RaiseException(TestFoundIt)), min_a=4.0) # TODO assert that cr1 got build as an Elem in the ws? self.assertEqual(fm.a(wa), 4.0) # The Want should build a Consume expected_co = Consume(plus, (5, 4)) fm.run(wa, force_slipnet_result=[expected_co]) co = fm.the(expected_co) self.assertEqual(co, Consume(plus, (5, 4), source=cr0, dest=cr1)) self.assertEqual(fm.builder_of(co), wa) self.assertIn(co, fm.built_by(wa)) self.assertTrue(fm.can_go(co)) # The Consume should build a LitPainter fm.run(co) expected_value = SeqState((6, 9), ArithDelta((5, 4), 9, plus)) lp = fm.the(LitPainter) self.assertEqual(lp, LitPainter(cr1, expected_value)) self.assertTrue(fm.is_mutual_support(co, lp)) self.assertFalse(fm.can_go(co)) # The LitPainter shouldn't paint yet: activation is too low self.assertFalse(fm.ok_to_paint(lp, cr1)) self.assertFalse(fm.can_go(lp)) # Eventually, the LitPainter should be able to paint fm.propagate_a(num=30) self.assertTrue(fm.ok_to_paint(lp, cr1)) self.assertTrue(fm.can_go(lp)) self.assertEqual(fm.the(cr1), cr1) # When the LitPainter paints, it should be marked Succeeded fm.run(lp) self.assertEqual(fm.agent_state(lp), Succeeded()) self.assertTrue(fm.has_succeeded(lp)) self.assertFalse(fm.can_go(lp)) # The Consume should be marked MustCheckIfSucceeded self.assertIsInstance(fm.agent_state(co), MustCheckIfSucceeded) # Running the Consume should make it see that it has succeeded fm.run(co) self.assertTrue(fm.has_succeeded(co)) self.assertFalse(fm.can_go(co)) self.assertIsInstance(fm.agent_state(wa), MustCheckIfSucceeded) # Running the Want should make it see that it has still not succeeded fm.run(wa) self.assertIsInstance(fm.agent_state(wa), Active) self.assertFalse(fm.has_succeeded(wa))
def test_vma(self) -> None: # Regression test for a bug: When a canvas cell was painted over # after a VariantMakerFromAvails was created using it as a source, # the VariantMakerFromAvails would produce an invalid Consumer # (missing an operand). fm = FARGModel( slipnet=Slipnet(Graph.with_features([VariantMakerFromAvails()]))) ca = fm.build(StepCanvas([Step([4, 5, 6])])) cr0 = CellRef(ca, 0) cr1 = CellRef(ca, 1) cr1.paint(Step([5, 2], StepDelta([6, 4], [2], '-'))) # Consumer(12+5) will snag: 5 is avail but 12 is unavail co1 = fm.build(Consumer(operator=plus, operands=(12, 5), source=cr1)) fm.run_agent(co1, num=2) vma: VariantMakerFromAvails = fm.the( VariantMakerFromAvails) # type: ignore[assignment] assert vma, 'Consumer did not produce a VariantMakerFromAvails' self.assertEqual(vma.agent, co1) # Some other agent paints over cr1, removing the avail 5 cr1.paint(Step([4, 11], StepDelta([6, 5], [11], '+'))) #lenable(Agent, Codelet, Fizzle) fm.run_agent(vma) #ldisable_all() co2: Any = fm.built_by(vma)[0] assert isinstance(co2, Consumer), \ f'built by {short(co2)}, not a Consumer' self.assertEqual(len(as_list(co2.operands)), 2) fm.run_agent(co2) # Check for crash lp: Any = fm.built_by(co2)[0] self.assertIsInstance(lp, LitPainter)
def test_litpainter(self) -> None: fm = FARGModel() ca = fm.build(self.pons_start_canvas()) cr1 = CellRef(ca, 1) lp = fm.build(LitPainter(dest=cr1, value=self.step1)) fm.run_agent(lp) self.assertEqual(ca[1], self.step1) self.assertEqual(fm.agent_state(lp), Succeeded)
def r4_5_6__15(*args, **kwargs): global fm, ca, wa fm = Numbo(*args, **kwargs) ca = fm.build(SeqCanvas([SeqState((4, 5, 6), None)])) wa = fm.build(Want(15, CellRef(ca, 0))) fm.do_timestep(num=19) pr(fm, edges=True) print() #fm.pr_flows() print(f'seed={fm.seed}')
def test_litpainter(self): fm = FARGModel() ca = fm.build(SeqCanvas([SeqState((4, 5, 6), None)])) cr = CellRef(ca, 1) # pointer to 2nd cell on canvas state1 = SeqState((9, 6), ArithDelta((4, 5), 9, plus)) lp = fm.build(LitPainter(cr, state1)) lp.go(fm) # TODO Call fm.go() and let it fill in args? # TODO A threshold for painting self.assertEqual(ca[1], state1) self.assertTrue(fm.is_mutual_support(lp, cr))
def test_values_not_avail(self): fm = FARGModel() ca = fm.build(SeqCanvas([SeqState((4, 5, 6), None)])) cr0 = CellRef(ca, 0) cr1 = CellRef(ca, 1) # This must fail because there is only one 4 avail co = fm.build(Consume(plus, (4, 4), source=cr0, dest=cr1)) self.assertTrue(fm.can_go(co)) fm.run(co) self.assertIsNone(fm.the(LitPainter)) # TODO fm.tags_of() self.assertTrue(fm.is_tagged(co, Blocked)) self.assertTrue(fm.is_blocked(co)) self.assertFalse(fm.can_go(co)) bl = first(e for e in fm.built_by(co) if isinstance(e, Blocked)) self.assertIsNotNone(bl) self.assertEqual(bl.reason.avails, (4, None)) self.assertEqual(bl.reason.unavails, (None, 4)) self.assertGreater(fm.ae_weight(co, bl), 0.0)
def test_add_tag(self) -> None: fm = FARGModel() ca = fm.build(self.pons_start_canvas()) cr0 = fm.build(CellRef(ca, 0)) self.assertFalse(fm.has_tag(cr0, UTTag)) #fm.add_tag(cr0, UTTag()) fm.run_codelet(AddTag(tag=UTTag, taggee=cr0)) self.assertTrue(fm.has_tag(cr0, UTTag)) self.assertTrue(match_wo_none(UTTag(), UTTag())) self.assertTrue(fm.has_node(UTTag()))
def test_buildlitpainter(self) -> None: fm = FARGModel() ca = fm.build(self.pons_start_canvas()) cr1 = fm.build(CellRef(ca, 1)) codelet = BuildLitPainter(dest=cr1, value=self.step1) fm.run_codelet(codelet) lp: LitPainter = fm.the(LitPainter) # type: ignore[assignment] self.assertIsNotNone(lp) fm.run_agent(lp) self.assertEqual(ca[1], self.step1) self.assertEqual(fm.agent_state(lp), Succeeded)
def test_no_duplicate(self): fm = FARGModel() ca = fm.build(SeqCanvas([SeqState((4, 5, 6), None)])) cr = CellRef(ca, 1) # pointer to 2nd cell on canvas state1 = SeqState((9, 6), ArithDelta((4, 5), 9, plus)) lp1a = fm.build(LitPainter(cr, state1)) lp1b = fm.build(LitPainter(cr, state1)) self.assertIs(lp1a, lp1b) state2 = SeqState((10, 5), ArithDelta((4, 6), 10, plus)) lp2 = fm.build(LitPainter(cr, state2)) self.assertIsNot(lp1a, lp2)
def test_timestep1(self) -> None: # Verifies that Want does something on its first two timesteps, # and then sleeps, and then another Agent gets called upon. slipnet = Slipnet(Graph.with_features([ Consumer.make(plus, (4, 5)) ])) for seed in range(1, 3): fm = FARGModel(slipnet=slipnet, seed=seed, paint_threshold=0.0) ca = fm.build(self.pons_start_canvas()) cr0 = CellRef(ca, 0) wa = fm.build(Want( startcell=cr0, target=9, on_success=RaiseException(SolvedPuzzle) )) self.assertEqual(fm.agent_state(wa), Born) self.assertEqual(fm.t, 0) #lenable(Agent, Codelet) #pr(fm) #pr(wa.born) fm.do_timestep() # the Want should build a Detector self.assertEqual(fm.t, 1) #pr(fm.codelets_just_run) self.assertEqual(fm.agent_state(wa), Wake) fm.do_timestep() # the Want should build a Consumer self.assertEqual(fm.t, 2) self.assertEqual(fm.agent_state(wa), Sleeping) co: Any = fm.the(Consumer) self.assertIsInstance(co, Consumer) self.assertCountEqual(fm.nodes(Agent.CanRun), [co]) fm.do_timestep() # the Consumer should build a LitPainter self.assertEqual(fm.t, 3) self.assertTrue(fm.agent_just_ran(co)) self.assertEqual( fm.agent_state(co), Sleeping, f'{short(co)} is {fm.agent_state(co)}, not Sleeping' ) lp: Any = fm.the(LitPainter) self.assertIsInstance(lp, LitPainter) self.assertEqual(fm.builder_of(lp), co) fm.do_timestep() # the LitPainter should Paint self.assertEqual(fm.t, 4) self.assertTrue(fm.agent_just_ran(lp)) self.assertTrue(any(cr.clis(Paint) for cr in fm.codelets_just_run)) self.assertEqual(fm.agent_state(lp), Succeeded)
def test_blocked_makes_want(self): fm = TestFM(seed=1) ca = fm.build(SeqCanvas([SeqState((4, 5, 6), None)])) cr0 = fm.build(CellRef(ca, 0)) cr1 = cr0.next_cellref() co69 = fm.build(Consume(plus, (6, 9), source=cr0, dest=cr1)) fm.run(co69) bl = fm.the(Blocked) # TODO query for built_by co69 assert bl is not None fm.run(bl) wa = fm.the(Want) # TODO query for built_by bl self.assertIsNotNone(wa) self.assertEqual(wa.target, 9) self.assertEqual(wa.startcell, cr0) self.assertEqual(wa.sk, RemoveBlocked(bl)) fm.run(wa, force_slipnet_result=Consume(plus, (4, 5))) co45 = fm.the(Consume(plus, (4, 5))) self.assertEqual(co45, Consume(plus, (4, 5), source=cr0, dest=cr1)) fm.run(co45) lp9 = fm.the(LitPainter) self.assertIsNotNone(lp9) fm.run(lp9) self.assertTrue(fm.has_succeeded(lp9)) self.assertIsInstance(fm.agent_state(co45), MustCheckIfSucceeded) fm.run(co45) self.assertTrue(fm.has_succeeded(co45)) self.assertIsInstance(fm.agent_state(wa), MustCheckIfSucceeded) fm.run(wa) self.assertTrue(fm.has_succeeded(wa)) cr2 = cr1.next_cellref() co69b = fm.the(Consume(plus, (6, 9))) self.assertEqual(co69b.source, cr1) self.assertEqual(co69b.dest, cr2) self.assertFalse(fm.is_blocked(co69b)) self.assertIsNone(fm.the(Blocked)) self.assertIsInstance(fm.agent_state(co69b), Active) # TODO Make sure node for cr[2] gets built fm.run(co69b) lp15 = fm.the(LitPainter(cellref=cr2)) self.assertEqual(lp15.value.avails, (15, )) fm.run(lp15) self.assertTrue(fm.has_succeeded(lp15))
def test_consumer(self) -> None: fm = FARGModel() ca = fm.build(self.pons_start_canvas()) cr0 = CellRef(ca, 0) cr1 = CellRef(ca, 1) ag = fm.build(Consumer( operator=plus, operands=(4, 5), source=cr0, #dest=cr1 )) fm.run_agent(ag) lp: Any = fm.the(LitPainter) fm.run_agent(lp) self.assertEqual(ca[1], self.step1) self.assertEqual(fm.agent_state(lp), Succeeded) self.assertEqual( fm.agent_state(ag), Delegate_succeeded, f"Consumer's state is {fm.agent_state(ag)}, not Delegate_succeeded." )
def test_slipnet_kwargs2(self) -> None: fm = FARGModel() ca = fm.build(self.pons_start_canvas()) cr0 = CellRef(ca, 0) qargs = (QBeforeFromAvails(), QAfter(), SearchFor()) sources = dict(source=cr0, features=15, type=Equation) self.assertEqual( fm.mk_slipnet_args(qargs, sources), dict(activations_in=dict(((Before(4), 1.0), (Before(5), 1.0), (Before(6), 1.0), (After(15), 1.0))), pred=(Equation, ), k=20, num_get=1))
def test_snag_leads_to_variant_maker(self) -> None: #lenable(Codelet, Fizzle, LogPulse, Built) fm = FARGModel(seed=1, slipnet=Slipnet(desnag_graph)) ca = fm.build(self.pons_start_canvas()) cr0 = fm.build(CellRef(ca, 0)) cr1 = fm.build(CellRef(ca, 1)) ag1 = fm.build(Consumer( operator=plus, operands=(4, 4), source=cr0 )) fm.run_agent(ag1, num=2) vm = fm.the(VariantMakerFromAvails) self.assertTrue(fm.the(VariantMakerFromAvails)) self.assertEqual( fm.the(VariantMakerFromAvails), VariantMakerFromAvails( agent=ag1, cellref=cr0, avails=(4, None), unavails=(None, 4) ) ) #lenable(Agent, Codelet, Fizzle, LogPulse, Built) fm.do_timestep(num=3) #pr(fm) #print() #print(short(cr1.value)) self.assertTrue( short(cr1.value) == '[4 + 5; 6 9]' or short(cr1.value) == '[4 + 6; 5 10]' )