mirror of
https://github.com/HChaZZY/Stockfish.git
synced 2025-12-24 19:16:49 +08:00
Add representation of an opening book.
This commit is contained in:
@@ -63,6 +63,7 @@ SRCS = benchmark.cpp bitbase.cpp bitboard.cpp endgame.cpp evaluate.cpp main.cpp
|
||||
learn/sfen_packer.cpp \
|
||||
learn/learn.cpp \
|
||||
learn/gensfen.cpp \
|
||||
learn/opening_book.cpp \
|
||||
learn/convert.cpp
|
||||
|
||||
OBJS = $(notdir $(SRCS:.cpp=.o))
|
||||
|
||||
43
src/learn/opening_book.cpp
Normal file
43
src/learn/opening_book.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include "opening_book.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
namespace Learner {
|
||||
|
||||
EpdOpeningBook::EpdOpeningBook(const std::string& file, PRNG& prng) :
|
||||
OpeningBook(file)
|
||||
{
|
||||
std::ifstream in(file);
|
||||
if (!in)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
std::string line;
|
||||
while (std::getline(in, line))
|
||||
{
|
||||
if (line.empty())
|
||||
continue;
|
||||
|
||||
fens.emplace_back(line);
|
||||
}
|
||||
|
||||
Algo::shuffle(fens, prng);
|
||||
}
|
||||
|
||||
static bool ends_with(const std::string& lhs, const std::string& end)
|
||||
{
|
||||
if (end.size() > lhs.size()) return false;
|
||||
|
||||
return std::equal(end.rbegin(), end.rend(), lhs.rbegin());
|
||||
}
|
||||
|
||||
std::unique_ptr<OpeningBook> open_opening_book(const std::string& filename, PRNG& prng)
|
||||
{
|
||||
if (ends_with(filename, ".epd"))
|
||||
return std::make_unique<EpdOpeningBook>(filename, prng);
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
56
src/learn/opening_book.h
Normal file
56
src/learn/opening_book.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#ifndef LEARN_OPENING_BOOK_H
|
||||
#define LEARN_OPENING_BOOK_H
|
||||
|
||||
#include "misc.h"
|
||||
#include "position.h"
|
||||
#include "thread.h"
|
||||
|
||||
#include <vector>
|
||||
#include <random>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
||||
namespace Learner {
|
||||
|
||||
struct OpeningBook {
|
||||
|
||||
const std::string& next_fen()
|
||||
{
|
||||
assert(fens.size() > 0);
|
||||
|
||||
auto& fen = fens[current_index++];
|
||||
if (current_index >= fens.size())
|
||||
current_index = 0;
|
||||
|
||||
return fen;
|
||||
}
|
||||
|
||||
std::size_t size() const { return fens.size(); }
|
||||
|
||||
const std::string& get_filename() const { return filename; }
|
||||
|
||||
protected:
|
||||
OpeningBook(const std::string& file) :
|
||||
filename(file),
|
||||
current_index(0)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
std::string filename;
|
||||
std::vector<std::string> fens;
|
||||
std::size_t current_index;
|
||||
};
|
||||
|
||||
struct EpdOpeningBook : OpeningBook {
|
||||
|
||||
EpdOpeningBook(const std::string& file, PRNG& prng);
|
||||
};
|
||||
|
||||
std::unique_ptr<OpeningBook> open_opening_book(const std::string& filename, PRNG& prng);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user