Ejemplo n.º 1
0
    def test_corner_scorer(self):
        board = BitBoard(8)
        scorer = coord.CornerScorer()

        # Level1
        board._black_bitboard = 0x0000000000000000
        board._white_bitboard = 0x0000000000000000
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 0)
        board._black_bitboard = 0xE7C380000080C0C0
        board._white_bitboard = 0x0000000000010303
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 200)

        # Level2
        board._black_bitboard = 0x0000000080C0E0F0
        board._white_bitboard = 0xF7E703010000070F
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, -400)

        # Level3
        board._black_bitboard = 0x00000080C0E0F0F8
        board._white_bitboard = 0x0303030301000000
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 300)
        board._black_bitboard = 0x0000000103070707
        board._white_bitboard = 0xF8F0C00000000000
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 100)

        # Level4
        board._black_bitboard = 0xF08080C0E0F0F8FD
        board._white_bitboard = 0x0F0F070703010000
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 100)
        board._black_bitboard = 0xFCF8000000C0F8FC
        board._white_bitboard = 0x0303030303010000
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 400)

        # Level5
        board._black_bitboard = 0xFFFCF8F0E0C08000
        board._white_bitboard = 0x0000000000000000
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 900)
        board._black_bitboard = 0x0000000000000000
        board._white_bitboard = 0x0F0F0F0F07030100
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, -900)
        board._black_bitboard = 0xFFFEFCF8F0000000
        board._white_bitboard = 0x000000000F1F3F7F
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 0)

        # board size is not 8
        board = BitBoard(4)
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 0)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
    def test_fullreading_remain_9(self):
        class Test:
            pass

        board = BitBoard()
        fullreading = _FullReading(remain=9, base=Test())
        key = fullreading.__class__.__name__ + str(os.getpid())
        Measure.elp_time[key] = {'min': 10000, 'max': 0, 'ave': 0, 'cnt': 0}
        color = 'white'

        # pattern 1
        board._black_bitboard = 0x3FFFEE3E192CC07E
        board._white_bitboard = 0x400011C0E4523900
        board.update_score()
        self.assertEqual(fullreading.next_move(color, board), (0, 0))

        # pattern 2
        board._black_bitboard = 0x81878F170B470000
        board._white_bitboard = 0x7C387068F4381F3F
        board.update_score()
        self.assertEqual(fullreading.next_move(color, board), (6, 0))

        # pattern 3
        board._black_bitboard = 0xF27FBF650158381E
        board._white_bitboard = 0x9AFEA6C4E0
        board.update_score()
        self.assertEqual(fullreading.next_move(color, board), (7, 7))

        # pattern 4
        board._black_bitboard = 0x5C2353046C3874BA
        board._white_bitboard = 0x80DCACFB93850B00
        board.update_score()
        self.assertEqual(fullreading.next_move(color, board), (7, 0))

        # pattern 5
        board._black_bitboard = 0x828522161C1C07FF
        board._white_bitboard = 0x4858DC69E3E3B800
        board.update_score()
        self.assertEqual(fullreading.next_move(color, board), (7, 2))

        print()
        print(key, 'remain = 9')
        print(' min :', Measure.elp_time[key]['min'], '(s)')
        print(' max :', Measure.elp_time[key]['max'], '(s)')
        print(' ave :', Measure.elp_time[key]['ave'], '(s)')
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
    def test_edgecorner_scorer(self):
        board = BitBoard(8)
        scorer = coord.EdgeCornerScorer()

        # initial
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 0)

        # test1(left-bottom)
        board._black_bitboard = 0x00041800242014E0
        board._white_bitboard = 0x0000201C08182808
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 16)

        # test2(left-top)
        board._black_bitboard = 0x8084B8D6AFDCFEFC
        board._white_bitboard = 0x0000442850200000
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 24)

        # test3(right-bottom)
        board._black_bitboard = 0x1817A3DDADF4FBFF
        board._white_bitboard = 0x20886C22620A0400
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 24)

        # test4(right-top)
        board._black_bitboard = 0x1815E3DDADF4FBFF
        board._white_bitboard = 0x210A1C22520A0400
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 8)

        # test5(minus)
        board._black_bitboard = 0x1A16828CB6FAFCFC
        board._white_bitboard = 0x21497D7349050301
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, -8)
Ejemplo n.º 9
0
 def test_proto_alphabeta4_timeout(self):
     alphabeta4 = AlphaBeta4()
     pid = 'ALPHABETA4_TIMEOUT'
     board = BitBoard()
     board._black_bitboard = 0xC001
     board._white_bitboard = 0x2002
     Timer.deadline[pid] = 0
     Timer.timeout_value[pid] = -999
     self.assertEqual(
         alphabeta4.get_best_move('black', board, [(3, 6)], 0, pid=pid),
         (3, 6))
     Timer.deadline[pid] = time.time() + 0.01
     self.assertEqual(
         alphabeta4._get_score('black', board, 1000, -1000, 2, pid=pid),
         1000)
Ejemplo n.º 10
0
    def test_proto_alphabeta4_next_move(self):
        alphabeta4 = AlphaBeta4()
        board = BitBoard()

        board.put_disc('black', 3, 2)
        self.assertEqual(alphabeta4.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(alphabeta4.next_move('black', board), (2, 5))

        board._black_bitboard = 0xC001
        board._white_bitboard = 0x2002
        self.assertEqual(alphabeta4.next_move('black', board), (3, 6))
Ejemplo n.º 11
0
 def test_endgame_remain_16(self):
     board = BitBoard()
     endgame = _EndGame(depth=16)
     key = endgame.__class__.__name__ + str(os.getpid())
     Measure.elp_time[key] = {'min': 10000, 'max': 0, 'ave': 0, 'cnt': 0}
     Measure.count[key] = 0
     color = 'black'
     board._black_bitboard = 0xC07DBF650158381C
     board._white_bitboard = 0x0009A7CA6C4E0
     board.update_score()
     self.assertEqual(endgame.next_move(color, board), (6, 6))
     print()
     print(key, 'remain = 16')
     print(' min :', Measure.elp_time[key]['min'], '(s)')
     print(' max :', Measure.elp_time[key]['max'], '(s)')
     print(' ave :', Measure.elp_time[key]['ave'], '(s)')
     print('(5417116 / 1.94s)', Measure.count[key])
Ejemplo n.º 12
0
 def test_endgame_remain_12(self):
     # Windows10 Celeron 1.6GHz 4.00GB
     board = BitBoard()
     endgame = _EndGame(depth=12)
     key = endgame.__class__.__name__ + str(os.getpid())
     Measure.elp_time[key] = {'min': 10000, 'max': 0, 'ave': 0, 'cnt': 0}
     Measure.count[key] = 0
     color = 'black'
     board._black_bitboard = 0xF07DBF650158381C
     board._white_bitboard = 0x2009A7EA6C4E0
     board.update_score()
     self.assertEqual(endgame.next_move(color, board), (7, 0))
     print()
     print(key, 'remain = 12')
     print(' min :', Measure.elp_time[key]['min'], '(s)')
     print(' max :', Measure.elp_time[key]['max'], '(s)')
     print(' ave :', Measure.elp_time[key]['ave'], '(s)')
     print('(101890 / 0.03s)', Measure.count[key])
Ejemplo n.º 13
0
    def test_edge_scorer(self):
        board = BitBoard()
        scorer = coord.EdgeScorer()

        # stable disc
        board._black_bitboard = 0xC000000000000000
        board._white_bitboard = 0x0000000000000000
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 100)

        board._black_bitboard = 0xE000000000000000
        board._white_bitboard = 0x0000000000000000
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 200)

        board._black_bitboard = 0xFF00000000000000
        board._white_bitboard = 0x0000000000000000
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 1300)

        board._black_bitboard = 0xFF818181818181FF
        board._white_bitboard = 0x0000000000000000
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 5200)

        board._black_bitboard = 0x0000000000000000
        board._white_bitboard = 0xC3810000000081C3
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, -800)

        board._black_bitboard = 0x0000000000000000
        board._white_bitboard = 0xF7810080800081FF
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, -2200)

        # board size is not 8
        board = BitBoard(4)
        board.put_disc('black', 0, 0)
        board.put_disc('white', 1, 0)
        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, 1)
        board.put_disc('white', 1, 3)
        board.put_disc('black', 3, 3)
        board.put_disc('white', 3, 0)
        board.put_disc('black', 2, 3)
        board.put_disc('white', 3, 2)
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 600)
Ejemplo n.º 14
0
    def test_fullreading_remain_12(self):
        class Test:
            pass

        board = BitBoard()
        fullreading = _FullReading(remain=12, base=Test())
        key = fullreading.__class__.__name__ + str(os.getpid())
        Measure.elp_time[key] = {'min': 10000, 'max': 0, 'ave': 0, 'cnt': 0}
        color = 'black'

        board._black_bitboard = 0xF07DBF650158380E
        board._white_bitboard = 0x0009AFEA6C4E0
        board.update_score()
        self.assertEqual(fullreading.next_move(color, board), (7, 6))

        print()
        print(key, 'remain = 12')
        print(' min :', Measure.elp_time[key]['min'], '(s)')
        print(' max :', Measure.elp_time[key]['max'], '(s)')
        print(' ave :', Measure.elp_time[key]['ave'], '(s)')
Ejemplo n.º 15
0
    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))
Ejemplo n.º 16
0
    def test_specific_evaluator(self):
        board8 = BitBoard(8)
        board8.put_disc('black', 3, 2)
        board8.put_disc('white', 2, 2)
        board8.put_disc('black', 2, 3)
        board8.put_disc('white', 4, 2)
        board8.put_disc('black', 1, 1)
        board8.put_disc('white', 0, 0)

        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))

        # Evaluator_T
        evaluator = coord.Evaluator_T()
        score_b = evaluator.evaluate('black', board8, None, None)
        score_w = evaluator.evaluate('white', board8, None, None)
        self.assertEqual(score_b, -22)
        self.assertEqual(score_w, -22)

        # Evaluator_P
        evaluator = coord.Evaluator_P()
        score_b = evaluator.evaluate('black', board8, possibility_b,
                                     possibility_w)
        self.assertEqual(score_b, 5)

        # Evaluator_O
        evaluator = coord.Evaluator_O()
        score_b = evaluator.evaluate('black', board8, possibility_b,
                                     possibility_w)
        self.assertEqual(score_b, -8.25)

        # Evaluator_W
        evaluator = coord.Evaluator_W()

        board8._black_bitboard = 0xFFFFFFFFFFFFFFFF  # black win
        board8._white_bitboard = 0x0000000000000000
        board8.update_score()
        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))
        score_b = evaluator.evaluate('black', board8, possibility_b,
                                     possibility_w)
        score_w = evaluator.evaluate('white', board8, possibility_b,
                                     possibility_w)
        self.assertEqual(score_b, 10064)
        self.assertEqual(score_w, 10064)

        board8._black_bitboard = 0x0000000000000000
        board8._white_bitboard = 0xFFFFFFFFFFFFFFFF  # white win
        board8.update_score()
        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))
        score_b = evaluator.evaluate('black', board8, possibility_b,
                                     possibility_w)
        score_w = evaluator.evaluate('white', board8, possibility_b,
                                     possibility_w)
        self.assertEqual(score_b, -10064)
        self.assertEqual(score_w, -10064)

        # Evaluator_N
        board8 = BitBoard(8)
        board8.put_disc('black', 3, 2)
        board8.put_disc('white', 2, 2)
        board8.put_disc('black', 2, 3)
        board8.put_disc('white', 4, 2)
        board8.put_disc('black', 1, 1)
        board8.put_disc('white', 0, 0)

        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))

        evaluator = coord.Evaluator_N()
        score_b = evaluator.evaluate('black', board8, None, None)
        self.assertEqual(score_b, -6)

        # Evaluator_N_Fast
        evaluator = coord.Evaluator_N_Fast()
        score_b = evaluator.evaluate('black', board8, None, None)
        self.assertEqual(score_b, -6)

        # Evaluator_E
        evaluator = coord.Evaluator_E()
        score_b = evaluator.evaluate('black', board8, possibility_b,
                                     possibility_w)
        self.assertEqual(score_b, 0)

        # Evaluator_C
        evaluator = coord.Evaluator_C()
        score_b = evaluator.evaluate('black', board8, possibility_b,
                                     possibility_w)
        self.assertEqual(score_b, 0)

        # Evaluator_B
        evaluator = coord.Evaluator_B()
        score_b = evaluator.evaluate('black', board8, possibility_b,
                                     possibility_w)
        self.assertEqual(score_b, 19)

        # Evaluator_Ec
        evaluator = coord.Evaluator_Ec()
        score_b = evaluator.evaluate('black', board8, possibility_b,
                                     possibility_w)
        self.assertEqual(score_b, -16)

        # Evaluator_TP
        evaluator = coord.Evaluator_TP()
        score_b = evaluator.evaluate('black', board8, possibility_b,
                                     possibility_w)
        self.assertEqual(score_b, -17)

        # Evaluator_TPO
        evaluator = coord.Evaluator_TPO()
        score_b = evaluator.evaluate('black', board8, possibility_b,
                                     possibility_w)
        self.assertEqual(score_b, -25.25)

        # Evaluator_NW
        evaluator = coord.Evaluator_NW()
        score = evaluator.evaluate(None, board8, None, None)
        self.assertEqual(score, -10006)
        score = evaluator.evaluate(None, board8, possibility_b, possibility_w)
        self.assertEqual(score, -6)

        # Evaluator_PW
        evaluator = coord.Evaluator_PW()
        score = evaluator.evaluate(None, board8, None, None)
        self.assertEqual(score, -10006)
        score = evaluator.evaluate(None, board8, possibility_b, possibility_w)
        self.assertEqual(score, 5)

        # Evaluator_TPW
        evaluator = coord.Evaluator_TPW()
        score = evaluator.evaluate(None, board8, None, None)
        self.assertEqual(score, -10006)
        score = evaluator.evaluate(None, board8, possibility_b, possibility_w)
        self.assertEqual(score, -17)

        # Evaluator_TPW_Fast
        board8 = BitBoard(8)
        board8.put_disc('black', 3, 2)
        board8.put_disc('white', 2, 2)
        board8.put_disc('black', 2, 3)
        board8.put_disc('white', 4, 2)
        board8.put_disc('black', 1, 1)

        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))

        evaluator = coord.Evaluator_TPW_Fast()
        score = evaluator.evaluate(None, board8, None, None)
        self.assertEqual(score, 10001)

        board8.put_disc('white', 0, 0)

        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))

        score = evaluator.evaluate(None, board8, None, None)
        self.assertEqual(score, -10006)
        score = evaluator.evaluate(None, board8, possibility_b, possibility_w)
        self.assertEqual(score, -17)

        # Evaluator_TPOW
        evaluator = coord.Evaluator_TPOW()
        score_b = evaluator.evaluate('black', board8, None, None)
        self.assertEqual(score_b, -10006)
        score_b = evaluator.evaluate('black', board8, possibility_b,
                                     possibility_w)
        self.assertEqual(score_b, -25.25)

        # Evaluator_TPWE
        evaluator = coord.Evaluator_TPWE()
        board8._black_bitboard = 0x0000002010003C7E
        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))
        score = evaluator.evaluate('black', board8, possibility_b,
                                   possibility_w)
        self.assertEqual(score, -81)

        board8._black_bitboard = 0x0000002010003C7C
        score = evaluator.evaluate('black', board8, possibility_b,
                                   possibility_w)
        self.assertEqual(score, -61)

        score = evaluator.evaluate('black', board8, None, None)
        self.assertEqual(score, -10006)

        # Evaluator_TPWE_Fast
        board8 = BitBoard(8)
        board8.put_disc('black', 3, 2)
        board8.put_disc('white', 2, 2)
        board8.put_disc('black', 2, 3)
        board8.put_disc('white', 4, 2)
        board8.put_disc('black', 1, 1)

        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))

        evaluator = coord.Evaluator_TPWE_Fast()
        score = evaluator.evaluate(None, board8, None, None)
        self.assertEqual(score, 10001)

        board8.put_disc('white', 0, 0)

        board8._black_bitboard = 0x0000002010003C7E
        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))
        score = evaluator.evaluate('black', board8, possibility_b,
                                   possibility_w)
        self.assertEqual(score, -81)

        board8._black_bitboard = 0x0000002010003C7C
        score = evaluator.evaluate('black', board8, possibility_b,
                                   possibility_w)
        self.assertEqual(score, -61)

        score = evaluator.evaluate('black', board8, None, None)
        self.assertEqual(score, -10006)

        board4 = BitBoard(4)
        possibility_b = board4.get_bit_count(
            board4.get_legal_moves_bits('black'))
        possibility_w = board4.get_bit_count(
            board4.get_legal_moves_bits('white'))
        score = evaluator.evaluate('black', board4, possibility_b,
                                   possibility_w)
        self.assertEqual(score, 0)

        # -------------------------------

        # Evaluator_TPWEC
        evaluator = coord.Evaluator_TPWEC()
        board8._black_bitboard = 0x0000002010003C7C
        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))
        score = evaluator.evaluate('black', board8, possibility_b,
                                   possibility_w)
        self.assertEqual(score, -61)
        board8._black_bitboard = 0x0703012010003C7E
        score = evaluator.evaluate('black', board8, possibility_b,
                                   possibility_w)
        self.assertEqual(score, 422)

        score = evaluator.evaluate('black', board8, None, None)
        self.assertEqual(score, -10006)

        # Evaluator_PWE
        evaluator = coord.Evaluator_PWE()
        board8._black_bitboard = 0x0000002010003C7C
        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))
        score = evaluator.evaluate('black', board8, possibility_b,
                                   possibility_w)
        self.assertEqual(score, 10)
        board8._black_bitboard = 0x0703012010003C7E
        score = evaluator.evaluate('black', board8, possibility_b,
                                   possibility_w)
        self.assertEqual(score, 310)

        score = evaluator.evaluate('black', board8, None, None)
        self.assertEqual(score, -10006)

        # Evaluator_BW
        evaluator = coord.Evaluator_BW()
        board8._black_bitboard = 0x0000002010003C7C
        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))
        score = evaluator.evaluate('black', board8, possibility_b,
                                   possibility_w)
        self.assertEqual(score, 2)
        board8._black_bitboard = 0x0703012010003C7E
        score = evaluator.evaluate('black', board8, possibility_b,
                                   possibility_w)
        self.assertEqual(score, -2)

        score = evaluator.evaluate('black', board8, None, None)
        self.assertEqual(score, -10006)

        # Evaluator_EcW
        evaluator = coord.Evaluator_EcW()
        board8._black_bitboard = 0x0000002010003C7C
        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))
        score = evaluator.evaluate('black', board8, possibility_b,
                                   possibility_w)
        self.assertEqual(score, -16)
        board8._black_bitboard = 0x0703012010003C7E
        score = evaluator.evaluate('black', board8, possibility_b,
                                   possibility_w)
        self.assertEqual(score, 0)

        score = evaluator.evaluate('black', board8, None, None)
        self.assertEqual(score, -10006)

        # Evaluator_BWEc
        evaluator = coord.Evaluator_BWEc()
        board8._black_bitboard = 0x0000002010003C7C
        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))
        score = evaluator.evaluate('black', board8, possibility_b,
                                   possibility_w)
        self.assertEqual(score, -14)
        board8._black_bitboard = 0x0703012010003C7E
        score = evaluator.evaluate('black', board8, possibility_b,
                                   possibility_w)
        self.assertEqual(score, -2)

        score = evaluator.evaluate('black', board8, None, None)
        self.assertEqual(score, -10006)
Ejemplo n.º 17
0
    def test_evaluator_force_import_error(self):
        import os
        import importlib
        import reversi

        # -------------------------------
        # switch environ and reload module
        os.environ['FORCE_EVALUATORMETHODS_IMPORT_ERROR'] = 'RAISE'
        importlib.reload(reversi.strategies.coordinator.EvaluatorMethods)
        self.assertTrue(
            reversi.strategies.coordinator.EvaluatorMethods.SLOW_MODE)
        # -------------------------------

        # Evaluator_TPW_Fast
        board8 = BitBoard(8)
        board8.put_disc('black', 3, 2)
        board8.put_disc('white', 2, 2)
        board8.put_disc('black', 2, 3)
        board8.put_disc('white', 4, 2)
        board8.put_disc('black', 1, 1)

        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))

        evaluator = coord.Evaluator_TPW_Fast()
        score = evaluator.evaluate(None, board8, None, None)
        self.assertEqual(score, 10001)

        board8.put_disc('white', 0, 0)

        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))

        score = evaluator.evaluate(None, board8, None, None)
        self.assertEqual(score, -10006)
        score = evaluator.evaluate(None, board8, possibility_b, possibility_w)
        self.assertEqual(score, -17)

        # Evaluator_TPWE_Fast
        board8 = BitBoard(8)
        board8.put_disc('black', 3, 2)
        board8.put_disc('white', 2, 2)
        board8.put_disc('black', 2, 3)
        board8.put_disc('white', 4, 2)
        board8.put_disc('black', 1, 1)

        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))

        evaluator = coord.Evaluator_TPWE_Fast()
        score = evaluator.evaluate(None, board8, None, None)
        self.assertEqual(score, 10001)

        board8.put_disc('white', 0, 0)

        board8._black_bitboard = 0x0000002010003C7E
        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))
        score = evaluator.evaluate('black', board8, possibility_b,
                                   possibility_w)
        self.assertEqual(score, -81)

        board8._black_bitboard = 0x0000002010003C7C
        score = evaluator.evaluate('black', board8, possibility_b,
                                   possibility_w)
        self.assertEqual(score, -61)

        score = evaluator.evaluate('black', board8, None, None)
        self.assertEqual(score, -10006)

        board4 = BitBoard(4)
        possibility_b = board4.get_bit_count(
            board4.get_legal_moves_bits('black'))
        possibility_w = board4.get_bit_count(
            board4.get_legal_moves_bits('white'))
        score = evaluator.evaluate('black', board4, possibility_b,
                                   possibility_w)
        self.assertEqual(score, 0)

        # -------------------------------
        # recover environment and reload module
        del os.environ['FORCE_EVALUATORMETHODS_IMPORT_ERROR']
        importlib.reload(reversi.strategies.coordinator.EvaluatorMethods)
        self.assertFalse(
            reversi.strategies.coordinator.EvaluatorMethods.SLOW_MODE)
Ejemplo n.º 18
0
    def test_general_evaluator_evaluate(self):
        evaluator = coord.Evaluator(
            separated=[
                coord.WinLoseScorer(),
            ],
            combined=[
                coord.TableScorer(),
                coord.PossibilityScorer(),
                coord.EdgeScorer(),
            ],
        )

        board8 = BitBoard(8)
        board8._black_bitboard = 0xFFFFFFFFFFFFFFFF  # black win
        board8._white_bitboard = 0x0000000000000000
        board8.update_score()
        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))
        score_b = evaluator.evaluate('black', board8, possibility_b,
                                     possibility_w)
        score_w = evaluator.evaluate('white', board8, possibility_b,
                                     possibility_w)
        self.assertEqual(score_b, 10064)
        self.assertEqual(score_w, 10064)

        board8._black_bitboard = 0x0000000000000000
        board8._white_bitboard = 0xFFFFFFFFFFFFFFFF  # white win
        board8.update_score()
        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))
        score_b = evaluator.evaluate('black', board8, possibility_b,
                                     possibility_w)
        score_w = evaluator.evaluate('white', board8, possibility_b,
                                     possibility_w)
        self.assertEqual(score_b, -10064)
        self.assertEqual(score_w, -10064)

        board8 = BitBoard(8)
        board8.put_disc('black', 3, 2)
        board8.put_disc('white', 2, 2)
        board8.put_disc('black', 2, 3)
        board8.put_disc('white', 4, 2)
        board8.put_disc('black', 1, 1)
        board8.put_disc('white', 0, 0)
        board8._black_bitboard = 0x0000002010003C7E
        possibility_b = board8.get_bit_count(
            board8.get_legal_moves_bits('black'))
        possibility_w = board8.get_bit_count(
            board8.get_legal_moves_bits('white'))
        score = evaluator.evaluate('black', board8, possibility_b,
                                   possibility_w)
        self.assertEqual(score, -81)

        board8._black_bitboard = 0x0000002010003C7C
        score = evaluator.evaluate('black', board8, possibility_b,
                                   possibility_w)
        self.assertEqual(score, -61)
Ejemplo n.º 19
0
    def test_scorer_force_import_error(self):
        import os
        import importlib
        import reversi

        # -------------------------------
        # switch environ and reload module
        os.environ['FORCE_SCORERMETHODS_IMPORT_ERROR'] = 'RAISE'
        importlib.reload(reversi.strategies.coordinator.ScorerMethods)
        self.assertTrue(reversi.strategies.coordinator.ScorerMethods.SLOW_MODE)
        # -------------------------------

        # BlankScorer
        board = BitBoard(8)
        scorer = coord.BlankScorer()

        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 0)

        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)

        board.undo()
        board.put_disc('black', 2, 2)
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, -16)

        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)

        board._black_bitboard = 0x2058082010000000
        board._white_bitboard = 0x4020101C0C040000
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 1)

        board._black_bitboard = 0x20583E3C1C060200
        board._white_bitboard = 0x4020000000000000
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, -53)

        board._black_bitboard = 0x007A562C7C060200
        board._white_bitboard = 0xFA84A8D080000000
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, -42)

        board._black_bitboard = 0x043A170C14320000
        board._white_bitboard = 0xFAC4E8F1E8CCC241
        score = scorer.get_score(None, board, None, None)
        self.assertEqual(score, 8)

        # -------------------------------
        # recover environment and reload module
        del os.environ['FORCE_SCORERMETHODS_IMPORT_ERROR']
        importlib.reload(reversi.strategies.coordinator.ScorerMethods)
        self.assertFalse(
            reversi.strategies.coordinator.ScorerMethods.SLOW_MODE)
Ejemplo n.º 20
0
    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
            }))
Ejemplo n.º 21
0
    def test_alphabeta_force_import_error(self):
        import os
        import importlib
        import reversi

        # -------------------------------
        # switch environ and reload module
        os.environ['FORCE_ALPHABETAMETHODS_IMPORT_ERROR'] = 'RAISE'
        importlib.reload(reversi.strategies.AlphaBetaMethods)
        self.assertTrue(reversi.strategies.AlphaBetaMethods.SLOW_MODE)
        self.assertTrue(
            reversi.strategies.AlphaBetaMethods.ALPHABETA_SIZE8_64BIT_ERROR)
        # -------------------------------

        # measure
        pid = 'ALPHABETA_IMPORT_ERROR_MEASURE'
        for _ in range(3):
            reversi.strategies.AlphaBetaMethods.GetScore.measure(pid)
        self.assertEqual(Measure.count[pid], 3)

        # timer
        pid = 'ALPHABETA_IMPORT_ERROR_TIMER'
        Timer.deadline[pid] = 0
        Timer.timeout_value[pid] = 100
        self.assertIsNone(
            reversi.strategies.AlphaBetaMethods.GetScore.timer(None))
        self.assertEqual(
            reversi.strategies.AlphaBetaMethods.GetScore.timer(pid), 100)
        self.assertTrue(Timer.timeout_flag[pid])

        # get_score
        alphabeta = AlphaBeta(depth=2, evaluator=coord.Evaluator_N())
        color = 'black'
        board = BitBoard(4)
        alpha = 1
        beta = 1
        depth = 0
        pid = 'ALPHABETA_IMPORT_ERROR_GET_SCORE'

        # - depth == 0
        score = reversi.strategies.AlphaBetaMethods.GetScore.get_score(
            alphabeta, color, board, alpha, beta, depth, pid)
        self.assertEqual(score, 0)

        # - pass and score
        depth = 1
        board._black_bitboard = 0x4000
        board._white_bitboard = 0x8000
        score = reversi.strategies.AlphaBetaMethods.GetScore.get_score(
            alphabeta, color, board, alpha, beta, depth, pid)
        self.assertEqual(score, -3)

        # get_score_measure
        score = reversi.strategies.AlphaBetaMethods.GetScore.get_score_measure(
            alphabeta, color, board, alpha, beta, depth, pid)
        self.assertEqual(score, -3)
        self.assertEqual(Measure.count[pid], 3)

        # get_score_timer
        Timer.deadline[pid] = 0
        Timer.timeout_value[pid] = 100
        score = reversi.strategies.AlphaBetaMethods.GetScore.get_score_timer(
            alphabeta, color, board, alpha, beta, depth, pid)
        self.assertEqual(score, 100)
        self.assertTrue(Timer.timeout_flag[pid])

        Timer.deadline[pid] = time.time() + 1
        score = reversi.strategies.AlphaBetaMethods.GetScore.get_score_timer(
            alphabeta, color, board, alpha, beta, depth, pid)
        self.assertEqual(score, 1)

        # get_score_measure_timer
        Measure.count[pid] = 0
        Timer.deadline[pid] = time.time() + 1
        score = reversi.strategies.AlphaBetaMethods.GetScore.get_score_measure_timer(
            alphabeta, color, board, alpha, beta, depth, pid)
        self.assertEqual(score, 1)
        self.assertEqual(Measure.count[pid], 3)

        # -------------------------------
        # recover environment and reload module
        del os.environ['FORCE_ALPHABETAMETHODS_IMPORT_ERROR']
        importlib.reload(reversi.strategies.AlphaBetaMethods)
        self.assertFalse(reversi.strategies.AlphaBetaMethods.SLOW_MODE)
        self.assertFalse(
            reversi.strategies.AlphaBetaMethods.ALPHABETA_SIZE8_64BIT_ERROR)