-
Notifications
You must be signed in to change notification settings - Fork 0
/
agents.py
317 lines (288 loc) · 6.61 KB
/
agents.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import torch
from torch import nn, Tensor
from copy import deepcopy
from collections import deque
from random import randint
class Bare_minimum(torch.nn.Module):
def __init__(self):
super(Bare_minimum, self).__init__()
self.body = nn.Sequential(
nn.Linear(16, 5), # 85
nn.ReLU(),
nn.Sigmoid(),
nn.Linear(5, 3) # 103
)
def forward(self, x):
return self.body(x)
class Agent_16(torch.nn.Module):
def __init__(self):
super(Agent_16, self).__init__()
self.body = nn.Sequential(
nn.Linear(16, 7),
nn.ReLU(),
nn.Sigmoid(),
nn.Linear(7, 6),
nn.ReLU(),
nn.Sigmoid(),
nn.Linear(6, 3),
)
def forward(self, x):
return self.body(x)
class Agent_32(torch.nn.Module):
def __init__(self):
super(Agent_32, self).__init__()
self.body = nn.Sequential(
nn.Linear(16, 9),
nn.ReLU(),
nn.Sigmoid(),
nn.Linear(9, 11),
nn.ReLU(),
nn.Sigmoid(),
nn.Linear(11, 9),
nn.ReLU(),
nn.Sigmoid(),
nn.Linear(9, 3)
)
def forward(self, x):
return self.body(x)
class Agent_64(torch.nn.Module):
def __init__(self):
super(Agent_64, self).__init__()
self.body = nn.Sequential(
nn.Linear(16, 13),
nn.ReLU(),
nn.Sigmoid(),
nn.Linear(13, 21),
nn.ReLU(),
nn.Sigmoid(),
nn.Linear(21, 18),
nn.ReLU(),
nn.Sigmoid(),
nn.Linear(18, 9),
nn.ReLU(),
nn.Sigmoid(),
nn.Linear(9, 3)
)
def forward(self, x):
return self.body(x)
class Agent_128(torch.nn.Module):
def __init__(self):
super(Agent_128, self).__init__()
self.body = nn.Sequential(
nn.Linear(16, 10),
nn.ReLU(),
nn.Sigmoid(),
nn.Linear(10, 16),
nn.ReLU(),
nn.Sigmoid(),
nn.Linear(16, 27),
nn.ReLU(),
nn.Sigmoid(),
nn.Linear(27, 18),
nn.ReLU(),
nn.Sigmoid(),
nn.Linear(18, 27),
nn.ReLU(),
nn.Sigmoid(),
nn.Linear(27, 18),
nn.ReLU(),
nn.Sigmoid(),
nn.Linear(18, 9),
nn.ReLU(),
nn.Sigmoid(),
nn.Linear(9, 3)
)
def forward(self, x):
return self.body(x)
class Agent(object):
def __init__(self, VISUAL_CORTEX_PATH='VAE/VAE_model.pt', \
agent_name='Bare_minimum'):
AGENTS = {'Bare_minimum':Bare_minimum(),
'16_neurons' :Agent_16(),
'32_neurons' :Agent_32(),
'64_neurons' :Agent_64(),
'128_neurons' :Agent_128()}
self.body = AGENTS[agent_name]
self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
self.visual_cortex = torch.load(VISUAL_CORTEX_PATH)
self.visual_cortex = self.visual_cortex.to(self.device)
self.body = self.body.to(self.device)
self.state_dict_info = deepcopy(self.body.state_dict())
self.genome_length=0
# key: (parameters_number, exact_tensor_size)
for key in self.state_dict_info:
parameters_number = self.state_dict_info[key].view(-1).shape[0]
self.genome_length+=parameters_number
self.state_dict_info[key] = \
(parameters_number, self.state_dict_info[key].shape)
def create_generator(self):
ids = range(self.genome_length)
for i in ids:
yield i
def set_genome(self, genome):
generator = self.create_generator()
new_state_dict = deepcopy(self.state_dict_info)
for key in new_state_dict:
item = Tensor(
[genome[next(generator)] for _ in range(new_state_dict[key][0])]
)
item = item.view(new_state_dict[key][1])
new_state_dict[key] = item
self.body.load_state_dict(new_state_dict)
def __call__(self, x):
x = x.to(self.device)
with torch.no_grad():
x = self.visual_cortex(x) # [B,4,13,13]
action_space = self.body.forward(x)
return torch.argmax(action_space)-1
class Rule_based_agent(object):
"""Rule_based_agent could be used for 9x9 field only"""
def __init__(self, mask_id):
if mask_id=='1':
self.mask = deque([
((1,1), -1),
((1,7), -1),
((2,7), -1),
((2,2), 1),
((3,2), 1),
((3,7), -1),
((4,7), -1),
((4,2), 1),
((5,2), 1),
((5,7), -1),
((7,7), -1),
((7,6), -1),
((6,6), 1),
((6,5), 1),
((7,5), -1),
((7,4), -1),
((6,4), 1),
((6,3), 1),
((7,3), -1),
((7,2), 'choice')
])
elif mask_id=='2':
self.mask = deque([
((1,1), -1),
((1,2), -1),
((2,2), 1),
((2,3), 1),
((1,3), -1),
((1,4), -1),
((2,4), 1),
((2,5), 1),
((1,5), -1),
((1,6), 'choice'),
((3,7), -1),
((3,4), 1),
((4,4), 1),
((4,7), -1),
((5,7), -1),
((5,4), 1),
((6,4), 1),
((6,7), -1),
((7,7), -1),
((7,1), -1),
((6,1), -1),
((6,3), 1),
((5,3), 1),
((5,1), -1),
((4,1), -1),
((4,3), 1),
((3,3), 1),
((3,1), -1)
])
elif mask_id=='3':
self.mask = deque([
((1,1), -1),
((1,3), -1),
((2,3), -1),
((2,2), 1),
((3,2), 1),
((3,5), 1),
((2,5), 1),
((2,4), -1),
((1,4), -1),
((1,7), -1),
((2,7), -1),
((2,6), 1),
((3,6), 1),
((3,7), -1),
((7,7), -1),
((7,6), -1),
((4,6), 1),
((4,5), 1),
((7,5), -1),
((7,4), -1),
((4,4), 1),
((4,3), 1),
((7,3), -1),
((7,1), -1),
((6,1), -1),
((6,2), 1),
((5,2), 'choice')
])
else:
raise ValueError
self.mask_id = mask_id
self.chosen_route = []
self.position, self.action = self.get_condition((-1,-1))
def choice(self, food_position):
if self.mask_id=='1':
ROUTES = {-1: deque([((6,2), 1),
((6,1), -1)]),
0 : deque([((7, 1), -1)])}
if food_position in [(6,2), (7,1)]:
if food_position in [(6,2)]:
action = -1
else:
action = 0
else:
action = randint(-1,0)
self.chosen_route = ROUTES[action]
return action
elif self.mask_id=='2':
ROUTES = {-1: deque([((2,6), 1),
((2,7), -1)]),
0 : deque([((1, 7), -1)])}
if food_position in [(2,6), (1,7)]:
if food_position in [(2,6)]:
action = -1
else:
action = 0
else:
action = randint(-1,0)
self.chosen_route = ROUTES[action]
return action
elif self.mask_id=='3':
ROUTES = {0 : deque([((4,2), 1),
((4,1), -1)]),
1 : deque([((5, 1), -1)])}
if food_position in [(4,2), (5,1)]:
if food_position in [(4,2)]:
action = 0
else:
action = 1
else:
action = randint(0,1)
self.chosen_route = ROUTES[action]
return action
else:
raise ValueError
def get_condition(self, food_position):
if not self.chosen_route:
condition = self.mask.popleft()
self.mask.append(condition)
pos, act = condition
if act=='choice':
act = self.choice(food_position)
else:
pos, act = self.chosen_route.popleft()
return pos, act
def get_action(self, head_position, food_position):
if head_position not in [self.position]:
return 0
else:
output = self.action
self.position, self.action = self.get_condition(food_position)
return output