Lecture Notes on Moving Birds Using Neural Networks

Jul 21, 2024

Lecture on Moving Birds Using Neural Networks

Introduction

  • Goal: Move birds based on a neural network
  • Previous steps: Checked collisions, pipes, and removals

Inputs for Neural Network

  • Distance between top pipe and bottom pipe
  • Y position of the bird
  • Handling multiple pipes on the screen
    • Max two pipes at all times
    • Need to determine which pipe to focus on

Pipe Selection Logic

  • Initialize pipe_IND = 0
  • Condition to check which pipe to focus on:
    • If len(birds) > 0
    • If len(pipes) > 1
      • Compare bird’s X position with pipe’s X position and width
      • Adjust pipe_IND accordingly

Moving Birds

  • For each bird:
    • Get neural network output
    • Move the bird: bird.move()
    • Add fitness for survival: bird.fitness += 0.1 each frame
    • Activate neural network: output = net.activate()
    • Inputs for activation:
      • bird.Y
      • abs(bird.Y - pipe[pipe_IND].height) (top pipe distance)
      • abs(bird.Y - pipe[pipe_IND].bottom) (bottom pipe distance)
    • Condition for bird to jump: if output[0] > 0.5 then bird.jump()

Additional Conditions

  • Check if bird reaches the top of the screen (bird Y position < 0)
    • Bird should not survive if it does
  • Correct loops and function calls (e.g., drawing birds, quitting game on no birds left)

Testing and Errors

  • Birds performing well with random neural networks
    • Large number of birds (e.g., 100) covers many neural network combinations
  • Reducing number of birds affects performance
    • Demonstrates need for neuroevolution

Training and Fitness Function

  • Change fitness functions to observe effects
    • Minor changes greatly affect bird performance

Statistics and Generation Details

  • Population's average fitness, standard deviation, best fitness, etc.
  • Observations from running multiple generations
    • Quick improvement due to good fitness function

Saving and Using Best Bird

  • Introduction to saving models with pickle
  • Pickle the best genome and load it later to use its neural network
  • Importance of fitness threshold and breaking process in training
    • Example: Break loop if score > 50

Conclusion

  • Reminder to modify fitness functions and experiment
  • Encouragement to subscribe to the channel for more tutorials

Final Notes

  • The tutorial series demonstrates the integration of neural networks for game playing
  • Audience encouraged to apply learnings and explore further