A Pygame-based simulation where boxes evolve using a genetic algorithm to navigate obstacles and reach a goal.
- Each box has a set of "genes" representing decision points (jump or no action) at specific horizontal positions.
- Boxes move automatically from left to right, jumping when instructed by their genes.
- The goal is to reach the end while avoiding obstacles.
- Fitness is calculated based on obstacles cleared, reaching the goal, and penalizing unnecessary jumps.
- A genetic algorithm evolves the population over generations, using crossover and mutation to improve performance.
- Real-time simulation using Pygame.
- Genetic algorithm with:
- Roulette wheel selection
- One-point crossover
- Mutation introducing jumps near parent death
- Visual representation of boxes, obstacles, and goal.
- Console output showing the best fitness per generation.
WIDTH,HEIGHT: Window dimensions (default 800x400)GROUND: Ground levelPOP_SIZE: Number of boxes per generationGENE_COUNT: Number of decision points per boxFPS: Frames per secondBONUS: Fitness bonus per cleared obstacleJUMP_PENALTY: Fitness penalty per jumpMUTATION_PROB: Probability to introduce new jump near parent deathHORIZONTAL_SPEED: Box horizontal movement speedGRAVITY: Vertical acceleration due to gravityJUMP_VELOCITY: Initial vertical velocity for jumps
- Python 3.x
- Pygame (
pip install pygame)
- Install dependencies:
pip install pygame- Run the script:
python main.py- Watch boxes attempt to navigate obstacles in real-time. Console output shows the best fitness per generation.
- Boxes start with random gene sequences.
- Over generations, the population adapts to clear obstacles more effectively.
- Mutation occasionally introduces jumps near the position where parent boxes died, helping the evolution process.