()
Main function to be run.
Main function to be run.
Main function to be run .
def main():
    """ Main function to be run. """
    parser = argparse.ArgumentParser(description='Run the Guided Policy Search algorithm.')
    parser.add_argument('experiment', type=str,
                        help='experiment name')
    parser.add_argument('-n', '--new', action='store_true',
                        help='create new experiment')
    parser.add_argument('-t', '--targetsetup', action='store_true',
                        help='run target setup')
    parser.add_argument('-r', '--resume', metavar='N', type=int,
                        help='resume training from iter N')
    parser.add_argument('-p', '--policy', metavar='N', type=int,
                        help='take N policy samples (for BADMM/MDGPS only)')
    parser.add_argument('-s', '--silent', action='store_true',
                        help='silent debug print outs')
    parser.add_argument('-q', '--quit', action='store_true',
                        help='quit GUI automatically when finished')
    args = parser.parse_args()

    exp_name = args.experiment
    resume_training_itr = args.resume
    test_policy_N = args.policy

    from gps import __file__ as gps_filepath
    gps_filepath = os.path.abspath(gps_filepath)
    gps_dir = '/'.join(str.split(gps_filepath, '/')[:-3]) + '/'
    exp_dir = gps_dir + 'experiments/' + exp_name + '/'
    hyperparams_file = exp_dir + 'hyperparams.py'

    if args.silent:
        logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
    else:
        logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)

    if args.new:
        from shutil import copy

        if os.path.exists(exp_dir):
            sys.exit("Experiment '%s' already exists.\nPlease remove '%s'." %
                     (exp_name, exp_dir))
        os.makedirs(exp_dir)

        prev_exp_file = '.previous_experiment'
        prev_exp_dir = None
        try:
            with open(prev_exp_file, 'r') as f:
                prev_exp_dir = f.readline()
            copy(prev_exp_dir + 'hyperparams.py', exp_dir)
            if os.path.exists(prev_exp_dir + 'targets.npz'):
                copy(prev_exp_dir + 'targets.npz', exp_dir)
        except IOError as e:
            with open(hyperparams_file, 'w') as f:
                f.write('# To get started, copy over hyperparams from another experiment.\n' +
                        '# Visit rll.berkeley.edu/gps/hyperparams.html for documentation.')
        with open(prev_exp_file, 'w') as f:
            f.write(exp_dir)

        exit_msg = ("Experiment '%s' created.\nhyperparams file: '%s'" %
                    (exp_name, hyperparams_file))
        if prev_exp_dir and os.path.exists(prev_exp_dir):
            exit_msg += "\ncopied from     : '%shyperparams.py'" % prev_exp_dir
        sys.exit(exit_msg)

    if not os.path.exists(hyperparams_file):
        sys.exit("Experiment '%s' does not exist.\nDid you create '%s'?" %
                 (exp_name, hyperparams_file))

    hyperparams = imp.load_source('hyperparams', hyperparams_file)
    if args.targetsetup:
        try:
            import matplotlib.pyplot as plt
            from gps.agent.ros.agent_ros import AgentROS
            from gps.gui.target_setup_gui import TargetSetupGUI

            agent = AgentROS(hyperparams.config['agent'])
            TargetSetupGUI(hyperparams.config['common'], agent)

            plt.ioff()
            plt.show()
        except ImportError:
            sys.exit('ROS required for target setup.')
    elif test_policy_N:
        import random
        import numpy as np
        import matplotlib.pyplot as plt

        seed = hyperparams.config.get('random_seed', 0)
        random.seed(seed)
        np.random.seed(seed)

        data_files_dir = exp_dir + 'data_files/'
        data_filenames = os.listdir(data_files_dir)
        algorithm_prefix = 'algorithm_itr_'
        algorithm_filenames = [f for f in data_filenames if f.startswith(algorithm_prefix)]
        current_algorithm = sorted(algorithm_filenames, reverse=True)[0]
        current_itr = int(current_algorithm[len(algorithm_prefix):len(algorithm_prefix)+2])

        gps = GPSMain(hyperparams.config)
        if hyperparams.config['gui_on']:
            test_policy = threading.Thread(
                target=lambda: gps.test_policy(itr=current_itr, N=test_policy_N)
            )
            test_policy.daemon = True
            test_policy.start()

            plt.ioff()
            plt.show()
        else:
            gps.test_policy(itr=current_itr, N=test_policy_N)
    else:
        import random
        import numpy as np
        import matplotlib.pyplot as plt

        seed = hyperparams.config.get('random_seed', 0)
        random.seed(seed)
        np.random.seed(seed)

        gps = GPSMain(hyperparams.config, args.quit)
        if hyperparams.config['gui_on']:
            run_gps = threading.Thread(
                target=lambda: gps.run(itr_load=resume_training_itr)
            )
            run_gps.daemon = True
            run_gps.start()

            plt.ioff()
            plt.show()
        else:
            gps.run(itr_load=resume_training_itr)
def main ( ) : parser = argparse . ArgumentParser ( description = 'Run the Guided Policy Search algorithm.' ) parser . add_argument ( 'experiment' , type = str , help = 'experiment name' ) parser . add_argument ( '-n' , '--new' , action = 'store_true' , help = 'create new experiment' ) parser . add_argument ( '-t' , '--targetsetup' , action = 'store_true' , help = 'run target setup' ) parser . add_argument ( '-r' , '--resume' , metavar = 'N' , type = int , help = 'resume training from iter N' ) parser . add_argument ( '-p' , '--policy' , metavar = 'N' , type = int , help = 'take N policy samples (for BADMM/MDGPS only)' ) parser . add_argument ( '-s' , '--silent' , action = 'store_true' , help = 'silent debug print outs' ) parser . add_argument ( '-q' , '--quit' , action = 'store_true' , help = 'quit GUI automatically when finished' ) args = parser . parse_args ( ) exp_name = args . experiment resume_training_itr = args . resume test_policy_N = args . policy from gps import __file__ as gps_filepath gps_filepath = os . path . abspath ( gps_filepath ) gps_dir = '/' . join ( str . split ( gps_filepath , '/' ) [ : - 3 ] ) + '/' exp_dir = gps_dir + 'experiments/' + exp_name + '/' hyperparams_file = exp_dir + 'hyperparams.py' if args . silent : logging . basicConfig ( format = '%(levelname)s:%(message)s' , level = logging . INFO ) else : logging . basicConfig ( format = '%(levelname)s:%(message)s' , level = logging . DEBUG ) if args . new : from shutil import copy if os . path . exists ( exp_dir ) : sys . exit ( "Experiment '%s' already exists.\nPlease remove '%s'." % ( exp_name , exp_dir ) ) os . makedirs ( exp_dir ) prev_exp_file = '.previous_experiment' prev_exp_dir = None try : with open ( prev_exp_file , 'r' ) as f : prev_exp_dir = f . readline ( ) copy ( prev_exp_dir + 'hyperparams.py' , exp_dir ) if os . path . exists ( prev_exp_dir + 'targets.npz' ) : copy ( prev_exp_dir + 'targets.npz' , exp_dir ) except IOError as e : with open ( hyperparams_file , 'w' ) as f : f . write ( '# To get started, copy over hyperparams from another experiment.\n' + '# Visit rll.berkeley.edu/gps/hyperparams.html for documentation.' ) with open ( prev_exp_file , 'w' ) as f : f . write ( exp_dir ) exit_msg = ( "Experiment '%s' created.\nhyperparams file: '%s'" % ( exp_name , hyperparams_file ) ) if prev_exp_dir and os . path . exists ( prev_exp_dir ) : exit_msg += "\ncopied from     : '%shyperparams.py'" % prev_exp_dir sys . exit ( exit_msg ) if not os . path . exists ( hyperparams_file ) : sys . exit ( "Experiment '%s' does not exist.\nDid you create '%s'?" % ( exp_name , hyperparams_file ) ) hyperparams = imp . load_source ( 'hyperparams' , hyperparams_file ) if args . targetsetup : try : import matplotlib . pyplot as plt from gps . agent . ros . agent_ros import AgentROS from gps . gui . target_setup_gui import TargetSetupGUI agent = AgentROS ( hyperparams . config [ 'agent' ] ) TargetSetupGUI ( hyperparams . config [ 'common' ] , agent ) plt . ioff ( ) plt . show ( ) except ImportError : sys . exit ( 'ROS required for target setup.' ) elif test_policy_N : import random import numpy as np import matplotlib . pyplot as plt seed = hyperparams . config . get ( 'random_seed' , 0 ) random . seed ( seed ) np . random . seed ( seed ) data_files_dir = exp_dir + 'data_files/' data_filenames = os . listdir ( data_files_dir ) algorithm_prefix = 'algorithm_itr_' algorithm_filenames = [ f for f in data_filenames if f . startswith ( algorithm_prefix ) ] current_algorithm = sorted ( algorithm_filenames , reverse = True ) [ 0 ] current_itr = int ( current_algorithm [ len ( algorithm_prefix ) : len ( algorithm_prefix ) + 2 ] ) gps = GPSMain ( hyperparams . config ) if hyperparams . config [ 'gui_on' ] : test_policy = threading . Thread ( target = lambda : gps . test_policy ( itr = current_itr , N = test_policy_N ) ) test_policy . daemon = True test_policy . start ( ) plt . ioff ( ) plt . show ( ) else : gps . test_policy ( itr = current_itr , N = test_policy_N ) else : import random import numpy as np import matplotlib . pyplot as plt seed = hyperparams . config . get ( 'random_seed' , 0 ) random . seed ( seed ) np . random . seed ( seed ) gps = GPSMain ( hyperparams . config , args . quit ) if hyperparams . config [ 'gui_on' ] : run_gps = threading . Thread ( target = lambda : gps . run ( itr_load = resume_training_itr ) ) run_gps . daemon = True run_gps . start ( ) plt . ioff ( ) plt . show ( ) else : gps . run ( itr_load = resume_training_itr )