개발새발

바둑 AI 엔진 통합 본문

캡스톤디자인

바둑 AI 엔진 통합

비숑주인 2024. 9. 4. 12:43

KataGo 설치 및 설정

서버에 KataGo를 설치한다. (Ubuntu 기준)

 

sudo apt-get update
sudo apt-get install -y git cmake g++ libzip-dev zlib1g-dev

git clone https://github.com/lightvector/KataGo.git
cd KataGo/cpp
cmake . -DUSE_BACKEND=OPENCL  # GPU 지원
make

 

구글 드라이브로 다운로드

https://drive.usercontent.google.com/download?id=1ygkgiYkZD4oQeFbaCD3lgsmfChkEeTBe&export=download&authuser=1

 

Google Drive - 다운로드 경고

Google Drive가 다운로드 파일에서 문제를 감지했습니다 파일이 너무 커서 바이러스 검사를 할 수 없습니다. 실행 파일이기 때문에 컴퓨터에 해를 입힐 수 있습니다. 바둑AI통합설치팩_v4.21.3a_x64.exe(

drive.usercontent.google.com

 

AI 엔진과의 통신 인터페이스 구현

KataGo의 analysis 기능을 통해 바둑판 상태를 분석.

이를 위해 JSON 기반으로 KataGo와 통신하는 인터페이스를 구현했다. 

import subprocess
import json

def analyze_game(game_data):
    katago_path = '/path/to/katago'
    config_path = '/path/to/analysis_config.cfg'
    
    # Create JSON input for KataGo
    input_data = {
        "id": "analysis1",
        "action": "analyze",
        "board": game_data['moves'],
        "maxVisits": 1000
    }
    
    result = subprocess.run([katago_path, "analysis", "-config", config_path], input=json.dumps(input_data), capture_output=True, text=True)
    analysis_result = json.loads(result.stdout)
    
    return analysis_result

# Example usage
analysis_result = analyze_game(game_data)
print(analysis_result)

 

멀티스레딩 처리

Flask 서버에서 동시에 여러 게임을 처리하기 위해 멀티스레딩을 사용했다

 

from concurrent.futures import ThreadPoolExecutor

executor = ThreadPoolExecutor(max_workers=5)

@app.route('/analyze', methods=['POST'])
def analyze():
    file_path = request.form['file_path']
    game_data = parse_sgf_file(file_path)
    
    future = executor.submit(analyze_game, game_data)
    result = future.result()
    
    save_to_database(result)
    
    return 'Analysis complete', 200