Exemplo n.º 1
0
        output_space=environment.action_space,
        priority_metric=rules.PittRulesExecutable.PriorityMetric.RULE_ORDER)

    with open('./genomes.csv', 'w') as genomes_file:

        ea = generational_ea(
            max_generations=generations,
            pop_size=pop_size,
            # Solve a problem that executes agents in the
            # environment and obtains fitness from it
            problem=problems.EnvironmentProblem(runs_per_fitness_eval,
                                                simulation_steps,
                                                environment,
                                                'reward',
                                                gui=gui),
            representation=Representation(
                initialize=decoder.initializer(num_rules), decoder=decoder),

            # The operator pipeline.
            pipeline=[
                ops.tournament_selection,
                ops.clone,
                decoder.mutator(condition_mutator=genome_mutate_gaussian(
                    std=mutate_std,
                    hard_bounds=decoder.condition_bounds,
                    expected_num_mutations=1 / num_rules),
                                action_mutator=individual_mutate_randint(
                                    bounds=decoder.action_bounds,
                                    probability=1.0)),
                ops.evaluate,
                ops.pool(size=pop_size),
                *build_probes(genomes_file,
Exemplo n.º 2
0
        # specific infinite max_generations
        generations = float('inf')
    
    l = 2
    pop_size = 10
    ea = generational_ea(max_generations=generations,pop_size=pop_size,
                             problem=problem,  # Fitness function

                             # Stopping condition: we stop when fitness or diversity drops below a threshold
                             stop=lambda pop: (max(pop).fitness < fitness_threshold)
                                              or (probe.pairwise_squared_distance_metric(pop) < diversity_threshold),

                             # Representation
                             representation=Representation(
                                 # Initialize a population of integer-vector genomes
                                 initialize=create_real_vector(
                                     bounds=[problem.bounds] * l)
                             ),

                             # 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),
Exemplo n.º 3
0
    if os.environ.get(test_env_var, False) == 'True':
        generations = 2
    else:
        generations = 1000

    l = 2
    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,
Exemplo n.º 4
0
        if args.track_workers_file:
            track_workers_stream = open(args.track_workers_file, 'w')
            track_workers_func = log_worker_location(track_workers_stream)

        if args.track_pop_file is not None:
            track_pop_stream = open(args.track_pop_file, 'w')
            track_pop_func = log_pop(args.update_interval, track_pop_stream)

        final_pop = asynchronous.steady_state(
            client,
            births=args.max_births,
            init_pop_size=args.init_pop_size,
            pop_size=args.pop_size,
            representation=Representation(
                initialize=create_binary_sequence(args.length),
                individual_cls=DistributedIndividual),
            problem=MaxOnes(),
            offspring_pipeline=[
                ops.random_selection, ops.clone,
                mutate_bitflip(expected_num_mutations=1),
                ops.pool(size=1)
            ],
            evaluated_probe=track_workers_func,
            pop_probe=track_pop_func)

        logger.info('Final pop: \n%s', pformat(final_pop))
    except Exception as e:
        logger.critical(str(e))
        raise e
    finally:
Exemplo n.º 5
0
def ea_solve(function,
             bounds,
             generations=100,
             pop_size=cpu_count(),
             mutation_std=1.0,
             maximize=False,
             viz=False,
             viz_ylim=(0, 1),
             hard_bounds=True,
             dask_client=None):
    """Provides a simple, top-level interfact that optimizes a real-valued
    function using a simple generational EA.

    :param function: the function to optimize; should take lists of real
        numbers as input and return a float fitness value

    :param [(float, float)] bounds: a list of (min, max) bounds to define the
        search space

    :param int generations: the number of generations to run for
    :param int pop_size: the population size
    :param float mutation_std: the width of the mutation distribution
    :param bool maximize: whether to maximize the function (else minimize)
    :param bool viz: whether to display a live best-of-generation plot
    :param bool hard_bounds: if True, bounds are enforced at all times during
        evolution; otherwise they are only used to initialize the population.

    :param (float, float) viz_ylim: initial bounds to use of the plots
        vertical axis

    :param dask_client: is optional dask Client for enable parallel evaluations

    The basic call includes instrumentation that prints the best-so-far fitness
    value of each generation to stdout:

    >>> from leap_ec.simple import ea_solve
    >>> ea_solve(sum, bounds=[(0, 1)]*5) # doctest:+ELLIPSIS
    generation, bsf
    0, ...
    1, ...
    ...
    100, ...
    array([..., ..., ..., ..., ...])

    When `viz=True`, a live BSF plot will also display:

    >>> ea_solve(sum, bounds=[(0, 1)]*5, viz=True) # doctest:+ELLIPSIS
    generation, bsf
    0, ...
    1, ...
    ...
    100, ...
    array([..., ..., ..., ..., ...])

    .. plot::

        from leap_ec.simple import ea_solve
        ea_solve(sum, bounds=[(0, 1)]*5, viz=True)

    """

    if hard_bounds:
        mutation_op = mutate_gaussian(std=mutation_std,
                                      hard_bounds=bounds,
                                      expected_num_mutations='isotropic')
    else:
        mutation_op = mutate_gaussian(std=mutation_std,
                                      expected_num_mutations='isotropic')

    pipeline = [
        ops.tournament_selection,
        ops.clone,
        mutation_op,
        ops.uniform_crossover(p_swap=0.2),
    ]

    # If a dask client is given, then use the synchronous (map/reduce) parallel
    # evaluation of individuals; else, revert to serial evaluations.
    if dask_client:
        pipeline.append(
            synchronous.eval_pool(client=dask_client, size=pop_size))
    else:
        pipeline.extend([ops.evaluate, ops.pool(size=pop_size)])

    if viz:
        plot_probe = probe.FitnessPlotProbe(ylim=viz_ylim, ax=plt.gca())
        pipeline.append(plot_probe)

    ea = generational_ea(max_generations=generations,
                         pop_size=pop_size,
                         problem=FunctionProblem(function, maximize),
                         representation=Representation(
                             individual_cls=DistributedIndividual,
                             initialize=create_real_vector(bounds=bounds)),
                         pipeline=pipeline)

    best_genome = None
    print('generation, bsf')
    for g, ind in ea:
        print(f"{g}, {ind.fitness}")
        best_genome = ind.genome

    return best_genome
Exemplo n.º 6
0
        decorator=executable.ArgmaxExecutable)

    with open('./genomes.csv', 'w') as genomes_file:

        ea = generational_ea(
            max_generations=generations,
            pop_size=pop_size,
            # Solve a problem that executes agents in the
            # environment and obtains fitness from it
            problem=problems.EnvironmentProblem(runs_per_fitness_eval,
                                                simulation_steps,
                                                environment,
                                                'reward',
                                                gui=gui),
            representation=Representation(initialize=create_real_vector(
                bounds=([[-1, 1]] * decoder.wrapped_decoder.length)),
                                          decoder=decoder),

            # The operator pipeline.
            pipeline=[
                ops.tournament_selection,
                ops.clone,
                mutate_gaussian(std=mutate_std,
                                hard_bounds=(-1, 1),
                                expected_num_mutations=1),
                ops.evaluate,
                ops.pool(size=pop_size),
                *build_probes(
                    genomes_file)  # Inserting all the probes at the end
            ])
        list(ea)
Exemplo n.º 7
0
    if os.environ.get(test_env_var, False) == 'True':
        generations = 2
    else:
        generations = 1000

    with open('./coop_stats.csv', 'w') as log_stream:
        ea = multi_population_ea(max_generations=generations, pop_size=pop_size,
                                 num_populations=9,
                                 problem=MaxOnes(),
                                 # Fitness function

                                 init_evaluate=ops.const_evaluate(value=-100),

                                 representation=Representation(
                                     individual_cls=Individual,
                                     initialize=create_binary_sequence(
                                         length=1)
                                 ),

                                 # Operator pipeline
                                 shared_pipeline=[
                                     ops.tournament_selection,
                                     ops.clone,
                                     mutate_bitflip(expected_num_mutations=1),
                                     ops.CooperativeEvaluate(
                                         num_trials=1,
                                         collaborator_selector=ops.random_selection,
                                         log_stream=log_stream),
                                     ops.pool(size=pop_size)
                                 ])