def eval_genome(genome, config): net = neat.nn.FeedForwardNetwork.create(genome, config) fitnesses = [] for runs in range(runs_per_net): game = Game(20, 20) # Run the given simulation for up to num_steps time steps. fitness = 0.0 while True: inputs = game.get_normalized_state() action = net.activate(inputs) # Apply action to the simulated snake valid = game.step(np.argmax(action)) # Stop if the network fails to keep the snake within the boundaries or hits itself. # The per-run fitness is the number of pills eaten if not valid: break fitness = game.fitness fitnesses.append(fitness) # The genome's fitness is its worst performance across all runs. return min(fitnesses)
def build_agent(): game = Game( map_size=(20, 20), initial_snake_length=3, create_observation_strategy=InverseDistanceObservationStrategy, create_reward_strategy=SquareExpRewardStrategy) return snake.agent.Agent(env=game, hidden_nodes=[18, 18])
def eval_genome(genome, config): net = neat.nn.FeedForwardNetwork.create(genome, config) fitnesses = [] for runs in range(runs_per_net): #pygame.init() #screen = pygame.display.set_mode((20 * 16,20 * 16)) #screen.fill(pygame.Color('black')) #pygame.display.set_caption('Snake') #pygame.display.flip() sim = Game(20, 20) # Run the given simulation for up to num_steps time steps. fitness = 0.0 while True: inputs = sim.get_normalized_state() action = net.activate(inputs) # Apply action to the simulated snake valid = sim.step(np.argmax(action)) # Stop if the network fails to keep the snake within the boundaries or hits itself. # The per-run fitness is the number of pills eaten if not valid: break fitness = sim.score fitnesses.append(fitness) # The genome's fitness is its worst performance across all runs. return min(fitnesses)
def getTrainingData(self): print('Getting Training Data . . .') data = [] number = int(self.train_games / 20) for x in range(self.train_games): game = Game(x=self.x, y=self.y) c_data = [] self.game = game snake = game.start() current_state = self.getState(snake) for _ in range(self.max_steps): action = self.getAction() length = snake.length done, snake, closer = game.step(action) if done: break elif not closer: continue else: correct_output = [0, 0, 0] correct_output[action + 1] = 1 num = 1 if snake.length > length: num = 3 for _ in range(num): c_data.append([current_state, correct_output]) current_state = self.getState(snake) if snake.length > 2: for el in c_data: data.append(el) if x % number == 0: print(f'{int(x/self.train_games*100)}%') return data
def build_agent(): game = Game( map_size=(20, 20), create_observation_strategy=InverseDistanceObservationStrategy, create_reward_strategy=SurvivalRewardStrategy) game = MaxTimestepsWrapper(game, max_timesteps=1000) return snake.agent.Agent(env=game, hidden_nodes=[18, 18])
def test_egg(): g = Game((5, 5), (10, 10)) # create a snake that takes almost all the space g.snake = [ (i, j) for i in range(5) for j in range(5) ] g.snake.remove( (2, 2)) assert g.random_egg() == (2, 2)
def __init__(self): self.window = pygame.display.set_mode((270, 270)) self.game = Game(270, 270, 9, self.window, 0, 0) self.model = self.load_model() self.cnt = 0 if self.model != None: self.game.agent.update_model(self.model) self.game.agent.update_tgt(self.model)
def test_move(): g = Game((5, 5), (10, 10)) # create a small snake g.snake = [ (1, 1), (2, 1), (3, 1) ] g.egg = (0, 0) g.move_snake(1, 0, False) assert g.snake == [ (2, 1), (3, 1), (4, 1) ] g.move_snake(1, 0, False) assert g.snake == [ (3, 1), (4, 1), (0, 1) ] # no u-turn g.move_snake(-1, 0, False) assert g.snake == [ (3, 1), (4, 1), (0, 1) ]
def showGame(self, model): game = Game(x=self.x, y=self.y, gui=True) self.game = game while True: snake = game.start() steps = self.max_steps current_state = self.getState(snake) while True: m = model.predict(np.array([current_state])) action = list(m[0]).index(max(list(m[0]))) - 1 length = snake.length done, snake, c = game.step(action) if done: break elif snake.length > length: steps = self.max_steps else: current_state = self.getState(snake) time.sleep(.05) steps -= 1 if steps == 0: break
def test(self, model): print('Testing . . .') num = int(self.test_games / 20) lengths = [] game = Game(x=self.x, y=self.y) self.game = game for x in range(self.test_games): snake = game.start() steps = self.max_steps current_state = self.getState(snake) while True: m = model.predict(np.array([current_state])) action = list(m[0]).index(max(list(m[0]))) - 1 length = snake.length done, snake, _ = game.step(action) if done: break elif snake.length > length: steps = self.max_steps else: current_state = self.getState(snake) steps -= 1 if steps == 0: break lengths.append(snake.length) if x % num == 0: print(f'{int((x/self.test_games)*100)}%') print(f'Average: {sum(lengths)/len(lengths)}')
from snake import Game, Renderer, KeyboardInput H = 10 W = 10 game = Game(H, W) renderer = Renderer(game) input = KeyboardInput(renderer.window) while True: renderer.render_frame() action = input.get_input() if action: game.input(action) game.update() if game.has_ended(): renderer.close_window() print('THE END') break ''' try: change = game.changed_tiles renderer.render_frame(change) action = input.get_input() if action: game.input(action) game.update() if game.has_ended(): print('THE END')
return self.env.outdim # define action-value table # number of states is: # # current value: 1-21 # # number of actions: # # Stand=0, Hit=1 av_table = ActionValueTable(27, 4) av_table.initialize(2.) game = Game() # define Q-learning agent learner = Q(0.5, 0.2) learner._setExplorer(EpsilonGreedyExplorer(0.0)) agent = LearningAgent(av_table, learner) # define the environment env = SnakeEnv(4, 27) env.init_game(15) # define the task task = BlackjackTask(env) # finally, define experiment experiment = Experiment(task, agent)
def __init__(self): self.window = pygame.display.set_mode((270, 270)) self.game = Game(270, 270, 9, self.window, 0, 0) self.model = self.load_model()
def __init__(self): self.game = Game() self.square_size = 9 # the observation size self.timestep = 0
@property def optimizer(self): return self.q_network.optimizer @property def mask(self): return self.q_network.mask def __getattr__(self, name): if name in self.hyper_params: return self.hyper_params[name] else: raise AttributeError() def __del__(self): self.sess.close() if __name__ == '__main__': from snake import Game from window import Window number = 6 block_size = 20 g = Game(number=number) window = Window(number=number, block_size=block_size, expansion=1.5, speed=0.2) dqn = DQN(game=g) dqn.train(window=window)
f_x = int(0 if not food_relative[0] else 1 * np.sign(food_relative[0])) + 1 # Select food relative x f_y = int(0 if not food_relative[1] else 1 * np.sign(food_relative[1])) + 1 # Select food relative y for i, field in enumerate(view_area.ravel()): if not field: # Ignore 0=Path continue add = (FIELD_STATES ** i) * field discrete_index += add return direction, f_x, f_y, discrete_index if __name__ == "__main__": game = Game(food_ammount=1, render=True) valid = True observation = Game().reset() score = 0 q_table = np.load(f"{FILE}.npy", allow_pickle=True) os.makedirs(f"{FILE}", exist_ok=True) step = 0 while valid: game.draw() surface = pygame.display.get_surface() pygame.image.save(surface, f"{FILE}/image_{step}.png") old_observation = observation current_q_values = get_discrete_vals(q_table, old_observation)
from snake import Game # This is a sample Python script. # Press Shift+F10 to execute it or replace it with your code. # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. # Press the green button in the gutter to run the script. if __name__ == '__main__': snake = Game() snake.run() # See PyCharm help at https://www.jetbrains.com/help/pycharm/
if event.key not in KEY_TO_ACTION_MAP: continue # Act on bound keys. observation, reward, done, info = env.step( KEY_TO_ACTION_MAP[event.key]) print( f"Observation: {observation}\tReward: {reward}\tDone: {done}\tInfo: {info}" ) if done: env.reset() # Limit frame rate. update_clock.tick(30) do_game_loop() pygame.quit() if __name__ == "__main__": from snake import Game from snake.observation_strategies.default_observation_strategy import DefaultObservationStrategy from snake.reward_strategies.default_reward_strategy import DefaultRewardStrategy play( Game(map_size=[10, 10], initial_snake_length=3, create_observation_strategy=DefaultObservationStrategy, create_reward_strategy=DefaultRewardStrategy))
def test_game(self): snake = Snake([[5, 4], [4, 4], [3, 4]]) game = Game(10, snake, Direction.UP)
# on se limite ici un peu artificiellement à des cellules carrées parser = ArgumentParser() parser.add_argument("-W", "--width", default=15, type=int, help="horizontal size of board, in cells") parser.add_argument("-H", "--height", default=15, type=int, help="vertical size of board, in cells") parser.add_argument("-C", "--cell", default=20, type=int, help="cell size in pixels") args = parser.parse_args() # dans l'objet args on va retrouver les 3 réglages # tels que définis sur la ligne de commande # par exemple # args.width vaut l'entier 15 si on ne précise pas -W # args.width vaut l'entier 20 si on précise -W 20 # # on peut toujours invoquer le programme avec --help # python main.py --help print(args.width, args.height, args.cell) game = Game((args.width, args.height), (args.cell, args.cell)) game.run()
def __init__(self): self.action_space = Discrete(3) # 0 = turn left, 1 = do nothing, 2 = turn right self.state = [0, 0, 1, 0] self.game = Game() self.reward = 0 self.done = False
def init_game(self, snake_size): self.game = Game() self.game.init_game(snake_size) self.running = True
from snake import Game if __name__ == "__main__": a = Game('snake', 1280, 640, 10) a.run()