-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_black_box_policy.py
134 lines (116 loc) · 3.48 KB
/
train_black_box_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
import gym
import os
from stable_baselines3.dqn.dqn import DQN
from utils import set_seed, get_env, save_config, save_results_summary
# config = {
# "seed": 0,
# "env": "LunarLander-v2",
# "policy": "MlpPolicy",
# "lr": 0.00063,
# "buffer_size": 50000,
# "learning_starts": 0,
# "batch_size": 128,
# "gamma": 0.99,
# "train_freq": 4,
# "gradient_steps": -1,
# "target_update_interval": 250,
# "exploration_fraction": 0.12,
# "exploration_initial_eps": 1.0,
# "exploration_final_eps": 0.1,
# "policy_kwargs": {
# "net_arch": [256, 256]
# },
# "n_timesteps": 200000,
# }
# config = {
# "seed": 0,
# "env": "Taxi-v3",
# "policy": "MlpPolicy",
# "lr": 0.0005,
# "buffer_size": 1000000,
# "learning_starts": 50000,
# "batch_size": 32,
# "gamma": 0.99,
# "train_freq": 4,
# "gradient_steps": 1,
# "target_update_interval": 10000,
# "exploration_fraction": 0.1,
# "exploration_initial_eps": 1.0,
# "exploration_final_eps": 0.05,
# "policy_kwargs": {"net_arch": [64, 64]},
# "n_timesteps": 1000000,
# }
# config = {
# "seed": 0,
# "env": "FourRooms",
# "policy": "MlpPolicy",
# "lr": 0.0001,
# "buffer_size": 100000,
# "learning_starts": 1000,
# "batch_size": 64,
# "gamma": 0.99,
# "train_freq": 256,
# "gradient_steps": 128,
# "target_update_interval": 10,
# "exploration_fraction": 0.4,
# "exploration_initial_eps": 1.0,
# "exploration_final_eps": 0.04,
# "policy_kwargs": {"net_arch": [256, 256]},
# "n_timesteps": 1000000,
# }
config = {
"seed": 0,
"env": "highway-fast-v0",
"policy": "MlpPolicy",
"lr": 0.0005,
"buffer_size": 15000,
"learning_starts": 200,
"batch_size": 64,
"gamma": 0.8,
"train_freq": 1,
"gradient_steps": 1,
"target_update_interval": 50,
"exploration_fraction": 0.75,
"exploration_initial_eps": 0.5,
"exploration_final_eps": 0.01,
"policy_kwargs": {"net_arch": [256, 256]},
"n_timesteps": 150000,
}
set_seed(config["seed"])
env = get_env(config["env"])
model = DQN(
policy="MlpPolicy",
env=env,
learning_rate=config["lr"],
buffer_size=config["buffer_size"],
learning_starts=config["learning_starts"],
batch_size=config["batch_size"],
gamma=config["gamma"],
train_freq=config["train_freq"],
gradient_steps=config["gradient_steps"],
target_update_interval=config["target_update_interval"],
exploration_fraction=config["exploration_fraction"],
exploration_initial_eps=config["exploration_initial_eps"],
exploration_final_eps=config["exploration_final_eps"],
policy_kwargs=config["policy_kwargs"],
verbose=1,
seed=config["seed"],
)
model_dir = os.path.join("rl-trained-agents", "dqn", config["env"])
if not os.path.exists(model_dir):
os.makedirs(model_dir)
model.learn(total_timesteps=config["n_timesteps"], progress_bar=True)
model.save(os.path.join(model_dir, "model"))
save_config(model_dir, config)
episode_rewards = []
for episode_no in range(1000):
episode_reward = 0
state = env.reset()
done = False
while not done:
action, _ = model.predict(state, deterministic=True)
state, reward, done, info = env.step(int(action))
episode_reward += reward
episode_rewards.append(episode_reward)
print("Episode", str(episode_no + 1), "| Total Reward:", str(episode_reward))
results_summary = save_results_summary(model_dir, None, episode_rewards, [100], [0])