Beispiel #1
0
class TestGameState(unittest.TestCase):
    def setUp(self):
        self.gs = GameState()

    def test_outputs_to_move_max(self):
        outputs = np.linspace(0.0, 1.0, 100)
        self.gs.outputs_to_move_max(outputs)
        self.assertTrue((self.gs.board == np.array([[-1, -1, -2, -1, -1],
                                                    [0, 0, 0, 0, 1],
                                                    [0, 0, 0, 0, 0],
                                                    [0, 0, 0, 0, 0],
                                                    [1, 1, 2, 1, 0]])).all())
        self.gs.outputs_to_move_max(outputs)
        self.assertFalse((self.gs.board == np.array([[-1, -1, -2, -1, -1],
                                                     [0, 0, 0, 0, 1],
                                                     [0, 0, 0, 0, 0],
                                                     [0, 0, 0, 0, 0],
                                                     [1, 1, 2, 1, 0]])).all())

    def test_outputs_to_move_random(self):
        outputs = np.linspace(0.0, 1.0, 100)
        outputs /= np.sum(outputs)
        self.gs.outputs_to_move_random(outputs)

    def test_flip(self):
        self.assertTrue((self.gs.to_inputs() == self.gs.to_inputs(True)).all())
Beispiel #2
0
def take_action_eps_greedy(board: np.ndarray, episode: int, mainQN: QNetwork,
                           gs: GameState) -> Tuple[Winner, int]:
    """t+1での行動を返す
    boardは入力の型(README参照)で与えること
    returnは勝利判定と打った手"""
    # 徐々に最適行動のみをとる、ε-greedy法
    epsilon = 0.001 + 0.9 / (1.0 + episode)

    if epsilon <= np.random.uniform(0, 1):
        retTargetQs = mainQN.model.predict(board)[0]
        s = gs.outputs_to_move_max(retTargetQs)  # 最大の報酬を返す行動を選択する

    else:
        s = gs.random_play()  # ランダムに行動する

    return s