Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cpp/command/runtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ int MainCmds::runtests(const vector<string>& args) {
Tests::runBoardStressTest();

Tests::runSgfTests();
Tests::runTrainingDataResultTests();
Tests::runBasicSymmetryTests();
Tests::runBoardSymmetryTests();
Tests::runSymmetryDifferenceTests();
Expand Down
48 changes: 41 additions & 7 deletions cpp/command/writetrainingdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,22 @@
#include "../program/play.h"
#include "../command/commandline.h"
#include "../core/test.h"
#include "../tests/tests.h"
#include "../main.h"

#include <chrono>
#include <csignal>

using namespace std;

// Called after normalizing the result; other result forms have their own handling.
static bool parseSgfResultWithoutScore(const string& s, Player& winner) {
if(s != "b+" && s != "w+")
return false;
winner = s == "b+" ? P_BLACK : P_WHITE;
return true;
}

static ValueTargets makeForcedWinnerValueTarget(Player winner) {
ValueTargets targets;
if(winner == C_EMPTY) {
Expand All @@ -41,6 +50,37 @@ static ValueTargets makeForcedWinnerValueTarget(Player winner) {
return targets;
}

void Tests::runTrainingDataResultTests() {
cout << "Running training data result tests" << endl;
struct TestCase {
const char* result;
Player winner;
};
const TestCase cases[] = {
{"B+",P_BLACK}, {"W+",P_WHITE}, {"b+",P_BLACK}, {"w+",P_WHITE},
{" B+ ",P_BLACK}, {" w+ ",P_WHITE}
};
for(const TestCase& testCase: cases) {
std::unique_ptr<Sgf> sgf = Sgf::parse(string("(;GM[1]FF[4]SZ[9]RE[") + testCase.result + "])");
string result = Global::trim(Global::toLower(sgf->getRootPropertyWithDefault("RE", "")));
Player winner = C_EMPTY;
testAssert(parseSgfResultWithoutScore(result,winner));
testAssert(winner == testCase.winner);
ValueTargets targets = makeForcedWinnerValueTarget(winner);
// Training values are stored from White's perspective before per-row conversion.
testAssert(targets.win == (testCase.winner == P_WHITE ? 1.0f : 0.0f));
testAssert(targets.loss == (testCase.winner == P_BLACK ? 1.0f : 0.0f));
testAssert(targets.noResult == 0.0f);
testAssert(targets.score == (testCase.winner == P_WHITE ? 0.5f : -0.5f));
testAssert(!targets.hasLead);
}
for(const char* result: {"", "?", "draw", "b+r", "w+t", "w+3.5"}) {
Player winner = C_EMPTY;
testAssert(!parseSgfResultWithoutScore(result,winner));
testAssert(winner == C_EMPTY);
}
}

static void getNNEval(
const Board& board,
const BoardHistory& hist,
Expand Down Expand Up @@ -1575,15 +1615,9 @@ int MainCmds::writetrainingdata(const vector<string>& args) {
return;
}
}
else if(s == "b+") {
sgfGameEnded = true;
sgfGameEndedButRecordIncomplete = true;
sgfGameWinner = P_BLACK;
}
else if(s == "w+") {
else if(parseSgfResultWithoutScore(s,sgfGameWinner)) {
sgfGameEnded = true;
sgfGameEndedButRecordIncomplete = true;
sgfGameWinner = P_BLACK;
}
else {
logger.write("Game ended with unknown result " + fileName + " result " + sgfResult);
Expand Down
3 changes: 3 additions & 0 deletions cpp/tests/tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ namespace Tests {
void runSgfTests();
void runSgfFileTests();

//command/writetrainingdata.cpp
void runTrainingDataResultTests();

//testnninputs.cpp
void runNNInputsV3V4Tests();
void runExcludeTerritoryAtariNNInputsTests();
Expand Down