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_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_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 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()
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)