-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
51 lines (46 loc) · 1.55 KB
/
utils.py
File metadata and controls
51 lines (46 loc) · 1.55 KB
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
import numpy as np
import pickle
import gym
from envs import *
register_custom_envs()
def sample_minibatch(obs, next_obs, action_log_probs, batch_size):
random_indices = np.random.randint(0, obs.shape[0], size=batch_size)
return obs[random_indices], next_obs[random_indices], action_log_probs[random_indices]
def batchify(data, batch_size):
N = data[0].shape[0]
# batch_size = int(np.ceil(N / n_batches))
res = []
random_inds = np.arange(N)
np.random.shuffle(random_inds)
start_ind = 0
while start_ind < N:
batch_inds = random_inds[start_ind : min(start_ind + batch_size, N)]
res.append([category[batch_inds] for category in data])
start_ind += batch_size
return res
def collect_random_dataset(env_name, n_trials=1000, save_file=None):
if not save_file:
save_file = '{}_random_dataset.pkl'.format(env_name)
env = gym.make(env_name)
obs, actions, rewards, next_obs = [], [], [], []
for trial in range(n_trials):
done = False
ob = env.reset()
while not done:
obs.append(ob)
action = env.action_space.sample()
actions.append(action)
ob, reward, done, _ = env.step(action)
rewards.append([reward])
next_obs.append(ob)
obs.pop()
data_dict = {
'obs': obs,
'actions': actions,
'next_obs': next_obs,
'rewards': rewards
}
pickle.dump(data_dict, open(save_file, 'wb'))
return data_dict
if __name__ == '__main__':
collect_random_dataset('PointMass-v0')