-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
63 lines (46 loc) · 2.04 KB
/
Copy pathutils.py
File metadata and controls
63 lines (46 loc) · 2.04 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
52
53
54
55
56
57
58
59
60
61
62
63
import torch
import gym
import numpy as np
import sklearn.pipeline
from sklearn.kernel_approximation import RBFSampler
from sklearn.linear_model import LinearRegression
def get_init_state(env, seed):
try:
state, _ = env.reset(return_info=True)
except TypeError:
state = env.reset(seed=seed)
if isinstance(state, tuple):
state = state[0]
return state
def save(args, save_name, model, wandb, ep=None):
import os
save_dir = './trained_models/'
if not os.path.exists(save_dir):
os.makedirs(save_dir)
if not ep == None:
torch.save(model.state_dict(), save_dir + args.run_name + save_name + str(ep) + ".pth")
wandb.save(save_dir + args.run_name + save_name + str(ep) + ".pth")
else:
torch.save(model.state_dict(), save_dir + args.run_name + save_name + ".pth")
wandb.save(save_dir + args.run_name + save_name + ".pth")
def collect_random(env, dataset, episodes, knockoff_list, opt_policy, eps = 0.1, ar_cons = 0, _mu=0, _sigma=0.5, p=10):
n_actions = env.action_space.n
actions = np.arange(n_actions)
dim = len(knockoff_list)
for i in range(1, episodes + 1):
state = env.reset()
ar_state = np.random.normal(_mu, _sigma, p - len(env.observation_space.low))
a = env.action_space.sample()
while True:
next_state, r, done, _ = env.step(a) # check the openAI github repo
state1 = np.concatenate((state, ar_state))[knockoff_list]
ar_state = ar_cons * ar_state + np.random.normal(_mu, _sigma, p - len(env.observation_space.low))
state2 = np.concatenate((next_state, ar_state))[knockoff_list]
if done:
r=0
dataset.add(state1, a, r, state2, done)
break
dataset.add(state1, a, r, state2, done)
next_a = opt_policy(next_state, epsilon=eps)[0]
a = next_a
state = next_state