diff --git a/src/Makefile b/src/Makefile index 27969174..a48d5674 100644 --- a/src/Makefile +++ b/src/Makefile @@ -47,6 +47,7 @@ OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \ eval/nnue/features/k.o \ eval/nnue/features/p.o \ eval/nnue/features/castling_right.o \ + eval/nnue/features/enpassant.o \ eval/nnue/nnue_test_command.o \ extra/sfen_packer.o \ learn/gensfen2019.o \ diff --git a/src/eval/nnue/architectures/k-p-cr-ep_256x2-32-32.h b/src/eval/nnue/architectures/k-p-cr-ep_256x2-32-32.h new file mode 100644 index 00000000..17871169 --- /dev/null +++ b/src/eval/nnue/architectures/k-p-cr-ep_256x2-32-32.h @@ -0,0 +1,38 @@ +// NNUE評価関数で用いる入力特徴量とネットワーク構造の定義 + +#include "../features/feature_set.h" +#include "../features/k.h" +#include "../features/p.h" +#include "../features/castling_right.h" +#include "../features/enpassant.h" + +#include "../layers/input_slice.h" +#include "../layers/affine_transform.h" +#include "../layers/clipped_relu.h" + +namespace Eval { + + namespace NNUE { + + // 評価関数で用いる入力特徴量 + using RawFeatures = Features::FeatureSet; + + // 変換後の入力特徴量の次元数 + constexpr IndexType kTransformedFeatureDimensions = 256; + + namespace Layers { + + // ネットワーク構造の定義 + 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/enpassant.cpp b/src/eval/nnue/features/enpassant.cpp new file mode 100644 index 00000000..523fd966 --- /dev/null +++ b/src/eval/nnue/features/enpassant.cpp @@ -0,0 +1,47 @@ +// NNUE評価関数の入力特徴量Kの定義 + +#if defined(EVAL_NNUE) + +#include "enpassant.h" +#include "index_list.h" + +namespace Eval { + + namespace NNUE { + + namespace Features { + + // 特徴量のうち、値が1であるインデックスのリストを取得する + void EnPassant::AppendActiveIndices( + const Position& pos, Color perspective, IndexList* active) { + // コンパイラの警告を回避するため、配列サイズが小さい場合は何もしない + if (RawFeatures::kMaxActiveDimensions < kMaxActiveDimensions) return; + + auto epSquare = pos.state()->epSquare; + if (epSquare == SQ_NONE) { + return; + } + + if (perspective == BLACK) { + epSquare = Inv(epSquare); + } + + auto file = file_of(epSquare); + active->push_back(file); + } + + // 特徴量のうち、一手前から値が変化したインデックスのリストを取得する + void EnPassant::AppendChangedIndices( + const Position& pos, Color perspective, + IndexList* removed, IndexList* added) { + // Not implemented. + assert(false); + } + + } // namespace Features + + } // namespace NNUE + +} // namespace Eval + +#endif // defined(EVAL_NNUE) diff --git a/src/eval/nnue/features/enpassant.h b/src/eval/nnue/features/enpassant.h new file mode 100644 index 00000000..fe827584 --- /dev/null +++ b/src/eval/nnue/features/enpassant.h @@ -0,0 +1,48 @@ +// NNUE評価関数の入力特徴量Kの定義 + +#ifndef _NNUE_FEATURES_ENPASSANT_H_ +#define _NNUE_FEATURES_ENPASSANT_H_ + +#if defined(EVAL_NNUE) + +#include "../../../evaluate.h" +#include "features_common.h" + +namespace Eval { + + namespace NNUE { + + namespace Features { + + // 特徴量K:玉の位置 + class EnPassant { + public: + // 特徴量名 + static constexpr const char* kName = "EnPassant"; + // 評価関数ファイルに埋め込むハッシュ値 + static constexpr std::uint32_t kHashValue = 0x02924F91u; + // 特徴量の次元数 + static constexpr IndexType kDimensions = 8; + // 特徴量のうち、同時に値が1となるインデックスの数の最大値 + static constexpr IndexType kMaxActiveDimensions = 1; + // 差分計算の代わりに全計算を行うタイミング + static constexpr TriggerEvent kRefreshTrigger = TriggerEvent::kAnyPieceMoved; + + // 特徴量のうち、値が1であるインデックスのリストを取得する + static void AppendActiveIndices(const Position& pos, Color perspective, + IndexList* active); + + // 特徴量のうち、一手前から値が変化したインデックスのリストを取得する + 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 3170cdab..9f21d289 100644 --- a/src/eval/nnue/nnue_architecture.h +++ b/src/eval/nnue/nnue_architecture.h @@ -7,7 +7,8 @@ // 蜈・蜉帷音蠕エ驥上→繝阪ャ繝医Ρ繝シ繧ッ讒矩縺悟ョ夂セゥ縺輔l縺溘倥ャ繝繧段nclude縺吶k //#include "architectures/k-p_256x2-32-32.h" -#include "architectures/k-p-cr_256x2-32-32.h" +//#include "architectures/k-p-cr_256x2-32-32.h" +#include "architectures/k-p-cr-ep_256x2-32-32.h" //#include "architectures/halfkp_256x2-32-32.h" namespace Eval {