-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
316 lines (249 loc) · 9.46 KB
/
Copy pathtrain.py
File metadata and controls
316 lines (249 loc) · 9.46 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import yaml
import os
import numpy as np
import matplotlib.pyplot as plt
from game.snake import SnakeGame
from game.metrics import GameMetrics
from game.difficulty_manager import DifficultyManager
from agent.dqn import DQNAgent
from agent.ppo import PPOAgent
from agent.replay_buffer import ReplayBuffer
import pygame
def calculate_reward(metrics, game_over, prev_score, prev_time):
"""
Calculate reward based on player performance
Reward structure:
- Positive reward for scoring
- Positive reward for survival
- Negative reward for dying
"""
reward = 0
# Score increase
score_increase = metrics.score - prev_score
reward += score_increase * 10 # +10 per food eaten
# Survival bonus
time_increase = metrics.get_survival_time() - prev_time
reward += time_increase * 0.1 # Small bonus for staying alive
# Death penalty
if game_over:
reward -= 50
return reward
def train_dqn(config_path='config/hyperparameters.yaml', save_dir='models'):
"""Train DQN agent"""
print("Starting DQN Training...")
# Load configuration
with open(config_path, 'r') as f:
config = yaml.safe_load(f)
dqn_config = config['dqn']
train_config = config['training']
env_config = config['environment']
# Create directories
os.makedirs(save_dir, exist_ok=True)
os.makedirs('plots', exist_ok=True)
# Initialize components
agent = DQNAgent(
state_size=env_config['state_size'],
action_size=env_config['action_size'],
config=dqn_config
)
replay_buffer = ReplayBuffer(capacity=dqn_config['memory_size'])
# Training stats
episode_rewards = []
episode_scores = []
losses = []
for episode in range(train_config['episodes']):
# Initialize environment
game = SnakeGame()
game.display = pygame.display.set_mode((600, 400))
pygame.display.set_caption(f"DQN Training - Episode {episode + 1}")
metrics = GameMetrics()
difficulty_manager = DifficultyManager()
total_reward = 0
prev_score = 0
prev_time = 0
step = 0
game.reset()
while not game.game_over and step < train_config['max_steps']:
# Get current state
state = [
game.score,
int(pygame.time.get_ticks() / 1000),
0, # deaths (per episode)
difficulty_manager.get_difficulty_level()
]
# Select action
action = agent.select_action(state, training=True)
# Apply difficulty adjustment
speed = difficulty_manager.apply_action(action, game)
# Game step
game.handle_input()
game.move_snake()
if train_config.get('render', False):
game.draw()
game.clock.tick(speed)
# Get next state and reward
next_state = [
game.score,
int(pygame.time.get_ticks() / 1000),
1 if game.game_over else 0,
difficulty_manager.get_difficulty_level()
]
reward = calculate_reward(
metrics,
game.game_over,
prev_score,
prev_time
)
metrics.update_score(game.score)
# Store transition
replay_buffer.push(state, action, reward, next_state, game.game_over)
# Train agent
loss = agent.train(replay_buffer)
if loss is not None:
losses.append(loss)
total_reward += reward
prev_score = game.score
prev_time = int(pygame.time.get_ticks() / 1000)
step += 1
episode_rewards.append(total_reward)
episode_scores.append(game.score)
# Log progress
if (episode + 1) % 10 == 0:
avg_reward = np.mean(episode_rewards[-10:])
avg_score = np.mean(episode_scores[-10:])
avg_loss = np.mean(losses[-100:]) if losses else 0
print(f"Episode {episode + 1}/{train_config['episodes']} | "
f"Avg Reward: {avg_reward:.2f} | Avg Score: {avg_score:.2f} | "
f"Loss: {avg_loss:.4f} | Epsilon: {agent.epsilon:.3f}")
# Save model
if (episode + 1) % train_config['save_frequency'] == 0:
agent.save(f"{save_dir}/dqn_episode_{episode + 1}.pth")
print(f"Model saved at episode {episode + 1}")
pygame.quit()
# Save final model
agent.save(f"{save_dir}/dqn_final.pth")
# Plot training results
plot_training_results(episode_rewards, episode_scores, losses, 'DQN')
print("DQN Training Complete!")
return agent
def train_ppo(config_path='config/hyperparameters.yaml', save_dir='models'):
"""Train PPO agent"""
print("Starting PPO Training...")
# Load configuration
with open(config_path, 'r') as f:
config = yaml.safe_load(f)
ppo_config = config['ppo']
train_config = config['training']
env_config = config['environment']
# Create directories
os.makedirs(save_dir, exist_ok=True)
os.makedirs('plots', exist_ok=True)
# Initialize agent
agent = PPOAgent(
state_size=env_config['state_size'],
action_size=env_config['action_size'],
config=ppo_config
)
# Training stats
episode_rewards = []
episode_scores = []
losses = []
for episode in range(train_config['episodes']):
# Initialize environment
game = SnakeGame()
game.display = pygame.display.set_mode((600, 400))
pygame.display.set_caption(f"PPO Training - Episode {episode + 1}")
metrics = GameMetrics()
difficulty_manager = DifficultyManager()
total_reward = 0
prev_score = 0
prev_time = 0
step = 0
game.reset()
while not game.game_over and step < train_config['max_steps']:
# Get current state
state = [
game.score,
int(pygame.time.get_ticks() / 1000),
0,
difficulty_manager.get_difficulty_level()
]
# Select action
action = agent.select_action(state, training=True)
# Apply difficulty adjustment
speed = difficulty_manager.apply_action(action, game)
# Game step
game.handle_input()
game.move_snake()
if train_config.get('render', False):
game.draw()
game.clock.tick(speed)
# Calculate reward
reward = calculate_reward(
metrics,
game.game_over,
prev_score,
prev_time
)
metrics.update_score(game.score)
# Store transition
agent.store_transition(reward, game.game_over)
total_reward += reward
prev_score = game.score
prev_time = int(pygame.time.get_ticks() / 1000)
step += 1
# Train agent after episode
loss = agent.train()
if loss is not None:
losses.append(loss)
episode_rewards.append(total_reward)
episode_scores.append(game.score)
# Log progress
if (episode + 1) % 10 == 0:
avg_reward = np.mean(episode_rewards[-10:])
avg_score = np.mean(episode_scores[-10:])
avg_loss = np.mean(losses[-10:]) if losses else 0
print(f"Episode {episode + 1}/{train_config['episodes']} | "
f"Avg Reward: {avg_reward:.2f} | Avg Score: {avg_score:.2f} | "
f"Loss: {avg_loss:.4f}")
# Save model
if (episode + 1) % train_config['save_frequency'] == 0:
agent.save(f"{save_dir}/ppo_episode_{episode + 1}.pth")
print(f"Model saved at episode {episode + 1}")
pygame.quit()
# Save final model
agent.save(f"{save_dir}/ppo_final.pth")
# Plot training results
plot_training_results(episode_rewards, episode_scores, losses, 'PPO')
print("PPO Training Complete!")
return agent
def plot_training_results(rewards, scores, losses, agent_name):
"""Plot training metrics"""
fig, axes = plt.subplots(1, 3, figsize=(15, 4))
# Rewards
axes[0].plot(rewards)
axes[0].set_title(f'{agent_name} - Episode Rewards')
axes[0].set_xlabel('Episode')
axes[0].set_ylabel('Total Reward')
axes[0].grid(True)
# Scores
axes[1].plot(scores)
axes[1].set_title(f'{agent_name} - Episode Scores')
axes[1].set_xlabel('Episode')
axes[1].set_ylabel('Score')
axes[1].grid(True)
# Losses
axes[2].plot(losses)
axes[2].set_title(f'{agent_name} - Training Loss')
axes[2].set_xlabel('Training Step')
axes[2].set_ylabel('Loss')
axes[2].grid(True)
plt.tight_layout()
plt.savefig(f'plots/{agent_name.lower()}_training_results.png')
print(f"Training plots saved to plots/{agent_name.lower()}_training_results.png")
if __name__ == "__main__":
import sys
if len(sys.argv) > 1 and sys.argv[1] == 'ppo':
train_ppo()
else:
train_dqn()