def test_selector_w(self): board = BitBoard(8) board.put_disc('black', 3, 2) board.put_disc('white', 2, 4) board.put_disc('black', 1, 5) board.put_disc('white', 1, 4) board.put_disc('black', 2, 5) board.put_disc('white', 2, 6) board.put_disc('black', 1, 6) board.put_disc('white', 1, 7) selector = Selector_W(depth=2, limit=4) self.assertEqual(selector.depth, 2) self.assertEqual(selector.limit, 4) strategy = _AlphaBeta(evaluator=Evaluator_TPW()) selector = Selector_W() self.assertEqual(selector.depth, 3) self.assertEqual(selector.limit, 3) moves = board.get_legal_moves('black') best_move, scores = strategy.get_best_move('black', board, moves, 4) moves = selector.select_moves('black', board, board.get_legal_moves('black'), scores, 2) self.assertEqual(moves, [(0, 3), (2, 3), (0, 4), (5, 4), (0, 5), (4, 5), (5, 5), (0, 6), (0, 7), (2, 7)]) moves = selector.select_moves('black', board, board.get_legal_moves('black'), scores, 5) self.assertEqual(moves, [(2, 3), (0, 4), (5, 4), (0, 5), (4, 5), (5, 5), (0, 6), (0, 7), (2, 7)])
def test_selector(self): board = BitBoard(8) board.put_disc('black', 3, 2) selector = Selector() moves = selector.select_moves('white', board, board.get_legal_moves('white'), None, None) self.assertEqual(moves, [(2, 2), (4, 2), (2, 4)])
def test_orderer(self): board = BitBoard(8) board.put_disc('black', 3, 2) orderer = Orderer() moves = orderer.move_ordering(color='white', board=board, moves=board.get_legal_moves('white'), best_move=None) self.assertEqual(moves, [(2, 2), (4, 2), (2, 4)])
def test_orderer_p(self): board = BitBoard(8) board.put_disc('black', 3, 2) board.put_disc('white', 2, 4) board.put_disc('black', 1, 5) board.put_disc('white', 1, 4) board.put_disc('black', 2, 5) board.put_disc('white', 2, 6) board.put_disc('black', 1, 6) board.put_disc('white', 1, 7) orderer = Orderer_P() moves = orderer.move_ordering(color='black', board=board, moves=board.get_legal_moves('black'), best_move=None) self.assertEqual(moves, [(5, 4), (4, 5), (5, 5), (0, 7), (0, 3), (2, 3), (0, 4), (0, 5), (0, 6), (2, 7)])
def test_alphabeta_get_best_move(self): for instance in [_AlphaBeta_, _AlphaBeta, AlphaBeta_, AlphaBeta]: board = BitBoard() alphabeta = instance(evaluator=coord.Evaluator_TPW()) board.put_disc('black', 3, 2) board.put_disc('white', 2, 4) board.put_disc('black', 5, 5) board.put_disc('white', 4, 2) board.put_disc('black', 5, 2) board.put_disc('white', 5, 4) moves = board.get_legal_moves('black') self.assertEqual(alphabeta.get_best_move('black', board, moves, 5), ((2, 2), { (2, 2): 8, (2, 3): 8, (5, 3): 8, (1, 5): 8, (2, 5): 8, (3, 5): 8, (4, 5): 8, (6, 5): 8 })) # noqa: E501
def test_endgame_get_best_move(self): endgame = _EndGame_() # O--OOOOX # -OOOOOOX # OOXXOOOX # OOXOOOXX # OOOOOOXX # ---OOOOX # ----O--X # -------- # X board = BitBoard() board._black_bitboard = 0x0101312303010100 board._white_bitboard = 0x9E7ECEDCFC1E0800 board.update_score() # depth=20 : black : a2 board.put_disc('black', 0, 1) # depth=19 : white : b1 board.put_disc('white', 1, 0) # depth=18 : black : c1 board.put_disc('black', 2, 0) # depth=17 : white : -- # depth=17 : black : b6 board.put_disc('black', 1, 5) # print(board) # print(board._black_score, board._white_score) # depth=16 : white : c7 self.assertEqual( endgame.get_best_move('white', board, board.get_legal_moves('white')), ((1, 6), { (1, 6): -38.0, (2, 6): -38.0 })) board.put_disc('white', 2, 6) # depth=15 : black : a7 self.assertEqual( endgame.get_best_move('black', board, board.get_legal_moves('black')), ((0, 6), { (0, 5): 16.0, (0, 6): 38.0, (1, 7): 38.0, (2, 5): 30.0, (3, 6): 38.0, (3, 7): 38.0, (4, 7): 38.0, (5, 6): 38.0, (5, 7): 38.0, (6, 6): 38.0 })) # noqa: E501 board.put_disc('black', 0, 6) # depth=14 : white : b7 self.assertEqual( endgame.get_best_move('white', board, board.get_legal_moves('white')), ((1, 6), { (1, 6): -38.0 })) board.put_disc('white', 1, 6) # depth=13 : black : b8 self.assertEqual( endgame.get_best_move('black', board, board.get_legal_moves('black')), ((1, 7), { (0, 5): 8.0, (1, 7): 38.0, (2, 5): 36.0, (3, 6): 36.0, (3, 7): 38.0, (4, 7): 38.0, (5, 6): 36.0, (5, 7): 38.0, (6, 6): 36.0 })) # noqa: E501 board.put_disc('black', 1, 7) # depth=12 : white : d7 self.assertEqual( endgame.get_best_move('white', board, board.get_legal_moves('white')), ((3, 6), { (2, 5): -42.0, (3, 6): -38.0, (3, 7): -38.0 })) board.put_disc('white', 3, 6) # depth=11 : black : f8 self.assertEqual( endgame.get_best_move('black', board, board.get_legal_moves('black')), ((5, 7), { (0, 5): 2.0, (2, 5): 36.0, (2, 7): 36.0, (3, 7): 36.0, (4, 7): 36.0, (5, 6): 36.0, (5, 7): 38.0, (6, 6): 36.0 })) # noqa: E501 board.put_disc('black', 5, 7) # depth=10 : white : c6 self.assertEqual( endgame.get_best_move('white', board, board.get_legal_moves('white')), ((2, 5), { (2, 5): -38.0, (3, 7): -38.0, (4, 7): -38.0, (5, 6): -38.0 })) # noqa: E501 board.put_disc('white', 2, 5) # depth=9 : black : f7 self.assertEqual( endgame.get_best_move('black', board, board.get_legal_moves('black')), ((5, 6), { (0, 5): -8.0, (0, 7): 38.0, (2, 7): 38.0, (3, 7): 38.0, (5, 6): 38.0, (6, 6): 38.0 })) # noqa: E501 board.put_disc('black', 5, 6) # depth=8 : white : g7 self.assertEqual( endgame.get_best_move('white', board, board.get_legal_moves('white')), ((6, 6), { (4, 7): -38.0, (6, 6): -38.0, (6, 7): -38.0 })) board.put_disc('white', 6, 6) # depth=7 : black : a8 self.assertEqual( endgame.get_best_move('black', board, board.get_legal_moves('black')), ((0, 7), { (0, 5): 6.0, (0, 7): 38.0, (2, 7): 38.0, (3, 7): 38.0, (4, 7): 38.0, (6, 7): 38.0 })) # noqa: E501 board.put_disc('black', 0, 7) # depth=6 : white : -- # depth=6 : black : d8 self.assertEqual( endgame.get_best_move('black', board, board.get_legal_moves('black')), ((3, 7), { (0, 5): 34.0, (2, 7): 34.0, (3, 7): 38.0, (4, 7): 38.0, (6, 7): 38.0 })) # noqa: E501 board.put_disc('black', 3, 7) # depth=5 : white : e8 self.assertEqual( endgame.get_best_move('white', board, board.get_legal_moves('white')), ((4, 7), { (4, 7): -38.0 })) board.put_disc('white', 4, 7) # depth=4 : balck : c8 self.assertEqual( endgame.get_best_move('black', board, board.get_legal_moves('black')), ((2, 7), { (0, 5): 22.0, (2, 7): 38.0, (6, 7): 38.0, (7, 7): 38.0 })) # noqa: E501 board.put_disc('black', 2, 7) # depth=3 : white ; g8 self.assertEqual( endgame.get_best_move('white', board, board.get_legal_moves('white')), ((6, 7), { (6, 7): -38.0 })) board.put_disc('white', 6, 7) # depth=2 : balck : a6 self.assertEqual( endgame.get_best_move('black', board, board.get_legal_moves('black')), ((0, 5), { (0, 5): 38.0, (7, 7): 38.0 })) board.put_disc('black', 0, 5) # depth=1 : white : -- # depth=1 : black : h8 self.assertEqual( endgame.get_best_move('black', board, board.get_legal_moves('black')), ((7, 7), { (7, 7): 38.0 }))
def test_alphabeta_performance_of_get_score(self): board = BitBoard() board.put_disc('black', 3, 2) # AlphaBeta alphabeta = AlphaBeta(evaluator=coord.Evaluator_TPOW()) pid = alphabeta.__class__.__name__ + str(os.getpid()) Measure.count[pid] = 0 Timer.timeout_flag[pid] = False Timer.timeout_value[pid] = 0 Timer.deadline[pid] = time.time() + CPU_TIME score = alphabeta._get_score('white', board, alphabeta._MIN, alphabeta._MAX, 5, pid=pid) # depth 5 self.assertEqual(score, 4) self.assertEqual(Measure.count[pid], 703) # _AlphaBeta alphabeta = _AlphaBeta(evaluator=coord.Evaluator_TPOW()) pid = alphabeta.__class__.__name__ + str(os.getpid()) Measure.count[pid] = 0 score = alphabeta._get_score('white', board, alphabeta._MIN, alphabeta._MAX, 2, pid=pid) # depth 2 self.assertEqual(score, -10.75) self.assertEqual(Measure.count[pid], 16) Measure.count[pid] = 0 score = alphabeta._get_score('white', board, alphabeta._MIN, alphabeta._MAX, 3, pid=pid) # depth 3 self.assertEqual(score, 6.25) self.assertEqual(Measure.count[pid], 63) Measure.count[pid] = 0 score = alphabeta._get_score('white', board, alphabeta._MIN, alphabeta._MAX, 4, pid=pid) # depth 4 self.assertEqual(score, -8.25) self.assertEqual(Measure.count[pid], 257) Measure.count[pid] = 0 score = alphabeta._get_score('white', board, alphabeta._MIN, alphabeta._MAX, 5, pid=pid) # depth 5 self.assertEqual(score, 4) self.assertEqual(Measure.count[pid], 703) Measure.count[pid] = 0 score = alphabeta._get_score('white', board, alphabeta._MIN, alphabeta._MAX, 6, pid=pid) # depth 6 self.assertEqual(score, -3.5) self.assertEqual(Measure.count[pid], 2696) board.put_disc('white', 2, 4) board.put_disc('black', 5, 5) board.put_disc('white', 4, 2) board.put_disc('black', 5, 2) board.put_disc('white', 5, 4) Measure.elp_time[pid] = {'min': 10000, 'max': 0, 'ave': 0, 'cnt': 0} for _ in range(5): alphabeta.next_move('black', board) print() print(pid, 'depth = 3') print(' min :', Measure.elp_time[pid]['min'], '(s)') print(' max :', Measure.elp_time[pid]['max'], '(s)') print(' ave :', Measure.elp_time[pid]['ave'], '(s)') # best move class _AlphaBetaTest(_AlphaBeta): @Measure.time def get_best_move(self, color, board, moves, depth): return super().get_best_move(color, board, moves, depth) alphabeta = _AlphaBetaTest(evaluator=coord.Evaluator_TPOW()) pid = alphabeta.__class__.__name__ + str(os.getpid()) moves = board.get_legal_moves('black') Measure.elp_time[pid] = {'min': 10000, 'max': 0, 'ave': 0, 'cnt': 0} for _ in range(3): alphabeta.get_best_move('black', board, moves, 4) print() print(pid, 'depth = 4') print(' min :', Measure.elp_time[pid]['min'], '(s)') print(' max :', Measure.elp_time[pid]['max'], '(s)') print(' ave :', Measure.elp_time[pid]['ave'], '(s)') self.assertEqual(alphabeta.get_best_move('black', board, moves, 4), ((5, 3), { (2, 2): -4.25, (2, 3): -3.75, (5, 3): -1.75, (1, 5): -1.75, (2, 5): -1.75, (3, 5): -1.75, (4, 5): -1.75, (6, 5): -1.75 })) # noqa: E501 moves = coord.Orderer_B().move_ordering(color='black', board=board, moves=moves, best_move=(5, 3)) Measure.elp_time[pid] = {'min': 10000, 'max': 0, 'ave': 0, 'cnt': 0} for _ in range(3): alphabeta.get_best_move('black', board, moves, 4) print() print(pid, 'depth = 4 Orderer_B') print(' min :', Measure.elp_time[pid]['min'], '(s)') print(' max :', Measure.elp_time[pid]['max'], '(s)') print(' ave :', Measure.elp_time[pid]['ave'], '(s)')