def test_proto_negamax3_get_score(self): negamax3 = NegaMax3() board = BitBoard(4) board._black_bitboard = 0x0400 board._white_bitboard = 0x8030 self.assertEqual(negamax3.get_score('white', board, 2), -2)
def test_tora_init(self): joseki = {} joseki.update(MOUSE) joseki.update(BULL) joseki.update(RABBIT) joseki.update(SROSE) joseki.update(ROSEVILLE) joseki.update(FASTBOAT) joseki.update(CAT) joseki.update(TIGER) # no Measure _tora_ = _Tora_(Random()) key = _tora_.__class__.__name__ + str(os.getpid()) board = BitBoard() _tora_.next_move(c.black, board) self.assertEqual(_tora_.joseki, joseki) self.assertIsInstance(_tora_.base, Random) self.assertFalse(key in Measure.elp_time) # with Measure tora = Tora(Random()) key = tora.__class__.__name__ + str(os.getpid()) board = BitBoard() tora.next_move(c.black, board) self.assertEqual(tora.joseki, joseki) self.assertIsInstance(tora.base, Random) self.assertTrue(key in Measure.elp_time)
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_neko_init(self): joseki = {} joseki.update(BULL) joseki.update(MOUSE) joseki.update(RABBIT) joseki.update(TIGER) joseki.update(SROSE) joseki.update(ROSEVILLE) joseki.update(FASTBOAT) joseki.update(SHEEP) joseki.update(CAT) # no Measure _neko_ = _Neko_(Random()) key = _neko_.__class__.__name__ + str(os.getpid()) board = BitBoard() _neko_.next_move(c.black, board) self.assertEqual(_neko_.joseki, joseki) self.assertIsInstance(_neko_.base, Random) self.assertFalse(key in Measure.elp_time) # with Measure neko = Neko(Random()) key = neko.__class__.__name__ + str(os.getpid()) board = BitBoard() neko.next_move(c.black, board) self.assertEqual(neko.joseki, joseki) self.assertIsInstance(neko.base, Random) self.assertTrue(key in Measure.elp_time)
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_usagi_init(self): joseki = {} joseki.update(MOUSE) joseki.update(BULL) joseki.update(TIGER) joseki.update(SROSE) joseki.update(ROSEVILLE) joseki.update(FASTBOAT) joseki.update(CAT) joseki.update(RABBIT) # no Measure _usagi_ = _Usagi_(Random()) key = _usagi_.__class__.__name__ + str(os.getpid()) board = BitBoard() _usagi_.next_move(c.black, board) self.assertEqual(_usagi_.joseki, joseki) self.assertIsInstance(_usagi_.base, Random) self.assertFalse(key in Measure.elp_time) # with Measure usagi = Usagi(Random()) key = usagi.__class__.__name__ + str(os.getpid()) board = BitBoard() usagi.next_move(c.black, board) self.assertEqual(usagi.joseki, joseki) self.assertIsInstance(usagi.base, Random) self.assertTrue(key in Measure.elp_time)
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_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_proto_minmax2_get_score(self): minmax2 = MinMax2() board = BitBoard(4) board._black_bitboard = 0x0400 board._white_bitboard = 0x8030 self.assertEqual(minmax2.get_score('white', board, 2), 2)
def test_proto_negamax3_timeout(self): negamax3 = NegaMax3() board = BitBoard() board._black_bitboard = 0xC001 board._white_bitboard = 0x2002 Timer.timeout_flag[negamax3] = True self.assertEqual(negamax3.next_move('black', board), (3, 6)) self.assertEqual(negamax3.get_score('black', board, 2), -10000000)
def test_proto_alphabeta4_get_score(self): alphabeta4 = AlphaBeta4() board = BitBoard(4) board._black_bitboard = 0x0400 board._white_bitboard = 0x8030 self.assertEqual( alphabeta4.get_score((3, 3), 'white', board, 1000, -1000, 2), 10001)
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_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_proto_negamax3_evaluate(self): negamax3 = NegaMax3() board = BitBoard(4) self.assertEqual(negamax3.evaluate('black', board, [], []), 0) board._black_score = 3 self.assertEqual(negamax3.evaluate('black', board, [], []), 10001) board._white_score = 4 self.assertEqual(negamax3.evaluate('black', board, [], []), -10001)
def test_proto_alphabeta4_evaluate(self): alphabeta4 = AlphaBeta4() board = BitBoard(4) self.assertEqual(alphabeta4.evaluate('black', board, [], []), 0) board._black_score = 3 self.assertEqual(alphabeta4.evaluate('black', board, [], []), 10001) board._white_score = 4 self.assertEqual(alphabeta4.evaluate('black', board, [], []), -10001)
def test_proto_minmax2_evaluate(self): minmax2 = MinMax2() board = BitBoard(4) self.assertEqual(minmax2.evaluate(board, [], []), 0) board._black_score = 3 self.assertEqual(minmax2.evaluate(board, [], []), 10001) board._white_score = 4 self.assertEqual(minmax2.evaluate(board, [], []), -10001)
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_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_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_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_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_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_proto_ab_t4_next_move(self): ab_t4 = AB_T4() board = BitBoard() board.put_disc('black', 3, 2) self.assertEqual(ab_t4.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(ab_t4.next_move('black', board), (2, 5)) board = BitBoard(4) self.assertEqual(ab_t4.next_move('black', board), (1, 0))
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))