게시판SGF 기보로 학습하는 바둑 LLM 인공지능예제 소스

예제 소스

SGF 기보로 학습하는 바둑 LLM 인공지능 · 파일 169개

test_monitor_heuristic.py

from __future__ import annotations

import random
import sys
import unittest
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
    sys.path.insert(0, str(ROOT))

from scripts.monitor_mariago_web_play import (  # noqa: E402
    BLACK,
    WHITE,
    _framework_score,
    _heuristic_score,
    empty_board,
    heuristic_move,
    idx,
)


def board_with(*, black=(), white=()):
    board = empty_board()
    for r, c in black:
        board[idx(r, c)] = BLACK
    for r, c in white:
        board[idx(r, c)] = WHITE
    return board


class MonitorHeuristicTest(unittest.TestCase):
    def test_empty_board_starts_from_a_corner_star_point(self):
        move = heuristic_move(empty_board(), BLACK, None, random.Random(7))
        self.assertIn(move[:2], {(3, 3), (3, 15), (15, 3), (15, 15)})
        self.assertFalse(move[2])

    def test_opening_spreads_to_an_unoccupied_corner(self):
        board = board_with(black=[(3, 3)])
        move = heuristic_move(board, BLACK, None, random.Random(7))
        self.assertIn(move[:2], {(3, 15), (15, 3), (15, 15)})

    def test_capture_is_prioritized(self):
        board = board_with(
            black=[(2, 3), (3, 2), (4, 3)],
            white=[(3, 3)],
        )
        self.assertEqual(
            heuristic_move(board, BLACK, None, random.Random(1))[:2],
            (3, 4),
        )

    def test_atari_rescue_is_prioritized(self):
        board = board_with(
            black=[(3, 3)],
            white=[(2, 3), (3, 2), (4, 3)],
        )
        self.assertEqual(
            heuristic_move(board, BLACK, None, random.Random(1))[:2],
            (3, 4),
        )

    def test_non_capture_self_atari_is_penalized(self):
        board = board_with(
            black=[(3, 2)],
            white=[(2, 3), (4, 3), (3, 4)],
        )
        self_atari = _heuristic_score(board, 3, 3, BLACK, None)
        safe = _heuristic_score(board, 15, 15, BLACK, None)
        self.assertIsNotNone(self_atari)
        self.assertIsNotNone(safe)
        self.assertLess(self_atari, safe)

    def test_does_not_extend_an_atari_group_into_forced_capture(self):
        board = board_with(
            black=[
                (3, 3), (3, 6), (3, 7), (3, 11), (3, 12), (3, 15),
                (4, 3), (4, 15), (5, 3), (5, 15), (6, 3), (6, 15),
                (7, 3), (9, 15), (10, 2), (12, 3), (12, 15),
                (15, 3), (15, 6), (15, 11), (16, 8),
            ],
            white=[
                (2, 3), (2, 6), (2, 7), (2, 11), (2, 12), (2, 15),
                (3, 2), (3, 4), (3, 5), (3, 8), (3, 9), (3, 13),
                (3, 14), (3, 16), (4, 2), (4, 4), (4, 7),
                (4, 14), (4, 16), (15, 14), (15, 15),
            ],
        )
        futile_escape = _heuristic_score(board, 4, 6, BLACK, None)
        safe = _heuristic_score(board, 14, 2, BLACK, None)
        self.assertIsNotNone(futile_escape)
        self.assertIsNotNone(safe)
        self.assertLess(futile_escape, safe)
        self.assertNotEqual(
            heuristic_move(board, BLACK, None, random.Random(1))[:2],
            (4, 6),
        )

    def test_framework_prefers_supported_expansion_over_isolation(self):
        board = board_with(
            black=[(3, 3), (3, 9), (3, 15), (15, 3), (15, 9), (15, 15)],
        )
        supported = _framework_score(board, 3, 6, BLACK)
        isolated = _framework_score(board, 9, 9, BLACK)
        self.assertGreater(supported, isolated)

    def test_large_atari_group_is_saved_before_small_capture(self):
        board = board_with(
            black=[
                (3, 12), (3, 13), (3, 14), (3, 15), (4, 2), (4, 3),
                (4, 15), (5, 1), (5, 2), (5, 15), (6, 3), (6, 15),
                (7, 1), (7, 2), (7, 3), (7, 15), (8, 3), (8, 15),
                (9, 2), (9, 3), (9, 15), (12, 3), (12, 15), (13, 3),
                (14, 2), (14, 3), (15, 2), (15, 3), (15, 4), (15, 5),
                (15, 6), (15, 9), (15, 10), (15, 11), (15, 12),
                (16, 2), (16, 4),
            ],
            white=[
                (2, 12), (2, 13), (2, 14), (2, 15), (3, 2), (3, 3),
                (3, 4), (3, 11), (3, 16), (4, 4), (4, 12), (4, 13),
                (4, 14), (4, 16), (5, 3), (5, 4), (5, 14), (5, 16),
                (6, 1), (6, 2), (6, 4), (6, 14), (6, 16), (7, 4),
                (7, 14), (7, 16), (8, 1), (8, 2), (8, 4), (8, 14),
                (8, 16), (9, 4), (9, 14), (10, 15), (15, 13),
                (15, 15), (16, 12),
            ],
        )
        self.assertEqual(
            heuristic_move(board, BLACK, None, random.Random(1))[:2],
            (9, 16),
        )


if __name__ == "__main__":
    unittest.main()