-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patho2_policy_refine.py
executable file
·753 lines (637 loc) · 27.6 KB
/
o2_policy_refine.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
import random
import copy
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions.categorical import Categorical
import torchvision
from ipdb import set_trace
import os
from o0_state import State
# resnet
class BasicBlock(nn.Module):
"""Basic Block for resnet 18 and resnet 34"""
# BasicBlock and BottleNeck block
# have different output size
# we use class attribute expansion
# to distinct
expansion = 1
def __init__(self, in_channels, out_channels, stride=1):
super().__init__()
# residual function
self.residual_function = nn.Sequential(
nn.Conv2d(
in_channels,
out_channels,
kernel_size=3,
stride=stride,
padding=1,
bias=False,
),
# nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(
out_channels,
out_channels * BasicBlock.expansion,
kernel_size=3,
padding=1,
bias=False,
),
# nn.BatchNorm2d(out_channels * BasicBlock.expansion)
)
# shortcut
self.shortcut = nn.Sequential()
# the shortcut output dimension is not the same with residual function
# use 1*1 convolution to match the dimension
if stride != 1 or in_channels != BasicBlock.expansion * out_channels:
self.shortcut = nn.Sequential(
nn.Conv2d(
in_channels,
out_channels * BasicBlock.expansion,
kernel_size=1,
stride=stride,
bias=False,
)
# nn.BatchNorm2d(out_channels * BasicBlock.expansion)
)
def forward(self, x):
return nn.ReLU(inplace=True)(self.residual_function(x) + self.shortcut(x))
class GlobalAvgPool2d(nn.Module):
def __init__(self):
super(GlobalAvgPool2d, self).__init__()
def forward(self, x):
return torch.nn.functional.avg_pool2d(x, kernel_size=(x.shape[2], x.shape[3]))
class DeepQPolicy(nn.Module):
def __init__(
self,
block=BasicBlock,
num_block=[2, 2, 2, 2],
num_classes=(8 * 2) * 4,
action_type_num=4, # 动作的种类数量,用于确定通道数
EPS_START=0.9,
EPS_END=0.10,
EPS_DECAY=500,
device="cpu",
is_rnd_predictor=False,
pool_is_stochastic=True,
is_factor=False,
is_multi_obj=False,
):
super(DeepQPolicy, self).__init__()
self.in_channels = 64
self.action_type_num = action_type_num
# EPS Hyperparameter
self.EPS_START = EPS_START
self.EPS_END = EPS_END
self.EPS_DECAY = EPS_DECAY
# stage num
self.device = device
self.is_rnd_predictor = is_rnd_predictor
self.pool_is_stochastic = pool_is_stochastic
self.num_classes = num_classes
self.is_factor = is_factor
self.is_multi_obj = is_multi_obj
self.conv1 = nn.Sequential(
nn.Conv2d(2, 64, kernel_size=3, padding=1, bias=False),
# nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
)
# we use a different inputsize than the original paper
# so conv2_x's stride is 1
self.conv2_x = self._make_layer(block, 64, num_block[0], 1)
self.conv3_x = self._make_layer(block, 128, num_block[1], 1)
self.conv4_x = self._make_layer(block, 256, num_block[2], 1)
self.conv5_x = self._make_layer(block, 512, num_block[3], 1)
if pool_is_stochastic:
self.avg_pool = nn.AdaptiveAvgPool2d((1, 1))
else:
self.avg_pool = GlobalAvgPool2d()
if self.is_rnd_predictor:
self.fc = nn.Sequential(
nn.Linear(512 * block.expansion, 512),
nn.ReLU(),
nn.Linear(512, 512),
nn.ReLU(),
nn.Linear(512, num_classes),
)
elif not self.is_factor and not self.is_multi_obj:
self.fc = nn.Linear(512 * block.expansion, num_classes)
# action & reward buffer
self.saved_actions = []
self.rewards = []
def partially_reset(self, reset_type="xavier"):
if reset_type == "xavier":
nn.init.xavier_uniform_(self.fc.weight)
else:
raise NotImplementedError
def _make_layer(self, block, out_channels, num_blocks, stride):
"""make resnet layers(by layer i didnt mean this 'layer' was the
same as a neuron netowork layer, ex. conv layer), one layer may
contain more than one residual block
Args:
block: block type, basic block or bottle neck block
out_channels: output depth channel number of this layer
num_blocks: how many blocks per layer
stride: the stride of the first block of this layer
Return:
return a resnet layer
"""
# we have num_block blocks per layer, the first block
# could be 1 or 2, other blocks would always be 1
strides = [stride] + [1] * (num_blocks - 1)
layers = []
for stride in strides:
layers.append(block(self.in_channels, out_channels, stride))
self.in_channels = out_channels * block.expansion
return nn.Sequential(*layers)
def forward(self, x, is_target=False, state_mask=None):
# set_trace()
x = x.float().to(self.device)
if state_mask is not None:
mask = state_mask
else:
if is_target:
mask = self.mask_with_legality(x)
else:
mask = self.mask(x)
output = self.conv1(x)
output = self.conv2_x(output)
output = self.conv3_x(output)
output = self.conv4_x(output)
output = self.conv5_x(output)
output = self.avg_pool(output)
output = output.view(output.size(0), -1)
output = self.fc(output)
output = output.masked_fill(~mask.to(self.device), -1000)
# actor: choses action to take from state s_t
# by returning probability of each action
# action_prob = F.softmax(x, dim=-1)
# return values for both actor and critic as a tuple of 2 values:
# 1. a list with the probability of each action over the action space
# 2. the value from state s_t
return output
# fmt: off
def select_action(self, state: State, steps_done, deterministic=False, is_softmax=False):
info = {}
sample = np.random.uniform(0, 1)
eps_threshold = self.EPS_END + (self.EPS_START - self.EPS_END) * \
np.exp(-1. * steps_done / self.EPS_DECAY)
if deterministic:
eps_threshold = 0.
info["stage_num"] = state.get_stage_num()
info["eps_threshold"] = eps_threshold
if sample >= eps_threshold:
with torch.no_grad():
# t.max(1) will return largest column value of each row.
# second column on max result is index of where max element was
# found, so we pick action with the larger expected reward.
# print(state.shape)
tensor_state = torch.tensor(state.archive(), device=self.device)
mask = torch.tensor(state.mask_with_legality(), device=self.device)
simple_mask = torch.tensor(state.mask())
tensor_state = tensor_state.unsqueeze(0)
q: torch.Tensor = self(tensor_state, state_mask=mask)
neg_inf = torch.tensor(float('-inf'), device=self.device)
q = q.masked_fill(~mask, neg_inf)
info["mask"] = mask.cpu()
info["simple_mask"] = simple_mask
info["q_value"] = q
if is_softmax:
q_distribution = Categorical(logits=q)
action = q_distribution.sample()
else:
action = q.max(1)[1]
return action.view(1, 1), info
else:
mask = torch.tensor(state.mask_with_legality())
simple_mask = torch.tensor(state.mask())
index = torch.zeros(state.get_pp_len() * 4)
for i in range(0, state.get_pp_len() * 4):
index[i] = i
index = torch.masked_select(index, mask)
info["mask"] = mask
info["simple_mask"] = simple_mask
info["q_value"] = 0
return torch.tensor([[int(random.choice(index))]], device=self.device, dtype=torch.long), info
# fmt: on
class FactorDeepQPolicy(DeepQPolicy):
def __init__(self, block, **policy_kwargs):
super(FactorDeepQPolicy, self).__init__(block, is_factor=True, **policy_kwargs)
assert self.is_rnd_predictor != True
num_action_column = int(self.num_classes / 4)
num_action_type = 4
self.fc_column = nn.Linear(512 * block.expansion, num_action_column)
self.fc_type = nn.Linear(512 * block.expansion, num_action_type)
# action & reward buffer
self.saved_actions = []
self.rewards = []
def _combine(self, output_column, output_type):
batch_size = output_column.shape[0]
num_classes = output_column.shape[1] * output_type.shape[1]
output = torch.zeros(
(batch_size, num_classes), dtype=torch.float, device=output_column.device
)
for i in range(output_column.shape[1]):
for j in range(output_type.shape[1]):
output[:, i * 4 + j] = output_column[:, i] + output_type[:, j]
return output
def forward(self, x, is_target=False, state_mask=None):
x = x.to(self.device).float()
if state_mask is not None:
mask = state_mask
else:
if is_target:
mask = self.mask_with_legality(x)
else:
mask = self.mask(x)
output = self.conv1(x)
output = self.conv2_x(output)
output = self.conv3_x(output)
output = self.conv4_x(output)
output = self.conv5_x(output)
output = self.avg_pool(output)
output = output.view(output.size(0), -1)
output_column = self.fc_column(output) # (batch_size, num_column)
output_type = self.fc_type(output) # (batch_size, num_type)
output = self._combine(output_column, output_type)
output = output.masked_fill(~mask.to(self.device), -1000)
# actor: choses action to take from state s_t
# by returning probability of each action
# action_prob = F.softmax(x, dim=-1)
# return values for both actor and critic as a tuple of 2 values:
# 1. a list with the probability of each action over the action space
# 2. the value from state s_t
return output
class Resnet(nn.Module):
def __init__(
self,
block=BasicBlock,
num_block=[2, 2, 2, 2],
num_mask=90,
input_mask_channels=4,
pool_is_stochastic=True,
) -> None:
super().__init__()
self.in_channels = 64
self.conv1 = nn.Sequential(
nn.Conv2d(input_mask_channels, 64, kernel_size=3, padding=1, bias=False),
# nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
)
# we use a different inputsize than the original paper
# so conv2_x's stride is 1
self.conv2_x = self._make_layer(block, 64, num_block[0], 1)
self.conv3_x = self._make_layer(block, 128, num_block[1], 1)
self.conv4_x = self._make_layer(block, 256, num_block[2], 1)
self.conv5_x = self._make_layer(block, 512, num_block[3], 1)
if pool_is_stochastic:
self.avg_pool = nn.AdaptiveAvgPool2d((1, 1))
else:
self.avg_pool = GlobalAvgPool2d()
self.fc = nn.Linear(512 * block.expansion, num_mask)
def _make_layer(self, block, out_channels, num_blocks, stride):
"""make resnet layers(by layer i didnt mean this 'layer' was the
same as a neuron netowork layer, ex. conv layer), one layer may
contain more than one residual block
Args:
block: block type, basic block or bottle neck block
out_channels: output depth channel number of this layer
num_blocks: how many blocks per layer
stride: the stride of the first block of this layer
Return:
return a resnet layer
"""
# we have num_block blocks per layer, the first block
# could be 1 or 2, other blocks would always be 1
strides = [stride] + [1] * (num_blocks - 1)
layers = []
for stride in strides:
layers.append(block(self.in_channels, out_channels, stride))
self.in_channels = out_channels * block.expansion
return nn.Sequential(*layers)
def forward(self, x: torch.Tensor) -> torch.Tensor:
output = self.conv1(x)
output = self.conv2_x(output)
output = self.conv3_x(output)
output = self.conv4_x(output)
output = self.conv5_x(output)
output = self.avg_pool(output)
output = output.view(output.size(0), -1)
output = self.fc(output)
return output
class MaskDeepQPolicy(nn.Module):
"""
仿照 maskplace
"""
# fmt: off
def __init__(
self,
block=BasicBlock,
device="cpu",
num_classes=60, # pp_width x action_type_num
num_mask=90, # max_stage_num x pp_width
num_block=[2, 2, 2, 2],
input_state_channels=2, # e.g. ct 的通道数 (+ comap, = 4)
input_feature_channels=2, # e.g. power mask 的通道数
action_type_num=4, # 动作的种类数量,用于确定通道数
EPS_START=0.9,
EPS_END=0.10,
EPS_DECAY=500,
is_rnd_predictor=False,
pool_is_stochastic=True,
is_factor=False,
is_multi_obj=False,
pretrained_path=None,
) -> None:
super().__init__()
self.device = device
self.num_classes = num_classes
self.num_mask = num_mask
self.action_type_num = action_type_num
self.input_state_channels = input_state_channels
self.input_feature_channels = input_feature_channels
self.EPS_START = EPS_START
self.EPS_END = EPS_END
self.EPS_DECAY = EPS_DECAY
self.is_rnd_predictor = is_rnd_predictor
self.is_factor = is_factor
self.is_multi_obj = is_multi_obj
local_channels = action_type_num + input_feature_channels
self.local_fusion_net = nn.Sequential(
nn.Conv2d(local_channels, 2 * local_channels, 1), # action mask + power mask
nn.ReLU(),
nn.Conv2d(2 * local_channels, 2 * local_channels, 1),
nn.ReLU(),
nn.Conv2d(2 * local_channels, 1, 1),
)
self.global_encoder_net = Resnet(
block, num_block, num_mask, input_feature_channels + input_state_channels, pool_is_stochastic
)
if pretrained_path is not None and os.path.exists(pretrained_path):
self.global_encoder_net.load_state_dict(torch.load(pretrained_path))
self.merge = nn.Conv2d(2, 1, 1)
if self.is_rnd_predictor:
self.global_encoder_net.fc = nn.Sequential(
nn.Linear(block.expansion * 512, 512),
nn.ReLU(),
nn.Linear(512, 512),
nn.ReLU(),
nn.Linear(512, num_mask),
)
self.fc = nn.Linear(num_mask, num_classes)
elif not self.is_factor and not self.is_multi_obj:
self.fc = nn.Linear(num_mask, num_classes)
# fmt: off
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
Parameters:
x: shape [批次大小, 通道数量, max_stage_num, pp width]
通道 0, 1 : 压缩树
通道 2, 3 : power mask
通道 4 - 7: action_mask
"""
x = x.float().to(self.device)
max_stage_num = x.shape[2]
pp_width = x.shape[3]
# $$DEBUG
# assert self.num_classes == self.action_type_num * pp_width
state_mask = x[:, :self.input_state_channels, :, :]
feature_mask = x[:, self.input_state_channels : self.input_state_channels + self.input_feature_channels, :, :]
action_mask = x[:, self.input_state_channels + self.input_feature_channels:, :] # channel = self.action_type_num
local_mask_input = torch.concat([feature_mask, action_mask], 1) # channel = 6
local_mask = self.local_fusion_net(local_mask_input) # channel = 1
global_mask_input = torch.concat([state_mask, feature_mask], 1) # channel = 4
global_mask = self.global_encoder_net(global_mask_input).reshape([-1, 1, max_stage_num, pp_width]) # channel = 1
merge_input = local_mask_input = torch.concat([local_mask, global_mask], 1) # channel = 2
merge_mask = self.merge(merge_input) # channel = 1
merge_mask = merge_mask.reshape([-1, max_stage_num * pp_width])
action_value:torch.Tensor = self.fc(merge_mask)
action_mask_bool = action_mask > 0.0
batch_indices, action_type, _, action_column = torch.where(action_mask_bool)
action_indices = action_column * self.action_type_num + action_type
mask = torch.zeros((action_mask_bool.shape[0], self.num_classes), dtype=torch.bool)
mask[batch_indices, action_indices] = True
action_value = action_value.masked_fill(~mask.to(self.device), -1e3)
return action_value
# fmt: off
def select_action(self, state: State, steps_done, deterministic=False, is_softmax=False):
info = {}
sample = np.random.uniform(0, 1)
eps_threshold = self.EPS_END + (self.EPS_START - self.EPS_END) * \
np.exp(-1. * steps_done / self.EPS_DECAY)
if deterministic:
eps_threshold = 0.
info["stage_num"] = state.get_stage_num()
info["eps_threshold"] = eps_threshold
if sample >= eps_threshold:
with torch.no_grad():
mask = torch.tensor(state.mask_with_legality(), device=self.device)
simple_mask = torch.tensor(state.mask())
tensor_state = torch.tensor(state.archive(return_mask=True), device=self.device)
tensor_state = tensor_state.unsqueeze(0)
q: torch.Tensor = self(tensor_state)
neg_inf = torch.tensor(float('-inf'), device=self.device)
q = q.masked_fill(~mask, neg_inf)
info["mask"] = mask.cpu()
info["simple_mask"] = simple_mask
info["q_value"] = q
if is_softmax:
q_distribution = Categorical(logits=q)
action = q_distribution.sample()
else:
action = q.max(1)[1]
return action.view(1, 1), info
else:
mask = torch.tensor(state.mask_with_legality())
simple_mask = torch.tensor(state.mask())
index = torch.zeros(state.get_pp_len() * self.action_type_num)
for i in range(0, state.get_pp_len() * self.action_type_num):
index[i] = i
index = torch.masked_select(index, mask)
info["mask"] = mask
info["simple_mask"] = simple_mask
info["q_value"] = 0
return torch.tensor([[int(random.choice(index))]], device=self.device, dtype=torch.long), info
# fmt: on
class MaskFactorDeepQPolicy(MaskDeepQPolicy):
def __init__(self, block, **policy_kwargs) -> None:
super().__init__(block, is_factor=True, **policy_kwargs)
assert self.is_rnd_predictor != True
num_action_column = int(self.num_classes / self.action_type_num)
self.fc_column = nn.Linear(self.num_mask, num_action_column)
self.fc_type = nn.Linear(self.num_mask, self.action_type_num)
def _combine(self, output_column, output_type):
batch_size = output_column.shape[0]
num_classes = output_column.shape[1] * output_type.shape[1]
output = torch.zeros(
(batch_size, num_classes), dtype=torch.float, device=output_column.device
)
for i in range(output_column.shape[1]):
for j in range(output_type.shape[1]):
output[:, i * self.action_type_num + j] = (
output_column[:, i] + output_type[:, j]
)
return output
# fmt: off
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
Parameters:
x: shape [批次大小, 通道数量, max_stage_num, pp width]
通道 0, 1 : 压缩树
通道 2, 3 : power mask
通道 4 - 7: action_mask
"""
x = x.float().to(self.device)
max_stage_num = x.shape[2]
pp_width = x.shape[3]
# $$DEBUG
# assert self.num_classes == self.action_type_num * pp_width
state_mask = x[:, :self.input_state_channels, :, :]
feature_mask = x[:, self.input_state_channels : self.input_state_channels + self.input_feature_channels, :, :]
action_mask = x[:, self.input_state_channels + self.input_feature_channels:, :] # channel = self.action_type_num
local_mask_input = torch.concat([feature_mask, action_mask], 1) # channel = 6
local_mask = self.local_fusion_net(local_mask_input) # channel = 1
global_mask_input = torch.concat([state_mask, feature_mask], 1) # channel = 4
global_mask = self.global_encoder_net(global_mask_input).reshape([-1, 1, max_stage_num, pp_width]) # channel = 1
merge_input = local_mask_input = torch.concat([local_mask, global_mask], 1) # channel = 2
merge_mask = self.merge(merge_input) # channel = 1
merge_mask = merge_mask.reshape([-1, max_stage_num * pp_width])
# factor !
action_value_column = self.fc_column(merge_mask)
action_value_type = self.fc_type(merge_mask)
action_value = self._combine(action_value_column, action_value_type)
action_mask_bool = action_mask > 0.0
batch_indices, action_type, _, action_column = torch.where(action_mask_bool)
action_indices = action_column * self.action_type_num + action_type
mask = torch.zeros((action_mask_bool.shape[0], self.num_classes), dtype=torch.bool)
mask[batch_indices, action_indices] = True
action_value = action_value.masked_fill(~mask.to(self.device), -1e3)
return action_value
class PrefixDeepQPolicy(nn.Module):
def __init__(
self,
block=BasicBlock,
num_block=[2, 2, 2, 2],
input_feature_channels=4,
action_mask_loc=2,
input_width=8,
EPS_START=0.9,
EPS_END=0.10,
EPS_DECAY=500,
device="cpu",
pool_is_stochastic=True,
is_factor=False,
is_rnd_predictor=False,
) -> None:
super().__init__()
self.input_feature_channels = input_feature_channels
self.input_width = input_width
self.action_mask_loc = action_mask_loc
self.EPS_START = EPS_START
self.EPS_END = EPS_END
self.EPS_DECAY = EPS_DECAY
self.device = device
self.is_factor = is_factor
self.is_rnd_predictor = is_rnd_predictor
self.conv = Resnet(
block,
num_block,
2 * input_width * input_width,
input_feature_channels,
pool_is_stochastic,
)
# fmt: off
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
x:
channel 0: cell map
channel 1: level map
channel 2: action_mask_0
channel 3: action_mask_1
"""
x = x.float().to(self.device)
action_mask = x[:, self.action_mask_loc : self.action_mask_loc + 2, :, :].flatten()
action_mask_bool = action_mask > 0
action_value: torch.Tensor = self.conv(x)
action_value = action_value.masked_fill(~action_mask_bool.to(self.device), 1e-3)
return action_value
# fmt: off
def select_action(self, state: State, steps_done, deterministic=False, is_softmax=False):
"""
action_type = action // (input_bit ** 2)
x = (action % (input_bit ** 2)) // input_bit
y = (action % (input_bit ** 2)) % input_bit
action = action_type * input_bit ** 2 + x * input_bit + y
"""
info = {}
sample = np.random.uniform(0, 1)
eps_threshold = self.EPS_END + (self.EPS_START - self.EPS_END) * \
np.exp(-1. * steps_done / self.EPS_DECAY)
if deterministic:
eps_threshold = 0.
info["eps_threshold"] = eps_threshold
if sample >= eps_threshold:
with torch.no_grad():
if self.input_feature_channels == 4:
tensor_state = torch.tensor(state.archive_cell_map(), device=self.device)
else:
tensor_state = torch.tensor(state.archive_cell_map(True), device=self.device)
mask = torch.tensor(state.mask_cell_map(), device=self.device)
tensor_state = tensor_state.unsqueeze(0)
q: torch.Tensor = self(tensor_state)
neg_inf = torch.tensor(float('-inf'), device=self.device)
q = q.masked_fill(~mask, neg_inf)
info["mask"] = mask.cpu()
info["q_value"] = q
if is_softmax:
q_distribution = Categorical(logits=q)
action = q_distribution.sample()
else:
action = q.max(1)[1]
return action.view(1, 1), info
else:
mask = state.mask_cell_map()
mask = mask > 0
indices = np.where(mask)[0]
index = np.random.choice(indices)
info["mask"] = mask
info["q_value"] = 0
return torch.tensor([[int(index)]], device=self.device, dtype=torch.long), info
class FactorPrefixDeepQPolicy(PrefixDeepQPolicy):
def __init__(self, block, **policy_kwargs) -> None:
super().__init__(block, is_factor=True, **policy_kwargs)
assert self.is_rnd_predictor != True
self.conv.fc = nn.Identity()
self.fc_type = nn.Linear(512 * block.expansion, 2)
self.fc_x = nn.Linear(512 * block.expansion, self.input_width)
self.fc_y = nn.Linear(512 * block.expansion, self.input_width)
def __combine(self, output_type, output_x, output_y):
batch_size = output_type.shape[0]
action_value = torch.zeros((batch_size, 2 * self.input_width ** 2), dtype=torch.float, device=output_type.device)
for i in range(output_type.shape[1]):
for j in range(output_x.shape[1]):
for k in range(output_y.shape[1]):
action_value[:, i * (self.input_width ** 2) + j * self.input_width + k] = output_type[:, i] + output_x[:, j] + output_y[:, k]
return action_value
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
x:
channel 0: cell map
channel 1: level map
channel 2: action_mask_0
channel 3: action_mask_1
"""
x = x.float().to(self.device)
action_mask = x[:, self.action_mask_loc : self.action_mask_loc + 2, :, :].flatten()
action_mask_bool = action_mask > 0
output: torch.Tensor = self.conv(x)
output_type = self.fc_type(output)
output_x = self.fc_x(output)
output_y = self.fc_y(output)
action_value = self.__combine(output_type, output_x, output_y)
action_value = action_value.masked_fill(~action_mask_bool.to(self.device), 1e-3)
return action_value
if __name__ == "__main__":
pass