Ejemplo n.º 1
0
    def run_experiment(self, filename=None):
        param_parser = self.agent.agent_get_parameters()
        for trial in range(self.num_trials):
            parameters = copy.deepcopy(self.configuration['agent']['params'])
            # Randomize the parameters, those marked not optimizable get their default
            for name, value in randomize_parameters(param_parser):
                # Then, set the parameter value, but only if not already set
                parameters.setdefault(name, value)

            # Set params for current agent
            self.agent.params = parameters

            # Run a trial...
            tmp_file = "rndtrial" + str(numpy.random.randint(1.e10)) + ".dat"
            Episodic.run_experiment(self, filename = tmp_file)

            # Collect results
            locs, means, std = plotExperiment.processFile(tmp_file, self.evaluate, verbose=False, method=self.eval_reduce, kmeans_k=self.k)
            json_out = copy.deepcopy(self.configuration)
            json_out['agent']['params'] = parameters
            json_out['experiment']['episodes'] = locs.tolist()
            json_out['experiment']['returns'] = means.tolist()
            json_out['experiment']['deviations'] = std.tolist()

            if filename is None:
                print json.dumps(json_out)
            else:
                with open(filename, "a") as f:
                    f.write(json.dumps(json_out) + "\n")
            os.remove(tmp_file)
Ejemplo n.º 2
0
    def run_experiment(self, filename=None):
        param_parser = self.agent.agent_parameters()
        for trial in range(self.num_trials):
            parameters = copy.deepcopy(self.configuration['agent']['params'])
            # Randomize the parameters, those marked not optimizable get their default
            for name, value in randomize_parameters(param_parser):
                # Then, set the parameter value, but only if not already set
                parameters.setdefault(name, value)

            # Set params for current agent
            self.agent.params = parameters

            # Run a trial...
            tmp_file = "rndtrial" + str(numpy.random.randint(1.e10)) + ".dat"
            Episodic.run_experiment(self, filename=tmp_file)

            # Collect results
            locs, means, std = plotExperiment.processFile(
                tmp_file,
                self.evaluate,
                verbose=False,
                method=self.eval_reduce,
                kmeans_k=self.k)
            json_out = copy.deepcopy(self.configuration)
            json_out['agent']['params'] = parameters
            json_out['experiment']['episodes'] = locs.tolist()
            json_out['experiment']['returns'] = means.tolist()
            json_out['experiment']['deviations'] = std.tolist()

            if filename is None:
                print json.dumps(json_out)
            else:
                with open(filename, "a") as f:
                    f.write(json.dumps(json_out) + "\n")
            os.remove(tmp_file)
Ejemplo n.º 3
0
    def __init__(self, config, **kwargs):
        if not kwargs.has_key('agent') or not kwargs.has_key('environment'):
            print "ERROR: RandomizedTrial must be run locally in order to randomize parameters."
            import sys
            sys.exit(1)

        self.num_trials = kwargs.setdefault('num_trials', 1)
        self.evaluate = kwargs.setdefault('evaluate', 'reward') #reward, steps, time
        self.eval_reduce = kwargs.setdefault('evaluate_reduce', 'sum') # None, 'sum', 'final', 'kmeans'
        self.k = kwargs.setdefault('kmeans_k', 10)
        Episodic.__init__(self, config, **kwargs)
Ejemplo n.º 4
0
def main(job_id, params):
    print "Anything printed here will end up in the output directory for job #:", str(job_id)
    parameters = {}
    for key in params:
        if isinstance(key, unicode):
            parameters[key.encode("utf-8")] = params[key]
    else:
        parameters[key] = params[key]

    for key in parameters:
        parameters[key] = map(lambda k: k.encode("utf-8") if isinstance(k, unicode) else k, parameters[key])
        if len(parameters[key]) == 1:
            value = parameters[key][0]
            try:
                value = float(value)
            except:
                if value.lower() == "false":
                    value = False
                elif value.lower() == "true":
                    value = True

            parameters[key] = value
    print parameters

    my_path = os.path.dirname(os.path.abspath(__file__))
    tmp_file = os.path.join(my_path, "rndtrial" + str(numpy.random.randint(1.0e10)) + ".dat")
    my_path = os.path.abspath(os.path.join(my_path, "experiment.json"))
    agent, a_args, env, env_args, exp, exp_args = fromjson(my_path)

    for key in parameters:
        a_args.setdefault(key, parameters[key])

    config = {
        "agent": {"name": agent.name, "params": a_args},
        "environment": {"name": env.name, "params": env_args},
        "experiment": {"name": exp.name, "params": exp_args},
    }

    experiment = Episodic(config, agent=agent(**a_args), environment=env(**env_args), **exp_args)

    # Using this try/except makes debugging in spearmint 1mil times easier
    try:
        experiment.run_experiment(filename=tmp_file)
    except Exception as ex:
        import traceback

        traceback.print_exc()

    locs, means, std = plotExperiment.processFile(tmp_file, "reward", verbose=False, method="sum")
    os.remove(tmp_file)
    print "Result:", -means[0]
    return -means[0]
Ejemplo n.º 5
0
    def __init__(self, config, **kwargs):
        if not kwargs.has_key('agent') or not kwargs.has_key('environment'):
            print "ERROR: RandomizedTrial must be run locally in order to randomize parameters."
            import sys
            sys.exit(1)

        self.num_trials = kwargs.setdefault('num_trials', 1)
        self.evaluate = kwargs.setdefault('evaluate',
                                          'reward')  #reward, steps, time
        self.eval_reduce = kwargs.setdefault(
            'evaluate_reduce', 'sum')  # None, 'sum', 'final', 'kmeans'
        self.k = kwargs.setdefault('kmeans_k', 10)
        Episodic.__init__(self, config, **kwargs)
Ejemplo n.º 6
0
def main(job_id, params):
    print 'Anything printed here will end up in the output directory for job #:', str(
        job_id)
    parameters = {}
    for key in params:
        if isinstance(key, unicode):
            parameters[key.encode('utf-8')] = params[key]
    else:
        parameters[key] = params[key]

    for key in parameters:
        parameters[key] = map(
            lambda k: k.encode('utf-8')
            if isinstance(k, unicode) else k, parameters[key])
        if len(parameters[key]) == 1:
            value = parameters[key][0]
            try:
                value = float(value)
            except:
                if value.lower() == "false":
                    value = False
                elif value.lower() == "true":
                    value = True

            parameters[key] = value
    print parameters

    my_path = os.path.dirname(os.path.abspath(__file__))
    tmp_file = os.path.join(
        my_path, "rndtrial" + str(numpy.random.randint(1.e10)) + ".dat")
    my_path = os.path.abspath(os.path.join(my_path, "experiment.json"))
    agent, a_args, env, env_args, exp, exp_args = fromjson(my_path)

    for key in parameters:
        a_args.setdefault(key, parameters[key])

    config = {
        'agent': {
            'name': agent.name,
            'params': a_args
        },
        'environment': {
            'name': env.name,
            'params': env_args
        },
        'experiment': {
            'name': exp.name,
            'params': exp_args
        }
    }

    experiment = Episodic(config,
                          agent=agent(**a_args),
                          environment=env(**env_args),
                          **exp_args)

    # Using this try/except makes debugging in spearmint 1mil times easier
    try:
        experiment.run_experiment(filename=tmp_file)
    except Exception as ex:
        import traceback
        traceback.print_exc()

    locs, means, std = plotExperiment.processFile(tmp_file,
                                                  "reward",
                                                  verbose=False,
                                                  method="sum")
    os.remove(tmp_file)
    print "Result:", -means[0]
    return -means[0]