Exemplo n.º 1
0
def random(evals):
    """Use random search over a CGP representation to solve the XOR function."""
    ea = random_search(
        evals,
        representation=cgp_representation,

        # Our fitness function will be to solve the XOR problem
        problem=xor_problem,
        pipeline=[probe.FitnessStatsCSVProbe(stream=sys.stdout)] +
        cgp_visual_probes(modulo=10))

    list(ea)
Exemplo n.º 2
0
def get_probes(genomes_file, environment, rep):
    """Set up probes for writings results to file and terminal and
    displaying live metric plots."""
    assert (genomes_file is not None)
    assert (environment is not None)
    assert (rep is not None)
    assert (rep in ['neural', 'pitt'])
    num_inputs = int(np.prod(environment.observation_space.shape))
    num_outputs = int(np.prod(environment.action_space.shape))

    probes = []

    # Print fitness stats to stdout
    probes.append(probe.FitnessStatsCSVProbe(context, stream=sys.stdout))

    # Save genome of the best individual to a file
    probes.append(
        probe.AttributesCSVProbe(context,
                                 stream=genomes_file,
                                 best_only=True,
                                 do_fitness=True,
                                 do_genome=True))

    # Open a figure to plot a fitness curve to
    plt.figure()
    plt.ylabel("Fitness")
    plt.xlabel("Generations")
    plt.title("Best-of-Generation Fitness")
    probes.append(
        probe.PopulationPlotProbe(context,
                                  ylim=(0, 1),
                                  xlim=(0, 1),
                                  modulo=1,
                                  ax=plt.gca()))

    # Open a figure to plot a projection of the best Pitt rules to
    if rep == 'pitt':
        plt.figure()
        plt.ylabel("Sensor 1")
        plt.xlabel("Sensor 0")
        plt.title("Rule Coverage")
        probes.append(
            rules.PlotPittRuleProbe(context,
                                    num_inputs,
                                    num_outputs, (0, 1),
                                    ax=plt.gca()))

    return probes
Exemplo n.º 3
0
def do_cgp(gens):
    pop_size = 5

    ea = generational_ea(
        gens,
        pop_size,
        representation=cgp_representation,

        # Our fitness function will be to solve the XOR problem
        problem=xor_problem,
        pipeline=[
            ops.tournament_selection, ops.clone,
            cgp.cgp_mutate(cgp_decoder, expected_num_mutations=1),
            ops.evaluate,
            ops.pool(size=pop_size),
            probe.FitnessStatsCSVProbe(stream=sys.stdout)
        ] + cgp_visual_probes(modulo=10))

    list(ea)
Exemplo n.º 4
0
def build_probes(genomes_file, decoder):
    """Set up probes for writings results to file and terminal and
    displaying live metric plots."""
    assert (genomes_file is not None)

    probes = []

    # Print fitness stats to stdout
    probes.append(probe.FitnessStatsCSVProbe(stream=sys.stdout))

    # Save genome of the best individual to a file
    probes.append(
        probe.AttributesCSVProbe(stream=genomes_file,
                                 best_only=True,
                                 do_fitness=True,
                                 do_genome=True))

    # Open a figure to plot a fitness curve to
    plt.figure()
    plt.ylabel("Fitness")
    plt.xlabel("Generations")
    plt.title("Best-of-Generation Fitness")
    probes.append(
        probe.FitnessPlotProbe(ylim=(0, 1),
                               xlim=(0, 1),
                               modulo=1,
                               ax=plt.gca()))

    # Visualize the first two conditions of every rule
    plt.figure()
    plt.ylabel("Sensor 1")
    plt.xlabel("Sensor 0")
    plt.title("Rule Coverage")
    probes.append(
        rules.PlotPittRuleProbe(decoder,
                                xlim=(-1, 1),
                                ylim=(-1, 1),
                                ax=plt.gca()))

    return probes
Exemplo n.º 5
0
Arquivo: cgp.py Projeto: nicrox89/LEAP
def cgp_cmd(gens):
    """Use an evolutionary CGP approach to solve the XOR function."""
    pop_size = 5

    ea = generational_ea(gens, pop_size, 

            representation=cgp_representation,

            # Our fitness function will be to solve the XOR problem
            problem=xor_problem,

            pipeline=[
                ops.tournament_selection,
                ops.clone,
                cgp.cgp_mutate(cgp_decoder),
                ops.evaluate,
                ops.pool(size=pop_size),
                probe.FitnessStatsCSVProbe(context.context, stream=sys.stdout)
            ] + cgp_visual_probes(modulo=10)
    )

    list(ea)
Exemplo n.º 6
0
def build_probes(genomes_file):
    """Set up probes for writings results to file and terminal and
    displaying live metric plots."""
    assert (genomes_file is not None)

    probes = []

    # Print fitness stats to stdout
    probes.append(probe.FitnessStatsCSVProbe(stream=sys.stdout))

    # Save genome of the best individual to a file
    probes.append(
        probe.AttributesCSVProbe(stream=genomes_file,
                                 best_only=True,
                                 do_fitness=True,
                                 do_genome=True))

    # Open a figure to plot a fitness curve to
    plt.figure()
    plt.ylabel("Fitness")
    plt.xlabel("Generations")
    plt.title("Best-of-Generation Fitness")
    probes.append(
        probe.FitnessPlotProbe(ylim=(0, 1),
                               xlim=(0, 1),
                               modulo=1,
                               ax=plt.gca()))

    # Open a figure to plot the best-of-gen network graph to
    plt.figure()
    probes.append(
        neural_network.GraphPhenotypeProbe(modulo=1,
                                           ax=plt.gca(),
                                           weights=True,
                                           weight_multiplier=3.0))

    return probes
Exemplo n.º 7
0
                             ),

                             # Operator pipeline
                             pipeline=[
                                 ops.tournament_selection(k=2),
                                 ops.clone,
                                 # Apply binomial mutation: this is a lot like
                                 # additive Gaussian mutation, but adds an integer
                                 # value to each gene
                                 mutate_gaussian(std=0.2, hard_bounds=[problem.bounds]*l,
                                                 expected_num_mutations=1),
                                 ops.evaluate,
                                 ops.pool(size=pop_size),

                                 # Some visualization probes so we can watch what happens
                                 probe.CartesianPhenotypePlotProbe(
                                        xlim=problem.bounds,
                                        ylim=problem.bounds,
                                        contours=problem),
                                 probe.FitnessPlotProbe(),

                                 probe.PopulationMetricsPlotProbe(
                                     metrics=[ probe.pairwise_squared_distance_metric ],
                                     title='Population Diversity'),

                                 probe.FitnessStatsCSVProbe(stream=sys.stdout)
                             ]
                        )

    list(ea)
Exemplo n.º 8
0
                             # Representation
                             representation=Representation(
                                 individual_cls=Individual,
                                 initialize=create_real_vector(
                                     bounds=[bounds] * l),
                                 decoder=IdentityDecoder()
                             ),

                             # Operator pipeline
                             shared_pipeline=[
                                 ops.tournament_selection,
                                 ops.clone,
                                 mutate_gaussian(
                                     std=0.03, hard_bounds=bounds),
                                 ops.evaluate,
                                 ops.pool(size=pop_size),
                                 ops.migrate(context,
                                             topology=topology,
                                             emigrant_selector=ops.tournament_selection,
                                             replacement_selector=ops.random_selection,
                                             migration_gap=5,
                                             customs_stamp=problem_stamp(problems)),
                                 probe.FitnessStatsCSVProbe(context, stream=sys.stdout,
                                        extra_columns={ 'island': get_island(context) })
                             ],
                             subpop_pipelines=subpop_probes)

    list(ea)
    plt.show()
Exemplo n.º 9
0
    pop_size = 10
    ea = multi_population_ea(
        max_generations=generations,
        num_populations=topology.number_of_nodes(),
        pop_size=pop_size,
        problem=problem,  # Fitness function

        # Representation
        representation=Representation(
            individual_cls=Individual,
            initialize=create_real_vector(bounds=[problem.bounds] * l)),

        # Operator pipeline
        shared_pipeline=[
            ops.tournament_selection, ops.clone,
            mutate_gaussian(std=30,
                            expected_num_mutations=1,
                            hard_bounds=problem.bounds), ops.evaluate,
            ops.pool(size=pop_size),
            ops.migrate(topology=topology,
                        emigrant_selector=ops.tournament_selection,
                        replacement_selector=ops.random_selection,
                        migration_gap=50),
            probe.FitnessStatsCSVProbe(
                stream=sys.stdout,
                extra_metrics={'island': get_island(context)})
        ],
        subpop_pipelines=subpop_probes)

    list(ea)
Exemplo n.º 10
0
                             # Operator pipeline
                             pipeline=[
                                 ops.tournament_selection(k=2),
                                 ops.clone,

                                 # Apply Gaussian mutation
                                 mutate_gaussian(std=1.5, hard_bounds=[problem.bounds]*l,
                                                 expected_num_mutations=1),
                                 ops.evaluate,
                                 ops.pool(size=pop_size),

                                 # Some visualization probes so we can watch what happens
                                 probe.CartesianPhenotypePlotProbe(
                                        xlim=problem.bounds,
                                        ylim=problem.bounds,
                                        contours=problem),
                                 probe.FitnessPlotProbe(),

                                 # Collect diversity metrics along with the standard CSV columns
                                 probe.FitnessStatsCSVProbe(stream=sys.stdout,
                                    extra_metrics={
                                        'diversity_pairwise_dist': probe.pairwise_squared_distance_metric,
                                        'diversity_sum_variance': probe.sum_of_variances_metric,
                                        'diversity_num_fixated': probe.num_fixated_metric
                                        })
                             ]
                        )

    list(ea)