def test_hitsuji_next_move(self): board = BitBoard() hitsuji = Hitsuji(Random()) patterns = [ # turn, move # --- 猫定石 --- (c.black, (5, 4)), (c.white, (3, 5)), (c.black, (2, 3)), (c.white, (3, 2)), (c.black, (4, 5)), (c.white, (5, 3)), (c.black, (4, 2)), (c.white, (5, 2)), (c.black, (2, 5)), (c.white, (5, 5)), (c.black, (6, 4)), (c.white, (6, 5)), (c.black, (2, 2)), (c.white, (7, 5)), (c.black, (6, 3)), (c.white, (7, 4)), (c.black, (7, 2)), (c.white, (2, 4)), ] for turn, expected in patterns: move = hitsuji.next_move(turn, board) board.put_disc(turn, *move) self.assertEqual(move, expected)
def test_negamax_next_move_timeout_max_score(self): def func(): score = 10 def decliment_score(next_color, board, depth, pid): nonlocal score score -= 1 if score == 8: Timer.timeout_flag[pid] = True return score return decliment_score board = BitBoard() board.put_disc('black', 3, 2) negamax = NegaMax(evaluator=coord.Evaluator_TPOW()) pid = negamax.__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 negamax.get_score = func() # override get_score self.assertEqual(negamax.next_move('white', board), (2, 2))
def test_alphabeta_get_score(self): for instance in [_AlphaBeta_, _AlphaBeta, AlphaBeta_, AlphaBeta]: board = BitBoard() alphabeta = instance(evaluator=coord.Evaluator_T()) self.assertEqual( alphabeta._get_score('black', board, alphabeta._MIN, alphabeta._MAX, 1), -3) self.assertEqual( alphabeta._get_score('black', board, alphabeta._MIN, alphabeta._MAX, 2), -1) self.assertEqual( alphabeta._get_score('black', board, alphabeta._MIN, alphabeta._MAX, 3), -4) self.assertEqual( alphabeta._get_score('black', board, alphabeta._MIN, alphabeta._MAX, 4), 0) board.put_disc('black', 3, 2) self.assertEqual( alphabeta._get_score('white', board, alphabeta._MIN, alphabeta._MAX, 1), 1) self.assertEqual( alphabeta._get_score('white', board, alphabeta._MIN, alphabeta._MAX, 2), 4) self.assertEqual( alphabeta._get_score('white', board, alphabeta._MIN, alphabeta._MAX, 3), 0) self.assertEqual( alphabeta._get_score('white', board, alphabeta._MIN, alphabeta._MAX, 4), 3)
def test_neko_next_move(self): board = BitBoard() neko = Neko(Random()) patterns = [ # turn, move # --- 猫定石 --- (c.black, (5, 4)), (c.white, (3, 5)), (c.black, (2, 3)), (c.white, (3, 2)), (c.black, (2, 4)), (c.white, (5, 3)), (c.black, (4, 2)), (c.white, (5, 2)), (c.black, (4, 1)), (c.white, (2, 5)), (c.black, (4, 5)), (c.white, (5, 5)), (c.black, (3, 6)), (c.white, (2, 7)), ] for turn, expected in patterns: move = neko.next_move(turn, board) board.put_disc(turn, *move) self.assertEqual(move, expected)
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_negamax_timer_timeout(self): board = BitBoard() board.put_disc('black', 3, 2) negamax = NegaMax(depth=10, evaluator=coord.Evaluator_TPOW()) pid = negamax.__class__.__name__ + str(os.getpid()) Measure.elp_time[pid] = {'min': 10000, 'max': 0, 'ave': 0, 'cnt': 0} negamax.next_move('white', board) self.assertTrue(Timer.timeout_flag[pid]) self.assertLessEqual(Measure.elp_time[pid]['max'], CPU_TIME * 1.1)
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_endgame_timer_timeout(self): board = BitBoard() board.put_disc('black', 3, 2) endgame = EndGame(depth=20) pid = endgame.__class__.__name__ + str(os.getpid()) Measure.elp_time[pid] = {'min': 10000, 'max': 0, 'ave': 0, 'cnt': 0} Measure.count[pid] = 0 endgame.next_move('white', board) self.assertTrue(Timer.timeout_flag[pid]) self.assertLessEqual(Measure.elp_time[pid]['max'], CPU_TIME * 1.1) print('(1000000)', Measure.count[pid])
def test_alphabeta_timer_timeout(self): board = BitBoard() board.put_disc('black', 3, 2) alphabeta = AlphaBeta(depth=10, evaluator=coord.Evaluator_TPOW()) pid = alphabeta.__class__.__name__ + str(os.getpid()) Measure.elp_time[pid] = {'min': 10000, 'max': 0, 'ave': 0, 'cnt': 0} Measure.count[pid] = 0 alphabeta.next_move('white', board) self.assertTrue(Timer.timeout_flag[pid]) self.assertLessEqual(Measure.elp_time[pid]['max'], CPU_TIME * 1.1) print('(9000)', Measure.count[pid])
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_minmax_get_score(self): board = BitBoard() minmax = MinMax(evaluator=coord.Evaluator_T()) self.assertEqual(minmax.get_score('black', board, 1), -3) self.assertEqual(minmax.get_score('black', board, 2), -1) self.assertEqual(minmax.get_score('black', board, 3), -4) self.assertEqual(minmax.get_score('black', board, 4), 0) board.put_disc('black', 3, 2) self.assertEqual(minmax.get_score('white', board, 1), -1) self.assertEqual(minmax.get_score('white', board, 2), -4) self.assertEqual(minmax.get_score('white', board, 3), 0) self.assertEqual(minmax.get_score('white', board, 4), -3)
def test_endgame_force_import_error(self): import os import importlib import reversi # ------------------------------- # switch environ and reload module os.environ['FORCE_ENDGAMEMETHODS_IMPORT_ERROR'] = 'RAISE' importlib.reload(reversi.strategies.EndGameMethods) self.assertTrue( reversi.strategies.EndGameMethods.ENDGAME_SIZE8_64BIT_ERROR) # ------------------------------- for instance in [_EndGame_, _EndGame, EndGame_, EndGame]: board = BitBoard() endgame = instance(depth=6) board.put_disc('black', 3, 2) self.assertEqual(endgame.next_move('white', board), (2, 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) self.assertEqual(endgame.next_move('black', board), (1, 5)) # ------------------------------- # recover environment and reload module del os.environ['FORCE_ENDGAMEMETHODS_IMPORT_ERROR'] importlib.reload(reversi.strategies.EndGameMethods) self.assertFalse( reversi.strategies.EndGameMethods.ENDGAME_SIZE8_64BIT_ERROR)
def test_negamax_get_score(self): for instance in [_NegaMax_, _NegaMax, NegaMax_, NegaMax]: board = BitBoard() negamax = instance(evaluator=coord.Evaluator_T()) self.assertEqual(negamax.get_score('black', board, 1), -3) self.assertEqual(negamax.get_score('black', board, 2), -1) self.assertEqual(negamax.get_score('black', board, 3), -4) self.assertEqual(negamax.get_score('black', board, 4), 0) board.put_disc('black', 3, 2) self.assertEqual(negamax.get_score('white', board, 1), 1) self.assertEqual(negamax.get_score('white', board, 2), 4) self.assertEqual(negamax.get_score('white', board, 3), 0) self.assertEqual(negamax.get_score('white', board, 4), 3)
def test_montecarlo_playout_draw(self): montecarlo = MonteCarlo() board = BitBoard(4) board.put_disc('black', 1, 0) board.put_disc('white', 0, 2) board.put_disc('black', 0, 3) board.put_disc('white', 0, 0) board.put_disc('black', 0, 1) board.put_disc('white', 2, 0) board.put_disc('black', 3, 2) board.put_disc('white', 2, 3) board.put_disc('black', 3, 0) board.put_disc('white', 1, 3) self.assertEqual(montecarlo._playout('black', board, (3, 3)), 0)
def test_montecarlo_performance(self): board = BitBoard() board.put_disc('black', 3, 2) board.put_disc('white', 2, 4) board.put_disc('black', 1, 5) montecarlo = MonteCarlo() montecarlo.next_move('white', board) key = montecarlo.__class__.__name__ + str(os.getpid()) print() print(key) print(' count(120) :', Measure.count[key]) print(' min :', Measure.elp_time[key]['min'], '(s)') print(' max :', Measure.elp_time[key]['max'], '(s)') print(' ave :', Measure.elp_time[key]['ave'], '(s)')
def test_negamax_get_score_pass(self): board = BitBoard(4) board.put_disc('black', 0, 1) board.put_disc('white', 0, 2) board.put_disc('black', 0, 3) board.put_disc('white', 2, 0) board.put_disc('black', 3, 0) board.put_disc('white', 0, 0) board.put_disc('black', 1, 0) negamax = _NegaMax_(evaluator=coord.Evaluator_N()) self.assertEqual(negamax.get_score('white', board, 1), -10)
def test_minmax_get_score_pass(self): board = BitBoard(4) board.put_disc('black', 0, 1) board.put_disc('white', 0, 2) board.put_disc('black', 0, 3) board.put_disc('white', 2, 0) board.put_disc('black', 3, 0) board.put_disc('white', 0, 0) board.put_disc('black', 1, 0) minmax = MinMax(evaluator=coord.Evaluator_N()) self.assertEqual(minmax.get_score('white', board, 1), 10)
def test_negamax_performance_of_get_score(self): board = BitBoard() board.put_disc('black', 3, 2) # NegaMax negamax = NegaMax(evaluator=coord.Evaluator_TPOW()) pid = negamax.__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 = negamax.get_score('white', board, 4, pid=pid) # depth 4 self.assertEqual(score, -8.25) self.assertEqual(Measure.count[pid], 428) # _NegaMax negamax = _NegaMax(evaluator=coord.Evaluator_TPOW()) pid = negamax.__class__.__name__ + str(os.getpid()) Measure.count[pid] = 0 score = negamax.get_score('white', board, 2, pid=pid) # depth 2 self.assertEqual(score, -10.75) self.assertEqual(Measure.count[pid], 18) Measure.count[pid] = 0 score = negamax.get_score('white', board, 3, pid=pid) # depth 3 self.assertEqual(score, 6.25) self.assertEqual(Measure.count[pid], 79) Measure.count[pid] = 0 score = negamax.get_score('white', board, 4, pid=pid) # depth 4 self.assertEqual(score, -8.25) self.assertEqual(Measure.count[pid], 428) Measure.count[pid] = 0 score = negamax.get_score('white', board, 5, pid=pid) # depth 5 self.assertEqual(score, 4) self.assertEqual(Measure.count[pid], 2478) 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): negamax.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)')
def test_minmax_next_move(self): board = BitBoard() minmax = MinMax(evaluator=coord.Evaluator_TPOW()) board.put_disc('black', 3, 2) self.assertEqual(minmax.next_move('white', board), (2, 4)) 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) self.assertEqual(minmax.next_move('black', board), (2, 2))
def test_blank_scorer(self): board = BitBoard(8) scorer = coord.BlankScorer() # initial score = scorer.get_score(None, board, None, None) self.assertEqual(score, 0) # test1 board.put_disc('black', 5, 4) board.put_disc('white', 5, 3) board.put_disc('black', 3, 2) score = scorer.get_score(None, board, None, None) self.assertEqual(score, -20) # test2 board.undo() board.put_disc('black', 2, 2) score = scorer.get_score(None, board, None, None) self.assertEqual(score, -16) # test3 board.undo() board.put_disc('black', 4, 2) board.put_disc('white', 5, 5) score = scorer.get_score(None, board, None, None) self.assertEqual(score, 8) # test4(left-top) board._black_bitboard = 0x2058082010000000 board._white_bitboard = 0x4020101C0C040000 score = scorer.get_score(None, board, None, None) self.assertEqual(score, 1) # test5(right-bottom) board._black_bitboard = 0x20583E3C1C060200 board._white_bitboard = 0x4020000000000000 score = scorer.get_score(None, board, None, None) self.assertEqual(score, -53) # test6(right-top) board._black_bitboard = 0x007A562C7C060200 board._white_bitboard = 0xFA84A8D080000000 score = scorer.get_score(None, board, None, None) self.assertEqual(score, -42) # test7(left-bottom) board._black_bitboard = 0x043A170C14320000 board._white_bitboard = 0xFAC4E8F1E8CCC241 score = scorer.get_score(None, board, None, None) self.assertEqual(score, 8)
def test_tora_next_move(self): board = BitBoard() tora = Tora(Random()) patterns = [ # turn, move # --- 虎定石 --- (c.black, (5, 4)), (c.white, (3, 5)), (c.black, (2, 2)), # --- ローズビル基本形 --- (c.white, (3, 2)), (c.black, (2, 3)), (c.white, (5, 3)), (c.black, (2, 4)), (c.white, (1, 2)), (c.black, (2, 1)), ] for turn, expected in patterns: move = tora.next_move(turn, board) board.put_disc(turn, *move) self.assertEqual(move, expected)
def test_negamax_next_move(self): for instance in [_NegaMax_, _NegaMax, NegaMax_, NegaMax]: board = BitBoard() negamax = instance(evaluator=coord.Evaluator_TPOW()) board.put_disc('black', 3, 2) self.assertEqual(negamax.next_move('white', board), (2, 4)) 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) self.assertEqual(negamax.next_move('black', board), (2, 2))
def test_endgame_next_move(self): for instance in [_EndGame_, _EndGame, EndGame_, EndGame]: board = BitBoard() endgame = instance(depth=6) board.put_disc('black', 3, 2) self.assertEqual(endgame.next_move('white', board), (2, 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) self.assertEqual(endgame.next_move('black', board), (1, 5))
def test_alphabeta_next_move(self): for instance in [_AlphaBeta_, _AlphaBeta, AlphaBeta_, AlphaBeta]: board = BitBoard() alphabeta = instance(evaluator=coord.Evaluator_TPOW()) board.put_disc('black', 3, 2) self.assertEqual(alphabeta.next_move('white', board), (2, 4)) 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) self.assertEqual(alphabeta.next_move('black', board), (2, 2))
def test_blank_performance(self): board = BitBoard() 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) iterative = IterativeDeepning( depth=4, selector=coord.Selector(), orderer=coord.Orderer_B(), search=Blank(), ) #Timer.time_limit = 1.2 key = iterative.__class__.__name__ + str(os.getpid()) Measure.elp_time[key] = {'min': 10000, 'max': 0, 'ave': 0, 'cnt': 0} key2 = iterative.search.__class__.__name__ + str(os.getpid()) Measure.count[key2] = 0 iterative.next_move('black', board) print() print(key) print('Blank : (310000)', Measure.count[key2]) print('(max_depth=9)', iterative.max_depth) print(' max :', Measure.elp_time[key]['max'], '(s)')
def test_fullreading_next_move(self): class Test: def next_move(self, color, board): return (3, 3) board = BitBoard() color = 'white' board._black_bitboard = 0x3FFFEE3E192CC07E board._white_bitboard = 0x400011C0E4523900 board.update_score() # remain = 9 : base fullreading = FullReading(remain=8, base=Test()) self.assertEqual(fullreading.next_move(color, board), (3, 3)) fullreading = _FullReading(remain=8, base=Test()) self.assertEqual(fullreading.next_move(color, board), (3, 3)) # remain = 8 : fullreading board.put_disc(color, 0, 0) color = 'black' self.assertEqual(fullreading.next_move(color, board), (7, 5))
def test_usagi_next_move(self): board = BitBoard() usagi = Usagi(Random()) patterns = [ # turn, move # --- 兎定石 --- (c.black, (5, 4)), (c.white, (3, 5)), (c.black, (2, 4)), (c.white, (5, 3)), (c.black, (4, 2)), # --- Sローズ基本形 --- (c.white, (2, 5)), (c.black, (3, 2)), (c.white, (5, 5)), (c.black, (4, 5)), (c.white, (3, 6)), (c.black, (6, 2)), (c.white, (2, 3)), ] for turn, expected in patterns: move = usagi.next_move(turn, board) board.put_disc(turn, *move) self.assertEqual(move, expected)
def test_ushi_next_move(self): board = BitBoard() ushi = Ushi(Random()) patterns = [ # turn, move # --- 牛定石 --- (c.black, (5, 4)), (c.white, (5, 5)), (c.black, (4, 5)), (c.white, (5, 3)), (c.black, (4, 2)), (c.white, (2, 4)), # --- 快速船基礎形 --- (c.black, (2, 3)), (c.white, (4, 6)), (c.black, (2, 5)), (c.white, (4, 1)), (c.black, (5, 2)), (c.white, (5, 1)), ] for turn, expected in patterns: move = ushi.next_move(turn, board) board.put_disc(turn, *move) self.assertEqual(move, expected)
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_proto_negamax3_next_move(self): negamax3 = NegaMax3() board = BitBoard() board.put_disc('black', 3, 2) self.assertEqual(negamax3.next_move('white', board), (2, 4)) board.put_disc('white', 2, 4) board.put_disc('black', 1, 5) board.put_disc('white', 1, 4) self.assertEqual(negamax3.next_move('black', board), (2, 5))