Esempio n. 1
0
    def load(self, output_folder=None, output_filename=None):
        '''
        Loads the class form disk
        '''
        if not hasattr(self, 'registered_types'):
            self.registered_types = set()
        if not hasattr(self, 'registered_keys'):
            self.registered_keys = set()

        output_folder = utils.get_output_dir(
        ) if output_folder is None else output_folder
        [output_filename,
         self.filename] = utils.sync_output_filename(output_filename,
                                                     self.filename, '.zip')
        path = os.path.join(output_folder, output_filename)

        # append the zip extension
        if not path.endswith('.zip'):
            path = path + '.zip'
        try:
            with open(path, 'rb') as f:
                utils.print_with_stamp('Loading state from %s' % (path),
                                       self.name)
                state = t_load(f)
                self.set_instance_state(state)
            self.state_changed = False
        except IOError as err:
            utils.print_with_stamp('Unable to load state from %s' % (path),
                                   self.name)
            print(err)
            return False
        return True
Esempio n. 2
0
    def save(self, output_folder=None, output_filename=None):
        '''
        Serializes the class using the theano pickling utility function, and saves it to disk
        '''
        sys.setrecursionlimit(100000)
        output_folder = utils.get_output_dir(
        ) if output_folder is None else output_folder
        [output_filename,
         self.filename] = utils.sync_output_filename(output_filename,
                                                     self.filename, '.zip')

        if self.state_changed or output_folder is not None or output_filename is not None:
            # check if output_folder exists, create it if necessary.
            if not os.path.exists(output_folder):
                try:
                    os.makedirs(output_folder)
                except OSError:
                    utils.print_with_stamp(
                        'Unable to create the directory: %s' % (output_folder),
                        self.name)
                    raise

            # construct file path
            path = os.path.join(output_folder, output_filename)
            # append the zip extension
            if not path.endswith('.zip'):
                path = path + '.zip'

            with open(path, 'wb') as f:
                utils.print_with_stamp('Saving state to %s' % (path),
                                       self.name)
                t_dump(self.get_instance_state(), f, 2)
            os.system('chmod 666 %s' % (path))
            self.state_changed = False
Esempio n. 3
0
 parser = argparse.ArgumentParser()
 parser.add_argument('-e',
                     '--exp',
                     type=int,
                     default=8,
                     help='id of experiment to run')
 parser.add_argument('-n', '--name', type=str, help='experiment name')
 parser.add_argument(
     '-H',
     '--horizon',
     type=int,
     help='steps for control horizon (length of trials in time steps)')
 parser.add_argument('-o',
                     '--output_folder',
                     type=str,
                     default=utils.get_output_dir(),
                     help='where to save the results of the experiment')
 parser.add_argument('-r',
                     '--render',
                     action='store_true',
                     help='whether to call env.render')
 parser.add_argument(
     '-d',
     '--debug_plot',
     type=int,
     default=0,
     help='whether to plot rollouts and other debugging information')
 parser.add_argument(
     '-k',
     '--kwarg',
     nargs=2,
import os
import numpy as np
import kusanagi.ghost.regression as kreg

from kusanagi import utils
from kusanagi.shell.cartpole import default_params#, CartpoleDraw
from kusanagi.shell.plant import SerialPlant
from kusanagi.ghost.algorithms.PILCO import PILCO, MC_PILCO
from kusanagi.ghost.control import NNPolicy
from kusanagi.utils import plot_results
#np.random.seed(31337)
np.set_printoptions(linewidth=500)

if __name__ == '__main__':
    # setup output directory
    utils.set_output_dir(os.path.join(utils.get_output_dir(),'cartpole_serial'))

    J = 4                                                                   # number of random initial trials
    N = 100                                                                 # learning iterations
    learner_params = default_params()
    
    # initialize learner
    learner_params['dynmodel_class'] = kreg.SSGP_UI
    learner_params['params']['dynmodel']['n_inducing'] = 100
    learner_params['plant_class'] = SerialPlant
    learner_params['params']['plant']['maxU'] = np.array(learner_params['params']['policy']['maxU'])*1.0/0.4
    learner_params['params']['plant']['state_indices'] = [0,2,3,1]
    learner_params['params']['plant']['baud_rate'] = 4000000
    learner_params['params']['plant']['port'] = '/dev/ttyACM0'
    #learner_params['min_method'] = 'ADAM'
    #learner_params['dynmodel_class'] = NN
Esempio n. 5
0
    if val is None:
        attr1, attr_next = attr.split('.')
        next_mod = getattr(obj, attr1)
        #print next_mod
        if attr == attr1 or next_mod is None:
            raise AttributeError('Can\'t find %s in module %s' % (obj, attr))
        else:
            return recursive_getattr(next_mod, attr_next)
    else:
        return val


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '-d', '--dataset_folder', type=str, default=utils.get_output_dir(),
        help='where to load the results of the experiment')
    parser.add_argument(
        '-r', '--render', action='store_true',
        help='whether to call env.render')
    parser.add_argument(
        '-k', '--kwarg', nargs=2, action='append', default=[],
        help='additional arguments for the experiment [name value]')
    parser.add_argument(
        '-e', '--env', type=str, default='cartpole.Cartpole',
        help='environment from kusanagi.shell')
    parser.add_argument(
        '-c', '--cost', type=str, default='cartpole.cartpole_loss',
        help='cost funciton from kusanagi.shell')   
    parser.add_argument(
        '-p', '--policy_class', type=str, default='NNPolicy',
Esempio n. 6
0
    return scenario_params, pol, dyn, learner_setup


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '-e', '--exp', type=int, default=8,
        help='id of experiment to run')
    parser.add_argument(
        '-n', '--name', type=str,
        help='experiment name')
    parser.add_argument(
        '-H', '--horizon', type=int,
        help='steps for control horizon (length of trials in time steps)')
    parser.add_argument(
        '-o', '--output_folder', type=str, default=utils.get_output_dir(),
        help='where to save the results of the experiment')
    parser.add_argument(
        '-r', '--render', action='store_true',
        help='whether to call env.render')
    parser.add_argument(
        '-d', '--debug_plot', type=int, default=0,
        help='whether to plot rollouts and other debugging information')
    parser.add_argument(
        '-k', '--kwarg', nargs=2, action='append', default=[],
        help='additional arguments for the experiment [name value]')
    args = parser.parse_args()
    e_id = args.exp
    kwargs = dict(args.kwarg)

    # prepare experiment parameters