forked from DKuan/MADDPG_torch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
132 lines (115 loc) · 5.19 KB
/
model.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
# Time: 2019-11-05
# Author: Zachary
# Name: MADDPG_torch
import torch
import torch.nn as nn
import torch.nn.functional as F
class abstract_agent(nn.Module):
def __init__(self):
super(abstract_agent, self).__init__()
def act(self, input):
policy, value = self.forward(input) # flow the input through the nn
return policy, value
class actor_agent(abstract_agent):
def __init__(self, num_inputs, action_size, args):
super(actor_agent, self).__init__()
self.linear_a1 = nn.Linear(num_inputs, args.num_units_1)
self.linear_a2 = nn.Linear(args.num_units_1, args.num_units_2)
self.linear_a = nn.Linear(args.num_units_2, action_size)
self.reset_parameters()
# Activation func init
self.LReLU = nn.LeakyReLU(0.01)
self.tanh= nn.Tanh()
self.train()
def reset_parameters(self):
gain = nn.init.calculate_gain('leaky_relu')
gain_tanh = nn.init.calculate_gain('tanh')
self.linear_a1.weight.data.mul_(gain)
self.linear_a2.weight.data.mul_(gain)
self.linear_a.weight.data.mul_(gain_tanh)
def forward(self, input):
"""
The forward func defines how the data flows through the graph(layers)
"""
x = self.LReLU(self.linear_a1(input))
x = self.LReLU(self.linear_a2(x))
policy = self.tanh(self.linear_a(x))
return policy
class critic_agent(abstract_agent):
def __init__(self, obs_shape_n, action_shape_n, args):
super(critic_agent, self).__init__()
self.linear_o_c1 = nn.Linear(obs_shape_n, args.num_units_1)
self.linear_a_c1 = nn.Linear(action_shape_n, args.num_units_1)
self.linear_c2 = nn.Linear(args.num_units_1*2, args.num_units_2)
self.linear_c = nn.Linear(args.num_units_2, 1)
self.reset_parameters()
self.LReLU = nn.LeakyReLU(0.01)
self.tanh= nn.Tanh()
self.train()
def reset_parameters(self):
gain = nn.init.calculate_gain('leaky_relu')
gain_tanh = nn.init.calculate_gain('tanh')
self.linear_o_c1.weight.data.mul_(gain)
self.linear_a_c1.weight.data.mul_(gain)
self.linear_c2.weight.data.mul_(gain)
self.linear_c.weight.data.mul_(gain)
def forward(self, obs_input, action_input):
"""
input_g: input_global, input features of all agents
"""
x_o = self.LReLU(self.linear_o_c1(obs_input))
x_a = self.LReLU(self.linear_a_c1(action_input))
x_cat = torch.cat([x_o, x_a], dim=1)
x = self.LReLU(self.linear_c2(x_cat))
value = self.linear_c(x)
return value
class openai_critic(abstract_agent):
def __init__(self, obs_shape_n, action_shape_n, args):
super(openai_critic, self).__init__()
self.LReLU = nn.LeakyReLU(0.01)
self.linear_c1 = nn.Linear(action_shape_n+obs_shape_n, args.num_units_openai)
self.linear_c2 = nn.Linear(args.num_units_openai, args.num_units_openai)
self.linear_c = nn.Linear(args.num_units_openai, 1)
self.reset_parameters()
self.train()
def reset_parameters(self):
gain = nn.init.calculate_gain('leaky_relu')
nn.init.xavier_uniform_(self.linear_c1.weight, gain=nn.init.calculate_gain('leaky_relu'))
nn.init.xavier_uniform_(self.linear_c2.weight, gain=nn.init.calculate_gain('leaky_relu'))
nn.init.xavier_uniform_(self.linear_c.weight, gain=nn.init.calculate_gain('leaky_relu'))
def forward(self, obs_input, action_input):
"""
input_g: input_global, input features of all agents
"""
x_cat = self.LReLU(self.linear_c1(torch.cat([obs_input, action_input], dim=1)))
x = self.LReLU(self.linear_c2(x_cat))
value = self.linear_c(x)
return value
class openai_actor(abstract_agent):
def __init__(self, num_inputs, action_size, args):
super(openai_actor, self).__init__()
self.tanh= nn.Tanh()
self.LReLU = nn.LeakyReLU(0.01)
self.linear_a1 = nn.Linear(num_inputs, args.num_units_openai)
self.linear_a2 = nn.Linear(args.num_units_openai, args.num_units_openai)
self.linear_a = nn.Linear(args.num_units_openai, action_size)
self.reset_parameters()
self.train()
def reset_parameters(self):
gain = nn.init.calculate_gain('leaky_relu')
gain_tanh = nn.init.calculate_gain('tanh')
nn.init.xavier_uniform_(self.linear_a1.weight, gain=nn.init.calculate_gain('leaky_relu'))
nn.init.xavier_uniform_(self.linear_a2.weight, gain=nn.init.calculate_gain('leaky_relu'))
nn.init.xavier_uniform_(self.linear_a.weight, gain=nn.init.calculate_gain('leaky_relu'))
def forward(self, input, model_original_out=False):
"""
The forward func defines how the data flows through the graph(layers)
flag: 0 sigle input 1 batch input
"""
x = self.LReLU(self.linear_a1(input))
x = self.LReLU(self.linear_a2(x))
model_out = self.linear_a(x)
u = torch.rand_like(model_out)
policy = F.softmax(model_out - torch.log(-torch.log(u)), dim=-1)
if model_original_out == True: return model_out, policy # for model_out criterion
return policy