Skip to content

Commit

Permalink
- ふかうら王に局後学習の機能追加
Browse files Browse the repository at this point in the history
  • Loading branch information
yaneurao committed Dec 16, 2024
1 parent c744962 commit dbdbe9f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
16 changes: 9 additions & 7 deletions source/engine/dlshogi-engine/UctSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -984,12 +984,14 @@ namespace dlshogi
// Policy Bookに従う。

u32 total = 0;
for (size_t i = 0; i < POLICY_BOOK_NUM; ++i)
size_t k1;
for (k1 = 0; k1 < POLICY_BOOK_NUM; ++k1)
{
if (policy_book_entry->move_freq[i].move16 == Move16::none())
if (policy_book_entry->move_freq[k1].move16 == Move16::none())
break;
total += policy_book_entry->move_freq[i].freq;
total += policy_book_entry->move_freq[k1].freq;
}
// ⇨ k1個だけ有効なmoveがあることがわかった。

// 元のPolicyの按分率
//
Expand All @@ -1008,14 +1010,14 @@ namespace dlshogi

for (ChildNumType j = 0; j < child_num; j++) {

uct_child[j].nnrate = legal_move_probabilities[j] * (1.0f - book_policy_ratio);
uct_child[j].nnrate = (1.0f - book_policy_ratio) * legal_move_probabilities[j];

// PolicyBookに出現していた指し手であれば、それで按分する。
for (size_t k = 0 ; k < POLICY_BOOK_NUM; ++k)
for (size_t k2 = 0 ; k2 < k1; ++k2)
{
if (policy_book_entry->move_freq[k].move16 == uct_child[j].move.to_move16())
if (uct_child[j].move.to_move16() == policy_book_entry->move_freq[k2].move16)
{
uct_child[j].nnrate += book_policy_ratio * policy_book_entry->move_freq[k].freq / total;
uct_child[j].nnrate += book_policy_ratio * policy_book_entry->move_freq[k2].freq / total;
break;
}
}
Expand Down
6 changes: 6 additions & 0 deletions source/engine/dlshogi-engine/YaneuraOu_dlshogi_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,13 @@ namespace dlshogi
{
return (u64)searcher.search_limits.nodes_searched;
}
}

// USIの"gameover"に対して呼び出されるハンドラ。
void gameover_handler(const std::string& cmd)
{
// dlshogiのゲームオーバーのハンドラを呼び出す。
searcher.GameOver();
}

#endif // defined(YANEURAOU_ENGINE_DEEP)
7 changes: 7 additions & 0 deletions source/engine/dlshogi-engine/dlshogi_searcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ namespace dlshogi
// 対局終了時に呼び出されるハンドラ
void DlshogiSearcher::GameOver()
{
#if defined(ENABLE_POLICY_BOOK_LEARN)
// 今回の棋譜をPolicyBookを書き出す必要がある。
auto last_position_cmd = Threads.main()->last_position_cmd_string;
auto sfen = last_position_cmd.substr(strlen("position "));
policy_book.append_sfen_to_db_bin(sfen);
#endif

}

// 投了の閾値設定
Expand Down
4 changes: 2 additions & 2 deletions source/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1496,9 +1496,9 @@ namespace SystemIO
// === BinaryWriter ===

// ファイルのopen
Tools::Result BinaryWriter::Open(const std::string& filename)
Tools::Result BinaryWriter::Open(const std::string& filename, bool append)
{
fp = fopen(filename.c_str(), "wb");
fp = fopen(filename.c_str(), append ? "ab" : "wb");
if (fp == nullptr)
return Tools::Result(Tools::ResultCode::FileOpenError);

Expand Down
3 changes: 2 additions & 1 deletion source/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,8 @@ namespace SystemIO
{
public:
// ファイルのopen
Tools::Result Open(const std::string& filename);
// append == trueで呼び出すと、このあとWriteしたものはファイル末尾に追記される。
Tools::Result Open(const std::string& filename, bool append = false);

// ptrの指すメモリからsize[byte]だけファイルに書き込む。
// ※ sizeは2GB制限があるので気をつけて。
Expand Down
8 changes: 5 additions & 3 deletions source/usi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void bench_cmd(Position& pos, istringstream& is);


// "gameover"コマンドに対するハンドラ
#if defined(USE_GAMEOVER_HANDLER)
#if defined(USE_GAMEOVER_HANDLER) || defined(YANEURAOU_ENGINE_DEEP)
void gameover_handler(const string& cmd);
#endif

Expand Down Expand Up @@ -678,10 +678,12 @@ void usi_cmdexec(Position& pos, StateListPtr& states, string& cmd)
// gameoverに対してbestmoveは返すべきではないのかも知れないが、
// それを言えばstopにだって…。

#if defined(USE_GAMEOVER_HANDLER)
// "gameover"コマンドに対するハンドラを呼び出したいのか?
#if defined(USE_GAMEOVER_HANDLER) || defined(YANEURAOU_ENGINE_DEEP)

if (token == "gameover")
// "gameover"コマンドに対するハンドラを呼び出したいのか?
gameover_handler(cmd);

#endif

// "go infinite" , "go ponder"などで思考を終えて寝てるかも知れないが、
Expand Down

0 comments on commit dbdbe9f

Please sign in to comment.