-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdqn_agent.py
203 lines (154 loc) · 7.32 KB
/
dqn_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import numpy as np
import torch as T
from deep_q_network import DeepQNetwork
from replay_memory import ReplayBuffer
from numba import jit
class DQNAgent(object):
def __init__(self, gamma, nnet_params, other_params, input_dims=4, dir=None, offline=False, status="online"):
self.gamma = gamma
self.eps_init = nnet_params['eps_init']
self.epsilon = 0.01 #nnet_params['eps_init']
self.eps_final = nnet_params['eps_final']
self.eps_decay_steps = other_params['eps_decay_steps']
self.n_actions = nnet_params['num_actions']
self.batch_size = nnet_params['batch_size']
self.lr = other_params['nn_lr']
self.global_step = 5.5
self.replace_target_cnt = nnet_params['update_target_net_steps']
self.input_dims = input_dims
self.algo = 'dgn'
self.env_name = 'catcher'
self.chkpt_dir = 'tmp/dqn'
self.action_space = [i for i in range( self.n_actions)]
self.learn_step_counter = 0
self.memory_load_direction = dir
self.offline = offline
self.status = status
self.memory = ReplayBuffer(nnet_params['replay_memory_size'], input_dims, self.n_actions, self.offline, self.memory_load_direction)
self.q_eval = DeepQNetwork(self.lr, self.n_actions,
name=self.env_name + '_' + self.algo + '_q_eval',
input_dims=self.input_dims,
chkpt_dir=self.chkpt_dir, number_unit=128)
self.q_next = DeepQNetwork(self.lr, self.n_actions,
name=self.env_name + '_' + self.algo + '_q_eval',
input_dims=self.input_dims,
chkpt_dir=self.chkpt_dir, number_unit=128)
# def choose_action(self, observation):
# state = T.tensor([observation], dtype=T.float).to(self.q_eval.device)
# actions = self.q_eval.forward(state)
# if np.random.random() > self.epsilon:
# action = T.argmax(actions).item()
# actions.detach().numpy()
# else:
# action = np.random.choice(self.action_space)
#
# return action, actions.detach()
# @jit(target='cuda')
def choose_action(self, observation):
if self.status == "offline":
epsilon = 0
elif self.status == "online":
epsilon = self.epsilon
elif self.status == "offline_online":
epsilon = self.epsilon
with T.no_grad():
state = T.tensor([observation], dtype=T.float).to(self.q_eval.device)
actions = self.q_eval.forward(state)
actions.detach()
if np.random.random() > epsilon:
action = T.argmax(actions).item()
else:
action = np.random.choice(self.action_space)
return action, actions
# @jit(target='cuda')
def store_transition(self, state, action, reward, state_, done):
self.memory.store_transition(state, action, reward, state_, done)
# @jit(target='cuda')
def sample_memory(self):
state, action, reward, new_state, done = \
self.memory.sample_buffer(self.batch_size)
states = T.tensor(state).to(self.q_eval.device)
rewards = T.tensor(reward).to(self.q_eval.device)
dones = T.tensor(done).to(self.q_eval.device)
actions = T.tensor(action).to(self.q_eval.device)
states_ = T.tensor(new_state).to(self.q_eval.device)
return states, actions, rewards, states_, dones
# @jit(target='cuda')
def replace_target_network(self):
if self.learn_step_counter % self.replace_target_cnt == 0:
self.q_next.load_state_dict(self.q_eval.state_dict())
# @jit(target='cuda')
def decrement_epsilon(self):
self.epsilon = self.epsilon
# self.epsilon = self.epsilon - self.eps_dec if self.epsilon > self.eps_min else self.eps_min
# self.epsilon = self.eps_init * (self.eps_final ** (
# self.global_step / self.eps_decay_steps)) if self.epsilon > self.eps_final else self.eps_final
# @jit(target='cuda')
def save_models(self):
self.q_eval.save_checkpoint()
self.q_next.save_checkpoint()
# @jit(target='cuda')
def load_models(self):
self.q_eval.load_checkpoint()
self.q_next.load_checkpoint()
def sample_memory_nextaction_shuffling(self, itr, shuffle_index):
state, action, reward, new_state, new_action, done = \
self.memory.sample_buffer_nextaction_givenindex( self.batch_size, itr, shuffle_index)
states = T.tensor(state).to(self.q_eval.device)
rewards = T.tensor(reward).to(self.q_eval.device)
dones = T.tensor(done).to(self.q_eval.device)
actions = T.tensor(action).to(self.q_eval.device)
states_ = T.tensor(new_state).to(self.q_eval.device)
actions_ = T.tensor(new_action).to(self.q_eval.device)
return states, actions, rewards, states_, actions_, dones
# @jit(target='cuda')
def learn(self):
if self.memory.mem_cntr < self.batch_size:
return
# print('Using device:', self.q_eval.device)
# print()
# Additional Info when using cuda
if self.q_eval.device.type == 'cuda':
print(T.cuda.get_device_name(0))
print('Memory Usage:')
print('Allocated:', round(T.cuda.memory_allocated(0) / 1024 ** 3, 1), 'GB')
print('Cached: ', round(T.cuda.memory_cached(0) / 1024 ** 3, 1), 'GB')
self.q_eval.optimizer.zero_grad()
self.replace_target_network()
states, actions, rewards, states_, dones = self.sample_memory()
indices = np.arange(self.batch_size)
q_pred = self.q_eval.forward(states)[indices, actions]
q_next = self.q_next.forward(states_).max(dim=1)[0]
q_next[dones] = 0.0
q_target = rewards + self.gamma*q_next
loss = self.q_eval.loss(q_target, q_pred).to(self.q_eval.device)
loss.backward()
self.q_eval.optimizer.step()
self.learn_step_counter += 1
self.decrement_epsilon()
return loss
def learn_nn_feature(self, itr, shuffle_index):
if self.memory.mem_cntr < self.batch_size:
return
# print('Using device:', self.q_eval.device)
# print()
# Additional Info when using cuda
if self.q_eval.device.type == 'cuda':
print(T.cuda.get_device_name(0))
print('Memory Usage:')
print('Allocated:', round(T.cuda.memory_allocated(0) / 1024 ** 3, 1), 'GB')
print('Cached: ', round(T.cuda.memory_cached(0) / 1024 ** 3, 1), 'GB')
self.q_eval.optimizer.zero_grad()
self.replace_target_network()
states, actions, rewards, states_, actions_, dones = self.sample_memory_nextaction_shuffling(itr, shuffle_index)
indices = np.arange(self.batch_size)
q_pred = self.q_eval.forward(states)[indices, actions]
q_next = self.q_next.forward(states_).max(dim=1)[0]
q_next[dones] = 0.0
q_target = rewards + self.gamma*q_next
loss = self.q_eval.loss(q_target, q_pred).to(self.q_eval.device)
loss.backward()
self.q_eval.optimizer.step()
self.learn_step_counter += 1
self.decrement_epsilon()
return loss