-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathrl_utils.py
More file actions
75 lines (72 loc) · 2.65 KB
/
rl_utils.py
File metadata and controls
75 lines (72 loc) · 2.65 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
64
65
66
67
68
69
70
71
72
73
74
75
import torch
from copy import deepcopy
#Visualise agent function
def visualise_agent(env, policy, n=5):
try:
for trial_i in range(n):
observation = env.reset()
done=False
t=0
episode_return=0
while not done:
env.render()
action = policy(torch.tensor([observation]).double())
observation, reward, done, info = env.step(action)
episode_return+=reward
t+=1
env.render()
time.sleep(1.5)
print("Episode {} finished after {} timesteps. Return = {}".format(trial_i, t, episode_return))
env.close()
except KeyboardInterrupt:
env.close()
### Upside Down RL ###
#Visualise agent
def visualise_agent_command(env, policy, command, n=5):
try:
for trial_i in range(n):
current_command = deepcopy(command)
observation = env.reset()
done=False
t=0
episode_return=0
while not done:
env.render()
action = policy(torch.tensor([observation]).double(), torch.tensor([command]).double())
observation, reward, done, info = env.step(action)
episode_return+=reward
current_command[0]-= reward
current_command[1] = max(1, current_command[1]-1)
t+=1
env.render()
time.sleep(1.5)
print("Episode {} finished after {} timesteps. Return = {}".format(trial_i, t, episode_return))
env.close()
except KeyboardInterrupt:
env.close()
def create_greedy_policy(policy_network, command=False):
if command:
def policy(obs, command):
action_logits = policy_network(obs, command)
action = np.argmax(action_logits.detach().numpy())
return action
else:
def policy(obs):
action_logits = policy_network(obs)
action = np.argmax(action_logits.detach().numpy())
return action
return policy
def create_stochastic_policy(policy_network, command=False):
if command:
def policy(obs, command):
action_logits = policy_network(obs, command)
action_probs = F.softmax(action_logits, dim=-1)
action = torch.distributions.Categorical(action_probs).sample().item()
return action
else:
def policy(obs):
action_logits = policy_network(obs)
action_probs = F.softmax(action_logits, dim=-1)
action = torch.distributions.Categorical(action_probs).sample().item()
return action
return policy