56 lines
2.2 KiB
Python
56 lines
2.2 KiB
Python
"""Six-slot public-state policy; inactive players have exactly zero value mass."""
|
|
|
|
import torch
|
|
from torch import nn
|
|
|
|
|
|
class MultiplayerPolicy(nn.Module):
|
|
def __init__(self, width=384, ordered_players=False):
|
|
super().__init__()
|
|
self.ordered_players = ordered_players
|
|
self.player = nn.Sequential(nn.Linear(74, 128), nn.LayerNorm(128), nn.SiLU())
|
|
# Global context, city ownership and territory occupancy retain seat order.
|
|
self.context = nn.Sequential(
|
|
nn.Linear(258 + 308 + 133 + (896 if ordered_players else 256) + 6, width),
|
|
nn.LayerNorm(width),
|
|
nn.SiLU(),
|
|
nn.Linear(width, width),
|
|
nn.SiLU(),
|
|
)
|
|
self.action = nn.Sequential(nn.Linear(98, 128), nn.SiLU())
|
|
self.score = nn.Sequential(
|
|
nn.Linear(width + 128, 256), nn.SiLU(), nn.Linear(256, 1)
|
|
)
|
|
self.value = nn.Sequential(
|
|
nn.Linear(width + 128, 128), nn.SiLU(), nn.Linear(128, 1)
|
|
)
|
|
nn.init.zeros_(self.score[-1].weight)
|
|
nn.init.zeros_(self.score[-1].bias)
|
|
|
|
def forward(self, state, actions, mask):
|
|
active = state[:, :6] > 0.5
|
|
p = self.player(state[:, 264:708].reshape(-1, 6, 74))
|
|
pooled = (p * active[:, :, None]).sum(1) / active.sum(1, keepdim=True).clamp(
|
|
min=1
|
|
)
|
|
# Keep holdings attached to the same relative seats used by city ownership
|
|
# and action territory features. The mean alone loses those associations.
|
|
player_context = (
|
|
(p * active[:, :, None]).flatten(1) if self.ordered_players else p[:, 0]
|
|
)
|
|
s = self.context(
|
|
torch.cat(
|
|
(state[:, 6:264], state[:, 708:], player_context, pooled, state[:, :6]),
|
|
-1,
|
|
)
|
|
)
|
|
a = self.action(actions)
|
|
logits = actions[:, :, 74] * 2 + self.score(
|
|
torch.cat((s[:, None].expand(-1, a.shape[1], -1), a), -1)
|
|
).squeeze(-1)
|
|
values = self.value(torch.cat((s[:, None].expand(-1, 6, -1), p), -1)).squeeze(
|
|
-1
|
|
)
|
|
return logits.masked_fill(~mask, -1e9), values.masked_fill(
|
|
~active, -1e9
|
|
).softmax(-1)
|