Skip to content

Commit

Permalink
- UnitTestに通常プレイヤーでの対局オプションを追加した。
Browse files Browse the repository at this point in the history
例)
unittest auto_player_loop 100000 auto_player_depth 16
  • Loading branch information
yaneurao committed Oct 24, 2023
1 parent bd5a939 commit d90b61d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
58 changes: 57 additions & 1 deletion source/engine/yaneuraou-engine/yaneuraou-search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4218,6 +4218,9 @@ void init_param()

#if defined (EVAL_LEARN)

#include "../../testcmd/unit_test.h"
#include "../../position.h"

namespace Learner
{
// 学習用に、1つのスレッドからsearch,qsearch()を呼び出せるようなスタブを用意する。
Expand Down Expand Up @@ -4303,7 +4306,7 @@ namespace Learner
}

// 読み筋と評価値のペア。Learner::search(),Learner::qsearch()が返す。
typedef std::pair<Value, std::vector<Move> > ValueAndPV;
using ValueAndPV = std::pair<Value, std::vector<Move>>;

// 静止探索。
//
Expand Down Expand Up @@ -4496,6 +4499,59 @@ namespace Learner
return ValueAndPV(bestValue, pvs);
}

// UnitTest : プレイヤー同士の対局
void UnitTest(Test::UnitTester& tester)
{
// 対局回数→0ならskip
s64 auto_player_loop = tester.options["auto_player_loop"];
if (auto_player_loop)
{
Position pos;
StateInfo si;

// 平手初期化
auto hirate_init = [&] { pos.set_hirate(&si, Threads.main()); };
// 探索深さ
auto depth = int(tester.options["auto_player_depth"]);

auto section2 = tester.section("GamesOfAutoPlayer");

// seed固定乱数(再現性ある乱数)
PRNG my_rand;
StateInfo s[512];

for (s64 i = 0; i < auto_player_loop; ++i)
{
// 平手初期化
hirate_init();
bool fail = false;

// 512手目まで
for (int ply = 0; ply < 512; ++ply)
{
MoveList<LEGAL_ALL> ml(pos);

// 指し手がない == 負け == 終了
if (ml.size() == 0)
break;

// depth 6で探索
auto r = search(pos, depth);
//Move m = ml.at(size_t(my_rand.rand(ml.size()))).move;

pos.do_move(r.second[0],s[ply]);

if (!pos.pos_is_ok())
fail = true;
}

// 今回のゲームのなかでおかしいものがなかったか
tester.test(std::string("game ")+std::to_string(i+1),!fail);
}
}
}


}
#endif

Expand Down
23 changes: 21 additions & 2 deletions source/testcmd/unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

using namespace std;

#if defined(YANEURAOU_ENGINE) && defined (EVAL_LEARN)
namespace Learner {
extern void UnitTest(Test::UnitTester& unittest);
}
#endif

namespace Test
{
// --------------------
Expand Down Expand Up @@ -115,17 +121,25 @@ namespace Test
// 入力文字列を解釈
string token;
s64 random_player_loop = 0; // ランダムプレイヤーの対局回数(0を指定するとskip)
s64 auto_player_loop = 0; // 自己対局の対局回数(0を指定するとskip)
s64 auto_player_depth = 6; // 自己対局の時のdepth
while (is >> token)
{
if (token == "random_player_loop")
{
is >> random_player_loop;
}
else if (token == "auto_player_loop")
is >> auto_player_loop;
else if (token == "auto_player_depth")
is >> auto_player_depth;
}
cout << "random_player_loop : " << random_player_loop << endl;
cout << "auto_player_loop : " << auto_player_loop << endl;
cout << "auto_player_depth : " << auto_player_depth << endl;

// testerのoptionsに代入しておく。
tester.options["random_player_loop"] << USI::Option(random_player_loop, (s64)0, INT64_MAX );
tester.options["auto_player_loop" ] << USI::Option(auto_player_loop , (s64)0, INT64_MAX );
tester.options["auto_player_depth" ] << USI::Option(auto_player_depth , (s64)0, INT64_MAX );

// --- run()の実行ごとに退避させていたものを元に戻す。

Expand All @@ -135,6 +149,11 @@ namespace Test

// --- 各classに対するUnitTest

#if defined(YANEURAOU_ENGINE) && defined (EVAL_LEARN)
// 自己対局のテスト(これはデバッガで追いかけたいことがあるので、他のをすっ飛ばして最初にやって欲しい)
tester.run(Learner::UnitTest);
#endif

// Book namespace
tester.run(Book::UnitTest);

Expand Down

0 comments on commit d90b61d

Please sign in to comment.