def eval_network(epoch, child_index, child_model): pyboy = PyBoy('SuperMarioLand.gb', game_wrapper=True) #, window_type="headless") pyboy.set_emulation_speed(3) mario = pyboy.game_wrapper() mario.start_game() # start off with just one life each mario.set_lives_left(0) run = 0 scores = [] fitness_scores = [] level_progress = [] time_left = [] # prev_action = np.asarray([1,0]) # do_action(prev_action, pyboy) while run < run_per_child: # do some things action = get_action(pyboy, mario, child_model) action = action.detach().numpy() action = np.where(action < np.max(action), 0, action) action = np.where(action == np.max(action), 1, action) action = action.astype(int) action = action.reshape((2, )) # do_action_multiple(prev_action,action,pyboy) do_action(action, pyboy) # prev_action = action # Game over: if mario.game_over() or mario.score == max_score: scores.append(mario.score) #fitness_scores.append(mario.fitness) fitness_scores.append( fitness_calc(mario.score, mario.level_progress, mario.time_left)) level_progress.append(mario.level_progress) time_left.append(mario.time_left) if run == run_per_child - 1: pyboy.stop() else: mario.reset_game() run += 1 child_fitness = np.average(fitness_scores) logger.info("-" * 20) logger.info("Iteration %s - child %s" % (epoch, child_index)) logger.info("Score: %s, Level Progress: %s, Time Left %s" % (scores, level_progress, time_left)) logger.info("Fitness: %s" % child_fitness) #logger.info("Output weight:") #weights = {} #for i, j in zip(feature_names, child_model.output.weight.data.tolist()[0]): # weights[i] = np.round(j, 3) #logger.info(weights) return child_fitness
def eval_genome(genome, config): global max_fitness pyboy = PyBoy('SuperMarioLand.gb', game_wrapper=True) #, window_type="headless") pyboy.set_emulation_speed(0) mario = pyboy.game_wrapper() mario.start_game() # start off with just one life each mario.set_lives_left(0) run = 0 scores = [] fitness_scores = [] level_progress = [] time_left = [] # create NN from neat model = neat.nn.FeedForwardNetwork.create(genome, config) child_fitness = 0 while run < run_per_child: # do some things action = get_action_neat(pyboy, mario, model) action = np.asarray([np.mean(val) for val in action]) action = np.where(action < np.max(action), 0, action) action = np.where(action == np.max(action), 1, action) action = action.astype(int) action = action.reshape((2, )) do_action(action, pyboy) # Game over: if mario.game_over() or mario.score == max_score: scores.append(mario.score) fitness_scores.append( fitness_calc(mario.score, mario.level_progress, mario.time_left)) level_progress.append(mario.level_progress) time_left.append(mario.time_left) if run == run_per_child - 1: pyboy.stop() else: mario.reset_game() run += 1 child_fitness = np.average(fitness_scores) #logger.info("-" * 20) #logger.info("Iteration %s - child %s" % (epoch, child_index)) #logger.info("Score: %s, Level Progress: %s, Time Left %s" % (scores, level_progress, time_left)) #logger.info("Fitness: %s" % child_fitness) return child_fitness
def eval_genome(genome, config): global max_fitness pyboy = PyBoy('tetris_1.1.gb', window_type='quiet', game_wrapper=True) pyboy.set_emulation_speed(0) tetris = pyboy.game_wrapper() tetris.start_game() # Set block animation to fall instantly pyboy.set_memory_value(0xff9a, 2) model = neat.nn.FeedForwardNetwork.create(genome, config) child_fitness = 0 while not pyboy.tick(): # Beginning of action best_action_score = np.NINF best_action = {'Turn': 0, 'Left': 0, 'Right': 0} begin_state = io.BytesIO() begin_state.seek(0) pyboy.save_state(begin_state) s_lines = tetris.lines # Determine how many possible rotations we need to check for the block block_tile = pyboy.get_memory_value(0xc203) turns_needed = check_needed_turn(block_tile) lefts_needed, rights_needed = check_needed_dirs(block_tile) # Do middle for move_dir in do_action('Middle', n_dir=1, n_turn=turns_needed, pyboy=pyboy): score = get_score(tetris, model, s_lines, neat=True) if score is not None and score > best_action_score: best_action_score = score best_action = { 'Turn': move_dir['Turn'], 'Left': move_dir['Left'], 'Right': move_dir['Right'] } begin_state.seek(0) pyboy.load_state(begin_state) # Do left for move_dir in do_action('Left', n_dir=lefts_needed, n_turn=turns_needed, pyboy=pyboy): score = get_score(tetris, model, s_lines, neat=True) if score is not None and score > best_action_score: best_action_score = score best_action = { 'Turn': move_dir['Turn'], 'Left': move_dir['Left'], 'Right': move_dir['Right'] } begin_state.seek(0) pyboy.load_state(begin_state) # Do right for move_dir in do_action('Right', n_dir=rights_needed, n_turn=turns_needed, pyboy=pyboy): score = get_score(tetris, model, s_lines, neat=True) if score is not None and score > best_action_score: best_action_score = score best_action = { 'Turn': move_dir['Turn'], 'Left': move_dir['Left'], 'Right': move_dir['Right'] } begin_state.seek(0) pyboy.load_state(begin_state) # Do best action for i in range(best_action['Turn']): do_turn(pyboy) for i in range(best_action['Left']): do_sideway(pyboy, 'Left') for i in range(best_action['Right']): do_sideway(pyboy, 'Right') drop_down(pyboy) pyboy.tick() # Game over: if tetris.game_over() or tetris.score == max_score: child_fitness = tetris.score if tetris.score == max_score: print("Max score reached") break # Dump best model if child_fitness >= max_fitness: max_fitness = child_fitness file_name = str(np.round(max_fitness, 2)) if tetris.level >= 20: with open('neat_models/%s' % file_name, 'wb') as f: pickle.dump(model, f) pyboy.stop() return child_fitness
def eval_network(epoch, child_index, child_model): pyboy = PyBoy('tetris_1.1.gb', game_wrapper=True, window_type="headless") pyboy.set_emulation_speed(0) tetris = pyboy.game_wrapper() tetris.start_game() # Set block animation to fall instantly pyboy.set_memory_value(0xff9a, 2) run = 0 scores = [] levels = [] lines = [] while run < run_per_child: # Beginning of action best_action_score = np.NINF best_action = {'Turn': 0, 'Left': 0, 'Right': 0} begin_state = io.BytesIO() begin_state.seek(0) pyboy.save_state(begin_state) # Number of lines at the start s_lines = tetris.lines # Determine how many possible rotations we need to check for the block block_tile = pyboy.get_memory_value(0xc203) turns_needed = check_needed_turn(block_tile) lefts_needed, rights_needed = check_needed_dirs(block_tile) # Do middle for move_dir in do_action('Middle', pyboy, n_dir=1, n_turn=turns_needed): score = get_score(tetris, child_model, s_lines) if score is not None and score >= best_action_score: best_action_score = score best_action = { 'Turn': move_dir['Turn'], 'Left': move_dir['Left'], 'Right': move_dir['Right'] } begin_state.seek(0) pyboy.load_state(begin_state) # Do left for move_dir in do_action('Left', pyboy, n_dir=lefts_needed, n_turn=turns_needed): score = get_score(tetris, child_model, s_lines) if score is not None and score >= best_action_score: best_action_score = score best_action = { 'Turn': move_dir['Turn'], 'Left': move_dir['Left'], 'Right': move_dir['Right'] } begin_state.seek(0) pyboy.load_state(begin_state) # Do right for move_dir in do_action('Right', pyboy, n_dir=rights_needed, n_turn=turns_needed): score = get_score(tetris, child_model, s_lines) if score is not None and score >= best_action_score: best_action_score = score best_action = { 'Turn': move_dir['Turn'], 'Left': move_dir['Left'], 'Right': move_dir['Right'] } begin_state.seek(0) pyboy.load_state(begin_state) # Do best action for _ in range(best_action['Turn']): do_turn(pyboy) for _ in range(best_action['Left']): do_sideway(pyboy, 'Left') for _ in range(best_action['Right']): do_sideway(pyboy, 'Right') drop_down(pyboy) pyboy.tick() # Game over: if tetris.game_over() or tetris.score == max_score: scores.append(tetris.score) levels.append(tetris.level) lines.append(tetris.lines) if run == run_per_child - 1: pyboy.stop() else: tetris.reset_game() run += 1 child_fitness = np.average(scores) logger.info("-" * 20) logger.info("Iteration %s - child %s" % (epoch, child_index)) logger.info("Score: %s, Level: %s, Lines %s" % (scores, levels, lines)) logger.info("Fitness: %s" % child_fitness) logger.info("Output weight:") weights = {} for i, j in zip(feature_names, child_model.output.weight.data.tolist()[0]): weights[i] = np.round(j, 3) logger.info(weights) return child_fitness
while n < n_plays: # Beginning of action best_child_score = np.NINF best_action = {'Turn': 0, 'Left': 0, 'Right': 0} begin_state = io.BytesIO() begin_state.seek(0) pyboy.save_state(begin_state) s_lines = tetris.lines # Determine how many possible rotations we need to check for the block block_tile = pyboy.get_memory_value(0xc203) turns_needed = check_needed_turn(block_tile) lefts_needed, rights_needed = check_needed_dirs(block_tile) # Do middle for move_dir in do_action('Middle', pyboy, n_dir=1, n_turn=turns_needed): score = get_score(tetris, model, s_lines, neat=True) if score is not None and score > best_child_score: best_child_score = score best_action = {'Turn': move_dir['Turn'], 'Left': move_dir['Left'], 'Right': move_dir['Right']} begin_state.seek(0) pyboy.load_state(begin_state) # Do left for move_dir in do_action('Left', pyboy, n_dir=lefts_needed, n_turn=turns_needed): score = get_score(tetris, model, s_lines, neat=True) if score is not None and score > best_child_score: best_child_score = score