-
Notifications
You must be signed in to change notification settings - Fork 2
/
torch_dqn.py
133 lines (107 loc) · 4.3 KB
/
torch_dqn.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
import collections
import random
import numpy as np
import csv
from pprint import pprint
# Import PyTorch library
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
# Experience memory
class ReplayBuffer():
def __init__(self, buffer_limit):
self.buffer = collections.deque(maxlen=buffer_limit)
def put(self, transition):
self.buffer.append(transition)
def sample(self, n):
mini_batch = random.sample(self.buffer, n)
s_lst, a_lst, r_lst, s_prime_lst, done_mask_lst = [], [], [], [], []
for transition in mini_batch:
s, a, r, s_prime, done_mask = transition
s_lst.append(s)
a_lst.append([a])
r_lst.append([r])
s_prime_lst.append(s_prime)
done_mask_lst.append([done_mask])
return torch.tensor(s_lst, dtype=torch.float), torch.tensor(a_lst), \
torch.tensor(r_lst), torch.tensor(s_prime_lst, dtype=torch.float), \
torch.tensor(done_mask_lst)
def size(self):
return len(self.buffer)
def writeToFile(self, file_name, transition):
f = open(file_name, 'a', newline='')
wr = csv.writer(f)
wr.writerow(transition)
f.close()
def readFromFile(self, file_name):
f= open(file_name, 'r', newline='')
rdr = csv.reader(f)
for line in rdr:
line[0] = line[0].replace('[ ', '')
line[0] = line[0].replace('[', '')
line[0] = line[0].replace(']', '')
line[0] = line[0].split(' ')
while '' in line[0]:
line[0].remove('')
temp = []
for value in line[0]:
temp.append(float(value))
line[0] = temp
line[3] = line[3].replace('[ ', '')
line[3] = line[3].replace('[', '')
line[3] = line[3].replace(']', '')
line[3] = line[3].split(' ')
while '' in line[3]:
line[3].remove('')
temp = []
for value in line[0]:
temp.append(float(value))
line[3] = temp
transition = (np.array(line[0]), int(line[1]), float(line[2]), np.array(line[3]), float(line[4]))
self.put(transition)
# Q-network
class Qnet(nn.Module):
def __init__(self, num_inputs, num_outputs, num_neurons):
super(Qnet, self).__init__()
self.num_inputs = num_inputs #3*num_tiers
self.num_outputs = num_outputs #3#2*num_nodes+1
self.num_neurons = num_neurons
self.fc1 = nn.Linear(self.num_inputs, self.num_neurons) # hidden layer 1
self.fc2 = nn.Linear(self.num_neurons, self.num_neurons) # hidden layer 2
self.fc3 = nn.Linear(self.num_neurons, self.num_neurons) # hidden layer 3
self.fc4 = nn.Linear(self.num_neurons, self.num_neurons) # hidden layer 4
self.fc5 = nn.Linear(self.num_neurons, self.num_outputs) # add 0~3 maintain 4 remove 5~8 maintain 2
def forward(self, x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
x = F.relu(self.fc4(x))
x = self.fc5(x)
return x
# Epsilon greedy implementation
def sample_action(self, obs, epsilon):
out = self.forward(obs)
coin = random.random()
policy = False
# Regarding coin to select action randomly
if coin < epsilon:
policy = False
return { "action": random.randrange(0,self.num_outputs), "type" : policy } # PARAMETER: RANDOM ACTION
else :
policy = True
return { "action": out.argmax().item(), "type": policy }
def save_model(self, path):
torch.save(self.state_dict(), path)
# Update Target Q-network
def train(q, q_target, memory, optimizer, gamma, batch_size):
for i in range(10):
s,a,r,s_prime,done_mask = memory.sample(batch_size)
q_out = q(s)
q_a = q_out.gather(1,a)
max_q_prime = q_target(s_prime).max(1)[0].unsqueeze(1)
target = r + gamma * max_q_prime * done_mask
loss = F.smooth_l1_loss(q_a, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()