-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.py
More file actions
148 lines (123 loc) · 5.18 KB
/
Copy pathmodel.py
File metadata and controls
148 lines (123 loc) · 5.18 KB
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
import torch
import torch.nn as nn
import torch.nn.functional as F
class DuelingDQN(nn.Module):
"""
Dueling DQN architecture.
Separates value estimation (how good is this state?) from
advantage estimation (how much better is each action?).
"""
def __init__(self, input_channels, action_dim=6, input_size=(84, 84)):
super(DuelingDQN, self).__init__()
# Input: (input_channels, H, W)
# Using 3 layers of convolutions as per Nature paper
self.conv1 = nn.Conv2d(input_channels, 32, kernel_size=8, stride=4)
self.conv2 = nn.Conv2d(32, 64, kernel_size=4, stride=2)
self.conv3 = nn.Conv2d(64, 64, kernel_size=3, stride=1)
# Calculate flat size dynamically based on input_size
with torch.no_grad():
dummy_input = torch.zeros(1, input_channels, *input_size)
x = self.conv1(dummy_input)
x = self.conv2(x)
x = self.conv3(x)
flat_size = x.view(1, -1).size(1)
# Value stream
self.value_stream = nn.Sequential(
nn.Linear(flat_size, 512),
nn.LeakyReLU(negative_slope=0.01),
nn.Linear(512, 1)
)
# Advantage stream
self.advantage_stream = nn.Sequential(
nn.Linear(flat_size, 512),
nn.LeakyReLU(negative_slope=0.01),
nn.Linear(512, action_dim)
)
self._init_weights()
def _init_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
nn.init.kaiming_normal_(m.weight, mode='fan_in', nonlinearity='leaky_relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
def forward(self, x):
# x shape: (B, C, H, W)
x = F.leaky_relu(self.conv1(x), 0.01)
x = F.leaky_relu(self.conv2(x), 0.01)
x = F.leaky_relu(self.conv3(x), 0.01)
x = x.reshape(x.size(0), -1) # Flatten
value = self.value_stream(x) # (B, 1)
advantage = self.advantage_stream(x) # (B, action_dim)
# Combine: Q(s,a) = V(s) + (A(s,a) - mean(A(s,a')))
q_values = value + (advantage - advantage.mean(dim=1, keepdim=True))
return q_values
class HybridDuelingDQN(nn.Module):
"""
Hybrid CNN + Sector Vector architecture.
CNN branch processes spatial matrix (food/enemies/self).
Sector branch processes 99-float vector with precise distances + enemy approach.
"""
def __init__(self, input_channels, action_dim=14, input_size=(64, 64), sector_dim=99):
super(HybridDuelingDQN, self).__init__()
# --- CNN Branch (spatial matrix) ---
self.conv1 = nn.Conv2d(input_channels, 32, kernel_size=8, stride=4)
self.conv2 = nn.Conv2d(32, 64, kernel_size=4, stride=2)
self.conv3 = nn.Conv2d(64, 64, kernel_size=3, stride=1)
# Extra conv for 128x128: compresses 12x12 -> 5x5 with stride=2
self.conv4 = nn.Conv2d(64, 64, kernel_size=3, stride=2)
with torch.no_grad():
dummy = torch.zeros(1, input_channels, *input_size)
x = F.leaky_relu(self.conv1(dummy), 0.01)
x = F.leaky_relu(self.conv2(x), 0.01)
x = F.leaky_relu(self.conv3(x), 0.01)
x = F.leaky_relu(self.conv4(x), 0.01)
self.cnn_flat_size = x.view(1, -1).size(1)
# --- Sector Branch (scalar vector) ---
self.sector_net = nn.Sequential(
nn.Linear(sector_dim, 128),
nn.LeakyReLU(negative_slope=0.01),
nn.Linear(128, 128),
nn.LeakyReLU(negative_slope=0.01),
)
self.sector_out_size = 128
# --- Merge ---
merge_size = self.cnn_flat_size + self.sector_out_size
self.merge = nn.Sequential(
nn.Linear(merge_size, 512),
nn.LeakyReLU(negative_slope=0.01),
)
# --- Dueling heads (deeper than DuelingDQN) ---
self.value_stream = nn.Sequential(
nn.Linear(512, 256),
nn.LeakyReLU(negative_slope=0.01),
nn.Linear(256, 1),
)
self.advantage_stream = nn.Sequential(
nn.Linear(512, 256),
nn.LeakyReLU(negative_slope=0.01),
nn.Linear(256, action_dim),
)
self._init_weights()
def _init_weights(self):
for m in self.modules():
if isinstance(m, (nn.Conv2d, nn.Linear)):
nn.init.kaiming_normal_(m.weight, mode='fan_in', nonlinearity='leaky_relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
def forward(self, matrix, sectors):
# CNN branch
x = F.leaky_relu(self.conv1(matrix), 0.01)
x = F.leaky_relu(self.conv2(x), 0.01)
x = F.leaky_relu(self.conv3(x), 0.01)
x = F.leaky_relu(self.conv4(x), 0.01)
x = x.reshape(x.size(0), -1)
# Sector branch
s = self.sector_net(sectors)
# Merge
combined = torch.cat([x, s], dim=1)
merged = self.merge(combined)
# Dueling
value = self.value_stream(merged)
advantage = self.advantage_stream(merged)
q_values = value + (advantage - advantage.mean(dim=1, keepdim=True))
return q_values