Пример #1
0
def random(evals, runs, steps, env):
    """Run an environment controlled by a random `Executable`."""
    environment = gym.make('CartPole-v0')
    controller = executable.RandomExecutable(environment.observation_space,
                                             environment.action_space)
    for i in range(evals):
        p = problems.EnvironmentProblem(runs,
                                        steps,
                                        environment,
                                        'reward',
                                        guit=True)
        print(p.evaluate(controller))
Пример #2
0
def keyboard(evals, steps, env):
    """Run an environment and control it with the keyboard."""
    environment = gym.make(env)
    controller = executable.KeyboardExecutable(environment.observation_space,
                                               environment.action_space)

    for i in range(evals):
        # Wire our controller's keyboard function into the viewer's events
        environment.render()
        environment.unwrapped.viewer.window.on_key_press = controller.key_press
        environment.unwrapped.viewer.window.on_key_release = controller.key_release
        # Hand over control of the environment to the Problem
        p = problems.EnvironmentProblem(1,
                                        steps,
                                        environment,
                                        'reward',
                                        gui=True)
        print(p.evaluate(controller))

    environment.close()
Пример #3
0
def evolve(runs, steps, env, rep, gens, pop_size, num_nodes, mutate_std,
           output, gui):
    """Evolve a controller using a Pitt-style rule system."""
    check_rep(rep)

    print(f"Loading environment '{env}'...")
    environment = gym.make(env)
    print(f"\tObservation space:\t{environment.observation_space}")
    print(f"\tAction space:     \t{environment.action_space}")

    with open(output, 'w') as genomes_file:
        if rep == 'pitt':
            representation = pitt_representation(environment,
                                                 num_rules=num_nodes)
        elif rep == 'neural':
            representation = neural_representation(environment,
                                                   num_hidden_nodes=num_nodes)

        probes = get_probes(genomes_file, environment, rep)

        with Client() as dask_client:
            ea = generational_ea(
                generations=gens,
                pop_size=pop_size,
                # Solve a problem that executes agents in the
                # environment and obtains fitness from it
                problem=problems.EnvironmentProblem(runs, steps, environment,
                                                    'reward', gui),
                representation=representation,

                # The operator pipeline.
                pipeline=[
                    ops.tournament_selection,
                    ops.clone,
                    mutate_gaussian(std=mutate_std, hard_bounds=(-1, 1)),
                    ops.evaluate,
                    ops.pool(size=pop_size),
                    #synchronous.eval_pool(client=dask_client, size=pop_size),
                    *probes  # Inserting all the probes at the end
                ])
            list(ea)
Пример #4
0
def run(evals, runs, steps, rep, env, num_nodes, gui):
    """
    Load the parameters defining a controller on stdin into an agent to drive 
    it in the given environment.
    """
    check_rep(rep)
    rule_string = sys.stdin.readlines()[0]
    # Convert parameter string into a list of floats
    rule_set = list(map(float, rule_string.split(',')))

    environment = gym.make(env)
    if rep == 'neural':
        decoder = neural_representation(environment,
                                        num_hidden_nodes=num_nodes).decoder
    elif rep == 'pitt':
        decoder = pitt_representation(environment, num_rules=num_nodes).decoder

    controller = decoder.decode(rule_set)
    for i in range(evals):
        p = problems.EnvironmentProblem(runs, steps, environment, 'reward',
                                        gui)
        print(p.evaluate(controller))
Пример #5
0
    # Setup a decoder to convert genomes into rule systems
    decoder = rules.PittRulesDecoder(
        input_space=input_space,
        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,