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_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 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 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_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)
else: print('Unknown arguments: {}'.format(args)) print( 'Possible arguments: [--task {pvt,vs}] [--agent {uagent,pvt,vs}] [--window {none,window,<host>}]') sys.exit(1) # create window (if needed) if window_name == 'window': window = Window(size=(300, 300), title='Task Window') elif window_name == 'none': window = None else: window = ClientWindow(host=window_name) # create environment env = Environment(window=window) # create task if task_name == 'pvt': task = (PVTTask(env, load_text('tasks/pvt/ace.txt')) if agent_name == 'uagent' else PVTTask(env)) elif task_name == 'vs': task = (VSTask(env, load_text('tasks/vs/ace.txt')) if agent_name == 'uagent' else VSTask(env)) else: print('Unknown task argument: {}'.format(task_name)) sys.exit(1) # 0 = console output, 1 = record log (saves to /data/logs/)
from tasks.pvt import PVTAgent, PVTTask from think import Environment, World if __name__ == '__main__': # env = Environment() env = Environment(window=(500, 500)) task = PVTTask(env) agent = PVTAgent(env) World(task, agent).run(60)
def run_agent(): env = Environment() task = PVTTask(env) agent = UndifferentiatedAgent(env) World(task, agent).run(60)