stateInfo.cc
Go to the documentation of this file.
/* stateInfo.cc
*/
#include "osl/move_probability/stateInfo.h"
#include "osl/move_probability/feature.h"
#include "osl/checkmate/fixedDepthSearcher.h"
#include "osl/checkmate/immediateCheckmateTable.h"
#include "osl/effect_util/sendOffSquare.h"
#include "osl/book/bookInMemory.h"
#include "osl/hashKey.h"
#include "osl/bits/ptypeTraits.h"
#include <iostream>
void osl::move_probability::
StateInfo::clearOldCache()
{
const osl::Player turn = state->turn();
threatmate_move = Move();
attack_shadow.fill(false); // need everytime
pin[BLACK] = state->pin(BLACK);
pin[WHITE] = state->pin(WHITE);
move_candidate_exists[BLACK] = king8Info(BLACK).hasMoveCandidate<WHITE>(*state);
move_candidate_exists[WHITE] = king8Info(WHITE).hasMoveCandidate<BLACK>(*state);
checkmate_defender[BLACK] = findCheckmateDefender(*state, BLACK);
checkmate_defender[WHITE] = findCheckmateDefender(*state, WHITE);
sendoffs.clear();
if (state->hasChangedEffects())
changed_effects = state->changedEffects(alt(turn));
else
changed_effects.invalidate();
threatened[turn] = state->inCheck(turn)
? state->kingPiece(turn) : state->findThreatenedPiece(turn);
threatened[alt(turn)] = state->findThreatenedPiece(alt(turn));
{
possible_threatmate_ptype
= checkmate::Immediate_Checkmate_Table.dropPtypeMask(king8Info(alt(state->turn())));
const Square king = state->kingSquare(alt(state->turn()));
const CArray<Direction,2> directions = {{ UUL, UUR }};
for (Direction d: directions) {
Square knight_attack = king - Board_Table.getOffset(state->turn(), d);
if (state->pieceAt(knight_attack).isEmpty()
&& ! state->hasEffectAt(alt(state->turn()), knight_attack)) {
possible_threatmate_ptype |= (1<<(KNIGHT-PTYPE_BASIC_MIN));
break;
}
}
for (int p=PTYPE_BASIC_MIN; p<=PTYPE_MAX; ++p) {
if (possible_threatmate_ptype & (1<<p))
if (state->hasPieceOnStand(state->turn(), static_cast<Ptype>(p)))
p &= ~(1<<p);
}
}
bookmove.fill(Move());
}
void osl::move_probability::
StateInfo::finishUpdate()
{
copy.copyFrom(*state);
const osl::Player turn = state->turn();
const Square king = state->kingSquare(alt(turn));
effect_util::SendOffSquare::find(turn, *state, king, sendoffs);
updatePinnedGenerals(BLACK);
updatePinnedGenerals(WHITE);
makePinOfLongPieces();
for (int d=SHORT8_DIRECTION_MIN; d<=SHORT8_DIRECTION_MAX; ++d) {
Piece p = state->pieceAt(state->kingMobilityAbs(alt(turn), (Direction)d));
if (p.isOnBoardByOwner(alt(turn)))
king8_long_pieces.push_back(p);
}
BlockLong::updateCache(*this);
if (! history->hasLastMove() || !history->lastMove().isNormal()) {
last_move_ptype5 = PTYPE_EMPTY;
last_add_effect = PieceMask();
} else {
last_move_ptype5 = unpromote(history->lastMove().ptype());
if (last_move_ptype5 == SILVER)
last_move_ptype5 = GOLD;
else if (last_move_ptype5 == KNIGHT)
last_move_ptype5 = LANCE;
else if (isMajor(last_move_ptype5))
last_move_ptype5 = KNIGHT;
last_add_effect = state->effectedMask(alt(turn))
& state->effectedChanged(alt(turn));
}
PatternCommon::updateCache(*this); // depends on attack_shadow, last_add_effect
{
MoveVector all;
static const BookInMemory& book = BookInMemory::instance();
book.find(HashKey(*state), all);
for (size_t i=0; i<bookmove.size(); ++i)
if (all.size() > i)
bookmove[i] = all[i];
}
dirty = false;
}
void osl::move_probability::
StateInfo::makePinOfLongPieces()
{
using namespace osl;
if (state->inCheck())
return;
for (int i=0; i<40; ++i) {
const Piece p = state->pieceOf(i);
if (!p.isOnBoard() || p.ptype() == KING)
continue;
CArray<mask_t,2> long_effect = {{
state->longEffectAt(p.square(), BLACK),
state->longEffectAt(p.square(), WHITE),
}};
if (long_effect[0].none() || long_effect[1].none()) continue;
CArray<Piece,2> attack_piece;
{
attack_piece[0] = state->findLongAttackAt(p, U);
attack_piece[1] = state->findLongAttackAt(p, D);
if (attack_piece[0].isPiece() && attack_piece[1].isPiece()
&& attack_piece[0].owner() != attack_piece[1].owner()) {
pin_by_opposing_sliders.push_back(p);
continue;
}
}
if ((long_effect[0] & mask_t::makeDirect(PtypeFuns<ROOK>::indexMask)).any()
&& (long_effect[0] & mask_t::makeDirect(PtypeFuns<ROOK>::indexMask)).any())
{
attack_piece[0] = state->findLongAttackAt(p, L);
attack_piece[1] = state->findLongAttackAt(p, R);
if (attack_piece[0].isPiece() && attack_piece[1].isPiece()
&& attack_piece[0].owner() != attack_piece[1].owner()) {
pin_by_opposing_sliders.push_back(p);
continue;
}
}
if ((long_effect[0] & mask_t::makeDirect(PtypeFuns<BISHOP>::indexMask)).any()
&& (long_effect[0] & mask_t::makeDirect(PtypeFuns<BISHOP>::indexMask)).any())
{
attack_piece[0] = state->findLongAttackAt(p, UL);
attack_piece[1] = state->findLongAttackAt(p, DR);
if (attack_piece[0].isPiece() && attack_piece[1].isPiece()
&& attack_piece[0].owner() != attack_piece[1].owner()) {
pin_by_opposing_sliders.push_back(p);
continue;
}
attack_piece[0] = state->findLongAttackAt(p, UR);
attack_piece[1] = state->findLongAttackAt(p, DL);
if (attack_piece[0].isPiece() && attack_piece[1].isPiece()
&& attack_piece[0].owner() != attack_piece[1].owner()) {
pin_by_opposing_sliders.push_back(p);
continue;
}
}
}
}
std::pair<osl::Piece,osl::Square> osl::move_probability::
StateInfo::findCheckmateDefender(const NumEffectState& state, Player king)
{
const King8Info king8info = state.king8Info(king);
const unsigned int spaces = king8info.spaces();
if (spaces == 0 || (spaces & (spaces-1)))
return std::make_pair(Piece::EMPTY(), Square());
const Square sq = state.kingSquare(king)
+ Board_Table.getOffset(king, (Direction)misc::BitOp::bsf(spaces));
assert(state.pieceAt(sq).isEmpty());
if (state.countEffect(king, sq) != 2 || ! state.hasEffectAt(alt(king), sq))
return std::make_pair(Piece::EMPTY(), Square());
unsigned int drop_candidate = king8info.libertyDropMask();
drop_candidate &= 0xff00;
drop_candidate += spaces;
mask_t drop_ptype=mask_t::makeDirect
(checkmate::Immediate_Checkmate_Table.dropPtypeMaskOf(drop_candidate));
while(drop_ptype.any()){
Ptype ptype=static_cast<Ptype>(drop_ptype.takeOneBit()+PTYPE_BASIC_MIN);
if (state.hasPieceOnStand(alt(king),ptype))
return std::make_pair(state.findCheapAttack(king, sq), sq);
}
return std::make_pair(Piece::EMPTY(), Square());
}
void osl::move_probability::
StateInfo::updatePinnedGenerals(Player owner)
{
exchange_pins[owner].clear();
PieceMask attacked = state->piecesOnBoard(owner)
& state->effectedMask(owner) & state->effectedMask(alt(owner));
while (attacked.any()) {
const Piece p = state->pieceOf(attacked.takeOneBit());
const int a = state->countEffect(alt(owner), p.square());
const int d = state->countEffect(owner, p.square());
if (d != a)
continue;
const Piece attack_p = state->findCheapAttack(alt(owner), p.square());
const Piece support = state->findCheapAttack(owner, p.square());
if (support.ptype() == PAWN || support.ptype() == LANCE)
continue;
unsigned int directions = Ptype_Table.getMoveMask(support.ptype());
while (directions) {
Direction d = static_cast<Direction>(misc::BitOp::bsf(directions));
directions &= directions-1;
Square target = support.square() + Board_Table.getOffset(owner, d);
if (target == p.square())
continue;
Piece tp = state->pieceAt(target);
if (tp.isEmpty() || tp.isOnBoardByOwner(owner)) {
assert(state->hasEffectByPiece(support, target));
if (state->countEffect(owner, target)
<= state->countEffect(alt(owner), target)+1
- state->hasEffectByPiece(attack_p, target)
&& ! state->hasEffectIf(support.ptypeO(), target, p.square()))
exchange_pins[owner].push_back
(PinnedGeneral(support, p, target));
}
}
}
}
osl::Move osl::move_probability::
StateInfo::findShortThreatmate(const NumEffectState& state, Move last_move)
{
if (state.inCheck())
return Move();
NumEffectState copy(state);
copy.changeTurn();
checkmate::FixedDepthSearcher searcher(copy);
Move best_move;
const ProofDisproof pdp
= searcher.hasCheckmateMoveOfTurn(2, best_move);
if (pdp.isCheckmateSuccess())
return best_move;
if (pdp.isFinal())
return Move();
if (last_move.isNormal() && Neighboring8Direct::hasEffect
(state, last_move.ptypeO(), last_move.to(), state.kingSquare(state.turn()))) {
const ProofDisproof pdp4
= searcher.hasCheckmateMoveOfTurn(4, best_move);
if (pdp4.isCheckmateSuccess())
return best_move;
if (state.hasPieceOnStand<GOLD>(alt(state.turn()))) {
const ProofDisproof pdp6
= searcher.hasCheckmateMoveOfTurn(6, best_move);
if (pdp6.isCheckmateSuccess())
return best_move;
}
}
return Move();
}
bool osl::move_probability::operator==(const StateInfo& l, const StateInfo& r)
{
for (int x=1; x<=9; ++x) {
for (int y=1; y<=9; ++y) {
const Square position(x,y);
if (! (l.pattern_cache[position.index()]
== r.pattern_cache[position.index()]))
return false;
}
}
return HashKey(*l.state) == HashKey(*r.state)
&& *l.history == *r.history
&& l.pin_by_opposing_sliders == r.pin_by_opposing_sliders
&& l.king8_long_pieces == r.king8_long_pieces
&& l.threatened == r.threatened
&& l.long_attack_cache == r.long_attack_cache
&& l.attack_shadow == r.attack_shadow
&& l.progress16 == r.progress16
&& l.last_move_ptype5 == r.last_move_ptype5
&& l.last_add_effect == r.last_add_effect
&& l.pin == r.pin && l.threatmate_move == r.threatmate_move
&& l.sendoffs == r.sendoffs
&& l.exchange_pins == r.exchange_pins
&& l.move_candidate_exists == r.move_candidate_exists
&& l.checkmate_defender == r.checkmate_defender
&& HashKey(l.copy) == HashKey(r.copy);
}
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
bookInMemory.h
osl::BookInMemory::instance
static const BookInMemory & instance(const std::string &filename="")
Definition bookInMemory.cc:77
osl::CArray
Definition container.h:20
osl::FixedCapacityVector::size
size_t size() const
Definition container.h:243
osl::MoveVector
Definition container.h:293
osl::Move
圧縮していない moveの表現 .
Definition basic_type.h:1052
osl::Move::ptypeO
PtypeO ptypeO() const
移動後のPtype, i.e., 成る手だった場合成った後
Definition basic_type.h:1162
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::Neighboring8Direct::hasEffect
static bool hasEffect(const NumEffectState &state, PtypeO ptypeo, Square from, Square target)
ptypeo の駒がfromからtargetの8近傍に直接の利きを持つか
Definition neighboring8Direct.h:108
osl::NumEffectState
利きを持つ局面
Definition numEffectState.h:34
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::ptypeO
PtypeO ptypeO() const
Definition basic_type.h:824
osl::Piece::ptype
Ptype ptype() const
Definition basic_type.h:821
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::isOnBoardByOwner
bool isOnBoardByOwner() const
piece がプレイヤーPの持ち物でかつボード上にある駒の場合は true.
Definition basic_type.h:852
osl::Piece::EMPTY
static const Piece EMPTY()
Definition basic_type.h:797
osl::Piece::isOnBoard
bool isOnBoard() const
Definition basic_type.h:985
osl::Square
Definition basic_type.h:532
osl::Square::index
unsigned int index() const
Definition basic_type.h:572
osl::book::BookInMemory
Definition bookInMemory.h:15
osl::checkmate::FixedDepthSearcher
深さ固定で，その深さまで depth first searchで読む詰将棋.
Definition fixedDepthSearcher.h:41
osl::checkmate::FixedDepthSearcher::hasCheckmateMoveOfTurn
const ProofDisproof hasCheckmateMoveOfTurn(int depth, Move &best_move)
Definition fixedDepthSearcher.cc:10
osl::checkmate::King8Info
敵玉の8近傍の状態を表す.
Definition king8Info.h:29
osl::checkmate::King8Info::libertyDropMask
unsigned int libertyDropMask() const
0-15bit
Definition king8Info.h:59
osl::checkmate::King8Info::spaces
unsigned int spaces() const
Definition king8Info.h:73
osl::checkmate::ProofDisproof
証明数(proof number)と反証数(disproof number).
Definition proofDisproof.h:17
osl::checkmate::ProofDisproof::isFinal
bool isFinal() const
Definition proofDisproof.h:88
osl::checkmate::ProofDisproof::isCheckmateSuccess
bool isCheckmateSuccess() const
Definition proofDisproof.h:86
osl::container::PieceMask64::takeOneBit
int takeOneBit()
Definition pieceMask64.h:82
osl::hash::HashKey
Definition hashKey.h:153
osl::misc::GeneralMask< mask_int_t >::makeDirect
static const GeneralMask makeDirect(mask_int_t value)
Definition mask.h:219
osl::misc::GeneralMask::any
bool any() const
Definition mask.h:298
osl::misc::GeneralMask::takeOneBit
int takeOneBit()
non-zeroのmaskのsetされているビットをLSBから探し，その番号を返す 副作用としてmaskの対応するビットをクリアする
Definition mask.h:265
osl::move_probability::BlockLong::updateCache
static void updateCache(StateInfo &)
Definition feature_.cc:113
osl::move_probability::PatternCommon::updateCache
static void updateCache(StateInfo &info)
Definition feature_.cc:14
fixedDepthSearcher.h
hashKey.h
immediateCheckmateTable.h
feature.h
osl::book
Definition bookInMemory.h:12
osl::checkmate::Immediate_Checkmate_Table
const ImmediateCheckmateTable Immediate_Checkmate_Table
Definition tables.cc:122
osl::misc::mask_t
GeneralMask< mask_int_t > mask_t
Definition mask.h:351
osl::move_probability::operator==
bool operator==(const PinnedGeneral &l, const PinnedGeneral &r)
Definition pinnedGeneral.h:20
osl
Definition additionalEffect.h:6
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
osl::PTYPE_MAX
@ PTYPE_MAX
Definition basic_type.h:105
osl::PAWN
@ PAWN
Definition basic_type.h:95
osl::KING
@ KING
Definition basic_type.h:93
osl::KNIGHT
@ KNIGHT
Definition basic_type.h:97
osl::PTYPE_EMPTY
@ PTYPE_EMPTY
Definition basic_type.h:85
osl::SILVER
@ SILVER
Definition basic_type.h:98
osl::PTYPE_BASIC_MIN
@ PTYPE_BASIC_MIN
Definition basic_type.h:103
osl::GOLD
@ GOLD
Definition basic_type.h:94
osl::LANCE
@ LANCE
Definition basic_type.h:96
osl::Ptype_Table
const PtypeTable Ptype_Table
Definition tables.cc:97
osl::unpromote
Ptype unpromote(Ptype ptype)
ptypeがpromote後の型の時に，promote前の型を返す． promoteしていない型の時はそのまま返す
Definition basic_type.h:157
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::SHORT8_DIRECTION_MIN
@ SHORT8_DIRECTION_MIN
Definition basic_type.h:312
osl::UUR
@ UUR
Definition basic_type.h:323
osl::UUL
@ UUL
Definition basic_type.h:322
osl::UL
@ UL
Definition basic_type.h:313
osl::DR
@ DR
Definition basic_type.h:320
osl::SHORT8_DIRECTION_MAX
@ SHORT8_DIRECTION_MAX
Definition basic_type.h:321
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::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::isMajor
bool isMajor(Ptype ptype)
Definition basic_type.h:185
osl::alt
constexpr Player alt(Player player)
Definition basic_type.h:13
ptypeTraits.h
sendOffSquare.h
stateInfo.h
osl::PtypeFuns::indexMask
static const uint64_t indexMask
Definition ptypeTraits.h:318
osl::effect_util::SendOffSquare::find
static SendOff8 find(const NumEffectState &state, Square king_position, Square8 &out)
osl::misc::BitOp::bsf
static int bsf(Integer mask)
Definition mask.h:138
osl::move_probability::PinnedGeneral
Definition pinnedGeneral.h:12
osl::move_probability::StateInfo
Definition stateInfo.h:21
osl::move_probability::StateInfo::clearOldCache
void clearOldCache()
Definition stateInfo.cc:14
osl::move_probability::StateInfo::updatePinnedGenerals
void updatePinnedGenerals(Player owner)
Definition stateInfo.cc:185
osl::move_probability::StateInfo::bookmove
CArray< Move, 2 > bookmove
Definition stateInfo.h:44
osl::move_probability::StateInfo::finishUpdate
void finishUpdate()
Definition stateInfo.cc:59
osl::move_probability::StateInfo::findCheckmateDefender
static std::pair< Piece, Square > findCheckmateDefender(const NumEffectState &state, Player king)
Definition stateInfo.cc:160
osl::move_probability::StateInfo::threatened
CArray< Piece, 2 > threatened
Definition stateInfo.h:26
osl::move_probability::StateInfo::sendoffs
Square8 sendoffs
Definition stateInfo.h:36
osl::move_probability::StateInfo::possible_threatmate_ptype
unsigned int possible_threatmate_ptype
Definition stateInfo.h:43
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::king8_long_pieces
PieceVector king8_long_pieces
Definition stateInfo.h:25
osl::move_probability::StateInfo::history
const MoveStack * history
Definition stateInfo.h:23
osl::move_probability::StateInfo::findShortThreatmate
static Move findShortThreatmate(const NumEffectState &, Move last_move)
Definition stateInfo.cc:222
osl::move_probability::StateInfo::threatmate_move
Move threatmate_move
Definition stateInfo.h:35
osl::move_probability::StateInfo::move_candidate_exists
CArray< bool, 2 > move_candidate_exists
Definition stateInfo.h:39
osl::move_probability::StateInfo::exchange_pins
CArray< pinned_gs_t, 2 > exchange_pins
Definition stateInfo.h:38
osl::move_probability::StateInfo::dirty
bool dirty
Definition stateInfo.h:45
osl::move_probability::StateInfo::changed_effects
BoardMask changed_effects
Definition stateInfo.h:41
osl::move_probability::StateInfo::checkmate_defender
CArray< std::pair< Piece, Square >, 2 > checkmate_defender
Definition stateInfo.h:42
osl::move_probability::StateInfo::pattern_cache
CArray< pattern_square_t, Square::SIZE > pattern_cache
Definition stateInfo.h:30
osl::move_probability::StateInfo::makePinOfLongPieces
void makePinOfLongPieces()
Definition stateInfo.cc:103
osl::move_probability::StateInfo::king8Info
King8Info king8Info(Player pl) const
Definition stateInfo.h:88
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::progress16
Progress16 progress16
Definition stateInfo.h:24
osl::move_probability::StateInfo::copy
NumEffectState copy
Definition stateInfo.h:40
osl::move_probability::StateInfo::long_attack_cache
CArray2d< long_attack_t, 40, 8 > long_attack_cache
Definition stateInfo.h:28
osl::move_probability::StateInfo::pin_by_opposing_sliders
PieceVector pin_by_opposing_sliders
Definition stateInfo.h:25