def run(): # Determine path to configuration file. local_dir = os.path.dirname(__file__) config_path = os.path.join(local_dir, 'novelty_config') # Note that we provide the custom stagnation class to the Config constructor. config = neatfast.Config(neatfast.DefaultGenome, neatfast.DefaultReproduction, neatfast.DefaultSpeciesSet, neatfast.DefaultStagnation, config_path) ne = NoveltyEvaluator(4, 'color') if ne.scheme == 'color': config.output_nodes = 3 else: config.output_nodes = 1 pop = neatfast.Population(config) # Add a stdout reporter to show progress in the terminal. pop.add_reporter(neatfast.StdOutReporter(True)) stats = neatfast.StatisticsReporter() pop.add_reporter(stats) while 1: pop.run(ne.evaluate, 1) winner = stats.best_genome() if ne.scheme == 'gray': image = eval_gray_image(winner, config, full_scale * width, full_scale * height) elif ne.scheme == 'color': image = eval_color_image(winner, config, full_scale * width, full_scale * height) elif ne.scheme == 'mono': image = eval_mono_image(winner, config, full_scale * width, full_scale * height) else: raise Exception('Unexpected scheme: {0!r}'.format(ne.scheme)) im = np.clip(np.array(image), 0, 255).astype(np.uint8) im = ne.image_from_array(im) im.save('winning-novelty-{0:06d}.png'.format(pop.generation)) if ne.scheme == 'gray': image = eval_gray_image(winner, config, width, height) elif ne.scheme == 'color': image = eval_color_image(winner, config, width, height) elif ne.scheme == 'mono': image = eval_mono_image(winner, config, width, height) else: raise Exception('Unexpected scheme: {0!r}'.format(ne.scheme)) float_image = np.array(image, dtype=np.float32) / 255.0 ne.archive.append(float_image)
def make_high_resolution(self, genome, config): genome_id, genome = genome # Make sure the output directory exists. if not os.path.isdir('rendered'): os.mkdir('rendered') if self.scheme == 'gray': image_data = eval_gray_image(genome, config, self.full_width, self.full_height) elif self.scheme == 'color': image_data = eval_color_image(genome, config, self.full_width, self.full_height) else: image_data = eval_mono_image(genome, config, self.full_width, self.full_height) image = self.make_image_from_data(image_data) pygame.image.save( image, "rendered/rendered-{}-{}.png".format(os.getpid(), genome_id)) with open("rendered/genome-{}-{}.bin".format(os.getpid(), genome_id), "wb") as f: pickle.dump(genome, f, 2)
def evaluate_lowres(genome, config, scheme): if scheme == 'gray': return eval_gray_image(genome, config, width, height) elif scheme == 'color': return eval_color_image(genome, config, width, height) elif scheme == 'mono': return eval_mono_image(genome, config, width, height) raise Exception('Unexpected scheme: {0!r}'.format(scheme))
def evaluate(genome, scheme): if scheme == 'gray': return eval_gray_image(genome, width, height) elif scheme == 'color': return eval_color_image(genome, width, height) elif scheme == 'mono': return eval_mono_image(genome, width, height) raise Exception('Unexpected scheme: {0!r}'.format(scheme))
def run(): cfg = config.Config('novelty_config') cfg.pop_size = 100 cfg.max_fitness_threshold = 1e38 ne = NoveltyEvaluator(4, 'gray') if ne.scheme == 'color': cfg.output_nodes = 3 else: cfg.output_nodes = 1 pop = population.Population(cfg) while 1: pop.run(ne.evaluate, 1) winner = pop.statistics.best_genome() if ne.scheme == 'gray': image = eval_gray_image(winner, full_scale * width, full_scale * height) elif ne.scheme == 'color': image = eval_color_image(winner, full_scale * width, full_scale * height) elif ne.scheme == 'mono': image = eval_mono_image(winner, full_scale * width, full_scale * height) else: raise Exception('Unexpected scheme: {0!r}'.format(ne.scheme)) im = np.clip(np.array(image), 0, 255).astype(np.uint8) im = ne.image_from_array(im) im.save('winning-novelty-{0:06d}.png'.format(pop.generation)) if ne.scheme == 'gray': image = eval_gray_image(winner, width, height) elif ne.scheme == 'color': image = eval_color_image(winner, width, height) elif ne.scheme == 'mono': image = eval_mono_image(winner, width, height) else: raise Exception('Unexpected scheme: {0!r}'.format(ne.scheme)) float_image = np.array(image, dtype=np.float32) / 255.0 ne.archive.append(float_image)
def run(): cfg = config.Config('config') cfg.pop_size = 100 cfg.max_fitness_threshold = 1e38 ne = NoveltyEvaluator(4, 'gray') if ne.scheme == 'color': cfg.output_nodes = 3 else: cfg.output_nodes = 1 pop = population.Population(cfg) while 1: pop.run(ne.evaluate, 1) winner = pop.statistics.best_genome() if ne.scheme == 'gray': image = eval_gray_image(winner, full_scale * width, full_scale * height) elif ne.scheme == 'color': image = eval_color_image(winner, full_scale * width, full_scale * height) elif ne.scheme == 'mono': image = eval_mono_image(winner, full_scale * width, full_scale * height) else: raise Exception('Unexpected scheme: {0!r}'.format(ne.scheme)) im = np.clip(np.array(image), 0, 255).astype(np.uint8) im = ne.image_from_array(im) im.save('winning-novelty-{0:06d}.png'.format(pop.generation)) if ne.scheme == 'gray': image = eval_gray_image(winner, width, height) elif ne.scheme == 'color': image = eval_color_image(winner, width, height) elif ne.scheme == 'mono': image = eval_mono_image(winner, width, height) else: raise Exception('Unexpected scheme: {0!r}'.format(ne.scheme)) float_image = np.array(image, dtype=np.float32) / 255.0 ne.archive.append(float_image)
def make_high_resolution(self, genome): # Make sure the output directory exists. if not os.path.isdir('rendered'): os.mkdir('rendered') if self.scheme == 'gray': image_data = eval_gray_image(genome, self.full_width, self.full_height) elif self.scheme == 'color': image_data = eval_color_image(genome, self.full_width, self.full_height) else: image_data = eval_mono_image(genome, self.full_width, self.full_height) image = self.make_image_from_data(image_data) pygame.image.save(image, "rendered/rendered-{}-{}.png".format(os.getpid(), genome.ID)) with open("rendered/genome-{}-{}.bin".format(os.getpid(), genome.ID), "wb") as f: pickle.dump(genome, f, 2)
def evaluate(self, genomes, config): jobs = [] for genome_id, genome in genomes: jobs.append( self.pool.apply_async(evaluate_lowres, (genome, config, self.scheme))) new_archive_entries = [] for (genome_id, genome), j in zip(genomes, jobs): image = np.clip(np.array(j.get()), 0, 255).astype(np.uint8) float_image = image.astype(np.float32) / 255.0 genome.fitness = (width * height)**0.5 for a in self.archive: adist = np.linalg.norm(float_image.ravel() - a.ravel()) genome.fitness = min(genome.fitness, adist) if random.random() < 0.02: new_archive_entries.append(float_image) #im = self.image_from_array(image) #im.save("novelty-{0:06d}.png".format(self.out_index)) if self.scheme == 'gray': image = eval_gray_image(genome, config, full_scale * width, full_scale * height) elif self.scheme == 'color': image = eval_color_image(genome, config, full_scale * width, full_scale * height) elif self.scheme == 'mono': image = eval_mono_image(genome, config, full_scale * width, full_scale * height) else: raise Exception('Unexpected scheme: {0!r}'.format( self.scheme)) im = np.clip(np.array(image), 0, 255).astype(np.uint8) im = self.image_from_array(im) im.save('novelty-{0:06d}.png'.format(self.out_index)) self.out_index += 1 self.archive.extend(new_archive_entries) print('{0} archive entries'.format(len(self.archive)))
def evaluate(self, genomes): jobs = [] for genome in genomes: jobs.append(self.pool.apply_async(evaluate, (genome, self.scheme))) new_archive_entries = [] for g, j in zip(genomes, jobs): image = np.clip(np.array(j.get()), 0, 255).astype(np.uint8) float_image = image.astype(np.float32) / 255.0 g.fitness = (width * height) ** 0.5 for a in self.archive: adist = np.linalg.norm(float_image.ravel() - a.ravel()) g.fitness = min(g.fitness, adist) if random.random() < 0.02: new_archive_entries.append(float_image) #im = self.image_from_array(image) #im.save("novelty-{0:06d}.png".format(self.out_index)) if self.scheme == 'gray': image = eval_gray_image(g, full_scale * width, full_scale * height) elif self.scheme == 'color': image = eval_color_image(g, full_scale * width, full_scale * height) elif self.scheme == 'mono': image = eval_mono_image(g, full_scale * width, full_scale * height) else: raise Exception('Unexpected scheme: {0!r}'.format(self.scheme)) im = np.clip(np.array(image), 0, 255).astype(np.uint8) im = self.image_from_array(im) im.save('novelty-{0:06d}.png'.format(self.out_index)) self.out_index += 1 self.archive.extend(new_archive_entries) print('{0} archive entries'.format(len(self.archive)))