1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-07-11 19:49:14 +00:00

Added the castling right feature. Added k-p-cr_256x2-32-32 architecture.

This commit is contained in:
Hisayori Noda 2019-07-07 17:02:34 +09:00
parent 09e529edd3
commit 5c0037de7f
4 changed files with 161 additions and 10 deletions

View file

@ -0,0 +1,37 @@
// NNUE評価関数で用いる入力特徴量とネットワーク構造の定義
#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 {
// 評価関数で用いる入力特徴量
using RawFeatures = Features::FeatureSet<Features::K, Features::P,
Features::CastlingRight>;
// 変換後の入力特徴量の次元数
constexpr IndexType kTransformedFeatureDimensions = 256;
namespace Layers {
// ネットワーク構造の定義
using InputLayer = InputSlice<kTransformedFeatureDimensions * 2>;
using HiddenLayer1 = ClippedReLU<AffineTransform<InputLayer, 32>>;
using HiddenLayer2 = ClippedReLU<AffineTransform<HiddenLayer1, 32>>;
using OutputLayer = AffineTransform<HiddenLayer2, 1>;
} // namespace Layers
using Network = Layers::OutputLayer;
} // namespace NNUE
} // namespace Eval

View file

@ -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)

View file

@ -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 {
// 特徴量K玉の位置
class CastlingRight {
public:
// 特徴量名
static constexpr const char* kName = "CastlingRight";
// 評価関数ファイルに埋め込むハッシュ値
static constexpr std::uint32_t kHashValue = 0x913968AAu;
// 特徴量の次元数
static constexpr IndexType kDimensions = 4;
// 特徴量のうち、同時に値が1となるインデックスの数の最大値
static constexpr IndexType kMaxActiveDimensions = 4;
// 差分計算の代わりに全計算を行うタイミング
static constexpr TriggerEvent kRefreshTrigger = TriggerEvent::kNone;
// 特徴量のうち、値が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

View file

@ -6,16 +6,9 @@
#if defined(EVAL_NNUE) #if defined(EVAL_NNUE)
// 入力特徴量とネットワーク構造が定義されたヘッダをincludeする // 入力特徴量とネットワーク構造が定義されたヘッダをincludeする
//#include "architectures/k-p_256x2-32-32.h"
// KP256型を使いたいときは、これを事前にdefineする。 #include "architectures/k-p-cr_256x2-32-32.h"
#define EVAL_NNUE_KP256 //#include "architectures/halfkp_256x2-32-32.h"
#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
namespace Eval { namespace Eval {