class SampleMemorizingLearner(BaseLearner): test_mode = False def __init__(self): self.memory = '' self.teacher_stopped_talking = False # the learner has the serialization hardcoded to # detect spaces self.serializer = StandardSerializer() self.silence_code = self.serializer.to_binary(' ') self.silence_i = 0 def reward(self, reward): # YEAH! Reward!!! Whatever... # Now this robotic teacher is going to mumble things again self.teacher_stopped_talking = False self.silence_i = 0 self.memory = '' def next(self, input): # If we have received a silence byte text_input = self.serializer.to_text(self.memory) if text_input and text_input[-2:] == ' ': self.teacher_stopped_talking = True if self.teacher_stopped_talking: # send the memorized sequence output, self.memory = self.memory[0], self.memory[1:] else: output = self.silence_code[self.silence_i] self.silence_i = (self.silence_i + 1) % len(self.silence_code) # memorize what the teacher said self.memory += input return output
class SampleMemorizingLearner(BaseLearner): def __init__(self): self.memory = '' self.teacher_stopped_talking = False # the learner has the serialization hardcoded to # detect spaces self.serializer = StandardSerializer() self.silence_code = self.serializer.to_binary(' ') self.silence_i = 0 def reward(self, reward): # YEAH! Reward!!! Whatever... # Now this robotic teacher is going to mumble things again self.teacher_stopped_talking = False self.silence_i = 0 self.memory = '' def next(self, input): # If we have received a silence byte text_input = self.serializer.to_text(self.memory) if text_input and text_input[-2:] == ' ': self.teacher_stopped_talking = True if self.teacher_stopped_talking: # send the memorized sequence output, self.memory = self.memory[0], self.memory[1:] else: output = self.silence_code[self.silence_i] self.silence_i = (self.silence_i + 1) % len(self.silence_code) # memorize what the teacher said self.memory += input return output
def __init__(self): self.memory = '' self.teacher_stopped_talking = False # the learner has the serialization hardcoded to # detect spaces self.serializer = StandardSerializer() self.silence_code = self.serializer.to_binary(' ') self.silence_i = 0
class SampleSilentLearner(BaseLearner): def __init__(self): self.serializer = StandardSerializer() self.silence_code = self.serializer.to_binary(' ') self.silence_i = 0 def reward(self, reward): # YEAH! Reward!!! Whatever... self.silence_i = 0 def next(self, input): output = self.silence_code[self.silence_i] self.silence_i = (self.silence_i + 1) % len(self.silence_code) return output
def process_world(conn, opt, tasks_config_file, world_id): try: serializer = StandardSerializer() task_scheduler = create_tasks_from_config(tasks_config_file) env = Environment(serializer, task_scheduler, opt.scramble, opt.max_reward_per_task, not opt.bit_mode) learner = create_learner(opt.learner, serializer, opt.learner_cmd, opt.learner_port, not opt.bit_mode) session = Session(env, learner, opt.time_delay) args = conn.recv() while not (args is None): episode_id, step_count, seed, weight = args # INTERACTION BETWEEN ENVIRONMENT AND AGENT learner.net.set_genotype_weight(weight, seed) del weight episode_reward = session.iterate_n(step_count) # save_results(session, opt.output) conn.send((episode_reward, seed)) args = conn.recv() except BaseException as e: print(e) conn.send(None)
def main(): setup_logging() op = OptionParser("Usage: %prog [options] " "(tasks_config.json | tasks_config.py)") op.add_option('-o', '--output', default='results.out', help='File where the simulation results are saved.') op.add_option('--scramble', action='store_true', default=False, help='Randomly scramble the words in the tasks for ' 'a human player.') op.add_option('-w', '--show-world', action='store_true', default=False, help='shows a visualization of the world in the console ' '(mainly for debugging)') op.add_option('-d', '--time-delay', default=0, type=float, help='adds some delay between each timestep for easier' ' visualization.') op.add_option('-l', '--learner', default='learners.human_learner.HumanLearner', help='Defines the type of learner.') op.add_option('-v', '--view', default='BaseView', help='Viewing mode.') op.add_option('--learner-cmd', help='The cmd to run to launch RemoteLearner.') op.add_option('--learner-port', default=5556, type=int, help='Port on which to accept remote learner.') op.add_option('--learner-address', help='Network address on which the remote learner listens.') op.add_option('--max-reward-per-task', default=2147483647, type=int, help='Maximum reward that we can give to a learner for' ' a given task.') op.add_option('--curses', action='store_true', default=False, help='Uses standard output instead of curses library.') op.add_option('--bit-mode', action='store_true', default=False, help='Environment receives input in bytes.') opt, args = op.parse_args() if len(args) == 0: op.error("Tasks schedule configuration file required.") # retrieve the task configuration file tasks_config_file = args[0] logger = logging.getLogger(__name__) logger.info("Starting new evaluation session") # we choose how the environment will produce and interpret # the bit signal serializer = StandardSerializer() # create a learner (the human learner takes the serializer) learner = create_learner(opt.learner, serializer, opt.learner_cmd, opt.learner_port, opt.learner_address, not opt.bit_mode) # create our tasks and put them into a scheduler to serve them task_scheduler = create_tasks_from_config(tasks_config_file) # construct an environment env = Environment(serializer, task_scheduler, opt.scramble, opt.max_reward_per_task, not opt.bit_mode) # a learning session session = Session(env, learner, opt.time_delay) # setup view view = create_view(opt.view, opt.learner, env, session, serializer, opt.show_world, opt.curses, not opt.bit_mode) try: # send the interface to the human learner learner.set_view(view) except AttributeError: # this was not a human learner, nothing to do pass try: view.initialize() # ok guys, talk session.run() except BaseException: view.finalize() save_results(session, opt.output) raise else: view.finalize()
def __init__(self): self.serializer = StandardSerializer() self.silence_code = self.serializer.to_binary(' ') self.silence_i = 0