def f_ind(n_gen, i_worker, params):
    flag_model = '2D'
    flag_ctrl_mode = '2D'  # use 2D
    seed = None
    difficulty = 0
    sim_dt = 0.01
    sim_t = 20
    timstep_limit = int(round(sim_t / sim_dt))

    init_error = True
    error_count = 0
    while init_error:
        try:
            locoCtrl = OsimReflexCtrl(mode=flag_ctrl_mode, dt=sim_dt)
            env = L2M2019Env(seed=seed, difficulty=difficulty, visualize=False)
            env.change_model(model=flag_model,
                             difficulty=difficulty,
                             seed=seed)
            obs_dict = env.reset(project=True,
                                 seed=seed,
                                 init_pose=init_pose,
                                 obs_as_dict=True)
            init_error = False
        except Exception as e_msg:
            error_count += 1
            print('\ninitialization error (x{})!!!'.format(error_count))
            #print(e_msg)
            #import pdb; pdb.set_trace()
    env.spec.timestep_limit = timstep_limit + 100

    total_reward = 0
    error_sim = 0
    t = 0
    while True:
        t += sim_dt

        locoCtrl.set_control_params(params)
        action = locoCtrl.update(obs_dict)
        obs_dict, reward, done, info = env.step(action,
                                                project=True,
                                                obs_as_dict=True)
        total_reward += reward

        if done:
            break

    print('\n    gen#={} sim#={}: score={} time={}sec #step={}'.format(
        n_gen, i_worker, total_reward, t, env.footstep['n']))

    return total_reward  # minimization
force_ind = []
X_list = []
y_list = []
acc_list = []

# timestep = 300

running_loss = 0.0

df = pd.DataFrame(index=range(timstep_limit), columns=['grf_r', 'grf_l'])

for i in range(timstep_limit):

    t += sim_dt
    # locoCtrl.set_control_params(params)
    action = locoCtrl.update(obs_dict)
    # done if either the pelvis of the human model falls below 0.6 meters or when it reaches 10 seconds (i=1000)
    obs_dict, reward, done, info = env.step(action,
                                            project=True,
                                            obs_as_dict=True)

    # hip_angle.append(-obs_dict['r_leg']['joint']['hip'])
    # knee_angle.append(-obs_dict['r_leg']['joint']['knee'])
    # ankle_angle.append(-obs_dict['r_leg']['joint']['ankle'])

    #    hip_angle[i, :] = -obs_dict['r_leg']['joint']['hip']
    #    knee_angle[i, :] = -obs_dict['r_leg']['joint']['knee']
    #    ankle_angle[i, :] = -obs_dict['r_leg']['joint']['ankle']
    hip_abd[0, :] = -obs_dict['r_leg']['joint']['hip_abd']
    hip_angle[0, :] = -obs_dict['r_leg']['joint']['hip']
    knee_angle[0, :] = -obs_dict['r_leg']['joint']['knee']
示例#3
0
A new simulation starts when `clinet.env_step` returns `done==True`
and all the simulations end when the subsequent `client.env_reset()` 
returns a False
"""
mode = '3D'
difficulty = 2
visualize = False
seed = None
sim_dt = 0.01
sim_t = 10
timstep_limit = int(round(sim_t / sim_dt))

params = np.loadtxt('./osim/control/params_3D.txt')

locoCtrl = OsimReflexCtrl(mode=mode, dt=sim_dt)
total_reward = 0
while True:
    locoCtrl.set_control_params(params)
    action = locoCtrl.update(observation)

    [observation, reward, done, info] = client.env_step(action)
    total_reward += reward
    print(total_reward)
    if done:
        total_reward = 0
        observation = client.env_reset()
        if not observation:
            break

client.submit()