-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnostic_ai_debug.py
More file actions
292 lines (223 loc) · 10.1 KB
/
diagnostic_ai_debug.py
File metadata and controls
292 lines (223 loc) · 10.1 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
import numpy as np
import time
from src.utils.constants import *
def test_neural_networks():
"""Test if neural networks are actually different"""
print("🧪 TESTING NEURAL NETWORKS")
print("="*50)
try:
from src.ai.neural_network import NeuralNetwork
# Create 3 neural networks with different seeds (FIXED: proper seed range)
networks = []
for i in range(3):
# FIXED: Use proper seed range for numpy
seed = (int(time.time() * 1000) + i * 1000) % (2**31 - 1)
np.random.seed(seed)
nn = NeuralNetwork(NN_INPUT_NODES, NN_HIDDEN_NODES, NN_OUTPUT_NODES)
networks.append(nn)
# Test with same input
test_input = [0.5, 0.0, 0.8, 0.5]
print("Testing with input:", test_input)
outputs = []
for i, nn in enumerate(networks):
output = nn.forward_pass(test_input)
decision = nn.predict(test_input)
weights_sample = nn.get_weights_as_array()[:5]
print(f"Network {i+1}:")
print(f" Output: {output:.4f}")
print(f" Decision: {decision}")
print(f" Sample weights: {weights_sample}")
print()
outputs.append(output)
# Check if networks are different
weights1 = networks[0].get_weights_as_array()
weights2 = networks[1].get_weights_as_array()
difference = np.mean(np.abs(weights1 - weights2))
output_variance = np.var(outputs)
print(f"Weight difference between networks: {difference:.6f}")
print(f"Output variance: {output_variance:.6f}")
success = difference > 0.0001 and output_variance > 0.0001
if success:
print("✅ Networks are properly different")
else:
print("❌ PROBLEM: Networks are too similar!")
return success
except Exception as e:
print(f"❌ Error testing neural networks: {e}")
return False
def test_game_state_inputs():
"""Test if game state inputs are valid"""
print("\n🎮 TESTING GAME STATE INPUTS")
print("="*50)
try:
# FIXED: Initialize pygame properly
import pygame
pygame.init()
pygame.display.set_mode((400, 600)) # Initialize display for image conversion
from src.game.bird import Bird
from src.game.pipe import Pipe
from src.utils.asset_loader import AssetLoader
# Create mock bird
asset_loader = AssetLoader()
asset_loader.load_all_assets()
bird_sprites = asset_loader.get_bird_sprites("BLUE")
bird = Bird(100, 300, bird_sprites, "BLUE")
# Create mock pipes
pipe_sprite = asset_loader.get_pipe_sprite("GREEN")
pipes = []
# Create a proper pipe pair
top_pipe = Pipe(400, 0, pipe_sprite, is_top=True)
top_pipe.rect.height = 200 # Top pipe height
top_pipe.rect.bottom = 200 # Top pipe bottom at y=200
bottom_pipe = Pipe(400, 350, pipe_sprite, is_top=False)
bottom_pipe.rect.height = 250 # Bottom pipe height
bottom_pipe.rect.top = 350 # Bottom pipe top at y=350
pipes = [top_pipe, bottom_pipe]
# Test game state extraction
game_state = bird.get_game_state(pipes)
print("Game state:", game_state)
print("Game state length:", len(game_state))
print("Game state types:", [type(x) for x in game_state])
# Check validity
valid = True
if len(game_state) != 4:
print("❌ PROBLEM: Game state should have 4 inputs")
valid = False
for i, val in enumerate(game_state):
if not isinstance(val, (int, float)):
print(f"❌ PROBLEM: Game state[{i}] is not numeric: {type(val)}")
valid = False
elif abs(val) > 10: # Reasonable range check
print(f"⚠️ WARNING: Game state[{i}] seems extreme: {val}")
if valid:
print("✅ Game state inputs look valid")
pygame.quit()
return valid
except Exception as e:
print(f"❌ Error testing game state: {e}")
return False
def test_collision_detection():
"""Test if collision detection is too aggressive"""
print("\n💥 TESTING COLLISION DETECTION")
print("="*50)
try:
# FIXED: Initialize pygame properly
import pygame
pygame.init()
pygame.display.set_mode((400, 600))
from src.game.bird import Bird
from src.utils.asset_loader import AssetLoader
# Create bird
asset_loader = AssetLoader()
asset_loader.load_all_assets()
bird_sprites = asset_loader.get_bird_sprites("BLUE")
bird = Bird(100, 300, bird_sprites, "BLUE")
print(f"Bird initial position: ({bird.rect.x}, {bird.rect.y})")
print(f"Bird size: {bird.rect.width} x {bird.rect.height}")
print(f"Screen height: {SCREEN_HEIGHT}")
print(f"Ground level: {SCREEN_HEIGHT - 112}")
# Simulate bird falling
frames_to_ground = 0
original_y = bird.rect.y
while bird.rect.bottom < SCREEN_HEIGHT - 112 and frames_to_ground < 100:
bird.update(False) # No jumping
frames_to_ground += 1
distance_fallen = bird.rect.y - original_y
print(f"Frames to reach ground: {frames_to_ground}")
print(f"Distance fallen: {distance_fallen} pixels")
print(f"Final bird position: ({bird.rect.x}, {bird.rect.y})")
# Analysis
if frames_to_ground < 40:
print("⚠️ WARNING: Bird reaches ground very quickly!")
print("This explains why all birds die at ~38 frames")
success = False
else:
print("✅ Bird takes reasonable time to fall")
success = True
# Test collision buffer
bird2 = Bird(100, SCREEN_HEIGHT - 112 - 3, bird_sprites, "BLUE") # Near ground
collision = bird2.check_collision([], SCREEN_HEIGHT - 112, SCREEN_HEIGHT)
print(f"Collision with 3px buffer: {collision}")
pygame.quit()
return success
except Exception as e:
print(f"❌ Error testing collision: {e}")
return False
def test_fitness_calculation():
"""Test fitness calculation logic"""
print("\n🎯 TESTING FITNESS CALCULATION")
print("="*50)
try:
from src.ai.fitness import Fitness
# Create mock bird data
class MockBird:
def __init__(self, score, alive, frames_survived=38):
self.score = score
self.alive = alive
self.fitness = frames_survived * FITNESS_BONUS_DISTANCE
birds = [
MockBird(0, False, 38), # Typical failing case
MockBird(1, False, 180), # Bird that scored once
MockBird(0, True, 200), # Still alive bird
]
print("Testing fitness calculation:")
for i, bird in enumerate(birds):
fitness = Fitness.calculate_fitness(bird, 600, 1) # 600ms = 0.6s
expected_fitness = (
600 * FITNESS_BONUS_DISTANCE + # Survival time bonus
bird.score * FITNESS_BONUS_PIPE + # Score bonus
bird.fitness + # Distance bonus
(0 if bird.alive else abs(FITNESS_PENALTY_DEATH)) # Death penalty
) * 1.1 # Generation multiplier
print(f"Bird {i+1}: score={bird.score}, alive={bird.alive}, frames={bird.fitness/FITNESS_BONUS_DISTANCE:.0f}")
print(f" Calculated fitness: {fitness:.1f}")
print(f" Expected range: ~{expected_fitness:.1f}")
print()
return True
except Exception as e:
print(f"❌ Error testing fitness: {e}")
return False
def main():
"""Run all diagnostic tests"""
print("🔬 FLAPPY BIRD AI DIAGNOSTIC TOOL (FIXED)")
print("="*60)
print("This will identify the exact issues with your AI training")
print()
results = {}
# Run all tests
results['neural_networks'] = test_neural_networks()
results['game_state'] = test_game_state_inputs()
results['collision'] = test_collision_detection()
results['fitness'] = test_fitness_calculation()
# Summary
print("\n📊 DIAGNOSTIC SUMMARY")
print("="*50)
issues_found = []
for test_name, passed in results.items():
status = "✅ PASS" if passed else "❌ FAIL"
print(f"{test_name.replace('_', ' ').title()}: {status}")
if not passed:
issues_found.append(test_name)
if issues_found:
print(f"\n🚨 ISSUES FOUND: {len(issues_found)}")
print("Problems detected in:", ", ".join(issues_found))
print("\n🔧 SPECIFIC FIXES NEEDED:")
if 'neural_networks' in issues_found:
print("- Fix neural network random seed initialization")
print("- Ensure each bird gets different weights")
if 'game_state' in issues_found:
print("- Fix bird.get_game_state() method")
print("- Ensure proper pipe detection and normalization")
if 'collision' in issues_found:
print("- Make collision detection more lenient")
print("- Add collision buffers to prevent instant death")
if 'fitness' in issues_found:
print("- Fix fitness calculation formula")
print(f"\n💡 PRIORITY: Fix neural network diversity first!")
print("All birds are making identical decisions because they have identical weights.")
else:
print("\n✅ ALL TESTS PASSED!")
print("The AI training should work properly now.")
print(f"\n{'='*60}")
if __name__ == "__main__":
main()