Esempio n. 1
0
    def _playAI(self) -> None:
        """User selects saved model from .../dna/trained. Opens GUI window and AI plays game."""
        # check npz format with same size networks or something like that? Cant save certain things...??

        algoIndex, algoChoice = ui.getSelection("Neural Network",
                                                "Multi",
                                                "Hierarchical",
                                                "Cycle",
                                                "Pathfinding",
                                                "Floodfill",
                                                "Back",
                                                msg="Select AI algorithm:")

        if algoChoice != "Back" and (snake := self._makeSnake(
                algoIndex, algoChoice)) is not None:
            numGames = ui.getValidInput(
                "How many games should be played? (select one game to render)",
                dtype=int,
                lower=1,
                end="\n")

            scores = []
            timer = time()

            if algoChoice in {"Multi", "Hierarchical"}:
                algos = snake.behavior.algoUsage.keys()
                avgUsage = {algo: 0 for algo in algos}

            for i in range(numGames):
                gameEnvironment = environment.Environment(snake,
                                                          settings.mapSize,
                                                          noise=settings.noise)
                #gameEnvironment = environment.Environment(snake, settings.mapSize, origin=(3, 0))  # for cycle to win or get close when odd
                games.playGame(gameEnvironment, render=(not (numGames - 1)))
                scores.append(snake.size)
                if algoChoice in {"Multi", "Hierarchical"}:
                    for algo in algos:
                        avgUsage[algo] += snake.behavior.algoUsage[algo]
                print("Game", str(i + 1) + " snake size:", snake.size)
            elapsed = time() - timer
            if not numGames - 1:
                print()
                ui.checkSave(gameEnvironment, self._saveGame)
            else:
                print("\nTime elapsed across",
                      str(numGames) + " games:", round(elapsed, 5), "secs")
                print("Average time per game", round(elapsed / numGames, 5),
                      "secs")
                print("Average snake score:", round(sum(scores) / numGames, 2))
                print("Scores std:", round(np.std(scores), 3))
                if algoChoice in {"Multi", "Hierarchical"}:
                    avgUsage = {
                        algo: round(avgUsage[algo] / numGames, 3)
                        for algo in algos
                    }
                    print(
                        "Snake average algorithm use (n=" + str(numGames) +
                        "):", avgUsage)
                print()
            self.prevGameEnvironment = gameEnvironment
Esempio n. 2
0
    def _replay(self, moves: list, origin: tuple, food: list, mapSize: tuple,
                color: tuple) -> None:
        """
		Constructs snake and environment from data and replays it in GUI window.

		Parameters
		----------
		moves: list
			Queue of directions snake chose to make at each time step
		origin: tuple
			Initial starting position of snake
		food: list
			Queue of food spawning positions
		mapSize: tuple
			(x, y) size of game map snake played on
		color: tuple
			Color of snake
		"""
        snakeKwargs = {
            "initialSize": settings.initialSnakeSize,
            "maxVision": settings.maxSnakeVision,
            "hungerFunc": settings.hungerFunc,
        }

        snake = snakes.Snake("ghost",
                             behaviorArgs=[moves],
                             **snakeKwargs,
                             color=color)
        gameEnvironment = environment.Environment(snake,
                                                  mapSize,
                                                  origin=origin,
                                                  food=food)
        games.playGame(gameEnvironment)
        ui.checkSave(gameEnvironment, self._saveGame)
Esempio n. 3
0
 def _playClassic(self) -> None:
     """Opens GUI window and lets user play Snake with keyboard controls."""
     snake = snakes.Snake.Player()
     gameEnvironment = environment.Environment(snake, settings.mapSize)
     print("Get ready...")
     games.playPlayerGame(gameEnvironment)
     finalScore = snake.score
     print("Final score: " + str(finalScore))
     print()
     ui.checkSave(gameEnvironment, self._saveGame)
     self.prevGameEnvironment = gameEnvironment
Esempio n. 4
0
def playTrainingGame(snake: snakes.Snake, render: bool = False) -> dict:
    """
	Plays game with settings.mapSize sized map.

	Parameters
	----------
	snake: snakes.Snake
		Snake to play game with
	render: bool, default=True
		Indicates if game should be rendered to GUI window
	"""
    #print("NEW GAME" + "!" * 20)  # delete
    # DONT MAKE NEW ENVIRONMENT EVERY TIME, MAKE ENVIRONMENT RESET
    gameEnvironment = environment.Environment(snake, settings.mapSize)
    playGame(gameEnvironment, render=render)
    id = snake.id
    return {"fitness": snake.fitness(snake), "score": snake.score, "id": id}
Esempio n. 5
0
def main():
    algoIndex, algoChoice = ui.getSelection("Neural Network",
                                            "Multi",
                                            "Hierarchical",
                                            "Cycle",
                                            "Pathfinding",
                                            "Floodfill",
                                            msg="Select AI algorithm:")

    if (snake := makeSnake(algoIndex, algoChoice)) is not None:
        #numGames = ui.getValidInput("How many games should be played?", dtype=int, lower=1, end="\n")
        S = []  # true states
        P = []  # perturbed states

        scores = []
        timer = time()

        if algoChoice in {"Multi", "Hierarchical"}:
            algos = snake.behavior.algoUsage.keys()
            avgUsage = {algo: 0 for algo in algos}

        numGames = 1  # hard coded in for now
        for i in range(numGames):
            gameEnvironment = environment.Environment(snake,
                                                      settings.mapSize,
                                                      noise=settings.noise)

            while gameEnvironment.active():
                gameEnvironment.step()
                S.append(gameEnvironment.gameMap.copy())  # adding true state

            scores.append(snake.size)
            if algoChoice in {"Multi", "Hierarchical"}:
                for algo in algos:
                    avgUsage[algo] += snake.behavior.algoUsage[algo]
            print("Game", str(i + 1) + " snake size:", snake.size)
        elapsed = time() - timer

        print("\nTime elapsed across",
              str(numGames) + " games:", round(elapsed, 5), "secs")
        print("Average time per game", round(elapsed / numGames, 5), "secs")
        print("Average snake score:", round(sum(scores) / numGames, 2))
        print("Scores std:", round(np.std(scores), 3))
        if algoChoice in {"Multi", "Hierarchical"}:
            avgUsage = {
                algo: round(avgUsage[algo] / numGames, 3)
                for algo in algos
            }
            print("Snake average algorithm use (n=" + str(numGames) + "):",
                  avgUsage)
        print()

        P = [perturb(s) for s in S]  # independently perturb each true state

        radius = 5  # threshold for which to search for solutions
        solutions = solve(P, radius)  # get a list of solutions within radius

        if S in solutions:
            print("Converged!")
        elif len(solutions) == 0:
            print("Failed to find any solutions")
        else:
            print("Failed to converge to true trajectory")

        print()