-
Notifications
You must be signed in to change notification settings - Fork 2
/
policy.py
140 lines (107 loc) · 5.06 KB
/
policy.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
from network import Net
import torch.nn as nn
import torch.nn.functional as F
import torch
import math
from torch.distributions import Categorical
from ortools_tsp import solve
class Agentembedding(nn.Module):
def __init__(self, node_feature_size, key_size, value_size):
super(Agentembedding, self).__init__()
self.key_size = key_size
self.q_agent = nn.Linear(2 * node_feature_size, key_size)
self.k_agent = nn.Linear(node_feature_size, key_size)
self.v_agent = nn.Linear(node_feature_size, value_size)
def forward(self, f_c, f):
q = self.q_agent(f_c)
k = self.k_agent(f)
v = self.v_agent(f)
u = torch.matmul(k, q.transpose(-1, -2)) / math.sqrt(self.key_size)
u_ = F.softmax(u, dim=-2).transpose(-1, -2)
agent_embedding = torch.matmul(u_, v)
return agent_embedding
class AgentAndNode_embedding(torch.nn.Module):
def __init__(self, in_chnl, hid_chnl, n_agent, key_size, value_size, dev):
super(AgentAndNode_embedding, self).__init__()
self.n_agent = n_agent
# gin
self.gin = Net(in_chnl=in_chnl, hid_chnl=hid_chnl).to(dev)
# agent attention embed
self.agents = torch.nn.ModuleList()
for i in range(n_agent):
self.agents.append(Agentembedding(node_feature_size=hid_chnl, key_size=key_size, value_size=value_size).to(dev))
def forward(self, batch_graphs, n_nodes, n_batch):
# get node embedding using gin
nodes_h, g_h = self.gin(x=batch_graphs.x, edge_index=batch_graphs.edge_index, batch=batch_graphs.batch)
nodes_h = nodes_h.reshape(n_batch, n_nodes, -1)
g_h = g_h.reshape(n_batch, 1, -1)
depot_cat_g = torch.cat((g_h, nodes_h[:, 0, :].unsqueeze(1)), dim=-1)
# output nodes embedding should not include depot, refer to paper: https://www.sciencedirect.com/science/article/abs/pii/S0950705120304445
nodes_h_no_depot = nodes_h[:, 1:, :]
# get agent embedding
agents_embedding = []
for i in range(self.n_agent):
agents_embedding.append(self.agents[i](depot_cat_g, nodes_h_no_depot))
agent_embeddings = torch.cat(agents_embedding, dim=1)
return agent_embeddings, nodes_h_no_depot
class Policy(nn.Module):
def __init__(self, in_chnl, hid_chnl, n_agent, key_size_embd, key_size_policy, val_size, clipping, dev):
super(Policy, self).__init__()
self.c = clipping
self.key_size_policy = key_size_policy
self.key_policy = nn.Linear(hid_chnl, self.key_size_policy, device=dev)
self.q_policy = nn.Linear(val_size, self.key_size_policy, device=dev)
# embed network
self.embed = AgentAndNode_embedding(in_chnl=in_chnl, hid_chnl=hid_chnl, n_agent=n_agent,
key_size=key_size_embd, value_size=val_size, dev=dev)
def forward(self, batch_graph, n_nodes, n_batch):
agent_embeddings, nodes_h_no_depot = self.embed(batch_graph, n_nodes, n_batch)
k_policy = self.key_policy(nodes_h_no_depot)
q_policy = self.q_policy(agent_embeddings)
u_policy = torch.matmul(q_policy, k_policy.transpose(-1, -2)) / math.sqrt(self.key_size_policy)
imp = self.c * torch.tanh(u_policy)
prob = F.softmax(imp, dim=-2)
return prob
def action_sample(pi):
dist = Categorical(pi.transpose(2, 1))
action = dist.sample()
log_prob = dist.log_prob(action)
return action, log_prob
def get_log_prob(pi, action_int):
dist = Categorical(pi.transpose(2, 1))
log_prob = dist.log_prob(action_int)
return log_prob
def get_cost(action, data, n_agent):
subtour_max_lengths = [0 for _ in range(data.shape[0])]
data = data * 1000 # why?
depot = data[:, 0, :].tolist()
sub_tours = [[[] for _ in range(n_agent)] for _ in range(data.shape[0])]
for i in range(data.shape[0]):
for tour in sub_tours[i]:
tour.append(depot[i])
for n, m in zip(action.tolist()[i], data.tolist()[i][1:]):
sub_tours[i][n].append(m)
for k in range(data.shape[0]):
for a in range(n_agent):
instance = sub_tours[k][a]
sub_tour_length = solve(instance)/1000
if sub_tour_length >= subtour_max_lengths[k]:
subtour_max_lengths[k] = sub_tour_length
return subtour_max_lengths
class Surrogate(nn.Module):
def __init__(self, in_dim: int, out_dim: int, n_hidden: int = 64, nonlin: str = 'relu', dev='cpu', **kwargs):
super(Surrogate, self).__init__()
nlist = dict(relu=nn.ReLU(), tanh=nn.Tanh(),
sigmoid=nn.Sigmoid(), softplus=nn.Softplus(), lrelu=nn.LeakyReLU(),
elu=nn.ELU())
self.layer = nn.Linear(in_dim, n_hidden, device=dev)
self.layer2 = nn.Linear(n_hidden, n_hidden, device=dev)
self.out = nn.Linear(n_hidden, out_dim, device=dev)
self.nonlin = nlist[nonlin]
def forward(self, x, **kwargs):
x = self.layer(x)
x = self.nonlin(x)
x = self.layer2(x)
x = self.nonlin(x)
x = self.out(x)
return x