Created
October 3, 2021 21:14
-
-
Save ahmedfgad/4b022016af1f2320b5424144d41bbe04 to your computer and use it in GitHub Desktop.
Fitness Function of the Bot Playing the Flappy Bird Game using PyGAD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fitness_func(solution, solution_idx): | |
| global SCREEN, playery, pipeHeight, playerx, upperPipes, lowerPipes, GAME_SPRITES, playerVelY, playerFlapAccv, playerFlapped, playerMaxVelY, playerAccY, playerHeight, GROUNDY, nearest_upper_pipe, nearest_lower_pipe | |
| if solution < 0: | |
| return -8888 | |
| if solution > GROUNDY - 25: | |
| return -9999 | |
| fitness_ground = abs(solution - GROUNDY) | |
| if fitness_ground < 50: | |
| fitness_ground = (-1.0/fitness_ground) * 999999 | |
| nearest_upper_pipe = upperPipes[closest_pipe(playerx, upperPipes)] | |
| pipeHeight = GAME_SPRITES['pipe'][0].get_height() | |
| fitness_upper = abs(solution - (pipeHeight + nearest_upper_pipe['y']) - 25) | |
| if(solution < pipeHeight + nearest_upper_pipe['y'] + 70): | |
| fitness_upper = (-1.0/fitness_upper) * 999999 | |
| else: | |
| fitness_upper = fitness_upper | |
| nearest_lower_pipe = lowerPipes[closest_pipe(playerx, lowerPipes)] | |
| fitness_lower = abs(solution + GAME_SPRITES['player'].get_height() - nearest_lower_pipe['y'] - 25) | |
| if (solution + GAME_SPRITES['player'].get_height() > nearest_lower_pipe['y'] - 50): | |
| fitness_lower = (-1.0/fitness_lower) * 999999 | |
| else: | |
| fitness_lower = fitness_lower | |
| fitness = (fitness_ground + fitness_upper + fitness_lower)/3 | |
| return fitness |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment