This repository has been archived by the owner on Sep 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
229 lines (178 loc) · 6.66 KB
/
models.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import torch
import torch.nn as nn
import torch.optim as optim
import torch.functional as F
import os
import random
from abc import ABC, abstractmethod
from collections import namedtuple
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
class SortingAgent(ABC):
"""Base sorting agent"""
def __init__(self, arr):
self.arr = arr
def switch_elements(self, idx_1, idx_2):
temp = self.arr[idx_1]
self.arr[idx_1] = self.arr[idx_2]
self.arr[idx_2] = temp
@abstractmethod
def update(self):
pass
class RandomAgent(SortingAgent):
"""Sorts with a random policy"""
def update(self):
self.switch_elements(
random.randint(0, len(self.arr)-1),
random.randint(0, len(self.arr)-1),
)
class BubbleSortAgent(SortingAgent):
"""Sorts using bubble sort"""
def __init__(self, arr):
super(BubbleSortAgent, self).__init__(arr)
self.index = 1
def update(self):
# Quick exit if sorted
if self.arr == sorted(self.arr):
return
while not self.arr[self.index - 1] > self.arr[self.index]:
self.index += 1
if self.index == len(self.arr):
self.index = 1
temp = self.arr[self.index - 1]
self.arr[self.index - 1] = self.arr[self.index]
self.arr[self.index] = temp
# === Reward funcs ===
def get_local_score(arr, idx):
"""Check if considered sorted in local area"""
score = 0
score += -1 if idx - 1 >= 0 and not arr[idx] >= arr[idx-1] else 1
score += -1 if idx + 1 < len(arr) and not arr[idx] <= arr[idx+1] else 1
return score
def get_ascending_score(arr):
"""Reward based on number of ascending items"""
score = 0
for i in range(len(arr)-1):
score += arr[i+1] - arr[i]
return score
def get_inplace_score(arr):
"""Reward based on position of item"""
score = 0
for i, val in enumerate(arr):
score += 1 if i == val else 0
return score
def get_reward(prev_state, state):
"""Reward function"""
prev_arr = prev_state.tolist()
arr = state.tolist()
# Bonus for completion
modifier = 0
if arr == sorted(arr):
modifier = 10
return get_inplace_score(arr) - get_inplace_score(prev_arr) + modifier
class DQN(nn.Module):
"""Q table approximater"""
def __init__(self, n):
super(DQN, self).__init__()
self.net = nn.Sequential(
nn.Linear(n, 32),
nn.ReLU(),
nn.Linear(32, 64),
nn.ReLU(),
nn.Linear(64, n**2),
)
def forward(self, x):
return self.net(x)
Transition = namedtuple('Transition', ('state', 'action', 'next_state', 'reward'))
class ReplayMemory:
"""Replay memory"""
def __init__(self, capacity):
self.capacity = capacity
self.memory = []
self.position = 0
def push(self, *args):
"""Push transition into memory"""
if len(self.memory) < self.capacity:
self.memory.append(None)
self.memory[self.position] = Transition(*args)
self.position = (self.position + 1) % self.capacity
def sample(self, batch_size):
"""Sample minibatch from memory"""
return random.sample(self.memory, batch_size)
def clear(self):
self.memory.clear()
self.position = 0
def __len__(self):
return len(self.memory)
class DQAgent(SortingAgent):
"""Deep Q learning agent"""
def __init__(self, arr, discount=0.99, is_train=False, lr=1e-4, batch_size=32):
super(DQAgent, self).__init__(arr)
self.is_train = is_train
self.dqn = DQN(len(arr))
self.discount = discount
self.loss_f = nn.MSELoss()
self.optimizer = optim.Adam(self.dqn.parameters(), lr=lr)
self.batch_size = batch_size
self.replay_memory = ReplayMemory(1000000)
self.steps = 0
self.total_loss = 0
def reset(self):
"""Reset agent"""
self.steps = 0
self.total_loss = 0
def load_model(self, path):
self.dqn.load_state_dict(torch.load(path))
self.dqn.eval()
def update(self):
# Training
if self.is_train:
state = torch.Tensor(self.arr)
# Exploit or explore
working_epsilon = 0.1 if self.steps > 1e6 else -self.steps*0.9/1e6 + 1
if random.random() > 1 - working_epsilon:
with torch.no_grad():
_, action = torch.max(self.dqn(torch.Tensor(self.arr)), 0)
idx_1 = action // len(self.arr)
idx_2 = action % len(self.arr)
else:
action = random.randint(0, len(self.arr)**2 - 1)
idx_1 = action // len(self.arr)
idx_2 = action % len(self.arr)
self.switch_elements(idx_1, idx_2)
next_state = torch.Tensor(self.arr)
reward = get_reward(state, next_state)
# Update memory and steps
self.replay_memory.push(state, action, next_state, reward)
self.steps += 1
# Train DQN
if len(self.replay_memory) >= self.batch_size:
# Extract minibatch
minibatch = self.replay_memory.sample(self.batch_size)
state_batch = torch.stack([ t.state for t in minibatch ])
action_batch = [t.action for t in minibatch]
next_state_batch = torch.stack([ t.next_state for t in minibatch ])
reward_batch = [t.reward for t in minibatch]
# Zero optimizer
self.optimizer.zero_grad()
# Get q values from net and bellman
q_pred = self.dqn(state_batch)
with torch.no_grad():
q_target = self.dqn(next_state_batch)
for i in range(self.batch_size):
# Do bellman
l = next_state_batch[i].tolist()
q_target[i][action_batch[i]] = reward_batch[i] + self.discount * torch.max(q_target[i])
# Calculate loss and optimize
loss = self.loss_f(q_pred, q_target)
loss.backward()
self.optimizer.step()
self.total_loss += loss.item()
# Testing
else:
with torch.no_grad():
q_values = self.dqn(torch.Tensor(self.arr))
q_value, action = torch.max(q_values, 0)
self.switch_elements(
action // len(self.arr),
action % len(self.arr),
)