def run(self):
    set_global_seeds(self.train_params['seed'])
    env_name = self.env_params['env_name']
    backend = self.env_params['backend']
    stdev = self.train_params['env_dist_stdev']
    mean_scale = self.train_params['mean_scale']
    seed = self.train_params['seed']
    num_eval_env = self.train_params['num_eval_env']
    collision_detector = self.env_params['collision_detector']
    env_dist = dr.dist.Normal(env_name, backend, mean_scale=mean_scale)
    init_mean_param = PPO_Pytorch._dict_to_vec(env_name, env_dist.default_parameters)
    init_stdev_param = np.array(([stdev] * len(init_mean_param)), dtype=np.float32)
    cem_init_mean = np.concatenate((init_mean_param, init_stdev_param))
    cem_init_stdev = np.array(([1.0] * len(cem_init_mean)), dtype=np.float32)
    self.env_dist = dr.dist.Normal(env_name, backend)
    self.env_dist.backend.set_collision_detector(env_dist.root_env, collision_detector)
    self.env_dist.seed(seed)
    if (env_name == 'Walker'):
        self.eval_envs = [gym.make('Walker2d-v2') for _ in range(num_eval_env)]
    elif (env_name == 'Hopper'):
        self.eval_envs = [gym.make('Hopper-v2') for _ in range(num_eval_env)]
    else:
        exit('Unrecognized environment')
    if (COMM.Get_rank() == 0):
        self.optimizer = CEMOptimizer(sol_dim=30, max_iters=300, popsize=self.train_params['pop_size'], num_elites=self.train_params['num_elites'], cost_function=self._cost_function, lower_bound=0.0, upper_bound=(cem_init_mean * 5.0), alpha=0.75, viz_dir=self.log_dir)
        self.optimizer.writer.add_text('env_params', str(self.env_params), 0)
        self.optimizer.writer.add_text('train_params', str(self.train_params), 0)
        res = self.optimizer.obtain_solution(cem_init_mean, cem_init_stdev)
        path = osp.join(self.log_dir, 'res.pkl')
        with open(path, 'wb') as f:
            pickle.dump(res, f)
        COMM.Abort(0)
    else:
        while True:
            args = COMM.recv(source=0)
            r = self.train(*args)
            COMM.send(r, dest=0)
def run(self):
    set_global_seeds(self.train_params['seed'])
    env_name = self.env_params['env_name']
    backend = self.env_params['backend']
    stdev = self.train_params['env_dist_stdev']
    mean_scale = self.train_params['mean_scale']
    seed = self.train_params['seed']
    num_eval_env = self.train_params['num_eval_env']
    collision_detector = self.env_params['collision_detector']
    env_dist = dr.dist.Normal(env_name, backend, mean_scale=mean_scale)
    init_mean_param = PPO_Pytorch._dict_to_vec(env_name, env_dist.default_parameters)
    init_stdev_param = np.array(([stdev] * len(init_mean_param)), dtype=np.float32)
    cem_init_mean = np.concatenate((init_mean_param, init_stdev_param))
    cem_init_stdev = np.array(([1.0] * len(cem_init_mean)), dtype=np.float32)
    self.env_dist = dr.dist.Normal(env_name, backend)
    self.env_dist.backend.set_collision_detector(env_dist.root_env, collision_detector)
    self.env_dist.seed(seed)
    if (env_name == 'Walker'):
        self.eval_envs = [gym.make('Walker2d-v2') for _ in range(num_eval_env)]
    elif (env_name == 'Hopper'):
        self.eval_envs = [gym.make('Hopper-v2') for _ in range(num_eval_env)]
    else:
        exit('Unrecognized environment')
    if (COMM.Get_rank() == 0):
        self.optimizer = CEMOptimizer(sol_dim=30, max_iters=300, popsize=self.train_params['pop_size'], num_elites=self.train_params['num_elites'], cost_function=self._cost_function, lower_bound=0.0, upper_bound=(cem_init_mean * 5.0), alpha=0.75, viz_dir=self.log_dir)
        self.optimizer.writer.add_text('env_params', str(self.env_params), 0)
        self.optimizer.writer.add_text('train_params', str(self.train_params), 0)
        res = self.optimizer.obtain_solution(cem_init_mean, cem_init_stdev)
        path = osp.join(self.log_dir, 'res.pkl')
        with open(path, 'wb') as f:
            pickle.dump(res, f)
        COMM.Abort(0)
    else:
        while True:
            args = COMM.recv(source=0)
            r = self.train(*args)
            COMM.send(r, dest=0)