Skip to content

Commit

Permalink
Stockfish7を取り込み中
Browse files Browse the repository at this point in the history
  • Loading branch information
pushpak51094 committed Oct 8, 2016
1 parent 79644b7 commit 3f2d669
Show file tree
Hide file tree
Showing 11 changed files with 428 additions and 83 deletions.
4 changes: 4 additions & 0 deletions about_Gikou_AperyEvalMix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,7 @@
 ・元々のGainsStatsは指し手の実現確率の算出で使用されているので、そのままにしておく。


■Gikou_AperyEvalMixNoTurn_Stockfish7_20161008
「Gikou_AperyEvalMixNoTurn_BothGains_20160918」からの変更点
 ・Stockfish7の探索部を取り込み中。

1 change: 1 addition & 0 deletions gikou_ja.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ Z01_AperyEvalJoban Aperyの評価値の割合(序盤、%)
Z02_AperyEvalChuban Aperyの評価値の割合(中盤、%)
Z03_AperyEvalShuban Aperyの評価値の割合(終盤、%)
Z04_AperyEvalFolder Apery評価関数バイナリのフォルダ
Z10_UseProbabilityMinDepth 探索で実現確率を使用する深さの最小値
40 changes: 40 additions & 0 deletions src/move_probability.cc
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,46 @@ std::unordered_map<uint32_t, float> MoveProbability::ComputeProbabilities(const
return move_probabilities;
}

// この処理の中では指し手生成を行わず、引数で受け取る。
std::unordered_map<uint32_t, float> MoveProbability::ComputeProbabilities(const Position& pos,
const HistoryStats& history,
const GainsStats& gains
, ExtMove* begin
, ExtMove* end) {
// 合法手を生成する
//SimpleMoveList<kAllMoves, true> legal_moves(pos);

// 局面情報を収集する
PositionSample sample;
PositionInfo pos_info(pos, history, gains);
sample.progress = Progress::EstimateProgress(pos);

//for (ExtMove em : legal_moves) {
// sample.features.push_back(ExtractMoveFeatures(em.move, pos, pos_info));
//}
for (ExtMove* it = begin; it != end; ++it) {
sample.features.push_back(ExtractMoveFeatures(it->move, pos, pos_info));
}

// 確率を計算する
std::valarray<double> p = ComputeMoveProbabilities(sample);

// 指し手と確率を対応付ける
std::unordered_map<uint32_t, float> move_probabilities;

//for (size_t i = 0; i < legal_moves.size(); ++i) {
// move_probabilities.emplace(legal_moves[i].move.ToUint32(), p[i]);
//}

size_t i = 0;
for (ExtMove* it = begin; it != end; ++it) {
move_probabilities.emplace(it->move.ToUint32(), p[i]);
i++;
}

return move_probabilities;
}

void MoveProbability::Init() {
g_weights.resize(kNumMoveFeatures);

Expand Down
10 changes: 10 additions & 0 deletions src/move_probability.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ struct MoveProbability {
const HistoryStats& history,
const GainsStats& gains);

/**
* 指し手が指される確率を計算します.
* ・この処理の中では指し手生成を行わず、引数で受け取る。
*/
static std::unordered_map<uint32_t, float> ComputeProbabilities(const Position& pos,
const HistoryStats& history,
const GainsStats& gains
, ExtMove* begin
, ExtMove* end);

/**
* 指し手が指される確率を棋譜から学習します.
*
Expand Down
94 changes: 82 additions & 12 deletions src/movepick.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ inline bool is_greater(const ExtMove& lhs, const ExtMove& rhs) {
}

inline bool has_good_score(const ExtMove& em) {
// TODO ScoreMoves<kQuiets>()でhistory_[move]を使用しない場合はここも変える必要があるので注意
return em.score > (HistoryStats::kMax / 2);
}

Expand All @@ -77,15 +78,17 @@ MovePicker::MovePicker(const Position& pos, const HistoryStats& history,
const Array<Move, 2>& killermoves,
const Array<Move, 2>& countermoves,
const Array<Move, 2>& followupmoves,
Search::Stack* const ss)
Search::Stack* const ss
, const Search& search, bool use_probability)
: pos_(pos),
history_(history),
gains_(gains),
ss_(ss),
depth_(depth),
killermoves_(killermoves),
countermoves_(countermoves),
followupmoves_(followupmoves) {
followupmoves_(followupmoves)
, search_(search) {
assert(hash_move.IsOk());
assert(depth > kDepthZero);
assert(ss != nullptr);
Expand All @@ -98,7 +101,8 @@ MovePicker::MovePicker(const Position& pos, const HistoryStats& history,
// 指し手生成のカテゴリをセットする
if (pos.in_check()) {
stage_ = kEvasion;
} else if (depth >= 8 * kOnePly) {
//} else if (depth >= 8 * kOnePly) {
} else if (use_probability) {
stage_ = kProbSearch;
} else {
stage_ = kMainSearch;
Expand All @@ -112,11 +116,13 @@ MovePicker::MovePicker(const Position& pos, const HistoryStats& history,
}

MovePicker::MovePicker(const Position& pos, const HistoryStats& history,
const GainsStats& gains, Depth depth, Move hash_move)
const GainsStats& gains, Depth depth, Move hash_move
, const Search& search)
: pos_(pos),
history_(history),
gains_(gains),
depth_(depth) {
depth_(depth)
, search_(search) {
assert(hash_move.IsOk());
assert(depth <= kDepthZero);

Expand Down Expand Up @@ -147,11 +153,13 @@ MovePicker::MovePicker(const Position& pos, const HistoryStats& history,
}

MovePicker::MovePicker(const Position& pos, const HistoryStats& history,
const GainsStats& gains, Move hash_move)
const GainsStats& gains, Move hash_move
, const Search& search)
: pos_(pos),
history_(history),
gains_(gains),
stage_(kProbCut) {
stage_(kProbCut)
, search_(search) {
// ポインタを初期化する
cur_ = moves_.begin();
end_ = moves_.begin();
Expand Down Expand Up @@ -291,23 +299,81 @@ void MovePicker::ScoreMoves<kCaptures>() {

template<>
void MovePicker::ScoreMoves<kQuiets>() {
// Stockfish7対応
const SfHistoryStats& sf_history = *(search_.sf_history_);
const FromToStats& from_to = *(search_.from_to_);

const CounterMoveStats* cm = (ss_-1)->counterMoves;
const CounterMoveStats* fm = (ss_-2)->counterMoves;
const CounterMoveStats* f2 = (ss_-4)->counterMoves;

Color c = pos_.side_to_move();

for (ExtMove* it = moves_.begin(); it != end_; ++it) {
it->score = history_[it->move];
Move move = it->move;
Piece pc = move.piece_after_move();
Square sq = move.to();

//it->score = history_[it->move];
it->score = history_[move]
+ sf_history[pc][sq]
+ (cm ? (*cm)[pc][sq] : kScoreZero)
+ (fm ? (*fm)[pc][sq] : kScoreZero)
+ (f2 ? (*f2)[pc][sq] : kScoreZero)
+ from_to.get(c, move);

// デバッグ用
#if 0
static int cnt = 0;
cnt++;

if (cnt % 10000 == 0) {
cnt++;

std::printf("-----\n");
std::printf("thread_id_=%d\n", search_.thread_id());
std::printf("Color=%d\n", c);
std::printf("move=%s\n", move.ToSfen().c_str());
std::printf("pc=%s\n", pc.ToSfen().c_str());
std::printf("sq=%s\n", sq.ToSfen().c_str());
std::printf("history_[move] =%7d\n", history_[move]);
std::printf("sf_history[pc][sq] =%7d\n", sf_history[pc][sq]);
std::printf("(*cm)[pc][sq] =%7d\n", (cm ? (*cm)[pc][sq] : kScoreZero));
std::printf("(*fm)[pc][sq] =%7d\n", (fm ? (*fm)[pc][sq] : kScoreZero));
std::printf("(*f2)[pc][sq] =%7d\n", (f2 ? (*f2)[pc][sq] : kScoreZero));
std::printf("from_to.get(c, move)=%7d\n", from_to.get(c, move));
std::printf("it->score =%7d\n", it->score);
}
#endif
}
}

template<>
void MovePicker::ScoreMoves<kEvasions>() {
// Stockfish7対応
const SfHistoryStats& sf_history = *(search_.sf_history_);
const FromToStats& from_to = *(search_.from_to_);

Color c = pos_.side_to_move();

for (ExtMove* it = moves_.begin(); it != end_; ++it) {
Move move = it->move;
Piece pc = move.piece_after_move();

Score swap_score = Swap::Evaluate(move, pos_);
if (swap_score < kScoreZero) {
it->score = swap_score - HistoryStats::kMax; // 末尾に
//it->score = swap_score - HistoryStats::kMax; // 末尾に
it->score = swap_score - 25000; // 末尾に
} else if (!move.is_quiet()) {
it->score = GetMvvLvaScore(move) + HistoryStats::kMax; // 先頭に
//it->score = GetMvvLvaScore(move) + HistoryStats::kMax; // 先頭に
it->score = GetMvvLvaScore(move) + 25000; // 先頭に
it->score += move.is_promotion();
} else {
it->score = history_[move];
// Stockfish7対応
//it->score = history_[move];
it->score = history_[move]
+ sf_history[pc][move.to()]
+ from_to.get(c, move);
}
}
}
Expand All @@ -318,9 +384,13 @@ void MovePicker::GenerateNext() {
cur_ = moves_.begin();
end_ = GenerateMoves<kAllMoves>(pos_, cur_);
end_ = RemoveIllegalMoves(pos_, cur_, end_);

// 指し手の実現確率を計算する
//auto probabilities = MoveProbability::ComputeProbabilities(pos_, history_,
// gains_);
auto probabilities = MoveProbability::ComputeProbabilities(pos_, history_,
gains_);
gains_, cur_, end_);

// 指し手の実現確率が高い順にソートする
for (ExtMove* it = cur_; it != end_; ++it) {
double p = probabilities[it->move.ToUint32()];
Expand Down
12 changes: 9 additions & 3 deletions src/movepick.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,22 @@ class MovePicker {
const GainsStats& gains, Depth depth, Move hash_move,
const Array<Move, 2>& killermoves,
const Array<Move, 2>& countermoves,
const Array<Move, 2>& followupmoves, Search::Stack* ss);
const Array<Move, 2>& followupmoves, Search::Stack* ss
, const Search& search, bool use_probability);

/**
* 静止探索用のコンストラクタです.
*/
MovePicker(const Position& pos, const HistoryStats& history,
const GainsStats& gains, Depth depth, Move hash_move);
const GainsStats& gains, Depth depth, Move hash_move
, const Search& search);

/**
* ProbCut用のコンストラクタです.
*/
MovePicker(const Position& pos, const HistoryStats& history,
const GainsStats& gains, Move hash_move);
const GainsStats& gains, Move hash_move
, const Search& search);

/**
* 次の手(残りの手の中で、最もβカットの可能性が高い手)を返します.
Expand Down Expand Up @@ -94,6 +97,9 @@ class MovePicker {
const Array<Move, 2> followupmoves_;
Array<ExtMove, 6> killers_;
Array<ExtMove, Move::kMaxLegalMoves> moves_;

// Stockfish7対応
const Search& search_;
};

#endif /* MOVEPICK_H_ */
Loading

0 comments on commit 3f2d669

Please sign in to comment.