def test_speech(self, output=False): agent = Agent(output=output) speech = Speech(agent) speech.say("Hello I am the speech module") speech.subvocalize("Goodbye all") agent.wait_for_all() self.assertAlmostEqual(agent.time(), 3.650, 2)
def test_language(self, output=False): agent = Agent(output=output) language = Language(agent) def interpreter(words): if len(words) == 2: return Item(isa='action', verb=words[0], object=words[1]) else: return Item(isa=words[0]) language.add_interpreter(interpreter) goal = language.interpret('to read') self.assertEqual('goal', goal.isa) self.assertEqual('read', goal.name) action = language.interpret('read a') self.assertEqual('action', action.isa) self.assertEqual('read', action.verb) self.assertEqual('a', action.object) done = language.interpret('done') self.assertEqual('done', done.isa) agent.wait_for_all()
def test_typing(self, output=False): agent = Agent(output=output) env = Environment() vision = Vision(agent, env.display) motor = Motor(agent, vision, env) motor.type('Hello there. What\'s up?') agent.wait_for_all() self.assertAlmostEqual(6.597, agent.time(), 1)
def run_trial(self, sentence): agent = Agent(output=False) eyes = Eyes(agent) vision = Vision(agent, eyes) tracker = EyeTracker(eyes) self.add_visuals(sentence, vision) self.read_sentence(agent, vision) agent.wait_for_all() self.analyze_trial(sentence, tracker)
def _test_decay(self, decay, prob, time, output): agent = Agent(output=output) memory = Memory(agent, decay) chunk = Chunk(isa='cat', name='Whiskers', owner='Jane') memory.store(chunk) agent.wait(.5) memory.rehearse(chunk) agent.wait(2.5) memory.rehearse(chunk) agent.wait(3.7) memory.rehearse(chunk) agent.wait(6.2) memory.activation_noise = 0.5 if prob: self.assertAlmostEqual(prob, memory.compute_prob_recall(chunk), 2) self.assertAlmostEqual(memory.compute_recall_time(chunk), time, 2)
def test_num_to_text(self): agent = Agent() arithmetic = Arithmetic(agent, Memory(agent), Speech(agent)) errors = 0 for pair in NumbersTest.TEST_PAIRS: if arithmetic.num_to_text(pair[0]) != pair[1]: errors += 1 self.assertEqual(errors, 0)
def test_audition(self, output=False): agent = Agent(output=output) speakers = Environment().speakers audition = Audition(agent, speakers) word = 'Hello' speakers.add('word', word) aural = audition.listen(isa='word') word2 = audition.encode(aural) self.assertEqual(word, word2) text = 'Looks like this is working' speakers.add_speech(text) word = audition.listen_and_encode(isa='word') heard = [] while word is not None: heard.append(word) word = audition.listen_and_encode(isa='word') self.assertEqual(text, ' '.join(heard)) agent.wait_for_all()
def test_mouse(self, output=False): agent = Agent(output=output) vision = Vision(agent) hands = Hands(agent) mouse = Mouse(hands, vision) self.button = None end = 20.0 def update(): if agent.time() < end: def fn(): vision.clear() agent.wait(1.0) self.button = Visual(random.randint(0, 500), random.randint(0, 500), 30, 30, 'button') vision.add(self.button, "X") agent.run_thread(fn) update() def fn(visual): if visual.equals(self.button): update() mouse.add_click_fn(fn) while agent.time() < end: visual = vision.wait_for(isa='button') mouse.point_and_click(visual) agent.wait_for_all() self.assertGreaterEqual(agent.time(), end)
def test_mouse(self, output=False): agent = Agent(output=output) env = Environment() vision = Vision(agent, env.display) motor = Motor(agent, vision, env) self.button = None end = 20.0 def update(): if agent.time() < end: def fn(): vision.clear() agent.wait(1.0) self.button = env.display.add_button( random.randint(0, 500), random.randint(0, 500), 'X') agent.run_thread(fn) update() def fn(visual): if visual.obj == 'X': update() env.mouse.add_click_fn(fn) while agent.time() < end: visual = vision.wait_for(isa='button') motor.point_and_click(visual) agent.wait_for_all() self.assertGreaterEqual(agent.time(), end)
def test_instruction_read(self, output=False): agent = Agent(output=output) memory = Memory(agent) env = Environment() vision = Vision(agent, env.display) audition = Audition(agent, env.speakers) def interpreter(words): if words[0] == 'read': sem = Item(isa='action', type='read', object=words[1]) pointer = vision.find(isa='pointer') if pointer is not None: vision.encode(pointer) sem.set('x', pointer.x).set('y', pointer.y) return sem elif words[0] == 'done': return Item(isa='done') else: return Item(isa='action', type=words[0], object=words[1]) language = Language(agent) language.add_interpreter(interpreter) def executor(action, context): query = Query(x=action.x, y=action.y) context.set(action.object, vision.find_and_encode(query)) instruction = Instruction(agent, memory, audition, language) instruction.add_executor(executor) equation = ['3', 'x', '/', '12', '=', '15', '/', '4'] for i in range(0, len(equation)): env.display.add_text(50 + 50 * i, 50, equation[i]) pointer = env.display.add(50, 50, 1, 1, 'pointer', 'pointer') speech = [ 'to solve', ['read a', (50, 50)], ['read A', (300, 50)], 'done' ] def thread(): for line in speech: agent.wait(3.0) if isinstance(line, str): audition.add(Aural(isa='speech'), line) else: audition.add(Aural(isa='speech'), line[0]) loc = line[1] pointer.move(loc[0], loc[1]) agent.run_thread(thread) goal = instruction.listen_and_learn() self.assertEqual('solve', goal) context = instruction.execute(goal) self.assertEqual('3', context.a) self.assertEqual('15', context.A) agent.wait_for_all()
def test_arithmetic(self): agent = Agent(output=False) arithmetic = Arithmetic(agent, Memory(agent), Speech(agent)) arithmetic.count_to(5) self.assertEqual(0, arithmetic.add(0, 0)) self.assertEqual(5, arithmetic.add(2, 3)) self.assertEqual(17, arithmetic.add(8, 9)) self.assertEqual(0, arithmetic.multiply(0, 0)) self.assertEqual(0, arithmetic.multiply(7, 0)) self.assertEqual(12, arithmetic.multiply(3, 4)) self.assertEqual(121, arithmetic.multiply(11, 11))
def test_audition(self, output=False): agent = Agent(output=output) audition = Audition(agent) word = 'Hello' aural = Aural('word') audition.add(aural, word) aural2 = audition.listen(isa='word') self.assertEqual(aural, aural2) word2 = audition.encode(aural2) self.assertEqual(word, word2) text = 'Looks like this is working' audition.add_speech(text) word = audition.listen_and_encode(isa='word') heard = [] while word is not None: heard.append(word) word = audition.listen_and_encode(isa='word') self.assertEqual(text, ' '.join(heard)) agent.wait_for_all()
def test_memory(self): agent = Agent(output=False) memory = Memory(agent) memory.add(Chunk(isa='cat', name='Whiskers', owner='Jane')) chunk = memory.recall(Query(isa='cat')) self.assertEqual('Jane', chunk.get('owner')) self.assertEqual('Whiskers', chunk.id) memory.add(isa='dog', name='Spot', owner='John') self.assertEqual('Spot', memory.recall(isa='dog').name) self.assertEqual('Whiskers', memory.recall( Query().ne('isa', 'dog')).name) chunk = Chunk(isa='cat', name='Whiskers', owner='Jen') self.assertEqual('Whiskers', chunk.id) memory.add(chunk) self.assertEqual('Whiskers~2', chunk.id) self.assertEqual(chunk, memory.get(chunk.id))
def test_vision(self, output=False): agent = Agent(output=output) eyes = Eyes(agent) vision = Vision(agent, eyes) eyes.move_to(100, 100) vision.add(Visual(50, 50, 20, 20, 'text'), "Hello") vision.add(Visual(150, 150, 20, 20, 'text'), "Goodbye") self.assertEqual("Hello", vision.find_and_encode( Query(isa='text').lt('x', 100))) self.assertEqual("Goodbye", vision.find_and_encode(seen=False)) vision.start_wait_for(isa='cross') agent.wait(2.0) vision.add(Visual(200, 200, 20, 20, 'cross'), "cross") self.assertEqual("cross", vision.encode(vision.get_found())) self.assertAlmostEqual(2.7, agent.time(), 1) agent.wait_for_all()
def test_vision(self, output=False): agent = Agent(output=output) display = Environment().display eyes = Eyes(agent) vision = Vision(agent, display, eyes) eyes.move_to(100, 100) display.add_text(50, 50, 'Hello') display.add_text(150, 150, 'Goodbye') self.assertEqual( "Hello", vision.find_and_encode(Query(isa='text').lt('x', 100))) self.assertEqual("Goodbye", vision.find_and_encode(seen=False)) vision.start_wait_for(isa='cross') agent.wait(2.0) display.add(200, 200, 20, 20, 'cross', "cross") self.assertEqual("cross", vision.encode(vision.get_found())) self.assertAlmostEqual(2.7, agent.time(), 1) agent.wait_for_all()
def run_trial(self): agent = Agent(output=False) memory = Memory(agent, Memory.OPTIMIZED_DECAY) memory.decay_rate = .5 memory.activation_noise = .5 memory.retrieval_threshold = -1.8 memory.latency_factor = .450 vision = Vision(agent) typing = Typing(Hands(agent)) self.trial_start = 0 self.block_index = 0 def fn(): for i in range(PairedAssociatesTest.N_BLOCKS): self.block_index = i pairs = PairedAssociatesTest.PAIRS.copy() random.shuffle(pairs) for pair in pairs: self.trial_start = agent.time() vision.clear().add(Visual(50, 50, 20, 20, 'word'), pair[0]) agent.wait(5.0) vision.clear().add( Visual(50, 50, 20, 20, 'digit'), pair[1]) agent.wait(5.0) agent.run_thread(fn) def type_fn(c): self.rt.add(self.block_index, agent.time() - self.trial_start) typing.add_type_fn(type_fn) for i in range(PairedAssociatesTest.N_BLOCKS): for _ in range(len(PairedAssociatesTest.PAIRS)): word = vision.encode(vision.wait_for(isa='word')) chunk = memory.recall(word=word) if chunk: typing.type(chunk.get('digit')) self.correct.add(i, 1) else: self.correct.add(i, 0) digit = vision.encode(vision.wait_for(isa='digit')) memory.store(word=word, digit=digit) agent.wait_for_all()
return self._num_to_text_help(n, 100, "hundred") elif n < 1000000: return self._num_to_text_help(n, 1000, "thousand") else: return self._num_to_text_help(n, 1000000, "million") def count_to(self, n): count = 0 while count < n: count = self.memory.recall(isa='count', i=count).next self.speech.subvocalize(count) def next(self, i): return self.memory.recall(isa='count', i=i).next def add(self, i, j): return self.memory.recall(isa='sum', i=i, j=j).sum def multiply(self, i, j): return self.memory.recall(isa='product', i=i, j=j).product if __name__ == "__main__": agent = Agent(output=True) memory = Memory(agent) speech = Speech(agent) arithmetic = Arithmetic(agent, memory, speech) arithmetic.count_to(5) speech.say(arithmetic.multiply(8, 11)) agent.wait_for_all()
def test_agent(self, output=False): agent = Agent(output=output) agent.wait(10.0) self.assertAlmostEqual(10.0, agent.time(), 2) agent.run_thread(lambda: agent.wait(5.0)) agent.wait(2.0) self.assertAlmostEqual(12.0, agent.time(), 2) agent.wait_for_all() self.assertAlmostEqual(15.0, agent.time(), 2)
def test_threads(self, output=False): agent = Agent(output=output) memory = Memory(agent) memory.add(isa='item') memory.add(isa='digit', value=1) memory.add(isa='number', value='three') speech = Speech(agent) def thread2(): for _ in range(2): number = memory.recall(isa='number') speech.say(number.value) agent.run_thread(thread2) agent.wait(.100) def thread3(): for _ in range(2): memory.recall('digit') agent.run_thread(thread3) for _ in range(2): memory.recall('item') agent.wait_for_all() self.assertAlmostEqual(1.200, agent.time(), 2)
def test_instruction_type(self, output=False): agent = Agent(output=output) env = Environment() memory = Memory(agent) vision = Vision(agent, env.display) audition = Audition(agent, env.speakers) motor = Motor(agent, vision, env) def interpreter(words): if words[0] == 'read': sem = Item(isa='action', type='read', object=words[1]) pointer = vision.find(isa='pointer') if pointer is not None: vision.encode(pointer) sem.set('x', pointer.x).set('y', pointer.y) return sem elif words[0] == 'done': return Item(isa='done') else: return Item(isa='action', type=words[0], object=words[1]) language = Language(agent) language.add_interpreter(interpreter) def executor(action, context): if action.type == 'read': query = Query(x=action.x, y=action.y) context.set(action.object, vision.find_and_encode(query)) elif action.type == 'type': motor.type(context.get(action.object)) instruction = Instruction(agent, memory, audition, language) instruction.add_executor(executor) typed = [] def type_handler(key): typed.append(key) env.keyboard.add_type_fn(type_handler) env.display.add_text(50, 50, 'a') pointer = env.display.add(50, 50, 1, 1, 'pointer', 'pointer') speech = ['to type', ['read letter', (50, 50)], 'type letter', 'done'] def thread(): for line in speech: agent.wait(3.0) if isinstance(line, str): audition.add(Aural(isa='speech'), line) else: audition.add(Aural(isa='speech'), line[0]) loc = line[1] pointer.move(loc[0], loc[1]) agent.run_thread(thread) goal = instruction.listen_and_learn() self.assertEqual('type', goal) context = instruction.execute(goal) self.assertEqual('a', context.letter) agent.wait_for_all() self.assertEqual(['a'], typed)
def test_typing(self, output=False): agent = Agent(output=output) typing = Typing(Hands(agent)) typing.type("Hello there. What's up?") agent.wait_for_all() self.assertAlmostEqual(6.597, agent.time(), 1)
def test_timing(self, output=False): agent = Agent(output=output) typing = Typing(Hands(agent)) self.assertAlmostEqual(6.597, typing.typing_time("Hello there. What's up?"), 1)