has_entropy = (work_dir / '{}_0/policy_entropies.pkl'.format(dir_name)).exists()
    print('has entropy:', has_entropy)

    Js = list()
    Rs = list()
    Qs = list()
    Es = list()
    best_J = float("-inf")
    best_stats = None
    best_agent = None

    skip_cnt = 0

    for run_dir in run_dirs: 
        logger = BenchmarkLogger(log_dir=str(run_dir.parent), log_id=str(run_dir.name), use_timestamp=False)
        print(run_dir, end=' ')
        if not logger.exists_best_agent(): 
            print("ABORT")
            skip_cnt += 1
            continue
        else:
            print("EXISTS")
        Js.extend(logger.load_Js())
        Rs.extend(logger.load_Rs())
        Qs.extend(logger.load_Qs())
        if has_entropy:
            Es.extend(logger.load_policy_entropies())
        stats = logger.load_stats()
        if stats['best_J'] > best_J:
            best_stats = stats
import os

from mushroom_rl_benchmark import BenchmarkLogger
from mushroom_rl_benchmark.experiment import exec_run
from mushroom_rl_benchmark.experiment.slurm import read_arguments_run

if __name__ == '__main__':

    log_dir, run_args = read_arguments_run()

    log_id = 'run_' + str(run_args['seed'])

    agent_builder = BenchmarkLogger._load_pickle(
        os.path.join(log_dir, 'agent_builder.pkl'))
    env_builder = BenchmarkLogger._load_pickle(
        os.path.join(log_dir, 'environment_builder.pkl'))

    logger = BenchmarkLogger(log_dir=log_dir,
                             log_id=log_id,
                             use_timestamp=False)

    logger.info('Starting experiment.')

    result = exec_run(agent_builder, env_builder, **run_args)

    logger.info('Saving result.')

    cmp_E = agent_builder.compute_policy_entropy

    logger.save_Js([result['Js']])
    logger.save_Rs([result['Rs']])
示例#3
0
import yaml
from pathlib import Path
from argparse import ArgumentParser
from mushroom_rl_benchmark import BenchmarkSuiteVisualizer, BenchmarkLogger


def get_args():
    parser = ArgumentParser()
    parser.add_argument(
        "-d",
        "--directory",
        type=str,
        required=True,
        help='Benchmark directory where the plots generation is needed')
    args = vars(parser.parse_args())
    return args.values()


if __name__ == '__main__':
    path, = get_args()

    plots_file = Path('cfg') / 'plots.yaml'
    logger = BenchmarkLogger.from_path(path)

    with open(plots_file, 'r') as plots_file:
        plot_params = yaml.safe_load(plots_file)

    visualizer = BenchmarkSuiteVisualizer(logger, **plot_params)
    visualizer.show_reports()
    visualizer.save_reports()
示例#4
0
"""
Script for benchmarking A2C Agent
"""

import time
from mushroom_rl_benchmark import BenchmarkExperiment, BenchmarkLogger
from mushroom_rl_benchmark.builders import EnvironmentBuilder, A2CBuilder

if __name__ == '__main__':

    logger = BenchmarkLogger(log_dir='./logs', log_id='a2c_pendulum')

    agent_builder = A2CBuilder.default(actor_lr=7e-4,
                                       critic_lr=7e-4,
                                       n_features=32)

    env_name = 'Gym'
    env_params = dict(env_id='Pendulum-v0', horizon=200, gamma=.99)

    parallel = dict(max_concurrent_runs=10)

    env_builder = EnvironmentBuilder(env_name, env_params)
    logger.info('Environment is imported')

    exp = BenchmarkExperiment(agent_builder, env_builder, logger)
    logger.info('BenchmarkExperiment was built successfully')

    start_time = time.time()
    exp.run(exec_type='parallel',
            n_runs=10,
            n_epochs=100,
示例#5
0
def run(res_dir, res_id):
    """
    Function to aggregate the benchmark results from running in SLURM mode.

    Args:
        res_dir (str): path to the result directory;
        res_id (str): log id of the result directory.
    
    """
    work_dir = Path(res_dir, res_id)

    # check if results are aggregated
    
    dir_name = 'run'

    run_dirs = list(work_dir.glob('{}_*'.format(dir_name)))
    print(run_dirs)

    has_entropy = (work_dir / '{}_0/entropy.pkl'.format(dir_name)).exists()
    print('has entropy:', has_entropy)

    J = list()
    R = list()
    V = list()
    E = list()
    best_J = float("-inf")
    best_stats = None
    best_agent = None

    skip_cnt = 0

    for run_dir in run_dirs: 
        logger = BenchmarkLogger(log_dir=str(run_dir.parent), log_id=str(run_dir.name), use_timestamp=False)
        print(run_dir, end=' ')
        if not logger.exists_best_agent(): 
            print("ABORT")
            skip_cnt += 1
            continue
        else:
            print("EXISTS")
        J.extend(logger.load_J())
        R.extend(logger.load_R())
        V.extend(logger.load_V())
        if has_entropy:
            E.extend(logger.load_entropy())
        stats = logger.load_stats()
        if stats['best_J'] > best_J:
            best_stats = stats
            best_agent = logger.load_best_agent()
    
    if skip_cnt > 0: print('NUMBER OF FAILED RUNS:', '{}/{}'.format(skip_cnt, len(run_dirs)))
    
    print('Name:', res_id)
    print('Directory:', res_dir)

    logger = BenchmarkLogger(log_dir=res_dir, log_id=res_id, use_timestamp=False)

    logger.save_J(J)
    logger.save_R(R)
    logger.save_V(V)
    if has_entropy:
        logger.save_entropy(E)
    logger.save_stats(best_stats)
    logger.save_best_agent(best_agent)

    visualizer = BenchmarkVisualizer(logger)
    visualizer.save_report()