-
Notifications
You must be signed in to change notification settings - Fork 0
/
mountaincar_Dueling_DDQN.py
285 lines (265 loc) · 9.8 KB
/
mountaincar_Dueling_DDQN.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from DuelingNetwork.model import Net
import numpy as np
import matplotlib.pyplot as plt
import gym
import random
import torch
from torch import nn
from torch import optim
from collections import namedtuple
import warnings
import time
import torch.nn.functional as F
warnings.filterwarnings("ignore", category=UserWarning)
<<<<<<< HEAD
torch.cuda.get_device_name(0)
torch.cuda.is_available()
torch.__version__
# 상수 정의
# ENV = 'CartPole-v0'
ENV = 'MountainCar-v0' # 태스크 이름
GAMMA = 0.999 # 시간할인율
MAX_STEPS = 200 # 1에피소드 당 최대 단계 수
NUM_EPISODES = 500 # 최대 에피소드 수
=======
# 상수 정의
ENV = 'CartPole-v0'
# ENV = 'MountainCar-v0' # 태스크 이름
GAMMA = 0.999 # 시간할인율
MAX_STEPS = 200 # 1에피소드 당 최대 단계 수
NUM_EPISODES = 400 # 최대 에피소드 수
>>>>>>> 5c957aadf0c5a0006a8d2d9de8d9e32f646208dc
BATCH_SIZE=32
env=gym.make(ENV)
print(env.observation_space)
num_states = env.observation_space.shape[0] # 태스크의 상태 변수 수(2)를 받아옴
num_actions = env.action_space.n # 태스크의 행동 가짓수(3)를 받아옴
<<<<<<< HEAD
# model_m=Net(num_states,32,num_actions)
model_m = torch.load("pth/MountainCar.pth")
model_m.optimizer=optim.Adam(model_m.parameters(),lr=0.0001)
model_t= torch.load("pth/MountainCar.pth")
model_t.optimizer=optim.Adam(model_m.parameters(),lr=0.0001)
Transition=namedtuple('Transition',('state','action','next_state','reward'))
# torch.set_default_tensor_type('torch.cuda.FloatTensor')
# cudnn.benchmark = True
=======
model_m=Net(num_states,32,num_actions)
model_m.optimizer=optim.Adam(model_m.parameters(),lr=0.0001)
model_t=Net(num_states,32,num_actions)
model_t.optimizer=optim.Adam(model_m.parameters(),lr=0.0001)
Transition=namedtuple('Transition',('state','action','next_state','reward'))
>>>>>>> 5c957aadf0c5a0006a8d2d9de8d9e32f646208dc
def get_action(state,episode):
epsilion=(1/(episode+1))
if epsilion<=np.random.uniform(0,1):
with torch.no_grad():
model_m.eval()
<<<<<<< HEAD
action=model_m(state ).max(1)[1].view(1,1)
=======
action=model_m(state).max(1)[1].view(1,1)
>>>>>>> 5c957aadf0c5a0006a8d2d9de8d9e32f646208dc
'''
state가 [1,num_states]이므로 model(state)의 결과는 당연히
[1,num_states]이 되고 거기다가 max(1)[1]을 하면 열원소중 가장큰 원소의 인덱스를 가지는
[1]짜리 텐서가 반환된다 그 결과를 다시 [1,1]로 바꾸기위해 view를 이용함
'''
else:
action=torch.LongTensor([[random.randrange(num_actions)]])
#action의 shape은 [1,1]
return action
<<<<<<< HEAD
def replay():
=======
def replay(fast_end):
>>>>>>> 5c957aadf0c5a0006a8d2d9de8d9e32f646208dc
if len(Transition_mem)<32:
print("too small Transition")
return
# elif fast_end is False:
batched=Transition_mem.sample()
# else:
# Transition_mem.memory.sort(key=lambda element:element[3],reverse=True)
# batched=Transition_mem.memory[:32]
'''
batched >> [(state,action,reward,next_state),(state,action,reward,next_state),(state,action,reward,next_state)]
요 상태이고 당연히 reward의 shape은 [1] 나머지는 [1,n] 인상태
ex)
batched: [Transition(state=tensor([[-0.4734, 0.0023]]),
action=tensor([[2]]),
next_state=tensor([[-0.4705, 0.0029]]),
reward=tensor([0.])),
Transition(state=tensor([[-0.4696, -0.0027]]),
action=tensor([[1]]),
next_state=tensor([[-0.4727, -0.0032]]),
reward=tensor([0.])).....
'''
batch=Transition(*zip(*batched))
'''
batch >> namedtuple형태로 state에 모든 state들이 쭉 튜플형태로 저장됨
ex)
batch: Transition(state=(tensor([[-0.4734, 0.0023]]), tensor([[-0.4696, -0.0027]]),
tensor([[-0.3772, -0.0010]]), tensor([[-0.4313, 0.0058]]), tensor([[-0.3769, 0.0013]]), tensor([[-0.3944, 0.0044]]),
'''
state_batch=torch.cat(batch.state)
# print(state_batch.shape)
action_batch=torch.cat(batch.action)
non_final_next_state_batch=torch.cat([s for s in batch.next_state if s is not None])
reward_batch=torch.cat(batch.reward)
model_m.eval()
model_t.eval()
state_action_value=model_m(state_batch).gather(1,action_batch)
#Qm(st,at)
mask=tuple(map(lambda s :s is not None,batch.next_state))
# print(mask)
non_final_mask=torch.ByteTensor(mask)
a_m=torch.zeros(BATCH_SIZE).type(torch.LongTensor)
a_m[non_final_mask]=model_m(non_final_next_state_batch).detach().max(1)[1]
# print(a_m.shape)
a_m_non_final_next_states=a_m[non_final_mask].view(-1,1)
# print(a_m_non_final_next_states)
next_state_value=torch.zeros(BATCH_SIZE)
next_state_value[non_final_mask]=model_t(non_final_next_state_batch).gather(1,a_m_non_final_next_states).detach().squeeze()
# print(reward_batch.shape)
# print(next_state_value.shape)
expected_state_value=reward_batch+GAMMA*next_state_value
model_m.train()
loss=F.smooth_l1_loss(state_action_value,expected_state_value.unsqueeze(1))
model_m.optimizer.zero_grad()
loss.backward()
model_m.optimizer.step()
class mem:
def __init__(self):
self.memory=[]
self.capacity=10000
self.indx=0
def push(self,state,action,next_state,reward):
if len(self.memory)<self.capacity:
self.memory.append(None)
self.memory[self.indx]=Transition(state,action,next_state,reward)
self.indx=(self.indx+1)%self.capacity
def __len__(self):
return len(self.memory)
def sample(self):
return random.sample(self.memory,32)
Transition_mem=mem()
count=0
for i in range(NUM_EPISODES):
<<<<<<< HEAD
=======
fast_end=False
>>>>>>> 5c957aadf0c5a0006a8d2d9de8d9e32f646208dc
total_reward=0
observation = env.reset()
state=torch.from_numpy(observation).type(torch.FloatTensor)
state=torch.unsqueeze(state,0)
'''
state는 [3]짜리 텐서였는데 [1,3]이 됨
'''
print("episode: ",i)
# print(len(Transition_mem))
if i%2==0:
model_t.load_state_dict(model_m.state_dict())
# if count==15:
# break
for j in range(MAX_STEPS):
<<<<<<< HEAD
action=get_action(state,i)
observation_next,reward,done,_=env.step(action.item())
# total_reward += reward
if done==True and j!=199:
reward = 20
print("get reward")
next_state=None
else:
# reward = abs(observation_next[0] - observation[0])
# reward=torch.Tensor(reward)
# if done==True:
# next_state=None
# if j>=199:
# reward=torch.Tensor([-1.0])
# else:
# reward=torch.Tensor([1.0])
# else:
# # if observation_next[0] < -0.5 and observation_next[0]>-0.55 :
# # reward=torch.FloatTensor([abs(observation_next[1])])
# if observation_next[0]>0.3:
# reward=torch.FloatTensor([abs(observation_next[0])])
# else:
# reward=torch.FloatTensor([0.0])
reward=-0.1
reward+=abs(observation_next[1])
reward+=abs(observation_next[0])/10
next_state=torch.from_numpy(observation_next).type(torch.FloatTensor)
next_state=torch.unsqueeze(next_state,0)
total_reward+=reward
Transition_mem.push(state,action,next_state,torch.FloatTensor([reward]))
#
replay()
#
state=next_state
if done ==True:
print("steps:",j)
print("total reward:",total_reward)
break
=======
action=get_action(state,i)
observation_next,reward,done,_=env.step(action.item())
if done==True:
next_state=None
if j>=198:
reward=torch.Tensor([1.0])
else:
fast_end=True
reward=torch.Tensor([-1.0])
else:
reward=torch.Tensor([0.0])
next_state=torch.from_numpy(observation_next).type(torch.FloatTensor)
next_state=torch.unsqueeze(next_state,0)
total_reward+=reward
Transition_mem.push(state,action,next_state,reward)
#
replay(fast_end)
#
state=next_state
if done ==True:
print("steps:",j)
print("total reward:",total_reward)
break
>>>>>>> 5c957aadf0c5a0006a8d2d9de8d9e32f646208dc
# for j in range(3):
observation = env.reset()
state=observation
state=torch.from_numpy(state).type(torch.FloatTensor)
state=torch.unsqueeze(state,0)
<<<<<<< HEAD
torch.save(model_m, "pth/MountainCar.pth")
=======
>>>>>>> 5c957aadf0c5a0006a8d2d9de8d9e32f646208dc
# env.monitor.start('/tmp/cartpole-experiment-1', force=True)
for i in range(200):
# env.render()
with torch.no_grad():
model_m.eval()
action=model_m(state).max(1)[1].view(1,1)
observation_next, _, done, _ = env.step(
action.item())
if done:
<<<<<<< HEAD
print("on eval",i,"steps")
=======
>>>>>>> 5c957aadf0c5a0006a8d2d9de8d9e32f646208dc
break
else:
state_next = observation_next # 관측 결과를 그대로 상태로 사용
state_next = torch.from_numpy(state_next).type(
torch.FloatTensor) # numpy 변수를 파이토치 텐서로 변환
state_next = torch.unsqueeze(state_next, 0)
state = state_next
env.render()
time.sleep(0.01)
# print(i)
env.close()
# env.monitor.close()
# env.close()