piecePairKing.cc
Go to the documentation of this file.
/* piecePairKing.cc
*/
#include "osl/eval/piecePairKing.h"
#include "osl/eval/weights.h"
#include <cstdint>
osl::CArray<int16_t, 1488375> osl::eval::ml::PiecePairKing::table;
void osl::eval::ml::
PiecePairKing::setUp(const Weights &weights)
{
for (size_t i=0; i<weights.dimension(); ++i)
table[i] = weights.value(i);
for (int x=1; x<=5; ++x)
{
for (int y=1; y<=3; ++y)
{
bool flipx;
const int king = indexKing(WHITE, Square(x,y), flipx);
for (int i=0; i<45*7; ++i)
for (int j=i+1; j<45*7; ++j)
table[composeIndex(king, j, i)] = table[composeIndex(king, i, j)];
}
}
}
osl::CArray<int,2> osl::eval::ml::
PiecePairKing::eval(const NumEffectState& state)
{
CArray<int,2> ret;
ret[BLACK] = evalOne<BLACK>(state);
ret[WHITE] = evalOne<WHITE>(state);
return ret;
}
template <osl::Player King>
int osl::eval::ml::
PiecePairKing::evalOne(const NumEffectState& state)
{
FixedCapacityVector<Piece,38> pieces;
if (state.template kingSquare<King>().template squareForBlack<King>().y() < 7)
return 0;
PieceMask bitset = state.piecesOnBoard(King) & ~state.promotedPieces();
bitset.clearBit<KING>();
while (! bitset.none())
{
const Piece p = state.pieceOf(bitset.takeOneBit());
if (p.square().squareForBlack<King>().y() >= 5)
pieces.push_back(p);
}
int sum = 0;
bool flipx;
const int index_king = indexKing(King, state.kingSquare(King), flipx);
if (flipx)
{
for (size_t i=0; i<pieces.size(); ++i)
{
const unsigned int i0 = indexPiece<true>(King, pieces[i].square(), pieces[i].ptype());
for (size_t j=i+1; j<pieces.size(); ++j)
{
const unsigned int i1 = indexPiece<true>(King, pieces[j].square(), pieces[j].ptype());
const unsigned int index = composeIndex(index_king, i0, i1);
sum += table[index];
}
}
}
else
{
for (size_t i=0; i<pieces.size(); ++i)
{
const unsigned int i0 = indexPiece<false>(King, pieces[i].square(), pieces[i].ptype());
for (size_t j=i+1; j<pieces.size(); ++j)
{
const unsigned int i1 = indexPiece<false>(King, pieces[j].square(), pieces[j].ptype());
const unsigned int index = composeIndex(index_king, i0, i1);
sum += table[index];
}
}
}
return (King == BLACK) ? sum : -sum;
}
template <osl::Player King>
int osl::eval::ml::
PiecePairKing::add(const NumEffectState& state, Square to, Ptype ptype)
{
const Square king = state.kingSquare(King);
bool flipx;
const int index_king = indexKing(King, king, flipx);
int sum = 0;
PieceMask bitset = state.piecesOnBoard(King) & ~state.promotedPieces();
bitset.clearBit<KING>();
unsigned int i0;
if (flipx)
{
i0 = indexPiece<true>(King, to, ptype);
while (! bitset.none())
{
const Piece p = state.pieceOf(bitset.takeOneBit());
if (p.square().squareForBlack(King).y() < 5)
continue;
const unsigned int i1 = indexPiece<true>(King, p.square(), p.ptype());
const unsigned int index = composeIndex(index_king, i0, i1);
sum += table[index];
}
}
else
{
i0 = indexPiece<false>(King, to, ptype);
while (! bitset.none())
{
const Piece p = state.pieceOf(bitset.takeOneBit());
if (p.square().squareForBlack(King).y() < 5)
continue;
const unsigned int i1 = indexPiece<false>(King, p.square(), p.ptype());
const unsigned int index = composeIndex(index_king, i0, i1);
sum += table[index];
}
}
sum -= table[composeIndex(index_king, i0, i0)];
return (King == BLACK) ? sum : -sum;
}
template <osl::Player King>
int osl::eval::ml::
PiecePairKing::sub(const NumEffectState& state, Square from, Ptype ptype)
{
const Square king = state.kingSquare(King);
bool flipx;
const int index_king = indexKing(King, king, flipx);
int sum = 0;
PieceMask bitset = state.piecesOnBoard(King) & ~state.promotedPieces();
bitset.clearBit<KING>();
if (flipx)
{
const unsigned int i0 = indexPiece<true>(King, from, ptype);
while (! bitset.none())
{
const Piece p = state.pieceOf(bitset.takeOneBit());
if (p.square().squareForBlack(King).y() < 5)
continue;
const unsigned int i1 = indexPiece<true>(King, p.square(), p.ptype());
const unsigned int index = composeIndex(index_king, i0, i1);
sum -= table[index];
}
}
else
{
const unsigned int i0 = indexPiece<false>(King, from, ptype);
while (! bitset.none())
{
const Piece p = state.pieceOf(bitset.takeOneBit());
if (p.square().squareForBlack(King).y() < 5)
continue;
const unsigned int i1 = indexPiece<false>(King, p.square(), p.ptype());
const unsigned int index = composeIndex(index_king, i0, i1);
sum -= table[index];
}
}
return (King == BLACK) ? sum : -sum;
}
template <osl::Player King>
int osl::eval::ml::
PiecePairKing::addSub(const NumEffectState& state, Square to, Ptype ptype, Square from)
{
const Square king = state.kingSquare(King);
bool flipx;
const int index_king = indexKing(King, king, flipx);
unsigned int i0, s0;
int sum = 0;
PieceMask bitset = state.piecesOnBoard(King) & ~state.promotedPieces();
bitset.clearBit<KING>();
FixedCapacityVector<Piece,38> pieces;
if (flipx)
{
i0 = indexPiece<true>(King, to, ptype);
s0 = indexPiece<true>(King, from, ptype);
while (! bitset.none())
{
const Piece p = state.pieceOf(bitset.takeOneBit());
if (p.square().squareForBlack(King).y() < 5)
continue;
const unsigned int i1 = indexPiece<true>(King, p.square(), p.ptype());
const unsigned int index = composeIndex(index_king, i0, i1);
sum += table[index];
const unsigned int sub_index = composeIndex(index_king, s0, i1);
sum -= table[sub_index];
}
}
else
{
i0 = indexPiece<false>(King, to, ptype);
s0 = indexPiece<false>(King, from, ptype);
while (! bitset.none())
{
const Piece p = state.pieceOf(bitset.takeOneBit());
if (p.square().squareForBlack(King).y() < 5)
continue;
const unsigned int i1 = indexPiece<false>(King, p.square(), p.ptype());
const unsigned int index = composeIndex(index_king, i0, i1);
sum += table[index];
const unsigned int sub_index = composeIndex(index_king, s0, i1);
sum -= table[sub_index];
}
}
sum -= table[composeIndex(index_king, i0, i0)];
sum += table[composeIndex(index_king, s0, i0)];
return (King == BLACK) ? sum : -sum;
}
template <osl::Player P>
void osl::eval::ml::
PiecePairKing::evalWithUpdateBang(const NumEffectState& state, Move moved, CArray<int,2>& last_value)
{
assert(P == moved.player());
if (moved.isPass())
return;
const Player Opponent = alt(P);
const Ptype captured = moved.capturePtype();
bool adjust_capture = (captured != PTYPE_EMPTY)
&& ! isPromoted(captured)
&& moved.to().squareForBlack(alt(P)).y() >= 5;
if (adjust_capture)
{
const Square roking = state.kingSquare(alt(P)).squareForBlack(alt(P));
adjust_capture = roking.y() >= 7;
}
if (moved.ptype() == KING)
{
last_value[P] = evalOne<P>(state);
if (adjust_capture)
last_value[alt(P)] += sub<Opponent>(state, moved.to(), captured);
return;
}
const Square rking = state.kingSquare(P).squareForBlack(P);
if (rking.y() < 7)
{
if (adjust_capture)
last_value[alt(P)] += sub<Opponent>(state, moved.to(), captured);
return;
}
const Square rto = moved.to().squareForBlack(P);
if (moved.isDrop())
{
if (rto.y() >= 5)
last_value[P] += add<P>(state, moved.to(), moved.ptype());
return;
}
const Square rfrom = moved.from().squareForBlack(P);
if (adjust_capture)
last_value[alt(P)] += sub<Opponent>(state, moved.to(), captured);
if (isPromoted(moved.oldPtype()))
return;
if (rfrom.y() < 5)
{
if (rto.y() >= 5 && ! isPromoted(moved.ptype()))
last_value[P] += add<P>(state, moved.to(), moved.ptype());
return;
}
if (rto.y() < 5 || isPromoted(moved.ptype()))
last_value[P] += sub<P>(state, moved.from(), moved.oldPtype());
else
last_value[P] += addSub<P>(state, moved.to(), moved.ptype(), moved.from());
}
namespace osl
{
namespace eval
{
namespace ml
{
template void PiecePairKing::evalWithUpdateBang<BLACK>(const NumEffectState&, Move, CArray<int,2>&);
template void PiecePairKing::evalWithUpdateBang<WHITE>(const NumEffectState&, Move, CArray<int,2>&);
}
}
}
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
osl::CArray
Definition container.h:20
osl::FixedCapacityVector
Definition container.h:137
osl::FixedCapacityVector::size
size_t size() const
Definition container.h:243
osl::FixedCapacityVector::push_back
void push_back(const T &e)
Definition container.h:204
osl::Move
圧縮していない moveの表現 .
Definition basic_type.h:1052
osl::Move::ptype
Ptype ptype() const
Definition basic_type.h:1155
osl::Move::player
Player player() const
Definition basic_type.h:1195
osl::Move::isDrop
bool isDrop() const
Definition basic_type.h:1150
osl::Move::isPass
bool isPass() const
Definition basic_type.h:1092
osl::Move::capturePtype
Ptype capturePtype() const
Definition basic_type.h:1180
osl::Move::oldPtype
Ptype oldPtype() const
移動前のPtype, i.e., 成る手だった場合成る前
Definition basic_type.h:1174
osl::Move::to
const Square to() const
Definition basic_type.h:1132
osl::Move::from
const Square from() const
Definition basic_type.h:1125
osl::NumEffectState
利きを持つ局面
Definition numEffectState.h:34
osl::NumEffectState::piecesOnBoard
const PieceMask & piecesOnBoard(Player p) const
Definition numEffectState.h:63
osl::PieceMask
駒番号のビットセット.
Definition pieceMask.h:21
osl::PieceMask::clearBit
void clearBit()
unpromote(PTYPE) の駒のbit を消す
Definition pieceMask.h:74
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::squareForBlack
const Square squareForBlack(Player player) const
Definition basic_type.h:598
osl::container::PieceMask64::none
bool none() const
Definition pieceMask64.h:57
osl::container::PieceMask64::takeOneBit
int takeOneBit()
Definition pieceMask64.h:82
osl::eval::ml::PiecePairKing::add
static int add(const NumEffectState &state, Square to, Ptype ptype)
Definition piecePairKing.cc:87
osl::eval::ml::PiecePairKing::setUp
static void setUp(const Weights &weights)
Definition piecePairKing.cc:10
osl::eval::ml::PiecePairKing::addSub
static int addSub(const NumEffectState &state, Square to, Ptype ptype, Square from)
Definition piecePairKing.cc:165
osl::eval::ml::PiecePairKing::evalWithUpdateBang
static void evalWithUpdateBang(const NumEffectState &state, Move moved, CArray< int, 2 > &last_value)
Definition piecePairKing.cc:214
osl::eval::ml::PiecePairKing::composeIndex
static int composeIndex(int king, int i0, int i1)
Definition piecePairKing.h:36
osl::eval::ml::PiecePairKing::evalOne
static int evalOne(const NumEffectState &)
Definition piecePairKing.cc:39
osl::eval::ml::PiecePairKing::table
static osl::CArray< int16_t, ONE_DIM > table
Definition piecePairKing.h:69
osl::eval::ml::PiecePairKing::sub
static int sub(const NumEffectState &state, Square from, Ptype ptype)
Definition piecePairKing.cc:127
osl::eval::ml::PiecePairKing::indexKing
static int indexKing(Player owner, Square king, bool &flipx)
Definition piecePairKing.h:44
osl::eval::ml::PiecePairKing::eval
static CArray< int, 2 > eval(const NumEffectState &)
Definition piecePairKing.cc:29
osl::eval::ml::PiecePairKing::indexPiece
static int indexPiece(Player owner, Square position, Ptype ptype)
Definition piecePairKing.h:59
osl::eval::ml
Definition eval_pieceStand.cc:470
osl::eval
Definition eval_pieceStand.cc:468
osl
Definition additionalEffect.h:6
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
osl::KING
@ KING
Definition basic_type.h:93
osl::PTYPE_EMPTY
@ PTYPE_EMPTY
Definition basic_type.h:85
osl::isPromoted
bool isPromoted(Ptype ptype)
ptypeがpromote後の型かどうかのチェック
Definition basic_type.h:137
osl::Player
Player
Definition basic_type.h:8
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
osl::captured
PtypeO captured(PtypeO ptypeO)
unpromoteすると共に，ownerを反転する．
Definition basic_type.h:264
piecePairKing.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
weights.h