-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGAMA_intersection_TD.py
215 lines (194 loc) · 9.43 KB
/
GAMA_intersection_TD.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
from utils import compute_returns, cross_loss_curve, GAMA_connect,reset
import os
from itertools import count
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
#from shared_adam import SharedAdam
#from torch.distributions import Categorical from pandas import DataFrame
from torch.distributions import MultivariateNormal
import numpy as np
import pandas as pd
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
state_size = 4
action_size = 1
lr = 0.0012
from_python_1 = 'D:/Software/GamaWorkspace/Python/python_AC_1.csv'
from_python_2 = 'D:/Software/GamaWorkspace/Python/python_AC_2.csv'
class Actor(nn.Module):
def __init__(self, state_size, action_size):
super(Actor, self).__init__()
self.state_size = state_size
self.action_size = action_size
self.linear1 = nn.Linear(self.state_size, 128)
self.linear2 = nn.Linear(128, 64)
self.mu = nn.Linear(64,self.action_size) #256 linear2
self.sigma = nn.Linear(64,self.action_size)
#self.linear3 = nn.Linear(256, self.action_size)
#self.action_var = torch.full((self.action_size,), action_std*action_std).to(device)
def forward(self, state):
output_1 = F.relu(self.linear1(state))
output_2 = F.relu(self.linear2(output_1))
mu = 2 * torch.tanh(self.mu(output_2)) #有正有负 sigmoid 0-1
sigma = F.relu(self.sigma(output_2)) + 0.001 # avoid 0 softplus output = F.softmax(output, dim=-1) action_mean = self.linear3(output)
#cov_mat = torch.diag(self.action_var).to(device)
mu = torch.diag_embed(mu).to(device)
sigma = torch.diag_embed(sigma).to(device) # change to 2D
dist = MultivariateNormal(mu,sigma) #N(μ,σ^2) σ超参不用训练 MultivariateNormal(action_mean, cov_mat)
#distribution = Categorical(F.softmax(output, dim=-1))
entropy = dist.entropy().mean()
action = dist.sample()
action_logprob = dist.log_prob(action)
return action.detach(),action_logprob,entropy #distribution .detach()
class Critic(nn.Module):
def __init__(self, state_size, action_size):
super(Critic, self).__init__()
self.state_size = state_size
self.action_size = action_size
self.linear1 = nn.Linear(self.state_size, 128)
self.linear2 = nn.Linear(128,64) #
self.linear3 = nn.Linear(64, 1)
def forward(self, state):
output_1 = F.relu(self.linear1(state))
output_2 = F.relu(self.linear2(output_1))
value = torch.tanh(self.linear3(output_2)) #有正有负
return value
def main():
################ load ###################
if os.path.exists('D:/Software/GamaWorkspace/Python/weight/actor.pkl'):
actor = Actor(state_size, action_size).to(device)
actor.load_state_dict(torch.load('D:/Software/GamaWorkspace/Python/weight/actor.pkl'))
print('Actor Model loaded')
else:
actor = Actor(state_size, action_size).to(device)
if os.path.exists('D:/Software/GamaWorkspace/Python/weight/critic.pkl'):
critic = Critic(state_size, action_size).to(device)
critic.load_state_dict(torch.load('D:/Software/GamaWorkspace/Python/weight/critic.pkl'))
print('Critic Model loaded')
else:
critic = Critic(state_size, action_size).to(device)
print("Waiting for GAMA...")
################### initialization ########################
reset()
optimizerA = optim.Adam(actor.parameters(), lr, betas=(0.95, 0.999))
optimizerC = optim.Adam(critic.parameters(), lr, betas=(0.95, 0.999))
episode = 0
test = "GAMA"
state,reward,done,time_pass,over = GAMA_connect(test) #connect
print("done:",done,"timepass:",time_pass)
log_probs = [] #log probability
values = []
rewards = []
masks = []
total_loss = []
total_rewards = []
entropy = 0
loss = []
value = 0
log_prob = 0
################## start #########################
while over!= 1:
#普通の場合
if(done == 0 and time_pass != 0):
#前回の報酬
reward = torch.tensor([reward], dtype=torch.float, device=device)
state = torch.FloatTensor(state).to(device)
rewards.append(reward)
with torch.autograd.set_detect_anomaly(True):
# TD:r(s) + gamma * v(s+1) - v(s)
advantage = reward.detach() + critic(state) - value #values[len(values)-1].detach()
actor_loss = -(log_prob * advantage.detach())
critic_loss = (reward.detach() + critic(state) - value).pow(2)
optimizerA.zero_grad()
optimizerC.zero_grad()
actor_loss.backward()
critic_loss.backward()
loss.append(critic_loss)
optimizerA.step()
optimizerC.step()
value = critic(state) #dist, # now is a tensoraction = dist.sample()
action,log_prob,entropy = actor(state) #action = dist.sample() # now is a tensor
log_prob = log_prob.unsqueeze(0) #log_prob = dist.log_prob(action).unsqueeze(0)
entropy += entropy
#print("acceleration: ",action.cpu().numpy())#,"action.cpu().numpy()",type(float(action.cpu().numpy()))
to_GAMA = [[1,float(action.cpu().numpy()*10)]] #行
np.savetxt(from_python_1,to_GAMA,delimiter=',')
np.savetxt(from_python_2,to_GAMA,delimiter=',')
masks.append(torch.tensor([1-done], dtype=torch.float, device=device)) #over-0; otherwise-1 contains the last
values.append(value)
log_probs.append(log_prob)
# 終わり
elif done == 1:
#print("restart acceleration: 0")
to_GAMA = [[1,0]]
np.savetxt(from_python_1,to_GAMA,delimiter=',')
np.savetxt(from_python_2,to_GAMA,delimiter=',')
#先传后计算
rewards.append(reward) #contains the last
reward = torch.tensor([reward], dtype=torch.float, device=device)
rewards.append(reward) #contains the last
total_reward = sum(rewards)
total_rewards.append(total_reward)
last_state = torch.FloatTensor(state).to(device)
last_value = critic(last_state)
#masks.append(torch.tensor([1-done], dtype=torch.float, device=device)) #over-0; otherwise-1 contains the last
#log_probs = torch.cat(log_probs) #Concatenates the given sequence of seq tensors in the given dimension.
#returns = compute_returns(last_value, rewards, masks)
#returns = torch.cat(returns)
#values_next = values[1:]
#values_next.append(torch.tensor([0], dtype=torch.float, device=device))
#values = torch.cat(values)
#values_next = torch.cat(values_next)
#rewards = torch.cat(rewards)
with torch.autograd.set_detect_anomaly(True):
advantage = reward.detach() + last_value - value
actor_loss = -( log_prob * advantage.detach())
print("actor_loss, ",actor_loss ," size",actor_loss.dim())
critic_loss = (reward.detach() + last_value - value).pow(2)
optimizerA.zero_grad()
optimizerC.zero_grad()
actor_loss.backward()
critic_loss.backward()
loss.append(critic_loss)
optimizerA.step()
optimizerC.step()
print("----------------------------------Net_Trained---------------------------------------")
print('--------------------------Iteration:',episode,'over--------------------------------')
episode += 1
log_probs = []
values = []
rewards = []
masks = []
torch.save(actor.state_dict(), 'D:/Software/GamaWorkspace/Python/weight/actor.pkl')
torch.save(critic.state_dict(), 'D:/Software/GamaWorkspace/Python/weight/critic.pkl')
#print("critic_loss: ",loss,"total_rewards:",total_rewards)
#entropys.append(entropy)
loss_sum = sum(loss)
total_loss.append(loss_sum)
#print("total_loss: ",total_loss)
cross_loss_curve(total_loss,total_rewards)
loss = []
if episode > 100 : #50
new_lr = lr * (0.94 ** ((episode-90) // 10)) #40
optimizerA = optim.Adam(actor.parameters(), new_lr, betas=(0.95, 0.999))
optimizerC = optim.Adam(critic.parameters(), new_lr, betas=(0.95, 0.999))
#最初の時
else:
print('Iteration:',episode)
state = torch.FloatTensor(state).to(device)
value = critic(state) #dist, # now is a tensoraction = dist.sample()
action,log_prob,entropy = actor(state)
print("acceleration: ",action.cpu().numpy())
to_GAMA = [[1,float(action.cpu().numpy()*10)]]
np.savetxt(from_python_1,to_GAMA,delimiter=',')
np.savetxt(from_python_1,to_GAMA,delimiter=',')
log_prob = log_prob.unsqueeze(0) #log_prob = dist.log_prob(action).unsqueeze(0) #entropy += dist.entropy().mean()
#log_probs.append(log_prob)
print(log_prob)
#values.append(value.cpu().numpy())
entropy += entropy
state,reward,done,time_pass,over = GAMA_connect(test)
return None
if __name__ == '__main__':
main()