From 5c0037de7fe0bc7efece17bf0573412c079bf909 Mon Sep 17 00:00:00 2001 From: Hisayori Noda Date: Sun, 7 Jul 2019 17:02:34 +0900 Subject: [PATCH] Added the castling right feature. Added k-p-cr_256x2-32-32 architecture. --- .../nnue/architectures/k-p-cr_256x2-32-32.h | 37 ++++++++++ src/eval/nnue/features/castling_right.cpp | 73 +++++++++++++++++++ src/eval/nnue/features/castling_right.h | 48 ++++++++++++ src/eval/nnue/nnue_architecture.h | 13 +--- 4 files changed, 161 insertions(+), 10 deletions(-) create mode 100644 src/eval/nnue/architectures/k-p-cr_256x2-32-32.h create mode 100644 src/eval/nnue/features/castling_right.cpp create mode 100644 src/eval/nnue/features/castling_right.h diff --git a/src/eval/nnue/architectures/k-p-cr_256x2-32-32.h b/src/eval/nnue/architectures/k-p-cr_256x2-32-32.h new file mode 100644 index 00000000..9ce7ecf1 --- /dev/null +++ b/src/eval/nnue/architectures/k-p-cr_256x2-32-32.h @@ -0,0 +1,37 @@ +// NNUE•]‰¿ŠÖ”‚Å—p‚¢‚é“ü—Í“Á’¥—ʂƃlƒbƒgƒ[ƒN\‘¢‚Ì’è‹` + +#include "../features/feature_set.h" +#include "../features/k.h" +#include "../features/p.h" +#include "../features/castling_right.h" + +#include "../layers/input_slice.h" +#include "../layers/affine_transform.h" +#include "../layers/clipped_relu.h" + +namespace Eval { + + namespace NNUE { + + // •]‰¿ŠÖ”‚Å—p‚¢‚é“ü—Í“Á’¥—Ê + using RawFeatures = Features::FeatureSet; + + // •ÏŠ·Œã‚Ì“ü—Í“Á’¥—ʂ̎ŸŒ³” + constexpr IndexType kTransformedFeatureDimensions = 256; + + namespace Layers { + + // ƒlƒbƒgƒ[ƒN\‘¢‚Ì’è‹` + using InputLayer = InputSlice; + using HiddenLayer1 = ClippedReLU>; + using HiddenLayer2 = ClippedReLU>; + using OutputLayer = AffineTransform; + + } // namespace Layers + + using Network = Layers::OutputLayer; + + } // namespace NNUE + +} // namespace Eval diff --git a/src/eval/nnue/features/castling_right.cpp b/src/eval/nnue/features/castling_right.cpp new file mode 100644 index 00000000..30e46e23 --- /dev/null +++ b/src/eval/nnue/features/castling_right.cpp @@ -0,0 +1,73 @@ +// NNUE評価関数ã®å…¥åŠ›ç‰¹å¾´é‡Kã®å®šç¾© + +#if defined(EVAL_NNUE) + +#include "castling_right.h" +#include "index_list.h" + +namespace Eval { + + namespace NNUE { + + namespace Features { + + // 特徴é‡ã®ã†ã¡ã€å€¤ãŒ1ã§ã‚るインデックスã®ãƒªã‚¹ãƒˆã‚’å–å¾—ã™ã‚‹ + void CastlingRight::AppendActiveIndices( + const Position& pos, Color perspective, IndexList* active) { + // コンパイラã®è­¦å‘Šã‚’回é¿ã™ã‚‹ãŸã‚ã€é…列サイズãŒå°ã•ã„å ´åˆã¯ä½•ã‚‚ã—ãªã„ + if (RawFeatures::kMaxActiveDimensions < kMaxActiveDimensions) return; + + int castling_rights = pos.state()->castlingRights; + int relative_castling_rights; + if (perspective == WHITE) { + relative_castling_rights = castling_rights; + } + else { + // Invert the perspective. + relative_castling_rights = ((castling_rights & 3) << 2) + & ((castling_rights >> 2) & 3); + } + + for (int i = 0; i < kDimensions; ++i) { + if (relative_castling_rights & (i << 1)) { + active->push_back(i); + } + } + } + + // 特徴é‡ã®ã†ã¡ã€ä¸€æ‰‹å‰ã‹ã‚‰å€¤ãŒå¤‰åŒ–ã—ãŸã‚¤ãƒ³ãƒ‡ãƒƒã‚¯ã‚¹ã®ãƒªã‚¹ãƒˆã‚’å–å¾—ã™ã‚‹ + void CastlingRight::AppendChangedIndices( + const Position& pos, Color perspective, + IndexList* removed, IndexList* added) { + + int previous_castling_rights = pos.state()->previous->castlingRights; + int current_castling_rights = pos.state()->castlingRights; + int relative_previous_castling_rights; + int relative_current_castling_rights; + if (perspective == WHITE) { + relative_previous_castling_rights = previous_castling_rights; + relative_current_castling_rights = current_castling_rights; + } + else { + // Invert the perspective. + relative_previous_castling_rights = ((previous_castling_rights & 3) << 2) + & ((previous_castling_rights >> 2) & 3); + relative_current_castling_rights = ((current_castling_rights & 3) << 2) + & ((current_castling_rights >> 2) & 3); + } + + for (int i = 0; i < kDimensions; ++i) { + if ((relative_previous_castling_rights & (i << 1)) && + (relative_current_castling_rights & (i << 1)) == 0) { + removed->push_back(i); + } + } + } + + } // namespace Features + + } // namespace NNUE + +} // namespace Eval + +#endif // defined(EVAL_NNUE) diff --git a/src/eval/nnue/features/castling_right.h b/src/eval/nnue/features/castling_right.h new file mode 100644 index 00000000..1384865f --- /dev/null +++ b/src/eval/nnue/features/castling_right.h @@ -0,0 +1,48 @@ +// NNUE•]‰¿ŠÖ”‚Ì“ü—Í“Á’¥—ÊK‚Ì’è‹` + +#ifndef _NNUE_FEATURES_CASTLING_RIGHT_H_ +#define _NNUE_FEATURES_CASTLING_RIGHT_H_ + +#if defined(EVAL_NNUE) + +#include "../../../evaluate.h" +#include "features_common.h" + +namespace Eval { + + namespace NNUE { + + namespace Features { + + // “Á’¥—ÊKF‹Ê‚̈ʒu + class CastlingRight { + public: + // “Á’¥—Ê–¼ + static constexpr const char* kName = "CastlingRight"; + // •]‰¿ŠÖ”ƒtƒ@ƒCƒ‹‚É–„‚ßž‚ÞƒnƒbƒVƒ…’l + static constexpr std::uint32_t kHashValue = 0x913968AAu; + // “Á’¥—ʂ̎ŸŒ³” + static constexpr IndexType kDimensions = 4; + // “Á’¥—ʂ̂¤‚¿A“¯Žž‚É’l‚ª1‚ƂȂéƒCƒ“ƒfƒbƒNƒX‚Ì”‚ÌÅ‘å’l + static constexpr IndexType kMaxActiveDimensions = 4; + // ·•ªŒvŽZ‚Ì‘ã‚í‚è‚É‘SŒvŽZ‚ðs‚¤ƒ^ƒCƒ~ƒ“ƒO + static constexpr TriggerEvent kRefreshTrigger = TriggerEvent::kNone; + + // “Á’¥—ʂ̂¤‚¿A’l‚ª1‚Å‚ ‚éƒCƒ“ƒfƒbƒNƒX‚ÌƒŠƒXƒg‚ðŽæ“¾‚·‚é + static void AppendActiveIndices(const Position& pos, Color perspective, + IndexList* active); + + // “Á’¥—ʂ̂¤‚¿AˆêŽè‘O‚©‚ç’l‚ª•ω»‚µ‚½ƒCƒ“ƒfƒbƒNƒX‚ÌƒŠƒXƒg‚ðŽæ“¾‚·‚é + static void AppendChangedIndices(const Position& pos, Color perspective, + IndexList* removed, IndexList* added); + }; + + } // namespace Features + + } // namespace NNUE + +} // namespace Eval + +#endif // defined(EVAL_NNUE) + +#endif diff --git a/src/eval/nnue/nnue_architecture.h b/src/eval/nnue/nnue_architecture.h index 5f11a02b..3170cdab 100644 --- a/src/eval/nnue/nnue_architecture.h +++ b/src/eval/nnue/nnue_architecture.h @@ -6,16 +6,9 @@ #if defined(EVAL_NNUE) // 入力特徴é‡ã¨ãƒãƒƒãƒˆãƒ¯ãƒ¼ã‚¯æ§‹é€ ãŒå®šç¾©ã•れãŸãƒ˜ãƒƒãƒ€ã‚’includeã™ã‚‹ - -// KP256型を使ã„ãŸã„ã¨ãã¯ã€ã“れを事å‰ã«defineã™ã‚‹ã€‚ -#define EVAL_NNUE_KP256 -#if defined(EVAL_NNUE_KP256) -#include "architectures/k-p_256x2-32-32.h" -#else // #if defined(EVAL_NNUE_HALFKP256) - -// NNUE評価関数ã®ãƒ‡ãƒ•ォルトã¯ã€halfKP256 -#include "architectures/halfkp_256x2-32-32.h" -#endif +//#include "architectures/k-p_256x2-32-32.h" +#include "architectures/k-p-cr_256x2-32-32.h" +//#include "architectures/halfkp_256x2-32-32.h" namespace Eval {