コード例 #1
0
def render_from_file(savefile,
                     model_actor,
                     env,
                     criterion='best',
                     num=1,
                     max_steps=1000):
    scored_genomes = load_genomes(savefile, criterion, num)
    for s, g in scored_genomes:
        actor = model_actor.from_genome(g)
        print('Score:', s)
        simulate(actor, env, render=True, max_steps=max_steps)
コード例 #2
0
    def run_best(num = 1):
        print('Loading best from savefile...')
        model = new_actor()
        best_actors = None
        try:
            best_actors = load_actors(savefile, model, criterion='best', num=num)
        except:
            print('Could not load from',savefile,'. Exiting...')
            return

        for actor in best_actors:
            simulate(actor, env, render = True, **render_args)
コード例 #3
0
def evolve(initial_population,
           environment,
           generations=100,
           p_mutation=0.01,
           mutation_scale=0.25,
           selection=roulette_selection,
           keep_parents_alive=False,
           simulation_reps=100,
           max_steps=1000,
           render_gens=10,
           savefile=None,
           dumpfile=None,
           savenum=1,
           allow_parallel=True,
           max_jobs=None):
    """Runs selection and simulation on initial_population for specified number of generations.
Returns the final generation.
Renders a random individual from the population every render_gens generations.
p_mutation - the chance that a particular value in an offspring genome is changed
simulation_reps - the number of times to execute each actor in the environment, to account for random variation in initial environmental conditions
max_steps - the maximum number of simulation steps for each run
"""
    population = initial_population
    try:
        for i in range(generations):
            population, best_actor, worst_actor = run_generation(
                population,
                environment,
                p_mutation=p_mutation,
                mutation_scale=mutation_scale,
                selection=selection,
                simulation_reps=simulation_reps,
                keep_parents_alive=keep_parents_alive,
                max_steps=max_steps,
                savefile=savefile,
                savenum=savenum,
                allow_parallel=allow_parallel,
                max_jobs=max_jobs)

            if render_gens != None and (i % render_gens) == 0:
                print('---=== Generation', i, '===---')
                simulate(best_actor,
                         environment,
                         video_postfix=i,
                         render=True,
                         max_steps=max_steps)
    except Exception as e:
        print(e)  # For debugging
        if dumpfile != None:
            dump_genomes(dumpfile, population)
    return population
コード例 #4
0
 def run_actor(actor):
     """Returns the actor's average score and genome."""
     score = np.mean([
         simulate(actor, environment, max_steps=max_steps, render=False)
         for _ in range(simulation_reps)
     ])
     return (score, actor.get_genome())
コード例 #5
0
import gym
import time
from actor import Actor
from execution import simulate

env = gym.make('CartPole-v1')
act = Actor(env.observation_space, env.action_space)

print('-- Without Rendering --')
print(time.time())
print(simulate(act, env, render=False))
print(time.time())
print('-- With Rendering --')
print(time.time())
print(simulate(act, env, render=True))
print(time.time())
input('Press ENTER to quit...')
コード例 #6
0
# Had some trouble getting parallel computations to work.
# If there are issues, disable this flag
ALLOW_PARALLEL = True

try:
    assert ALLOW_PARALLEL, 'Parallel computation manually disabled.'
    import gym
    from actor import GeneticPerceptronActor
    from joblib import Parallel, delayed
    # Actually execute a simple test of the parallel system, to make sure it works
    N_JOBS = 8
    # PARALLEL_TEST = Parallel(n_jobs=N_JOBS)(delayed(lambda x: x**0.5)(i) for i in range(100))
    env = gym.make('CartPole-v1')
    PARALLEL_TEST = Parallel(n_jobs=N_JOBS)(
        delayed(lambda a: simulate(a, env))(a) for a in [
            GeneticPerceptronActor(env.observation_space, env.action_space)
            for _ in range(100)
        ])
    USE_PARALLEL = True
    print('Parallel computation enabled.')
except Exception as e:
    USE_PARALLEL = False
    print('Warning! Parallel computation disabled.')
    print(e)

try:
    from multiprocessing import cpu_count
    N_JOBS = cpu_count()
    print('Detected', N_JOBS, 'CPUs.')
except Exception as e: