-
Notifications
You must be signed in to change notification settings - Fork 4
/
Agent.py
52 lines (44 loc) · 1.66 KB
/
Agent.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import time
import numpy as np
class Agent:
"""An general class for RL agents.
"""
def sample(self, horizon, policy, record=False, env=None, mode='train'):
"""Samples a rollout from the agent.
Arguments:
horizon: (int) The length of the rollout to generate from the agent.
policy: (policy) The policy that the agent will use for actions.
record: (bool) Whether to record the rollout
env: (gym.Env) Environment to rollout
mode: (str) Whether we're training or testing, used for resetting the env
Returns: (dict) A dictionary containing data from the rollout.
"""
times, rewards = [], []
policy.mode = mode
O, A, reward_sum, done = [env.reset(mode=mode)], [], 0, False
policy.reset()
for t in range(horizon):
start = time.time()
policy_action = policy.act(O[t], t)
A.append(policy_action)
times.append(time.time() - start)
obs, reward, done, info = env.step(policy_action) # A[t]
O.append(obs)
reward_sum += reward
rewards.append(reward)
if done:
break
if record:
env.close()
print("Average action selection time: ", np.mean(times))
print("Rollout length: ", len(A))
return {
"obs": np.array(O),
"ac": np.array(A),
"reward_sum": reward_sum,
"rewards": np.array(rewards),
"catastrophe": info['Catastrophe'],
}