コード例 #1
0
ファイル: testAgent.py プロジェクト: gladisor/checkers-RL
from game import Checkers
from dqn import DQN

import torch

PATH = "models/30Improve.pt"
dqn = DQN()
dqn.load_state_dict(torch.load(PATH))

game = Checkers()
game.reset()

board = game.board
## Display
print(board)

color = 'red'
while not game.get_game_ended(board, color):
    actions = game.get_possible_actions(board, color)
    state = game.get_state(board, color)
    state_actions = game.make_inputs(state, actions)
    if color == 'black':
        print("Agent turn")
        print("Agent possible actions")

        out = dqn(state_actions)
        for a, q in zip(actions, out):
            print(game.flip_action(a), q.item())
        print(torch.max(out))
        idx = torch.argmax(out)
        action = actions[idx]
コード例 #2
0
class TestBoard(unittest.TestCase):
    def setUp(self):
        self.test_Game = Checkers()

    def test_move(self):
        self.test_Game.move((5, 0, 4, 1))
        print('move')
        expected_result = np.array([[0, -1, 0, -1, 0, -1, 0, -1],
                                    [-1, 0, -1, 0, -1, 0, -1, 0],
                                    [0, -1, 0, -1, 0, -1, 0, -1],
                                    [0, 0, 0, 0, 0, 0, 0, 0],
                                    [0, 1, 0, 0, 0, 0, 0, 0],
                                    [0, 0, 1, 0, 1, 0, 1, 0],
                                    [0, 1, 0, 1, 0, 1, 0, 1],
                                    [1, 0, 1, 0, 1, 0, 1, 0]])

        np.testing.assert_array_equal(expected_result,
                                      self.test_Game.board_state)

        self.test_Game.reset()
        print("capture")
        self.test_Game.board_state = np.array([[0, -1, 0, -1, 0, -1, 0, -1],
                                               [-1, 0, -1, 0, -1, 0, -1, 0],
                                               [0, -1, 0, 0, 0, -1, 0, -1],
                                               [0, 0, -1, 0, -1, 0, 0, 0],
                                               [0, 1, 0, 0, 0, 0, 0, 0],
                                               [0, 0, 1, 0, 1, 0, 1, 0],
                                               [0, 1, 0, 1, 0, 1, 0, 1],
                                               [1, 0, 1, 0, 1, 0, 1, 0]])

        self.test_Game.move((4, 1, 2, 3))

        expected_result = np.array([[0, -1, 0, -1, 0, -1, 0, -1],
                                    [-1, 0, -1, 0, -1, 0, -1, 0],
                                    [0, -1, 0, 1, 0, -1, 0, -1],
                                    [0, 0, 0, 0, -1, 0, 0, 0],
                                    [0, 0, 0, 0, 0, 0, 0, 0],
                                    [0, 0, 1, 0, 1, 0, 1, 0],
                                    [0, 1, 0, 1, 0, 1, 0, 1],
                                    [1, 0, 1, 0, 1, 0, 1, 0]])

        np.testing.assert_array_equal(expected_result,
                                      self.test_Game.board_state)

        self.test_Game.reset()
        print("trying no capture")
        self.test_Game.board_state = np.array([[0, -1, 0, -1, 0, -1, 0, -1],
                                               [-1, 0, -1, 0, -1, 0, -1, 0],
                                               [0, -1, 0, 0, 0, -1, 0, -1],
                                               [0, 0, -1, 0, -1, 0, 0, 0],
                                               [0, 1, 0, 0, 0, 0, 0, 0],
                                               [0, 0, 1, 0, 1, 0, 1, 0],
                                               [0, 1, 0, 1, 0, 1, 0, 1],
                                               [1, 0, 1, 0, 1, 0, 1, 0]])

        self.test_Game.move((4, 1, 0, 3))

        expected_result = np.array([[0, -1, 0, -1, 0, -1, 0, -1],
                                    [-1, 0, -1, 0, -1, 0, -1, 0],
                                    [0, -1, 0, 0, 0, -1, 0, -1],
                                    [0, 0, -1, 0, -1, 0, 0, 0],
                                    [0, 1, 0, 0, 0, 0, 0, 0],
                                    [0, 0, 1, 0, 1, 0, 1, 0],
                                    [0, 1, 0, 1, 0, 1, 0, 1],
                                    [1, 0, 1, 0, 1, 0, 1, 0]])

        np.testing.assert_array_equal(expected_result,
                                      self.test_Game.board_state)

        self.test_Game.reset()
        print("capture from first col")
        self.test_Game.turn = -1
        self.test_Game.board_state = np.array([[0, -1, 0, -1, 0, -1, 0, -1],
                                               [-1, 0, -1, 0, -1, 0, -1, 0],
                                               [0, -1, 0, 0, 0, -1, 0, -1],
                                               [-1, 0, -1, 0, -1, 0, 0, 0],
                                               [0, 1, 0, 0, 0, 0, 0, 0],
                                               [0, 0, 0, 0, 1, 0, 1, 0],
                                               [0, 1, 0, 1, 0, 1, 0, 1],
                                               [1, 0, 1, 0, 1, 0, 1, 0]])

        self.test_Game.move((3, 0, 5, 2))

        expected_result = np.array([[0, -1, 0, -1, 0, -1, 0, -1],
                                    [-1, 0, -1, 0, -1, 0, -1, 0],
                                    [0, -1, 0, 0, 0, -1, 0, -1],
                                    [0, 0, -1, 0, -1, 0, 0, 0],
                                    [0, 0, 0, 0, 0, 0, 0, 0],
                                    [0, 0, -1, 0, 1, 0, 1, 0],
                                    [0, 1, 0, 1, 0, 1, 0, 1],
                                    [1, 0, 1, 0, 1, 0, 1, 0]])

        np.testing.assert_array_equal(expected_result,
                                      self.test_Game.board_state)