forked from wilson1yan/rlpyt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
random_policy.py
54 lines (37 loc) · 1.16 KB
/
random_policy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from os.path import join
import json
import torch
import numpy as np
from rlpyt.envs.dm_control_env import DMControlEnv
def cloth_corner_random(obs):
idx = np.random.randint(0, 4)
one_hot = np.zeros(4)
one_hot[idx] = 1
delta = np.random.rand(3) * 2 - 1
return np.concatenate((one_hot, delta)).astype(np.float32)
def rope_v2_random(obs):
return np.random.rand(3) * 2 - 1
def cloth_point_random(obs):
return np.random.rand(4) * 2 - 1
def simulate_policy():
policy = cloth_point_random
env = DMControlEnv(domain='cloth_point', task='easy',
max_path_length=120, task_kwargs=dict(random_location=False))
n_episodes = 40
returns = []
for i in range(n_episodes):
o = env.reset()
done = False
reward = 0
while not done:
o, r, done, info = env.step(policy(o))
reward += r
if done or info.traj_done:
break
print(reward)
returns.append(reward)
print('Finished episode', i)
print('Rewards', returns)
print('Average Reward', np.mean(returns))
if __name__ == '__main__':
simulate_policy()