“play_a” - contains main logic for AI to play the game once.“genetic_alg.lua” - contains functions to implement genetic algorithm.outputs a csv file of information on each population.“a” - runs genetic algorithm that finds the fittest heuristic coefficient.
"play_a" - plays the game once with the fittest heuristic coefficients.Add new heuristics or improve current ones.Utilize tucks and spins to reduce amount of holes in game.The final reward function is calculated using the fittest 50% of the last generation. Population AnalysisĪfter the algorithm is completed, a csv file of the populations is given.Ī python script is used to summarize the data. Once the alorithm terminates, we can analyze the last population the determine the best heuristic parameters to play Tetris. When removing genomes with lowest fitness from population, we remove the same amount of the children generated. When mutation occurs, one parameter will be randomly selected for the child and it random value from -1 to 1 will be added. The crossover is weighted by fitness (child will resemble the fitter parent more).Īnd during crossover, mutation has a small chance of occuring to preserve diversity. Then from the selected genomes, crossover is performed by combining the 2 fittest genomes to create a child. Then until the generation limit is reached, we create new children using selection and crossover and remove the least fit genomes from the current population.ĭuring selection, we get a fixed percentage of randomly selected genomes from current population. To start the algorithm, we create the initial population that contains genomes with randomly generated heuristic parameters ranging from -1 to 1. Remove genomes with lowest fitness from population Get 2 fittest parents and crossover to create child The fitness is determined by the game score because it considers the number of lines cleared at a timeįor (children_size) // selection and crossover.The weakest ones get replaced with children created by crossover. For every generation, the fittest heuristic parameters stay in the population.The genetic algorithm plays the game numerous times to determine the best coefficients for the heuristics.The coefficient values of the linear function will be determined by a genetic algorithm.d) bumpiness - sum of the absolute differences between every pair of adjacent columns.a hole occurs when a cell is empty and at least one cell in the same column is filled above it.c) holes - number of holes in playfield.b) complete lines - number of rows that will be complete.a) aggergate height - sum of all column heights.Reward = (a * aggregate_height) + (b * complete_lines) + (c * holes) + (d * bumpiness).The best move will give the highest reward. The AI plays the game by calculating the reward in every possible piece placement and rotation.more lines cleared at once gives more points.Score is increased every time a line clears.Goal is to clear lines without reaching top of playfield.To run game at normal speed, edit play_a and comment out "emu.speedmode("turbo")".To run play the game once with the best heuristics, select "lua_scripts/play_a".
Tetris nes install#
Install FCEUX emulator here, which is currently only supported on windows.