Ejemplo n.º 1
0
class Play():
    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 load_model(self):
        try:
            f = torch.load("best.pth")
        except:
            f = None
        return f

    def run(self):
        clock = pygame.time.Clock()
        while True:
            pygame.time.delay(50)
            clock.tick(10)

            self.window.fill((0, 0, 0))

            self.game.game_loop(train=False, model=self.model)

            pygame.display.update()
Ejemplo n.º 2
0
class Play():
    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 load_model(self):
        try:
            f = torch.load("best.pth")
        except:
            f = None
        return f

    def get_average(self, arr):
        s = 0.0
        for a in arr:
            s += a

        return s / len(arr)

    def run(self):
        clock = pygame.time.Clock()

        cicles = 0
        inf_loops = 0
        prev = 0

        while True:
            pygame.time.delay(1)
            clock.tick(1000000)

            self.window.fill((0, 0, 0))

            self.game.game_loop(train=False, model=self.model)

            if self.game.reward == 0:
                cicles += 1
                inf_loops = 0

            if inf_loops == 300 and prev == self.game.points:
                self.game.restart()
                cicles += 1
                inf_loops = 0

            if prev < self.game.points:
                prev = self.game.points

            inf_loops += 1

            if cicles == 1000:
                print(
                    f'Max: {max(self.game.points_ls)}, Average: {self.get_average(self.game.points_ls)}'
                )
                exit()

            pygame.display.update()
Ejemplo n.º 3
0
class Play():
    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 save_model(self):
        print("Saving model")
        torch.save(self.game.agent.tgt, "best.pth")

    def load_model(self):
        try:
            f = torch.load("best.pth")
        except:
            f = None
        return f

    def run(self):
        clock = pygame.time.Clock()
        while True:
            pygame.time.delay(1)
            clock.tick(1000000)

            self.window.fill((0, 0, 0))

            self.game.game_loop(train=True)

            if self.game.agent.tgt_updated:
                self.cnt += 1
                print(self.cnt, " Target model updated:",
                      self.game.get_average_reward())
                self.game.agent.tgt_updated = False
                self.save_model()

            if self.cnt == 500:
                exit()

            pygame.display.update()