-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
61 lines (35 loc) · 1.54 KB
/
train.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
from stable_baselines3 import PPO #PPO
from TrafficEnv import SpeedLimitEnv
import time
import os
from stable_baselines3.common.callbacks import CallbackList
from callbacks import *
from stable_baselines3.common.vec_env import VecNormalize, DummyVecEnv
from stable_baselines3.common.monitor import Monitor
from stable_baselines3.common.callbacks import CheckpointCallback
logdir = f"logs/{int(time.time())}/"
if not os.path.exists(logdir):
os.makedirs(logdir)
def simulation_loop():
# Create Callback
save_callback = SaveOnBestTrainingRewardCallback(check_freq=100, log_dir=logdir, verbose=1)
tensor = TensorboardCallback()
checkpoint = CheckpointCallback(save_freq=500, save_path=logdir, verbose=1 )
env= SpeedLimitEnv()
env = Monitor(env, logdir)
env = DummyVecEnv([lambda: env])
env = VecNormalize(env)
model = PPO('MlpPolicy', env, verbose=2, tensorboard_log=logdir)
TIMESTEPS = 500000
model.learn(total_timesteps=TIMESTEPS, reset_num_timesteps=False, tb_log_name=f"PPO", progress_bar=True,
callback = CallbackList([tensor, save_callback, checkpoint]))
# ==============================================================================
# -- main() --------------------------------------------------------------------
# ==============================================================================
def main():
try:
simulation_loop()
except KeyboardInterrupt:
print('\nCancelled by user. Bye!')
if __name__ == '__main__':
main()