def elite_architecture(pop: Population,
                       show: bool = False,
                       del_cache: bool = True):
    """
    Visualize each architectural change in the population's elites.
    
    :param pop: Population object
    :param show: Show the result
    :param del_cache: Remove intermediate architecture-results when images are created
    """
    # Initialize the architecture-list with the first genome
    distance = GenomeDistanceCache(config=pop.config.genome)
    new_architectures = [(0, pop.best_genome_hist[0][1])
                         ]  # Only interested in genome itself
    for gen in range(1, pop.generation):
        gen_genome = pop.best_genome_hist[gen][1]
        if distance(
                gen_genome, new_architectures[-1]
            [1]) > 2:  # Take only the more significant changes into account
            new_architectures.append((gen, gen_genome))
    new_architectures.append(
        (pop.generation - 1, pop.best_genome_hist[pop.generation - 1][1]))

    # Create the architectures of the unique genomes
    for _, g in new_architectures:
        pop.visualize_genome(
            debug=False,  # Keep the networks simple
            genome=g,
            show=False,
        )

    # Combine in one figure
    hor = min(len(new_architectures), 5)
    vert = max((len(new_architectures) - 1) // 5 + 1, 1)
    plt.figure(figsize=(5 * hor, 5 * vert))
    plt.tight_layout()
    f_images = get_subfolder(
        f"population{'_backup' if pop.use_backup else ''}/storage/{pop.folder_name}/{pop}/",
        "images")
    f_architectures = get_subfolder(f_images, "architectures")
    for i, (gen, g) in enumerate(new_architectures):
        plt.subplot(vert, hor, i + 1)
        img = mpimg.imread(f'{f_architectures}genome_{g.key}.png')
        plt.imshow(img)
        plt.title(f'Generation {gen}')
        plt.axis('off')

    # Save the result
    f_elites = get_subfolder(f_images, "elites")
    plt.savefig(f'{f_elites}/architecture_timeline.png', bbox_inches='tight')
    if show:
        plt.show()
    plt.close()

    if del_cache:
        for (_, g) in new_architectures:
            path = f'{f_architectures}genome_{g.key}.png'
            if os.path.exists(path): os.remove(path)
Esempio n. 2
0
def visualize_genome(
    population: Population,
    genome: Genome,
    debug: bool = True,
    show: bool = False,
):
    """Visualize the requested genome."""
    print("\n===> VISUALIZING GENOME <===\n")
    print(f"Genome {genome.key} with size: {genome.size()}")

    population.visualize_genome(
        debug=debug,
        genome=genome,
        show=show,
    )
Esempio n. 3
0
def specie_representatives(pop: Population,
                           show: bool = True,
                           del_cache: bool = True):
    """Show for each of the current species their representative's architecture."""
    species = pop.species.species
    elite_id = dict()
    for sid, s in sorted(species.items()):
        elite_id[sid] = s.representative.key
        pop.visualize_genome(
            debug=False,  # Keep the networks simple
            genome=s.representative,
            show=False,
        )

    hor = min(len(elite_id), 5)
    vert = max(len(elite_id) // 5 + 1, 1)
    plt.figure(figsize=(5 * hor, 5 * vert))
    plt.tight_layout()
    path = get_subfolder(
        f"population{'_backup' if pop.use_backup else ''}/storage/{pop.folder_name}/{pop}/",
        "images")
    path_architectures = get_subfolder(path, "architectures")
    for i, (sid, eid) in enumerate(elite_id.items()):
        plt.subplot(vert, hor, i + 1)
        img = mpimg.imread(f'{path_architectures}genome_{eid}.png')
        plt.imshow(img)
        plt.title(f'Specie {sid}')
        plt.axis('off')

    # Save the result
    path_species = get_subfolder(path, 'species')
    plt.savefig(f'{path_species}representatives_gen{pop.generation}.png',
                bbox_inches='tight')
    if show:
        plt.show()
    plt.close()

    if del_cache:
        for eid in elite_id.values():
            path = f'{path_architectures}genome_{eid}.png'
            if os.path.exists(path): os.remove(path)