-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
61 lines (49 loc) · 2.05 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
53
54
55
56
57
58
59
60
61
import collections
import numpy as np
import torch
Experience = collections.namedtuple('Experience', field_names=['state', 'action', 'reward', 'done', 'new_state'])
class ExperienceBuffer:
def __init__(self, capacity):
self.buffer = collections.deque(maxlen=capacity)
def __len__(self):
return len(self.buffer)
def append(self, experience):
self.buffer.append(experience)
def sample(self, batch_size):
indices = np.random.choice(len(self.buffer), batch_size, replace=False)
states, actions, rewards, dones, next_states = zip(*[self.buffer[idx] for idx in indices])
return np.array(states), np.array(actions), np.array(rewards, dtype=np.float32), \
np.array(dones, dtype=np.uint8), np.array(next_states)
class Agent:
def __init__(self, env, exp_buffer):
self.env = env
self.reset_env = env
self.exp_buffer = exp_buffer
self._reset()
self.generation_count = 0
def _reset(self):
#self.state = self.env.reset()
self.state = self.reset_env.reset()
self.total_reward = 0.0
@torch.no_grad()
def play_step(self, net, epsilon=0.0, device="cpu"):
done_reward = None
if np.random.random() < epsilon: # select random action or action from NN
action = self.env.sample_action()
else:
state_a = np.array([self.state], copy=False).astype("float32")
state_v = torch.from_numpy(state_a).to(device, dtype=torch.float32)
q_vals_v = net(state_v)
_, action_v = torch.max(q_vals_v, dim=1)
action = int(action_v.item())
# do step in the environment
new_state, reward, is_done, _ = self.env.step(action)
self.total_reward += reward
exp = Experience(self.state, action, reward, is_done, new_state)
self.exp_buffer.append(exp)
self.state = new_state
if is_done:
done_reward = self.total_reward
self._reset()
self.generation_count += 1
return done_reward