Skip to content

Commit

Permalink
Merge pull request #66 from kobanium/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
yssaya authored Dec 21, 2022
2 parents 9b961e3 + e484dc7 commit 1e01885
Show file tree
Hide file tree
Showing 19 changed files with 1,181 additions and 153 deletions.
133 changes: 133 additions & 0 deletions learn/convert/hcpe3_to_csa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
from cshogi import *
from cshogi import CSA
import numpy as np
import sys
import os
import glob
import math
import argparse

HuffmanCodedPosAndEval3 = np.dtype([
('hcp', dtypeHcp), # 開始局面
('moveNum', np.uint16), # 手数
('result', np.uint8), # 結果(xxxxxx11:勝敗、xxxxx1xx:千日手、xxxx1xxx:入玉宣言、xxx1xxxx:最大手数)
('opponent', np.uint8), # 対戦相手(0:自己対局、1:先手usi、2:後手usi)
])
MoveInfo = np.dtype([
('selectedMove16', dtypeMove16), # 指し手
('eval', dtypeEval), # 評価値
('candidateNum', np.uint16), # 候補手の数
])
MoveVisits = np.dtype([
('move16', dtypeMove16), # 候補手
('visitNum', np.uint16), # 訪問回数
])

ENDGAME_SYMBOLS = {
1 : '%TORYO',
2 : '%TORYO',
4 : '%SENNICHITE',
9 : '%KACHI',
10: '%KACHI',
16: '%CHUDAN',
}

parser = argparse.ArgumentParser()
parser.add_argument('hcpe3')
parser.add_argument('csa')
parser.add_argument('--range')
parser.add_argument('--nyugyoku', action='store_true')
parser.add_argument('--aoba', action='store_true')
parser.add_argument('--out_v', action='store_true')
parser.add_argument('--sort_visits', action='store_true')
args = parser.parse_args()

f = open(args.hcpe3, 'rb')

if args.aoba:
sep = ','
else:
sep = '\n'

if args.range:
start_end = args.range.split(':')
if len(start_end) == 1:
start = int(start_end[0])
end = start + 1
else:
if start_end[0] == '':
start = 0
else:
start = int(start_end[0])
if start_end[1] == '':
end = sys.maxsize
else:
end = int(start_end[1])
else:
start = 0
end = sys.maxsize

board = Board()
csa = CSA.Exporter(args.csa)
v_pos_sum = 0
move_sum = 0
p = 0
while p < end:
data = f.read(HuffmanCodedPosAndEval3.itemsize)
if len(data) == 0:
break
hcpe = np.frombuffer(data, HuffmanCodedPosAndEval3, 1)[0]
board.set_hcp(hcpe['hcp'])
assert board.is_ok()
move_num = hcpe['moveNum']
result = hcpe['result']
move_sum += move_num
need_output = p >= start and (not args.nyugyoku or result & 8 != 0)
if need_output:
# csa.info(board, comments=[f"moveNum={move_num},result={result},opponent={hcpe['opponent']}"])
print ("'move_num=" +str(move_num) +",result=" +str(result)+ ",opponent=" + str(hcpe['opponent']))
print (board)
for i in range(move_num):
move_info = np.frombuffer(f.read(MoveInfo.itemsize), MoveInfo, 1)[0]
candidate_num = move_info['candidateNum']
move_visits = np.frombuffer(f.read(MoveVisits.itemsize * candidate_num), MoveVisits, candidate_num)
move = board.move_from_move16(move_info['selectedMove16'])
if need_output:
if candidate_num > 0:
v_pos_sum += 1
if args.aoba:
if args.out_v:
v = 1.0 / (1.0 + math.exp(-move_info['eval'] * 0.0013226))
comment = f"v={v:.3f},"
else:
comment = ''
comment += f"{move_visits['visitNum'].sum()}"
if args.sort_visits:
move_visits = np.sort(move_visits, order='visitNum')[::-1]
for move16, visit_num in zip(move_visits['move16'], move_visits['visitNum']):
comment += ',' + move_to_csa(board.move_from_move16(move16)) + ',' + str(visit_num)
else:
comment = '** ' + str(move_info['eval'] * (1 - board.turn * 2))
else:
if args.aoba or move_info['eval'] == 0:
# comment = None
comment = ""
else:
comment = '** ' + str(move_info['eval'] * (1 - board.turn * 2))
# csa.move(move, comment=comment, sep=sep)
# csa.move(move, sep=sep)
if (board.turn) == 0:
sen = "+"
else:
sen = "-"
print (sen + move_to_csa(move) + ",'" + comment)
board.push(move)
assert board.is_ok()
if need_output:
print (ENDGAME_SYMBOLS[hcpe['result']])
print ("/")
# csa.endgame(ENDGAME_SYMBOLS[hcpe['result']])
p += 1
print (p,move_sum,v_pos_sum)


59 changes: 59 additions & 0 deletions learn/convert/hcpe_to_csa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from cshogi import *
import numpy as np
import math

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('hcpe')
#parser.add_argument('psv')
args = parser.parse_args()

hcpes = np.fromfile(args.hcpe, dtype=HuffmanCodedPosAndEval)
#psvs = np.zeros(len(hcpes), PackedSfenValue)
move_sum = 0
res = [0,0,0]
s_max = -999999
s_min = +999999
board = Board()
for hcpe in hcpes:
#for hcpe in zip(hcpes):
board.set_hcp(hcpe['hcp'])
# board.to_psfen(psv['sfen'])
score = hcpe['eval']
move16 = hcpe['bestMove16']
move_csa = move_to_csa(board.move_from_move16(move16))
#move_num = hcpe['moveNum']
result = hcpe['gameResult']
# gameResult -> 0: DRAW, 1: BLACK_WIN, 2: WHITE_WIN
#if board.turn == gameResult - 1:
# psv['game_result'] = 1
#elif board.turn == 2 - gameResult:
# psv['game_result'] = -1
move_sum += 1
res[result] += 1
if score > s_max:
s_max = score
if score < s_min:
s_min = score
print ("'move_sum=" +str(move_sum) +",result=" +str(result) + ",move=" + move_csa + ",score=" + str(score))
print (board)

if (board.turn) == 0:
sen = "+"
else:
sen = "-"
v = 1.0 / (1.0 + math.exp(-score * 0.0013226))
comment = f"v={v:.3f},"
print (sen + move_csa + "," + comment)
s_res = "%SENNICHITE"
if result == 1:
s_res = "%-ILLEGAL_ACTION"
elif result == 2:
s_res = "%+ILLEGAL_ACTION"
print (s_res)
print ("/")

#psvs.tofile(args.psv)
print ("'move_sum=" +str(move_sum) +",result=" +str(result) + ",move=" + move_csa + ",score=" + str(score))
print ("'res[0]=" +str(res[0]) +",[1]=" +str(res[1]) + ",[2]=" + str(res[2]) + ",s_max="+str(s_max)+",s_min"+str(s_min))
91 changes: 91 additions & 0 deletions learn/convert/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
GCT�̊w�K�Ɏg�p�����f�[�^�Z�b�g�����J
https://tadaoyamaoka.hatenablog.com/entry/2021/05/06/223701

python3 ./hcpe3_to_csa.py --aoba --out_v --sort_visits selfplay_gct-051.hcpe3 dummy.csa >> selfplay_gct-051.csa

����AI�̎����m�[�g�F����̕��z���w�K ���̂Q
https://tadaoyamaoka.hatenablog.com/entry/2021/03/16/235251
���t�ǖʃt�H�[�}�b�g(hcpe)
�ǖʒP�ʂɏo�͂��āA�s�v�ȋǖʁi���@�肪1�肵���Ȃ��ǖʁj�́A�o�͂��Ă��Ȃ������B
�܂��A�΋ǂ̋��ڂ����m�ɂ킩��Ȃ�


hcpe_to_csa.py �͈ȉ��̃f�B���N�g���ɒu���Ď��s

$ python3 /home/yss/shogi/dlshogi_dr2_exhi/dlshogi/utils/hcpe_to_csa.py dlshogi_with_gct-024.hcpe > dlshogi_with_gct-024.csa

��菟���A�̋ǖʂ�
%-ILLEGAL_ACTION (��肪��������w�����̂Ő�菟��)
��菟���A�̋ǖʂ�
%+ILLEGAL_ACTION (��肪��������w�����̂Ō�菟��)
�𗘗p�B







selfplay_gct-001.hcpe3
308133 ���� 3638986140 byte�ŁB
308133 * 75 = 23,109,975
2300���������x�B����75����x�ŁA15���ǖʁB




dlshogi_with_gct-001.hcpe , 356250760 byte
'move_sum=9375020,'res[0]=215305,[1]=4828653,[2]=4331062,s_max=30000,s_min-30000

339MB�� 937���ǖʁA���łɃV���b�t������Ă���B�����ɖ߂��͍̂���B
024 �܂�24�‚�
9375020 * 24 = 225,000,480 2���ǖ�

339 MB *24 = 8GB

hcpe�ɂ͋ǖ�(���)�A���̎�A�]���l(-30000 <= v <= +30000),�΋nj���(0...���������A1...��菟���A2...��菟��)
��4�‚̂݁B�萔�͂Ȃ��B
����
4828653��
4331062�s
215305��
-------------
(4828653 + 215305/2) / 9375020 = 0.5265 ��菟��0.526�ł���قǍ����Ȃ��B


suisho3kai-001.csa , 55417414 byte / 1458353 �ǖ� = 38 byte/�ǖʁB�Ֆʂ�32byte���B
842825��
615528�s
0��
-------------
1458353 �ǖ�, 0.5779 ��菟����0.57�ō����B �]���l(-31996 <= v <= +31997)�B�͈͂��Ⴄ�ȁB
24�‚� 1458353 * 24 = 35,000,472 �ǖ�

floodgate_2019-2021_r3500-001.hcpe , 11078976 byte
155969 ��
125196 �s
10387 ��
-----------
291552 �ǖ� 0.5527 �]���l(-32767 <= v <= +32767) �B������������ɔ͈͂��Ⴄ
24�‚� 291552 * 24 = 6,997,248 �ǖ�


���v
225,000,480 dlshogi_with_gct
35,000,472 suisho3kai
6,997,248 floodgate_2019-2021_r3500
--------------------
266,998,200 2��6�疜�ǖʁB2021�N5��6���ł͖�6���B

1�ǖ�100byte�Ƃ���25GB�B

CSA�ɕϊ����ēǂݍ��񂾌�A
266998238 �ǖʁB��⑽���B
76.9 * 128GB = 98GB���g�p�B


139910027 ��菟��
121668247 ��菟��
5419964 ��������
----------------------
266998238 ��菟�� (0.534)
13 changes: 11 additions & 2 deletions learn/yss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,15 @@ int shogi::LoadCSA()
ZERO_DB *pz = &zdb_one;
pz->handicap = get_handicap_from_board();
if ( pz->handicap && fGotekara==0 ) DEBUG_PRT("pz->handiacp=%d\n",pz->handicap);
#if (GCT_SELF==1)
if ( pz->handicap != 0 ) DEBUG_PRT("");
for (int y=0;y<9;y++) for (int x=0;x<9;x++) {
pz->v_init_pos.push_back(init_ban[(y+1)*16+(x+1)]);
}
for (int i=0;i<7;i++) pz->v_init_pos.push_back(mo_m[i+1]);
for (int i=0;i<7;i++) pz->v_init_pos.push_back(mo_c[i+1]);
pz->v_init_pos.push_back(fGotekara);
#endif
}

// csa形式のコメントを取り込む
Expand Down Expand Up @@ -1006,7 +1015,7 @@ int shogi::LoadCSA()
b0 = b1 = 0;
} else {
if ( getMoveFromCsaStr(&bz, &az, &tk, &nf, str)==0 ) DEBUG_PRT("");
int c = (tesuu+(pz->handicap!=0))&1;
int c = (tesuu + fGotekara)&1;
if ( is_pseudo_legalYSS((Move)pack_te(bz,az,tk,nf), (Color)(c==1) ) == false ) {
DEBUG_PRT("move Err %3d:%s\n",tesuu,str);
}
Expand Down Expand Up @@ -1167,7 +1176,7 @@ P-00AL
pz->moves = tesuu;
pz->result = ZD_DRAW;
pz->result_type = RT_NONE;
int is_gote_turn = (tesuu + (pz->handicap!=0))& 1;
int is_gote_turn = (tesuu + fGotekara) & 1;

if ( strstr(lpLine,"TORYO") ) {
if ( is_gote_turn ) {
Expand Down
6 changes: 4 additions & 2 deletions learn/yss.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@ class shogi {
void clear_init_ban();
void hirate_ban_init(int n);/*** 盤面を初期状態に戻す(駒落ち付き) ***/
int get_handicap_from_board();
void ban_saikousei(void); // 現在の持ち駒、盤面の状態(ban_init)を元に盤面状態を再構成する。
void ban_saikousei(); // 現在の持ち駒、盤面の状態(ban_init)を元に盤面状態を再構成する。
void ban_saikousei_without_kiki();
int is_hirate_ban(); // 平手の盤面か判定する
void hanten_with_hash_kifu();

Expand Down Expand Up @@ -440,7 +441,8 @@ class shogi {
int wait_and_get_new_kif(int next_weight_n);
int add_a_little_from_archive();
int make_www_samples();

void get_piece_num_diff(bool bGoteTurn, int d[]);
void sum_pwv(double z, bool bGoteTurn, double sumd[]);


// fish関連
Expand Down
Loading

0 comments on commit 1e01885

Please sign in to comment.