feature_.cc
Go to the documentation of this file.
/* feature.cc
*/
#include "osl/move_probability/feature.h"
#include "osl/checkmate/checkmateIfCapture.h"
#include <iostream>
osl::move_probability::
Feature::~Feature()
{
}
␌
void osl::move_probability::PatternCommon::
updateCache(StateInfo& info)
{
for (int x=1; x<=9; ++x) {
for (int y=1; y<=9; ++y) {
const Square position(x,y);
updateCacheOne(position, info);
}
}
}
void osl::move_probability::PatternCommon::
updateCacheOne(Square position, StateInfo& info)
{
const NumEffectState& state = *info.state;
StateInfo::pattern_square_t& cache
= info.pattern_cache[position.index()];
cache.fill(-1);
const CArray<Square,2> kings = {{
state.kingSquare(BLACK),
state.kingSquare(WHITE),
}};
const Player turn = state.turn();
int cur = 0;
Piece target = state.pieceAt(position);
PtypeO ptypeo = target.ptypeO();
if (turn == WHITE)
ptypeo = altIfPiece(ptypeo);
size_t basic = ptypeOIndex(ptypeo)*SquareDim;
std::pair<Ptype,Ptype> pair;
ToEffect::supportAttack(state, position,
info.pin[turn], info.pin[alt(turn)],
turn, pair);
int effect9 = classifyEffect9(state, turn, position);
cache[cur++] = basic + pair.first;
cache[cur++] = basic + AttackBase + pair.second;
cache[cur++] = basic + EffectBase + effect9;
assert(pair.first != PTYPE_EDGE);
assert(pair.second != PTYPE_EDGE);
if (isPiece(ptypeo)) {
if (info.attack_shadow[target.number()][turn])
cache[cur++] = basic + PTYPE_EDGE;
if (info.attack_shadow[target.number()][alt(turn)])
cache[cur++] = basic + AttackBase + PTYPE_EDGE;
}
assert(basic + EffectBase + effect9 < PatternCacheSize);
int op_king_distance = abs(kings[alt(turn)].x()-position.x())
+ abs(kings[alt(turn)].y()-position.y());
if (op_king_distance == 0) // KING
op_king_distance = state.king8Info(alt(turn)).libertyCount();
else
--op_king_distance;
if (op_king_distance < OpKingSize)
cache[cur++] = basic + OpKingBase + op_king_distance;
int my_king_distance = abs(kings[turn].x()-position.x())
+ abs(kings[turn].y()-position.y());
if (my_king_distance == 0) // KING
my_king_distance = state.king8Info(turn).libertyCount();
else
--my_king_distance;
if (my_king_distance < MyKingSize)
cache[cur++] = basic + MyKingBase + my_king_distance;
// promotion
if (position.canPromote(turn))
cache[cur++] = basic + PromotionBase;
else if (position.canPromote(alt(turn)))
cache[cur++] = basic + PromotionBase + 1;
if (target.isPiece()) {
// pin or open
if (state.pinOrOpen(turn).test(target.number()))
cache[cur++] = basic + PinOpenBase + (target.owner() == turn);
if (state.pinOrOpen(alt(turn)).test(target.number()))
cache[cur++] = basic + PinOpenBase + 2 + (target.owner() == alt(turn));
// last to
if (info.history->hasLastMove()) {
if (info.history->lastMove().to() == position)
cache[cur++] = basic + LastToBase;
for (int i=1; i<4; ++i) {
if (! info.history->hasLastMove(i+1))
break;
if (info.history->lastMove(i+1).to() == position)
cache[cur++] = basic + LastToBase + i;
}
}
}
// last effect changed
if (info.history->hasLastMove()) {
if (info.changed_effects.test(position))
cache[cur++] = basic + LastEffectChangedBase;
if (target.isPiece() && info.last_add_effect.test(target.number())) {
int ptype_index = info.last_move_ptype5 - PTYPE_BASIC_MIN + 1;
cache[cur++] = basic + LastEffectChangedBase + ptype_index;
}
}
}
␌
void osl::move_probability::
BlockLong::updateCache(StateInfo& info)
{
const NumEffectState& state = *info.state;
for (int i=PtypeTraits<LANCE>::indexMin;
i<PtypeTraits<LANCE>::indexLimit; ++i) {
const Piece p = state.pieceOf(i);
if (! p.isOnBoard() || p.isPromoted())
continue;
const Direction d = p.owner() == BLACK ? U : D;
makeLongAttackOne(info, p, d);
}
for (int i=PtypeTraits<BISHOP>::indexMin;
i<PtypeTraits<BISHOP>::indexLimit; ++i) {
const Piece p = state.pieceOf(i);
if (! p.isOnBoard())
continue;
makeLongAttackOne(info, p, UL);
makeLongAttackOne(info, p, UR);
makeLongAttackOne(info, p, DL);
makeLongAttackOne(info, p, DR);
}
for (int i=PtypeTraits<ROOK>::indexMin;
i<PtypeTraits<ROOK>::indexLimit; ++i) {
const Piece p = state.pieceOf(i);
if (! p.isOnBoard())
continue;
makeLongAttackOne(info, p, L);
makeLongAttackOne(info, p, R);
makeLongAttackOne(info, p, U);
makeLongAttackOne(info, p, D);
}
}
void osl::move_probability::
BlockLong::makeLongAttackOne(StateInfo& info,
Piece piece, Direction d)
{
const NumEffectState& state = *info.state;
StateInfo::long_attack_t& out
= info.long_attack_cache[piece.number()][d];
const Player turn = state.turn();
const PtypeO attacking = (turn == BLACK)
? piece.ptypeO() : alt(piece.ptypeO());
Square attack_to = state.mobilityOf(d, piece.number());
Square attack_to2 = attack_to;
assert(! attack_to.isPieceStand());
if (attack_to.isEdge())
attack_to -= Board_Table.getOffsetForBlack(d);
else {
const Offset o = Board_Table.getOffsetForBlack(d);
attack_to2 += o; // allow edge if neighboring
if (state.pieceAt(attack_to2).isEmpty()) {
do {
attack_to2 += o;
} while (state.pieceAt(attack_to2).isEmpty());
if (state.pieceAt(attack_to2).isEdge())
attack_to2 -= o;
}
}
PtypeO attacked = state.pieceOnBoard(attack_to).ptypeO();
if (isPiece(attacked) && turn == WHITE)
attacked = alt(attacked);
int index = (longAttackIndex(attacking)*PTYPEO_SIZE
+ ptypeOIndex(attacked))*OptionSize;
out.push_back(index);
if (attack_to.isNeighboring8(state.kingSquare(turn)))
out.push_back(index + 1); // 1,King8
if (! state.hasEffectAt(turn, attack_to))
out.push_back(index + 2); // 2,HasSupport;
if (attack_to.canPromote(alt(turn)))
out.push_back(index + 3); // 3,Promotable;
Piece attacked2 = state.pieceAt(attack_to2);
if (attacked2.isOnBoardByOwner(turn)) {
out.push_back(index + 4); // 4,Shadowing;
if (! state.hasEffectAt(turn, attack_to2))
out.push_back(index + 5); // 5,
if (attack_to2.canPromote(alt(turn)))
out.push_back(index + 6); // 6,
info.attack_shadow[attacked2.number()][piece.owner()] = true;
}
if (info.threatmate_move.isNormal()) {
Square threat_at = info.threatmate_move.to();
if (threat_at == attack_to
|| (! Board_Table.getShortOffsetNotKnight(Offset32(threat_at, attack_to)).zero()
&& Board_Table.isBetween(threat_at,
piece.square(), attack_to)))
out.push_back(index + 7); // 7, threatmate block?
}
}
␌
bool osl::move_probability::
CheckmateIfCapture::hasSafeCapture(NumEffectState& state, Move move)
{
return ! checkmate::CheckmateIfCapture::effectiveAttack(state, move, 0);
}
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
checkmateIfCapture.h
osl::CArray
Definition container.h:20
osl::CArray::fill
void fill(const T_simple &value=T_simple())
Definition container.h:67
osl::FixedCapacityVector::push_back
void push_back(const T &e)
Definition container.h:204
osl::Move
圧縮していない moveの表現 .
Definition basic_type.h:1052
osl::Move::isNormal
bool isNormal() const
INVALID でも PASS でもない.
Definition basic_type.h:1088
osl::Move::to
const Square to() const
Definition basic_type.h:1132
osl::NumEffectState
利きを持つ局面
Definition numEffectState.h:34
osl::NumEffectState::king8Info
const checkmate::King8Info king8Info(Player king) const
Definition numEffectState.cc:44
osl::NumEffectState::hasEffectAt
bool hasEffectAt(Square target) const
対象とするマスにあるプレイヤーの利きがあるかどうか.
Definition numEffectState.h:324
osl::NumEffectState::mobilityOf
Square mobilityOf(Direction d, int num) const
Definition numEffectState.h:175
osl::NumEffectState::pinOrOpen
PieceMask pinOrOpen(Player king) const
Definition numEffectState.h:74
osl::Offset
座標の差分
Definition basic_type.h:430
osl::PieceMask::test
bool test(int num) const
Definition pieceMask.h:45
osl::Piece
駒.
Definition basic_type.h:788
osl::Piece::ptypeO
PtypeO ptypeO() const
Definition basic_type.h:824
osl::Piece::isPromoted
bool isPromoted() const
promoteした駒かどうかをチェックする
Definition basic_type.h:898
osl::Piece::square
const Square square() const
Definition basic_type.h:832
osl::Piece::isEmpty
bool isEmpty() const
Definition basic_type.h:913
osl::Piece::isEdge
bool isEdge() const
Definition basic_type.h:919
osl::Piece::owner
Player owner() const
Definition basic_type.h:963
osl::Piece::isPiece
bool isPiece() const
Definition basic_type.h:953
osl::Piece::isOnBoardByOwner
bool isOnBoardByOwner() const
piece がプレイヤーPの持ち物でかつボード上にある駒の場合は true.
Definition basic_type.h:852
osl::Piece::number
int number() const
Definition basic_type.h:828
osl::Piece::isOnBoard
bool isOnBoard() const
Definition basic_type.h:985
osl::SimpleState::pieceOnBoard
const Piece pieceOnBoard(Square sq) const
Definition simpleState.h:170
osl::SimpleState::turn
Player turn() const
Definition simpleState.h:220
osl::SimpleState::pieceOf
const Piece pieceOf(int num) const
Definition simpleState.h:76
osl::SimpleState::kingSquare
Square kingSquare() const
Definition simpleState.h:94
osl::SimpleState::pieceAt
const Piece pieceAt(Square sq) const
Definition simpleState.h:167
osl::Square
Definition basic_type.h:532
osl::Square::index
unsigned int index() const
Definition basic_type.h:572
osl::Square::isPieceStand
bool isPieceStand() const
Definition basic_type.h:576
osl::Square::y
int y() const
将棋としてのY座標を返す.
Definition basic_type.h:567
osl::Square::isNeighboring8
bool isNeighboring8(Square to) const
Definition basic_type.cc:202
osl::Square::canPromote
bool canPromote() const
Definition basic_type.h:659
osl::Square::isEdge
bool isEdge() const
onBoardから8近傍のオフセットを足した点がedgeかどうかの判定 そこそこ速くなった．
Definition basic_type.h:591
osl::Square::x
int x() const
将棋としてのX座標を返す.
Definition basic_type.h:563
osl::checkmate::King8Info::libertyCount
unsigned int libertyCount() const
libertyの数
Definition king8Info.h:82
osl::container::BoardMask::test
bool test(unsigned int i) const
Definition boardMask.h:52
osl::container::MoveStack::hasLastMove
bool hasLastMove(size_t last=1) const
Definition moveStack.h:27
osl::container::MoveStack::lastMove
const Move lastMove(size_t last=1) const
Definition moveStack.h:28
osl::move_probability::BlockLong::makeLongAttackOne
static void makeLongAttackOne(StateInfo &info, Piece piece, Direction d)
Definition feature_.cc:147
osl::move_probability::BlockLong::updateCache
static void updateCache(StateInfo &)
Definition feature_.cc:113
osl::move_probability::BlockLong::longAttackIndex
static int longAttackIndex(osl::PtypeO ptypeo)
Definition move_probability/feature.h:713
osl::move_probability::BlockLong::OptionSize
@ OptionSize
Definition move_probability/feature.h:706
osl::move_probability::CheckmateIfCapture::hasSafeCapture
static bool hasSafeCapture(NumEffectState &state, Move)
Definition feature_.cc:205
osl::move_probability::Feature::~Feature
virtual ~Feature()
Definition feature_.cc:8
osl::move_probability::Feature::classifyEffect9
static int classifyEffect9(const NumEffectState &state, Player player, Square to)
Definition move_probability/feature.h:30
osl::move_probability::PatternCommon::OpKingSize
@ OpKingSize
Definition move_probability/feature.h:477
osl::move_probability::PatternCommon::MyKingSize
@ MyKingSize
Definition move_probability/feature.h:478
osl::move_probability::PatternCommon::LastToBase
@ LastToBase
Definition move_probability/feature.h:481
osl::move_probability::PatternCommon::OpKingBase
@ OpKingBase
Definition move_probability/feature.h:477
osl::move_probability::PatternCommon::SquareDim
@ SquareDim
Definition move_probability/feature.h:483
osl::move_probability::PatternCommon::MyKingBase
@ MyKingBase
Definition move_probability/feature.h:478
osl::move_probability::PatternCommon::AttackBase
@ AttackBase
Definition move_probability/feature.h:475
osl::move_probability::PatternCommon::LastEffectChangedBase
@ LastEffectChangedBase
Definition move_probability/feature.h:482
osl::move_probability::PatternCommon::PinOpenBase
@ PinOpenBase
Definition move_probability/feature.h:480
osl::move_probability::PatternCommon::PatternCacheSize
@ PatternCacheSize
Definition move_probability/feature.h:484
osl::move_probability::PatternCommon::EffectBase
@ EffectBase
Definition move_probability/feature.h:476
osl::move_probability::PatternCommon::PromotionBase
@ PromotionBase
Definition move_probability/feature.h:479
osl::move_probability::PatternCommon::updateCacheOne
static void updateCacheOne(Square target, StateInfo &info)
Definition feature_.cc:25
osl::move_probability::PatternCommon::updateCache
static void updateCache(StateInfo &info)
Definition feature_.cc:14
osl::move_probability::ToEffect::supportAttack
static void supportAttack(const NumEffectState &state, Square to, const PieceMask &my_pin, const PieceMask &op_pin, Player turn, std::pair< Ptype, Ptype > &out)
Definition move_probability/feature.h:366
feature.h
osl::PTYPE_EDGE
@ PTYPE_EDGE
Definition basic_type.h:86
osl::PTYPE_BASIC_MIN
@ PTYPE_BASIC_MIN
Definition basic_type.h:103
osl::PTYPEO_SIZE
const int PTYPEO_SIZE
Definition basic_type.h:308
osl::Board_Table
const BoardTable Board_Table
Definition tables.cc:95
osl::Direction
Direction
Definition basic_type.h:310
osl::R
@ R
Definition basic_type.h:317
osl::D
@ D
Definition basic_type.h:319
osl::UL
@ UL
Definition basic_type.h:313
osl::DR
@ DR
Definition basic_type.h:320
osl::U
@ U
Definition basic_type.h:314
osl::L
@ L
Definition basic_type.h:316
osl::UR
@ UR
Definition basic_type.h:315
osl::DL
@ DL
Definition basic_type.h:318
osl::ptypeOIndex
unsigned int ptypeOIndex(PtypeO ptypeo)
Definition basic_type.h:205
osl::Offset32
Offset32Base< 8, 9 > Offset32
Definition offset32.h:63
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::isPiece
constexpr bool isPiece(Ptype ptype)
ptypeが空白やEDGEでないかのチェック
Definition basic_type.h:120
osl::PtypeO
PtypeO
Player + Ptype [-15, 15] PtypeO の O は Owner の O.
Definition basic_type.h:199
osl::alt
constexpr Player alt(Player player)
Definition basic_type.h:13
osl::altIfPiece
PtypeO altIfPiece(PtypeO ptypeO)
Pieceの時にはowner を反転する
Definition basic_type.h:281
osl::PtypeTraits
Definition ptypeTraits.h:12
osl::checkmate::CheckmateIfCapture::effectiveAttack
static bool effectiveAttack(NumEffectState &state, Move move, int depth)
move を指した後，alt(move.player())が取ると詰みかどうか.
Definition checkmateIfCapture.cc:120
osl::move_probability::StateInfo
Definition stateInfo.h:21
osl::move_probability::StateInfo::state
const NumEffectState * state
Definition stateInfo.h:22
osl::move_probability::StateInfo::attack_shadow
CArray2d< bool, 40, 2 > attack_shadow
Definition stateInfo.h:31
osl::move_probability::StateInfo::last_move_ptype5
Ptype last_move_ptype5
Definition stateInfo.h:33
osl::move_probability::StateInfo::history
const MoveStack * history
Definition stateInfo.h:23
osl::move_probability::StateInfo::threatmate_move
Move threatmate_move
Definition stateInfo.h:35
osl::move_probability::StateInfo::pattern_square_t
CArray< int, 16 > pattern_square_t
Definition stateInfo.h:29
osl::move_probability::StateInfo::long_attack_t
FixedCapacityVector< int, 8 > long_attack_t
Definition stateInfo.h:27
osl::move_probability::StateInfo::changed_effects
BoardMask changed_effects
Definition stateInfo.h:41
osl::move_probability::StateInfo::pattern_cache
CArray< pattern_square_t, Square::SIZE > pattern_cache
Definition stateInfo.h:30
osl::move_probability::StateInfo::last_add_effect
PieceMask last_add_effect
Definition stateInfo.h:32
osl::move_probability::StateInfo::pin
CArray< PieceMask, 2 > pin
Definition stateInfo.h:34
osl::move_probability::StateInfo::long_attack_cache
CArray2d< long_attack_t, 40, 8 > long_attack_cache
Definition stateInfo.h:28