Skip to content

Commit

Permalink
beta2
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenpham committed May 15, 2022
1 parent 7260c7a commit 807847c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 49 deletions.
32 changes: 10 additions & 22 deletions src/bookmaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,8 @@ void BookMaker::toBook()
auto maxScore = 0;
for(auto && it : nodeMap) {
if (it.second.hitCount() >= paraRecord.minHit) {
auto isWhite = it.first.find(" w ") > 0;
for(auto && wdl : it.second.moveMap) {
auto score = scoreForPolyglot(wdl.second, isWhite);
auto score = scoreForPolyglot(wdl.second, it.second.isWhite());
maxScore = std::max(maxScore, score);
}
}
Expand All @@ -401,8 +400,6 @@ void BookMaker::toBook()
if (maxScore > 0xffff) {
polyglotScaleFactor = 0xffff / static_cast<double>(maxScore);
}

tmpPolyglotBoard = bslib::Funcs::createBoard(bslib::ChessVariant::standard);
}

uint64_t cnt = 0;
Expand Down Expand Up @@ -460,11 +457,6 @@ void BookMaker::toBook()

} else if (textBookFile.is_open()) {
textBookFile.close();

if (tmpPolyglotBoard) {
delete tmpPolyglotBoard;
tmpPolyglotBoard = nullptr;
}
}

auto elapsed = getElapse(startTime2);
Expand Down Expand Up @@ -501,8 +493,7 @@ void BookMaker::toBookPgnEpd(bslib::BoardCore* board, std::set<uint64_t>& visted
assert(board);

// check if the position is in the tree and the hit number is accepted
auto epdString = board->getEPD(true, false);
auto it = nodeMap.find(epdString);
auto it = nodeMap.find(board->hashKey);
if ( it == nodeMap.end()
|| it->second.hitCount() < paraRecord.minHit
|| vistedSet.find(board->hashKey) != vistedSet.end()) {
Expand Down Expand Up @@ -595,7 +586,7 @@ void BookMaker::savePgnEpd(bslib::BoardCore* board)
}
}

void BookMaker::add(int& fenID, const std::string& epdString, const BookNode& node)
void BookMaker::add(int& fenID, uint64_t key, const BookNode& node)
{
std::vector<MoveWDL> moveWDLVec;
for(auto && it : node.moveMap) {
Expand All @@ -616,10 +607,10 @@ void BookMaker::add(int& fenID, const std::string& epdString, const BookNode& no

switch (bookType) {
case CreateBookType::obs:
add2Db(fenID, epdString, moveWDLVec);
add2Db(fenID, node.epd, moveWDLVec);
break;
case CreateBookType::polyglot:
add2Polyglot(epdString, moveWDLVec);
add2Polyglot(key, moveWDLVec, node.isWhite());
break;

default:
Expand Down Expand Up @@ -673,11 +664,10 @@ int BookMaker::scoreForPolyglot(const WinDrawLoss& a, bool isWhite) const
return score;
}

void BookMaker::add2Polyglot(const std::string& epdString, std::vector<MoveWDL>& moveWDLVec)
void BookMaker::add2Polyglot(uint64_t hashKey, std::vector<MoveWDL>& moveWDLVec, bool isWhite)
{
assert(!epdString.empty() && !moveWDLVec.empty() && tmpPolyglotBoard);
bool isWhite = epdString.find(" w ") > 0;

assert(hashKey && !moveWDLVec.empty());

// sort by values
std::sort(moveWDLVec.begin(), moveWDLVec.end(),
[=](const WinDrawLoss& a, const WinDrawLoss& b) -> bool {
Expand All @@ -701,10 +691,8 @@ void BookMaker::add2Polyglot(const std::string& epdString, std::vector<MoveWDL>&
bslib::Move move;
int castled;
a.moveFromInt(move, castled);
tmpPolyglotBoard->newGame(epdString);

bslib::BookPolyglotItem item(tmpPolyglotBoard->key(), move.from, move.dest, move.promotion, castled, score);
assert(item.move);
bslib::BookPolyglotItem item(hashKey, move.from, move.dest, move.promotion, castled, score);
assert(item.key == hashKey && item.move);
item.convertLittleEndian();
vec.push_back(item);
}
Expand Down
10 changes: 4 additions & 6 deletions src/bookmaker.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ class BookMaker : public ocgdb::DbRead, public ocgdb::PGNRead

void threadAddGame(const std::unordered_map<char*, char*>& itemMap, const char* moveText);

void add(int& fenID, const std::string& epdString, const BookNode&);
void add2Db(int& fenID, const std::string& epdString, const std::vector<MoveWDL>&);
void add2Polyglot(const std::string& epdString, std::vector<MoveWDL>&);
void add(int& fenID, uint64_t key, const BookNode&);
void add2Db(int& fenID, const std::string& fenString, const std::vector<MoveWDL>&);
void add2Polyglot(uint64_t hashKey, std::vector<MoveWDL>&, bool isWhite);
int scoreForPolyglot(const WinDrawLoss& a, bool isWhite) const;
void setupRandonSavingPly();

Expand All @@ -65,7 +65,7 @@ class BookMaker : public ocgdb::DbRead, public ocgdb::PGNRead
SQLite::Database* bookDb = nullptr;

std::mutex nodeMapMutex;
std::map<std::string, oobs::BookNode> nodeMap;
std::map<uint64_t, oobs::BookNode> nodeMap;
std::set<std::string> lastEpdSet;
int randomSavingPly;

Expand All @@ -78,8 +78,6 @@ class BookMaker : public ocgdb::DbRead, public ocgdb::PGNRead
CreateBookType bookType = CreateBookType::obs;

std::ofstream textBookFile;

bslib::BoardCore* tmpPolyglotBoard = nullptr;
};


Expand Down
16 changes: 14 additions & 2 deletions src/bookrecords.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace oobs {

const std::string VersionString = "beta 1";
const std::string VersionString = "beta 2";
const std::string VersionDatabaseString = "0.1";

class WinDrawLoss {
Expand Down Expand Up @@ -58,14 +58,21 @@ class MoveWDL : public WinDrawLoss {

class BookNode {
public:
// map move-int to BookNodeMove
std::string epd;

// map move int to BookNodeMove
std::unordered_map<int, WinDrawLoss> moveMap;

void clear() {
epd.clear();
moveMap.clear();
}

bool isValid() const {
if (epd.empty()) {
return false;
}

for(auto && it : moveMap) {
if (it.first == 0 || !it.second.isValid()) {
return false;
Expand All @@ -83,6 +90,7 @@ class BookNode {
}

void addFrom(const BookNode& node) {
assert(epd == node.epd);
assert(node.isValid());

for(auto && m : node.moveMap) {
Expand All @@ -96,6 +104,10 @@ class BookNode {

assert(isValid());
}

bool isWhite() const {
return epd.find(" w ") != std::string::npos;
}
};


Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void print_usage()
" PGN: .pgn\n" \
" EPD: .epd (Extended Position Description)\n" \
" -elo <n> discard games with Elo under n (for creating), default is 0\n" \
" -plycount <n> discard games with ply-count under n (for creating), default is 0\n" \
" -plycount <n> discard games with ply-count under n (for creating), set 0 for all, default is 0\n" \
" -plytake <n> use first n moves to build openings, default is 20\n" \
" -plydelta <n> random range for plytake, use for PGN/EPD books, default is 1\n" \
" -hit <n> ignore nodes with number of games/hit under n, default is 1\n" \
Expand Down
25 changes: 8 additions & 17 deletions src/records.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ void ThreadRecord::resetStats()
errCnt = gameCnt = nodeCnt = 0;
}

void ThreadRecord::boardToNodes(std::map<std::string, oobs::BookNode>& nodeMap, const ParaRecord& paraRecord, const std::string& resultString)

void ThreadRecord::boardToNodes(std::map<uint64_t, oobs::BookNode>& nodeMap, const ParaRecord& paraRecord, const std::string& resultString)
{
auto n = board->getHistListSize();
if (n < paraRecord.limitLen) {
Expand All @@ -269,28 +270,18 @@ void ThreadRecord::boardToNodes(std::map<std::string, oobs::BookNode>& nodeMap,

board2->newGame(board->getStartingFen());
for(auto i = 0, e = std::min(paraRecord.ply_take + paraRecord.ply_delta + 1, n); i < e; i++) {
// auto hashKey = board2->hashKey;

auto epdString = board2->getEPD(true, false);
auto hashKey = board2->hashKey;
auto hist = board->getHistAt(i);

assert(hist.move.isValid());

auto node = &nodeMap[epdString];
if (node->moveMap.empty()) {
auto node = &nodeMap[hashKey];
if (node->epd.empty()) {
nodeCnt++;
// auto epdString = board2->getEPD(true, false);
// node->epd = epdString;
auto epdString = board2->getEPD(true, false);
node->epd = epdString;
} else {
// if (node->epd != board2->getEPD(true, false)) {
// std::cout << "node->epd : " << node->epd
// << "\nboard2->getEPD: " << board2->getEPD(true, false)
// << std::endl;
//
// std::cout << "board:\n" << board->toString() << std::endl;
// std::cout << "board2:\n" << board2->toString() << std::endl;
// }
// assert(node->epd == board2->getEPD(true, false));
assert(node->epd == board2->getEPD(true, false));
}

auto moveInt = oobs::MoveWDL::move2Int(hist.move.from, hist.move.dest, hist.move.promotion, hist.castled);
Expand Down
2 changes: 1 addition & 1 deletion src/records.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class ThreadRecord
void init();

void resetStats();
void boardToNodes(std::map<std::string, oobs::BookNode>&, const ParaRecord&, const std::string&);
void boardToNodes(std::map<uint64_t, oobs::BookNode>&, const ParaRecord&, const std::string&);

public:
bslib::BoardCore *board = nullptr, *board2 = nullptr;
Expand Down

0 comments on commit 807847c

Please sign in to comment.