-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_dqn.py
260 lines (237 loc) · 7.4 KB
/
test_dqn.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
import os
from os.path import dirname
import sys
import gymnasium as gym
import torch
try:
dir_ = dirname(dirname(__file__))
except Exception as e:
dir_ = dirname(dirname('__file__'))
if len(dir_) == 0:
dir_ = os.getcwd() + '/src'
print(dir_)
sys.path.append(dir_)
from RLAlgo.DQN import DQN
from RLUtils import train_off_policy, play, Config, gym_env_desc
from RLUtils.env_wrapper import FrameStack, baseSkipFrame, GrayScaleObservation, ResizeObservation
def dqn_test():
# num_episode=100 action_contiguous_ = True
env_name = 'Pendulum-v1'
# num_episode=500 action_contiguous_ = False epsilon=0.01
env_name = 'CartPole-v1'
# 要需要提升探索率,steps需要足够大
# num_episode=200 action_contiguous_ = False epsilon=0.05 max_episode_steps=500
env_name = 'MountainCar-v0'
gym_env_desc(env_name)
env = gym.make(env_name)
action_contiguous_ = False # 是否将连续动作离散化
cfg = Config(
env,
# 环境参数
split_action_flag=True,
save_path=r'D:\TMP\dqn_target_q.ckpt',
seed=42,
# 网络参数
hidden_layers_dim=[32, 32],
# agent参数
learning_rate=2e-3,
target_update_freq=3,
gamma=0.95,
epsilon=0.05,
# 训练参数
num_episode=200,
off_buffer_size=2048+1024,
off_minimal_size=1024,
sample_size=256,
max_episode_steps=500,
# agent 其他参数
dqn_type = 'duelingDQN'
)
dqn = DQN(
state_dim=cfg.state_dim,
hidden_layers_dim=cfg.hidden_layers_dim,
action_dim=cfg.action_dim,
learning_rate=cfg.learning_rate,
gamma=cfg.gamma,
epsilon=cfg.epsilon,
target_update_freq=cfg.target_update_freq,
device=cfg.device,
dqn_type=cfg.dqn_type
)
# train_off_policy(env, dqn, cfg, action_contiguous=action_contiguous_)
dqn.target_q.load_state_dict(
torch.load(cfg.save_path)
)
play(gym.make(env_name, render_mode='human'), dqn, cfg, episode_count=2, action_contiguous=action_contiguous_)
def Acrobot_dqn_test():
# num_episode=100 action_contiguous_ = True
env_name = 'Acrobot-v1'
gym_env_desc(env_name)
env = gym.make(env_name)
action_contiguous_ = False # 是否将连续动作离散化
cfg = Config(
env,
# 环境参数
split_action_flag=True,
save_path=r'D:\TMP\Acrobot_dqn_target_q.ckpt',
seed=42,
# 网络参数
hidden_layers_dim=[128, 64],
# agent参数
learning_rate=2e-3,
target_update_freq=3,
gamma=0.95,
epsilon=0.05,
# 训练参数
num_episode=300,
off_buffer_size=20480,
off_minimal_size=1024,
sample_size=256,
max_episode_steps=400,
# agent 其他参数
dqn_type = 'duelingDQN'
)
dqn = DQN(
state_dim=cfg.state_dim,
hidden_layers_dim=cfg.hidden_layers_dim,
action_dim=cfg.action_dim,
learning_rate=cfg.learning_rate,
gamma=cfg.gamma,
epsilon=cfg.epsilon,
target_update_freq=cfg.target_update_freq,
device=cfg.device,
dqn_type=cfg.dqn_type
)
# train_off_policy(env, dqn, cfg, action_contiguous=action_contiguous_)
dqn.target_q.load_state_dict(
torch.load(cfg.save_path)
)
play(gym.make(env_name, render_mode='human'), dqn, cfg, episode_count=2, action_contiguous=action_contiguous_)
def LunarLander_dqn_test():
# num_episode=100 action_contiguous_ = True
env_name = 'LunarLander-v2'
# https://www.lfd.uci.edu/~gohlke/pythonlibs/#pybox2d 下载包
# pip install + 路径/xxx.whl
gym_env_desc(env_name)
env = gym.make(env_name)
path_ = os.path.dirname(__file__)
cfg = Config(
env,
# 环境参数
split_action_flag=True,
save_path=os.path.join(path_, "test_models" ,f'dqn_{env_name}_1.ckpt'),
seed=42,
# 网络参数
hidden_layers_dim=[128, 64],
# agent参数
learning_rate=2e-3,
target_update_freq=3,
gamma=0.99,
epsilon=0.05,
# 训练参数
num_episode=800,
off_buffer_size=20480,
off_minimal_size=2048,
sample_size=128,
max_episode_steps=200,
# agent 其他参数
dqn_type = 'duelingDQN'
)
dqn = DQN(
state_dim=cfg.state_dim,
hidden_layers_dim=cfg.hidden_layers_dim,
action_dim=cfg.action_dim,
learning_rate=cfg.learning_rate,
gamma=cfg.gamma,
epsilon=cfg.epsilon,
target_update_freq=cfg.target_update_freq,
device=cfg.device,
dqn_type=cfg.dqn_type
)
# train_off_policy(env, dqn, cfg)
dqn.target_q.load_state_dict(
torch.load(cfg.save_path)
)
play(gym.make(env_name, render_mode='human'), dqn, cfg, episode_count=2)
def DemonAttack_v5_dqn_new_test():
# [ seed=7270 ] Get reward 1500.0. Last 596 times
env_name = 'ALE/DemonAttack-v5'
gym_env_desc(env_name)
env = gym.make(env_name, obs_type="rgb")
print("gym.__version__ = ", gym.__version__ )
env = FrameStack(
ResizeObservation(
GrayScaleObservation(baseSkipFrame(
env,
skip=5,
cut_slices=[[15, 188], [0, 160]],
start_skip=14,
# neg_action_kwargs={0: -0.1, 1: -0.01, 2: -0.01, 3: -0.01}
)),
shape=84
),
num_stack=4
)
path_ = os.path.dirname(__file__)
cfg = Config(
env,
# 环境参数
split_action_flag=False,
save_path=os.path.join(path_, "test_models", f'dqn_DemonAttack-v5-new_1'),
seed=42,
# 网络参数
hidden_layers_dim=[200, 200],
# agent参数
learning_rate=1.5e-4,
target_update_freq=16,
gamma=0.99,
epsilon=0.05,
# 训练参数
num_episode=1500,
off_buffer_size=10000,
off_minimal_size=1024,
sample_size=32,
max_episode_steps=280,
# agent 其他参数
dqn_type = 'DoubleDQN-CNN',
epsilon_start=0.95,
epsilon_decay_steps=20000
)
dqn = DQN(
state_dim=cfg.state_dim,
hidden_layers_dim=cfg.hidden_layers_dim,
action_dim=cfg.action_dim,
learning_rate=cfg.learning_rate,
gamma=cfg.gamma,
epsilon=cfg.epsilon,
target_update_freq=cfg.target_update_freq,
device=cfg.device,
dqn_type=cfg.dqn_type,
epsilon_start=cfg.epsilon_start,
epsilon_decay_steps=cfg.epsilon_decay_steps
)
# dqn.train()
# train_off_policy(env, dqn, cfg, done_add=False,
# train_without_seed=True,
# wandb_flag=False,
# test_ep_freq=50, test_episode_count=5)
dqn.load_model(cfg.save_path)
dqn.eval()
env = gym.make(env_name, obs_type="rgb", render_mode='human')
env = FrameStack(
ResizeObservation(
GrayScaleObservation(baseSkipFrame(
env,
skip=5,
cut_slices=[[15, 188], [0, 160]],
start_skip=14
)),
shape=84
),
num_stack=4
)
play(env, dqn, cfg, episode_count=1,
play_without_seed=True, render=True)
if __name__ == '__main__':
# LunarLander_dqn_test()
DemonAttack_v5_dqn_new_test()