-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_ppo_parallel.py
90 lines (74 loc) · 2.53 KB
/
main_ppo_parallel.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
import os
import csv
import gym
import numpy as np
import pygame
from stable_baselines3 import PPO
from stable_baselines3.common.vec_env import DummyVecEnv
from stable_baselines3.common.env_util import make_vec_env
from old_files.parallel_parking_old import Environment
class CustomParkingEnvironment(gym.Env):
def __init__(self):
super(CustomParkingEnvironment, self).__init__()
self.env = Environment()
self.action_space = gym.spaces.Discrete(4)
self.observation_space = gym.spaces.Box(low=np.array([0, 0, -360]),
high=np.array([400, 600, 360]),
dtype=np.float32)
self.current_step = 0
self.max_episode_steps = 120
def step(self, action):
state, reward, done = self.env.step(action)
self.current_step += 1
if self.current_step >= self.max_episode_steps:
done = True
return state, reward, done, {}
def reset(self):
state = self.env.reset()
self.current_step = 0
return state
def render(self, mode='human'):
self.env.render()
def main():
file = "parallel_63_ppo"
env = make_vec_env(CustomParkingEnvironment, n_envs=1)
hyperparams = {
"learning_rate": 0.0003,
"n_steps": 2048,
"batch_size": 64,
"n_epochs": 10,
"gamma": 0.99,
"gae_lambda": 0.95,
"clip_range": 0.2,
"ent_coef": 0.0,
"vf_coef": 0.5,
"max_grad_norm": 0.5,
}
model_file_path = f"past_runs/{file}.zip"
if os.path.exists(model_file_path):
model = PPO.load(model_file_path, env, **hyperparams, tensorboard_log="data/")
else:
model = PPO("MlpPolicy", env, verbose=1, **hyperparams, tensorboard_log="data/")
total_episodes = 5000
total_timesteps = 150000
log_dir = "data/"
os.makedirs(log_dir, exist_ok=True)
model.learn(total_timesteps=total_timesteps, tb_log_name=f"{file}")
model.save(model_file_path)
clock = pygame.time.Clock()
fps = 30
episode_length = 0
for episode in range(total_episodes):
obs = env.reset()
done = False
total_reward = 0
while not done:
action, _states = model.predict(obs, deterministic=True)
obs, reward, done, info = env.step(action)
env.render()
total_reward += reward
clock.tick(fps)
episode_length += 1
print(f"Episode {episode+1}/{total_episodes}, Score: {total_reward}")
if __name__ == "__main__":
main()