def _get_fitness_(self): """ Helper method to perform a single simulation to evaluate the performance of the chromosome. This will be called multiple times and the overall performance of the chromosome is the average of all the runs. """ tetrominos = [ Tetromino.ITetromino(), Tetromino.OTetromino(), Tetromino.TTetromino(), Tetromino.STetromino(), Tetromino.ZTetromino(), Tetromino.JTetromino(), Tetromino.LTetromino() ] field = Field() field_score = -1 for length in range(self.max_simulation_length): tetromino = random.choice(tetrominos) _, __, _field, _field_score = field.get_optimal_drop( tetromino, self.genes) if _field_score == math.inf: return length, field_score else: field = _field field_score = _field_score return length, field_score
def show(chromosome): tetrominos = [ Tetromino.ITetromino(), Tetromino.OTetromino(), Tetromino.TTetromino(), Tetromino.STetromino(), Tetromino.ZTetromino(), Tetromino.JTetromino(), Tetromino.LTetromino() ] field = Field() pieces = 0 while True: tetromino = random.choice(tetrominos) _, __, field, ___ = field.get_optimal_drop(tetromino, chromosome.genes) if field == None: break print(field) pieces += 1 time.sleep(0.1) print('Performance: {}'.format(pieces))
next_rows = driver.find_element_by_xpath( '/html/body/div[2]/div/div/div[1]/div[2]/div[1]/div[2]/div/table/tbody' ).find_elements_by_tag_name("tr") field = Field() current_tetromino = get_t(first_rows)() print(current_tetromino) next_tetromino = None while True: time.sleep(1) try: next_tetromino = get_next_t(next_rows)() except Exception as e: driver.quit() quit() print(str(e)) print(next_tetromino) opt = field.get_optimal_drop(current_tetromino) rotation = opt[-1] column = opt[1] current_tetromino.rotate(rotation) field.drop(current_tetromino, column) keys = get_keystrokes(rotation, column, { 'rotate_right': 'k', 'move_left': 'h', 'move_right': 'l', 'drop': ' ' }) pyautogui.typewrite(keys) current_tetromino = next_tetromino time.sleep(0.2)