eval_pin.cc
Go to the documentation of this file.
#include "osl/eval/pin.h"
using osl::MultiInt;
osl::CArray<int, osl::PTYPE_SIZE>
osl::eval::ml::SimplePin::table;
osl::CArray2d<MultiInt, osl::PTYPE_SIZE, 17 * 9>
osl::eval::ml::Pin::table;
void osl::eval::ml::
SimplePin::setUp(const Weights &weights)
{
table.fill(0);
for (size_t i = 0; i < weights.dimension(); ++i)
{
table[i] = weights.value(i);
}
}
int osl::eval::ml::
SimplePin::eval(const NumEffectState &state,
PieceMask black_mask, PieceMask white_mask) const
{
int value = 0;
while (black_mask.any())
{
const osl::Piece piece = state.pieceOf(black_mask.takeOneBit());
value += table[piece.ptype()];
}
while (white_mask.any())
{
const osl::Piece piece = state.pieceOf(white_mask.takeOneBit());
value -= table[piece.ptype()];
}
return value;
}
␌
void osl::eval::ml::
Pin::setUp(const Weights &weights,int stage)
{
for (int i = PTYPE_PIECE_MIN; i <= PTYPE_MAX; ++i)
{
for (int y = 0; y <= 16; ++y)
{
for (int x = 0; x <= 8; ++x)
{
const int distance = x * 17 + y;
table[i][distance][stage] =
weights.value((i - PTYPE_PIECE_MIN) * 17 * 9 + distance);
}
}
}
}
MultiInt osl::eval::ml::
Pin::eval(const NumEffectState &state,
PieceMask black_mask, PieceMask white_mask)
{
MultiInt value;
const Square black_king = state.kingSquare<BLACK>();
const Square white_king = state.kingSquare<WHITE>();
while (black_mask.any())
{
const osl::Piece piece = state.pieceOf(black_mask.takeOneBit());
value += table[piece.ptype()][index(black_king, piece)];
}
while (white_mask.any())
{
const osl::Piece piece = state.pieceOf(white_mask.takeOneBit());
value -= table[piece.ptype()][index(white_king, piece)];
}
return value;
}
osl::CArray<MultiInt, 80>
osl::eval::ml::PinPtypeAll::table;
osl::CArray<MultiInt, 48>
osl::eval::ml::PinPtypeAll::pawn_table;
osl::CArray<MultiInt, 560>
osl::eval::ml::PinPtypeAll::distance_table;
void osl::eval::ml::
PinPtype::setUp(const Weights &weights)
{
for (size_t i = 0; i < ONE_DIM; ++i)
{
for (int s=0; s<NStages; ++s)
table[i][s] = weights.value(i + ONE_DIM*s);
}
}
void osl::eval::ml::
PinPtypeDistance::setUp(const Weights &weights)
{
for (size_t i = 0; i < ONE_DIM; ++i)
{
for (int s=0; s<NStages; ++s)
distance_table[i][s] = weights.value(i + ONE_DIM*s);
}
}
void osl::eval::ml::
PinPtypePawnAttack::setUp(const Weights &weights)
{
for (size_t i = 0; i < ONE_DIM; ++i)
{
for (int s=0; s<NStages; ++s)
pawn_table[i][s] = weights.value(i + ONE_DIM*s);
}
}
template <osl::Player Defense>
osl::MultiInt osl::eval::ml::
PinPtypeAll::evalOne(const NumEffectState &state)
{
MultiInt result;
const Square king = state.kingSquare<Defense>();
PieceMask pin_mask = state.pin(Defense);
while (pin_mask.any())
{
const Piece piece = state.pieceOf(pin_mask.takeOneBit());
if (!state.hasEffectAt<Defense>(piece.square()))
continue;
if (king.y() == piece.square().y()) // rook h
{
result +=
(distance_table[(piece.ptype() + PTYPE_SIZE * 1) * 7 +
std::abs(king.x() - piece.square().x()) - 1] +
table[(piece.ptype() + PTYPE_SIZE * 1)]);
if (pawnAttack<Defense>(state, piece))
{
result += pawn_table[(piece.ptype() + PTYPE_SIZE * 0)];
}
}
else if (king.x() == piece.square().x())
{
if (state.hasEffectByPtypeStrict<LANCE>(alt(Defense),
piece.square())) // lance
{
result +=
(distance_table[(piece.ptype() + PTYPE_SIZE * 4) * 7 +
std::abs(king.y() - piece.square().y()) - 1] +
table[piece.ptype() + PTYPE_SIZE * 4]);
}
else // rook v
{
result +=
(distance_table[(piece.ptype() + PTYPE_SIZE * 0) * 7 +
std::abs(king.y() - piece.square().y()) - 1] +
table[piece.ptype() + PTYPE_SIZE * 0]);
}
}
else // bishop
{
if ((Defense == BLACK && piece.square().y() < king.y()) ||
(Defense == WHITE && piece.square().y() > king.y())) // u
{
result +=
(distance_table[(piece.ptype() + PTYPE_SIZE * 2) * 7 +
std::abs(king.x() - piece.square().x()) - 1] +
table[piece.ptype() + PTYPE_SIZE * 2]);
if (pawnAttack<Defense>(state, piece))
{
result += pawn_table[(piece.ptype() + PTYPE_SIZE * 1)];
}
}
else
{
result +=
(distance_table[(piece.ptype() + PTYPE_SIZE * 3) * 7 +
std::abs(king.x() - piece.square().x()) - 1] +
table[piece.ptype() + PTYPE_SIZE * 3]);
if (pawnAttack<Defense>(state, piece))
{
result += pawn_table[(piece.ptype() + PTYPE_SIZE * 2)];
}
}
}
}
return result;
}
osl::MultiInt osl::eval::ml::
PinPtypeAll::eval(const NumEffectState &state)
{
return evalOne<BLACK>(state) - evalOne<WHITE>(state);
}
␌
osl::CArray<MultiInt, 80>
osl::eval::ml::CheckShadowPtype::table;
void osl::eval::ml::
CheckShadowPtype::setUp(const Weights &weights)
{
for (size_t i = 0; i < ONE_DIM; ++i)
{
for (int s=0; s<NStages; ++s)
table[i][s] = weights.value(i + ONE_DIM*s);
}
}
template <osl::Player Defense>
osl::MultiInt osl::eval::ml::
CheckShadowPtype::evalOne(const NumEffectState &state)
{
MultiInt result;
const Square king = state.kingSquare<Defense>();
PieceMask open_mask = state.checkShadow(alt(Defense));
while (open_mask.any())
{
const Piece piece = state.pieceOf(open_mask.takeOneBit());
if (king.y() == piece.square().y()) // rook h
{
result += table[piece.ptype() + PTYPE_SIZE * 1];
}
else if (king.x() == piece.square().x())
{
if (state.hasEffectByPtypeStrict<LANCE>(alt(Defense),
piece.square())) // lance
{
result += table[piece.ptype() + PTYPE_SIZE * 4];
}
else // rook v
{
result += table[piece.ptype() + PTYPE_SIZE * 0];
}
}
else // bishop
{
if ((Defense == BLACK && piece.square().y() < king.y()) ||
(Defense == WHITE && piece.square().y() > king.y())) // u
{
result += table[piece.ptype() + PTYPE_SIZE * 2];
}
else
{
result += table[piece.ptype() + PTYPE_SIZE * 3];
}
}
}
return result;
}
osl::MultiInt osl::eval::ml::
CheckShadowPtype::eval(const NumEffectState &state)
{
return evalOne<BLACK>(state) - evalOne<WHITE>(state);
}
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
osl::CArray
Definition container.h:20
osl::NumEffectState
利きを持つ局面
Definition numEffectState.h:34
osl::NumEffectState::checkShadow
const PieceMask checkShadow(Player attack) const
attack の駒で動くと開き王手になる可能性がある集合
Definition numEffectState.h:70
osl::NumEffectState::hasEffectAt
bool hasEffectAt(Square target) const
対象とするマスにあるプレイヤーの利きがあるかどうか.
Definition numEffectState.h:324
osl::NumEffectState::pin
const PieceMask pin(Player king) const
Definition numEffectState.h:65
osl::NumEffectState::hasEffectByPtypeStrict
bool hasEffectByPtypeStrict(Player attack, Square target) const
target に ptype の利きがあるか?
Definition numEffectState.h:363
osl::PieceMask
駒番号のビットセット.
Definition pieceMask.h:21
osl::PieceMask::any
bool any() const
Definition pieceMask.h:57
osl::Piece
駒.
Definition basic_type.h:788
osl::Piece::ptype
Ptype ptype() const
Definition basic_type.h:821
osl::Piece::square
const Square square() const
Definition basic_type.h:832
osl::SimpleState::pieceOf
const Piece pieceOf(int num) const
Definition simpleState.h:76
osl::SimpleState::kingSquare
Square kingSquare() const
Definition simpleState.h:94
osl::Square
Definition basic_type.h:532
osl::Square::y
int y() const
将棋としてのY座標を返す.
Definition basic_type.h:567
osl::Square::x
int x() const
将棋としてのX座標を返す.
Definition basic_type.h:563
osl::container::PieceMask64::takeOneBit
int takeOneBit()
Definition pieceMask64.h:82
osl::eval::ml::CheckShadowPtype::evalOne
static MultiInt evalOne(const NumEffectState &state)
osl::eval::ml::CheckShadowPtype::eval
static MultiInt eval(const NumEffectState &state)
Definition eval_pin.cc:248
osl::eval::ml::CheckShadowPtype::setUp
static void setUp(const Weights &weights)
Definition eval_pin.cc:196
osl::eval::ml::CheckShadowPtype::table
static CArray< MultiInt, ONE_DIM > table
Definition pin.h:102
osl::eval::ml::CheckShadowPtype::ONE_DIM
@ ONE_DIM
Definition pin.h:95
osl::eval::ml::PinPtypeAll::pawnAttack
static bool pawnAttack(const NumEffectState &state, Piece piece)
Definition pin.h:53
osl::eval::ml::PinPtypeAll::pawn_table
static CArray< MultiInt, 48 > pawn_table
Definition pin.h:65
osl::eval::ml::PinPtypeAll::distance_table
static CArray< MultiInt, 560 > distance_table
Definition pin.h:66
osl::eval::ml::PinPtypeAll::table
static CArray< MultiInt, 80 > table
Definition pin.h:64
osl::eval::ml::PinPtypeAll::eval
static MultiInt eval(const NumEffectState &state)
Definition eval_pin.cc:186
osl::eval::ml::PinPtypeAll::evalOne
static MultiInt evalOne(const NumEffectState &state)
osl::eval::ml::PinPtypeDistance::ONE_DIM
@ ONE_DIM
Definition pin.h:79
osl::eval::ml::PinPtypeDistance::setUp
static void setUp(const Weights &weights)
Definition eval_pin.cc:95
osl::eval::ml::PinPtypePawnAttack::ONE_DIM
@ ONE_DIM
Definition pin.h:86
osl::eval::ml::PinPtypePawnAttack::setUp
static void setUp(const Weights &weights)
Definition eval_pin.cc:105
osl::eval::ml::PinPtype::setUp
static void setUp(const Weights &weights)
Definition eval_pin.cc:85
osl::eval::ml::PinPtype::ONE_DIM
@ ONE_DIM
Definition pin.h:72
osl::eval::ml::Pin::table
static CArray2d< MultiInt, PTYPE_SIZE, 17 *9 > table
Definition pin.h:36
osl::eval::ml::Pin::eval
static MultiInt eval(const NumEffectState &state, PieceMask black_mask, PieceMask white_mask)
Definition eval_pin.cc:58
osl::eval::ml::Pin::setUp
static void setUp(const Weights &weights, int stage)
Definition eval_pin.cc:41
osl::eval::ml::Pin::index
static int index(const Square king, const Piece piece)
Definition pin.h:29
osl::eval::ml::SimplePin::table
static CArray< int, PTYPE_SIZE > table
Definition pin.h:19
osl::eval::ml::SimplePin::eval
int eval(const NumEffectState &state, PieceMask black_mask, PieceMask white_mask) const
Definition eval_pin.cc:21
osl::eval::ml::SimplePin::setUp
static void setUp(const Weights &weights)
Definition eval_pin.cc:11
osl::PTYPE_PIECE_MIN
@ PTYPE_PIECE_MIN
Definition basic_type.h:104
osl::PTYPE_MAX
@ PTYPE_MAX
Definition basic_type.h:105
osl::LANCE
@ LANCE
Definition basic_type.h:96
osl::CArray2d
CArray< CArray< T, Capacity2 >, Capacity1 > CArray2d
Definition container.h:95
osl::PTYPE_SIZE
const int PTYPE_SIZE
Definition basic_type.h:107
osl::NStages
const int NStages
Definition midgame.h:11
osl::MultiInt
QuadInt MultiInt
Definition midgame.h:13
osl::WHITE
@ WHITE
Definition basic_type.h:10
osl::BLACK
@ BLACK
Definition basic_type.h:9
osl::alt
constexpr Player alt(Player player)
Definition basic_type.h:13
pin.h
osl::eval::ml::Weights
Definition weights.h:18
osl::eval::ml::Weights::dimension
size_t dimension() const
Definition weights.h:29
osl::eval::ml::Weights::value
int value(size_t index) const
Definition weights.h:27