-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraining_PPO.py
More file actions
140 lines (116 loc) · 3.84 KB
/
Copy pathtraining_PPO.py
File metadata and controls
140 lines (116 loc) · 3.84 KB
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
import gymnasium
import highway_env
import numpy as np
import torch
import random
from agent_PPO import Agent_PPO
import matplotlib.pyplot as plt
from evaluate import evaluate_model
if __name__ == '__main__':
# Set the seed and create the environment
np.random.seed(0)
random.seed(0)
torch.manual_seed(0)
# hyperparameters
batch_size = 24
clip = 0.2
critic_rep = 10
actor_rep = 10
discount = 0.9
MAX_EPISODES = 4000
env_name = 'highway-fast-v0' # 'fast' env just for faster training
env = gymnasium.make(env_name,
config={'action': {'type': 'DiscreteMetaAction'}, 'duration': 40, 'vehicles_count': 50})
# Initialize the agent
agent = Agent_PPO(env, discount=discount, actor_rep=actor_rep, critic_rep=critic_rep, clip=clip, batch_size=batch_size)
state, _ = env.reset()
state = state.reshape(-1)
done, truncated = False, False
episode = 1
episode_steps = 0
episode_return = 0
# for training
rewards = []
crashes = []
states = []
next_states = []
actions = []
masks = []
# for plotting
rewards_plot = []
history = []
history_ev_ret = []
history_ev_rew = []
t = 0
# Training loop
while episode <= MAX_EPISODES:
episode_steps += 1
t += 1
# Select the action to be performed by the agent
mask = agent.mask()
action = agent.act(torch.tensor(state))
masks.append(mask)
actions.append(action)
next_state, reward, done, truncated, _ = env.step(action)
next_state = next_state.reshape(-1)
# Store transition in memory
rewards.append(reward)
rewards_plot.append(reward)
states.append(state)
next_states.append(next_state)
crashes.append(done)
state = next_state
episode_return += reward
if done or truncated:
# Update given the entire trajectory
agent.learn(torch.from_numpy(np.array(states)),
torch.from_numpy(np.array(actions)),
torch.from_numpy(np.array(rewards)),
torch.from_numpy(np.array(next_states)),
torch.from_numpy(np.array(crashes)),
torch.stack(masks))
if episode % 10 == 0:
print(f'Total T: {t} Episode Num: {episode} Episode T: {episode_steps} Return: {episode_return:.3f}, truncated: {truncated}')
# Save training information and model parameters
history.append(np.mean(rewards_plot))
# Discard data since it is on-policy
rewards_plot = []
rewards = []
crashes = []
states = []
next_states = []
actions = []
masks = []
if episode % 250 == 0:
agent.save_models(episode, 'Models/PPO/')
print('Evaluating model..')
res = evaluate_model(episode, 'PPO')
history_ev_ret.append(np.mean(res[0]))
history_ev_rew.append(np.mean(res[1]))
print(f'Average reward evaluation: {history_ev_rew[-1]}')
print(f'Average return evaluation: {history_ev_ret[-1]}')
state, _ = env.reset()
state = state.reshape(-1)
episode += 1
episode_steps = 0
episode_return = 0
env.close()
# ========= Plotting =========
f = plt.figure()
plt.plot(history)
plt.xlabel('Episode')
plt.ylabel('Average reward')
plt.title('Average reward over episode')
f.savefig('Figures/PPO_training.pdf', bbox_inches='tight')
f = plt.figure()
plt.plot(history_ev_ret)
plt.xlabel('Evaluation Step')
plt.ylabel('Average return')
plt.title('Average return over steps')
f.savefig('Figures/PPO_training_eval_return.pdf', bbox_inches='tight')
f = plt.figure()
plt.plot(history_ev_rew)
plt.xlabel('Evaluation Step')
plt.ylabel('Average reward')
plt.title('Average reward over steps')
f.savefig('Figures/PPO_training_eval_avg_reward.pdf', bbox_inches='tight')