def train_snakes_step(self): input_array = self.snake_game.board.get_features(normalize=True) next_move = self.snake_pop.population[self.curr_indv].get_mov(input_array) is_alive = self.snake_game.run(next_move) if not is_alive: if self.max_apples < self.snake_game.eaten_apples: self.max_apples = self.snake_game.eaten_apples self.snake_pop.population[self.curr_indv].add_fit(self.snake_game.score) self.curr_indv += 1 if self.curr_indv == self.snake_pop.pop_size: self.curr_indv = 0 if self.snake_pop.generation == 500000: quit() self.snake_pop.improve_pop() print('G =', self.snake_pop.generation) print('best fit =', self.snake_pop.population[0].get_fit()) print('max_apples =', self.max_apples) self.max_apples = 0 self.snake_pop.population[0].indv.save_neural_net('save.pkl') print('safe to ctrl_c') if self.snake_pop.population[0].get_fit() > self.max_fit: self.max_fit = self.snake_pop.population[0].get_fit() print('max fit =', self.max_fit, '\n') self.snake_game = snake.Game(max_moves=self.max_moves, print=self.print_board) return False return True
def nn_playGame(model): print("Now we load weight") model.load_weights("model.h5") print("Weight load successfully") print("Let the game begin!") game_state = game.Game() # Starting up a game game_state.set_start_state() game_image, score, game_lost = game_state.run( 4 ) # The game is started but no action is performed s_t = stack_image(game_image) s_t1 = s_t a_t = 4 while True: if game_lost: print("Game lost") time.sleep(2) print("Game is restarting") game_state.set_start_state() action_index = np.argmax(model.predict(s_t1)) a_t = GAME_INPUT[action_index] x_t1_colored, _, terminal = game_state.run(a_t) s_t1 = stack_image(x_t1_colored) game_lost = terminal
def watch_snake(self): input_array = self.snake_game.board.get_features(normalize=True) next_move = self.snake_pop.population[0].get_mov(input_array) is_alive = self.snake_game.run(next_move) if not is_alive: self.snake_game = snake.Game(max_moves=self.max_moves, print=self.print_board) return False return True
def play_snake(self): input_array = self.snake_game.board.get_features(normalize=False) print('Angle =', input_array[3], '\n') is_alive = self.snake_game.run() if not is_alive: self.snake_game = snake.Game(max_moves=400, print=self.print_board, use_keyboard=self.manual) return False return True
def main(): parser = argparse.ArgumentParser(description='Python Snake Game') parser.add_argument('--map_size', type=int, help='Map Size', default=50) parser.add_argument('--snake_size', type=int, help='Snake Size', default=3) parser.add_argument('--block_size', type=int, help='Block Size', default=15) args = parser.parse_args() game = snake.Game(args.map_size, args.snake_size) while True: while not game.done: screen = game.draw(args.block_size) cv2.imshow('cvwindow', screen) key = cv2.waitKey(50) if key == 81 or key == ord('a') or key == ord('h'): direction = game.snake.y_direction elif key == 82 or key == ord('w') or key == ord('k'): direction = -game.snake.x_direction elif key == 83 or key == ord('d') or key == ord('l'): direction = -game.snake.y_direction elif key == 84 or key == ord('s') or key == ord('j'): direction = game.snake.x_direction elif key == ord('r'): game.reset() elif key == 27: break else: direction = 0 game.step(direction) size = game.map_size * args.block_size scale = 0.004 * size if game.won: screen = game.draw(args.block_size) text = 'WON' else: text = 'Game Over' thickness = int(0.0053 * size) cv2.putText(img = screen, text = text, org = (int(size//2-len(text)*9*scale), int(size//2+9*scale)), fontFace = cv2.FONT_HERSHEY_SIMPLEX, fontScale = scale, color = (1, 1, 1), thickness = thickness) cv2.imshow('cvwindow', screen) key = cv2.waitKey(0) if key == ord('r'): game.reset() elif key == 27: break
def fitness(self, x, y, graphics_enabled, ticks, bonus_ticks, delay=0, score_mult=2, gen=0, screen=pygame.display.set_mode((320, 200))): game = snake.Game(y, x, True) self.fit = game.mainloop(graphics_enabled, ticks, bonus_ticks, self, delay, score_mult, gen, screen) self.game_score = game.score return self.fit
def __init__(self, print_board=False, indv=None, manual=False, max_moves=100): self.manual = manual self.indv = indv self.print_board = print_board self.max_moves = max_moves self.snake_game = snake.Game(max_moves=self.max_moves, print=print_board, use_keyboard=manual) self.snake_pop = None self._configure_pop() self.curr_indv = 0 self.max_fit = 0 self.max_apples = 0
def benchmark(): game = snake.Game(WIDTH, HEIGHT, WALLS_NUMBER, REWARDS) model = ai.Model(STATE_SIZE, ACTION_SIZE) model.load(MODEL_FILE) scores = np.zeros(500) for i in range(500): game.reset() step = 0 while step < STEPS: if not game.move(model.act_best(game.state())): break step += 1 scores[i] = game.score print('Average/ Min / Max score on a new game: {} / {} / {}'.format(np.mean(scores), np.min(scores), np.max(scores))) plt.hist(scores) plt.show()
def calculate_fitness(population): game = snake.Game(hidden=True) for _ in population: game.snakes.append( snake.Snake(snake.CELL_COUNT / 2, snake.CELL_COUNT / 2, 5)) step = 0 while step < WAIT_STEPS: game.render() best = max(game.snakes, key=attrgetter('score')) old_apple = best.apple for i in range(len(population)): apple = game.snakes[i].observe_apple().to_norm_relative() obstacles = [ obstacle.to_norm_relative() for obstacle in game.snakes[i].observe_obstacle() ] inputs = [ apple[0], apple[1], obstacles[0][0], obstacles[1][0], obstacles[2][0] ] action = np.argmax(population[i].compute_outputs(inputs)) if action == 1: game.snakes[i].turn_right() elif action == 2: game.snakes[i].turn_left() game.update(FPS) game.stop = True for s in game.snakes: if not s.dead: game.stop = False break if game.stop: break best = max(game.snakes, key=attrgetter('score')) if old_apple is not best.apple: step = 0 step += 1 fitness = [] for s in game.snakes: fitness.append(s.score) game.end() return fitness
def train(): game = snake.Game(WIDTH, HEIGHT, WALLS_NUMBER, REWARDS) model = ai.Model(STATE_SIZE, ACTION_SIZE) if os.path.isfile(MODEL_FILE): model.load(MODEL_FILE) for e in range(EPISODES): game.reset() tot_reward = 0 for step in range(STEPS): state = game.state() previous_score = game.score action = model.act(state) alive = game.move(action) next_state = None reward = game.score - previous_score tot_reward += reward if alive: next_state = game.state() else: reward -= 100 model.remember((state, action, reward, next_state)) model.replay() print('Episode {}: score {}'.format(e + 1, game.score)) if e % 200 == 199: scores = np.zeros(100) for i in range(100): game.reset() step = 0 while step < STEPS: if not game.move(model.act_best(game.state())): break step += 1 scores[i] = game.score print('Episode {} of {}'.format(e + 1, EPISODES)) print('Average/ Min / Max score on a new game: {} / {} / {}'.format(np.mean(scores), np.min(scores), np.max(scores))) model.save(MODEL_FILE)
def calculate_fitness(population): game = snake.Game(show=SHOW) for _ in population: game.snakes.append( snake.Snake(snake.CELL_COUNT / 2, snake.CELL_COUNT / 2, 5)) step = 0 while not game.stop and step < WAIT_STEPS: game.render() best = game.best_snake() old_apple = best.apple for i in range(POPULATION_SIZE): apple = game.snakes[i].observe_apple().to_norm_relative() obstacles = [ obstacle.to_norm_relative() for obstacle in game.snakes[i].observe_obstacle() ] inputs = [ apple[0], apple[1], obstacles[0][0], obstacles[1][0], obstacles[2][0] ] action = np.argmax(population[i].compute_outputs(inputs)) if action == 1: game.snakes[i].turn_right() elif action == 2: game.snakes[i].turn_left() game.update(FPS) if all(s.dead for s in game.snakes): game.stop = True else: best = game.best_snake() if old_apple is not best.apple: step = 0 step += 1 fitness = [] for s in game.snakes: fitness.append(s.score) game.end() return fitness
import snake import numpy as np def policy1(board): return np.random.choice(["U", "R"]) def policy2(board): return np.random.choice(["U", "L"]) n = 10 player1 = snake.Player(n, policy1) player2 = snake.Player(n, policy2) game = snake.Game(n, player1, player2) game.play()
import snake import graphics import brain class DummyGraphics: def __init(self): pass def update(self): return True def setSize(self, x): pass g = graphics.GraphicsEngine() game = snake.Game(10, g) game.addSnake((2, 2), "S", brain.RandomHead()) # game.addSnake((2,2), "S", brain.RandomHead()) game.game()
import snake import visualize import ai if __name__ == "__main__": field_size = (30, 30) generation_size = 10 surviver_count = 5 #the amount of fittest selected to breed the next generation child_per_survivor = generation_size // surviver_count mutation = 1 generation = 1 generations = [] for i in range(generation_size): game = snake.Game( field_size, snake.Snake(snake.Vector(14, 14), snake.Vector.UP, 3)) model = ai.create_model(field_size) generations.append((model, game)) vis = visualize.Visualizer((600, 450), "Snake", 5, (255, 0, 0), (0, 255, 0)) last_update = 0 update_frequency = 200 max_ticks = 50 tick_count = 0 dead = 0 while vis.running: vis.begin_frame() for i in range(len(generations)): #display fields
def play_ai(): game = snake.Game(WIDTH, HEIGHT, WALLS_NUMBER, REWARDS) model = ai.Model(STATE_SIZE, ACTION_SIZE) model.load(MODEL_FILE) game.play_new_game(10, model.act_best)
def play_human(speed): game = snake.Game(WIDTH, HEIGHT, WALLS_NUMBER, REWARDS) game.play_new_game(speed)
import snake snake.Game()
snake_ai = neat.NEAT(n_networks=300, input_size=(VISION_RADIUS * 2 + 1)**2 - 2, output_size=3, bias=True, c1=1.0, c2=1.0, c3=4.0, distance_threshold=2.5, weight_mutation_rate=0.8, node_mutation_rate=0.3, connection_mutation_rate=0.5, interspecies_mating_rate=0.001, disable_rate=0.75, stegnant_threshold=15) snake_game = snake.Game(MAP_SIZE, 3) ACTIONS = [-1, 0, 1] while True: rewards = [] for genome in snake_ai.population: snake_game.reset() length = len(snake_game.snake.body) playback = [] step = 0 while not snake_game.done: screen = snake_game.draw(50) playback.append(screen) map = snake_game.map.copy() map[snake_game.apple[1], snake_game.apple[0]] = -1
""" A simple snake game bot. This bot serves as an an example of how to control the snake game programmatically. It's just a quick-and-dirty script used to test win conditions, and only works if the number of columns and rows are both even and greater than 4 or thereabout. """ import snake game = snake.Game() def loop(): while not game.win and not game.lose: while game.snake['head']['x'] != game.COLUMNS - 1\ and not game.win and not game.lose: game.move(1) while game.snake['head']['y'] != game.ROWS - 1\ and not game.win and not game.lose: game.move(2) while game.snake['head']['x'] != 1\ and not game.win and not game.lose: game.move(3) while game.snake['head']['y'] != 1\ and not game.win and not game.lose: game.move(0) game.move(3) while game.snake['head']['y'] != game.ROWS - 1\ and not game.win and not game.lose: game.move(2)
def trial_snake(self, network): score = [] snake.Game(network, score) return score[0]
def train_network(model): epsilon = EPSILON game_state = game.Game() # Starting up a game game_state.set_start_state() game_image, score, game_lost = game_state.run( 0 ) # The game is started but no action is performed s_t = stack_image(game_image) terminal = False t = 0 exp_replay = experience_replay(BATCH) exp_replay.__next__() # Start experience replay coroutine epsilon = epsilon * EPSILON_DECAY if epsilon > FINAL_EPSILON else FINAL_EPSILON while True: loss = 0 Q_sa = 0 action_index = 4 r_t = 0 a_t = "no nothing" if terminal: game_state.set_start_state() if t % NB_FRAMES == 0: if random.random() <= epsilon: action_index = random.randrange(NB_ACTIONS) else: action_index = np.argmax(model.predict(s_t)) a_t = GAME_INPUT[action_index] # run the selected action and observed next state and reward x_t1_colored, r_t, terminal = game_state.run(a_t) s_t1 = stack_image(x_t1_colored) experience = (s_t, a_t, r_t, s_t1) batch = exp_replay.send(experience) s_t1 = stack_image(x_t1_colored) if batch: inputs = np.zeros((BATCH, s_t.shape[1], s_t.shape[2], s_t.shape[3])) targets = np.zeros((BATCH, NB_ACTIONS)) i = 0 for s, a, r, s_pred in batch: inputs[i : i + 1] = s if r < 0: targets[i, a] = r else: Q_sa = model.predict(s_pred) targets[i, a] = r + GAMMA * np.max(Q_sa) i += 1 loss += model.train_on_batch(inputs, targets) # Exploration vs Exploitation t += 1 # save progress every 10000 iterations if t % 1000 == 0: print("Now we save model") model.save_weights("model.h5", overwrite=True) with open("model.json", "w") as outfile: json.dump(model.to_json(), outfile) if t % 500 == 0: print( "TIMESTEP", t, "/ EPSILON", epsilon, "/ ACTION", action_index, "/ REWARD", r_t, "/ Q_MAX ", np.max(Q_sa), "/ Loss ", loss, ) print("Episode finished!") print("************************")
def Ser(rt, can): can.destroy() sn = s.Game(rt) rt.bind("<Key>", sn.move) pass