My Project
piecePairKing.cc
Go to the documentation of this file.
1 /* piecePairKing.cc
2  */
4 #include "osl/eval/weights.h"
5 #include <cstdint>
6 
8 
10 PiecePairKing::setUp(const Weights &weights)
11 {
12  for (size_t i=0; i<weights.dimension(); ++i)
13  table[i] = weights.value(i);
14 
15  for (int x=1; x<=5; ++x)
16  {
17  for (int y=1; y<=3; ++y)
18  {
19  bool flipx;
20  const int king = indexKing(WHITE, Square(x,y), flipx);
21  for (int i=0; i<45*7; ++i)
22  for (int j=i+1; j<45*7; ++j)
23  table[composeIndex(king, j, i)] = table[composeIndex(king, i, j)];
24  }
25  }
26 }
27 
30 {
31  CArray<int,2> ret;
32  ret[BLACK] = evalOne<BLACK>(state);
33  ret[WHITE] = evalOne<WHITE>(state);
34  return ret;
35 }
36 
37 template <osl::Player King>
40 {
42  if (state.template kingSquare<King>().template squareForBlack<King>().y() < 7)
43  return 0;
44 
45  PieceMask bitset = state.piecesOnBoard(King) & ~state.promotedPieces();
46  bitset.clearBit<KING>();
47  while (! bitset.none())
48  {
49  const Piece p = state.pieceOf(bitset.takeOneBit());
50  if (p.square().squareForBlack<King>().y() >= 5)
51  pieces.push_back(p);
52  }
53  int sum = 0;
54  bool flipx;
55  const int index_king = indexKing(King, state.kingSquare(King), flipx);
56  if (flipx)
57  {
58  for (size_t i=0; i<pieces.size(); ++i)
59  {
60  const unsigned int i0 = indexPiece<true>(King, pieces[i].square(), pieces[i].ptype());
61  for (size_t j=i+1; j<pieces.size(); ++j)
62  {
63  const unsigned int i1 = indexPiece<true>(King, pieces[j].square(), pieces[j].ptype());
64  const unsigned int index = composeIndex(index_king, i0, i1);
65  sum += table[index];
66  }
67  }
68  }
69  else
70  {
71  for (size_t i=0; i<pieces.size(); ++i)
72  {
73  const unsigned int i0 = indexPiece<false>(King, pieces[i].square(), pieces[i].ptype());
74  for (size_t j=i+1; j<pieces.size(); ++j)
75  {
76  const unsigned int i1 = indexPiece<false>(King, pieces[j].square(), pieces[j].ptype());
77  const unsigned int index = composeIndex(index_king, i0, i1);
78  sum += table[index];
79  }
80  }
81  }
82  return (King == BLACK) ? sum : -sum;
83 }
84 
85 template <osl::Player King>
87 PiecePairKing::add(const NumEffectState& state, Square to, Ptype ptype)
88 {
89  const Square king = state.kingSquare(King);
90  bool flipx;
91  const int index_king = indexKing(King, king, flipx);
92  int sum = 0;
93  PieceMask bitset = state.piecesOnBoard(King) & ~state.promotedPieces();
94  bitset.clearBit<KING>();
95  unsigned int i0;
96  if (flipx)
97  {
98  i0 = indexPiece<true>(King, to, ptype);
99  while (! bitset.none())
100  {
101  const Piece p = state.pieceOf(bitset.takeOneBit());
102  if (p.square().squareForBlack(King).y() < 5)
103  continue;
104  const unsigned int i1 = indexPiece<true>(King, p.square(), p.ptype());
105  const unsigned int index = composeIndex(index_king, i0, i1);
106  sum += table[index];
107  }
108  }
109  else
110  {
111  i0 = indexPiece<false>(King, to, ptype);
112  while (! bitset.none())
113  {
114  const Piece p = state.pieceOf(bitset.takeOneBit());
115  if (p.square().squareForBlack(King).y() < 5)
116  continue;
117  const unsigned int i1 = indexPiece<false>(King, p.square(), p.ptype());
118  const unsigned int index = composeIndex(index_king, i0, i1);
119  sum += table[index];
120  }
121  }
122  sum -= table[composeIndex(index_king, i0, i0)];
123  return (King == BLACK) ? sum : -sum;
124 }
125 template <osl::Player King>
127 PiecePairKing::sub(const NumEffectState& state, Square from, Ptype ptype)
128 {
129  const Square king = state.kingSquare(King);
130  bool flipx;
131  const int index_king = indexKing(King, king, flipx);
132  int sum = 0;
133  PieceMask bitset = state.piecesOnBoard(King) & ~state.promotedPieces();
134  bitset.clearBit<KING>();
135  if (flipx)
136  {
137  const unsigned int i0 = indexPiece<true>(King, from, ptype);
138  while (! bitset.none())
139  {
140  const Piece p = state.pieceOf(bitset.takeOneBit());
141  if (p.square().squareForBlack(King).y() < 5)
142  continue;
143  const unsigned int i1 = indexPiece<true>(King, p.square(), p.ptype());
144  const unsigned int index = composeIndex(index_king, i0, i1);
145  sum -= table[index];
146  }
147  }
148  else
149  {
150  const unsigned int i0 = indexPiece<false>(King, from, ptype);
151  while (! bitset.none())
152  {
153  const Piece p = state.pieceOf(bitset.takeOneBit());
154  if (p.square().squareForBlack(King).y() < 5)
155  continue;
156  const unsigned int i1 = indexPiece<false>(King, p.square(), p.ptype());
157  const unsigned int index = composeIndex(index_king, i0, i1);
158  sum -= table[index];
159  }
160  }
161  return (King == BLACK) ? sum : -sum;
162 }
163 template <osl::Player King>
165 PiecePairKing::addSub(const NumEffectState& state, Square to, Ptype ptype, Square from)
166 {
167  const Square king = state.kingSquare(King);
168  bool flipx;
169  const int index_king = indexKing(King, king, flipx);
170  unsigned int i0, s0;
171  int sum = 0;
172  PieceMask bitset = state.piecesOnBoard(King) & ~state.promotedPieces();
173  bitset.clearBit<KING>();
175  if (flipx)
176  {
177  i0 = indexPiece<true>(King, to, ptype);
178  s0 = indexPiece<true>(King, from, ptype);
179  while (! bitset.none())
180  {
181  const Piece p = state.pieceOf(bitset.takeOneBit());
182  if (p.square().squareForBlack(King).y() < 5)
183  continue;
184  const unsigned int i1 = indexPiece<true>(King, p.square(), p.ptype());
185  const unsigned int index = composeIndex(index_king, i0, i1);
186  sum += table[index];
187  const unsigned int sub_index = composeIndex(index_king, s0, i1);
188  sum -= table[sub_index];
189  }
190  }
191  else
192  {
193  i0 = indexPiece<false>(King, to, ptype);
194  s0 = indexPiece<false>(King, from, ptype);
195  while (! bitset.none())
196  {
197  const Piece p = state.pieceOf(bitset.takeOneBit());
198  if (p.square().squareForBlack(King).y() < 5)
199  continue;
200  const unsigned int i1 = indexPiece<false>(King, p.square(), p.ptype());
201  const unsigned int index = composeIndex(index_king, i0, i1);
202  sum += table[index];
203  const unsigned int sub_index = composeIndex(index_king, s0, i1);
204  sum -= table[sub_index];
205  }
206  }
207  sum -= table[composeIndex(index_king, i0, i0)];
208  sum += table[composeIndex(index_king, s0, i0)];
209  return (King == BLACK) ? sum : -sum;
210 }
211 
212 template <osl::Player P>
214 PiecePairKing::evalWithUpdateBang(const NumEffectState& state, Move moved, CArray<int,2>& last_value)
215 {
216  assert(P == moved.player());
217  if (moved.isPass())
218  return;
219  const Player Opponent = alt(P);
220  const Ptype captured = moved.capturePtype();
221  bool adjust_capture = (captured != PTYPE_EMPTY)
222  && ! isPromoted(captured)
223  && moved.to().squareForBlack(alt(P)).y() >= 5;
224  if (adjust_capture)
225  {
226  const Square roking = state.kingSquare(alt(P)).squareForBlack(alt(P));
227  adjust_capture = roking.y() >= 7;
228  }
229  if (moved.ptype() == KING)
230  {
231  last_value[P] = evalOne<P>(state);
232  if (adjust_capture)
233  last_value[alt(P)] += sub<Opponent>(state, moved.to(), captured);
234  return;
235  }
236  const Square rking = state.kingSquare(P).squareForBlack(P);
237  if (rking.y() < 7)
238  {
239  if (adjust_capture)
240  last_value[alt(P)] += sub<Opponent>(state, moved.to(), captured);
241  return;
242  }
243  const Square rto = moved.to().squareForBlack(P);
244  if (moved.isDrop())
245  {
246  if (rto.y() >= 5)
247  last_value[P] += add<P>(state, moved.to(), moved.ptype());
248  return;
249  }
250  const Square rfrom = moved.from().squareForBlack(P);
251  if (adjust_capture)
252  last_value[alt(P)] += sub<Opponent>(state, moved.to(), captured);
253 
254  if (isPromoted(moved.oldPtype()))
255  return;
256  if (rfrom.y() < 5)
257  {
258  if (rto.y() >= 5 && ! isPromoted(moved.ptype()))
259  last_value[P] += add<P>(state, moved.to(), moved.ptype());
260  return;
261  }
262  if (rto.y() < 5 || isPromoted(moved.ptype()))
263  last_value[P] += sub<P>(state, moved.from(), moved.oldPtype());
264  else
265  last_value[P] += addSub<P>(state, moved.to(), moved.ptype(), moved.from());
266 }
267 
268 namespace osl
269 {
270  namespace eval
271  {
272  namespace ml
273  {
274  template void PiecePairKing::evalWithUpdateBang<BLACK>(const NumEffectState&, Move, CArray<int,2>&);
275  template void PiecePairKing::evalWithUpdateBang<WHITE>(const NumEffectState&, Move, CArray<int,2>&);
276  }
277  }
278 }
279 
280 // ;;; Local Variables:
281 // ;;; mode:c++
282 // ;;; c-basic-offset:2
283 // ;;; End:
osl::eval::ml::PiecePairKing::sub
static int sub(const NumEffectState &state, Square from, Ptype ptype)
Definition: piecePairKing.cc:127
osl::Square
Definition: basic_type.h:532
osl::NumEffectState::piecesOnBoard
const PieceMask & piecesOnBoard(Player p) const
Definition: numEffectState.h:63
osl::FixedCapacityVector::size
size_t size() const
Definition: container.h:243
osl::WHITE
@ WHITE
Definition: basic_type.h:10
osl::container::PieceMask64::none
bool none() const
Definition: pieceMask64.h:57
osl::eval::ml::PiecePairKing::addSub
static int addSub(const NumEffectState &state, Square to, Ptype ptype, Square from)
Definition: piecePairKing.cc:165
osl::alt
constexpr Player alt(Player player)
Definition: basic_type.h:13
eval
weights.h
osl::Move
圧縮していない moveの表現 .
Definition: basic_type.h:1052
osl::eval::ml::Weights
Definition: weights.h:18
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:84
osl::eval::ml::PiecePairKing::setUp
static void setUp(const Weights &weights)
Definition: piecePairKing.cc:10
osl::eval::ml::Weights::value
int value(size_t index) const
Definition: weights.h:27
osl::FixedCapacityVector
Definition: container.h:137
osl::Piece
駒.
Definition: basic_type.h:788
osl::SimpleState::pieceOf
const Piece pieceOf(int num) const
Definition: simpleState.h:76
osl::Move::oldPtype
Ptype oldPtype() const
移動前のPtype, i.e., 成る手だった場合成る前
Definition: basic_type.h:1174
osl::KING
@ KING
Definition: basic_type.h:93
piecePairKing.h
osl::container::PieceMask64::takeOneBit
int takeOneBit()
Definition: pieceMask64.h:82
osl::Move::capturePtype
Ptype capturePtype() const
Definition: basic_type.h:1180
osl::Move::isDrop
bool isDrop() const
Definition: basic_type.h:1150
osl::eval::ml::PiecePairKing::eval
static CArray< int, 2 > eval(const NumEffectState &)
Definition: piecePairKing.cc:29
osl::NumEffectState::promotedPieces
const PieceMask promotedPieces() const
Definition: numEffectState.h:64
osl::Move::from
const Square from() const
Definition: basic_type.h:1125
osl::captured
PtypeO captured(PtypeO ptypeO)
unpromoteすると共に,ownerを反転する.
Definition: basic_type.h:264
osl::NumEffectState
利きを持つ局面
Definition: numEffectState.h:34
osl::SimpleState::kingSquare
Square kingSquare() const
Definition: simpleState.h:94
osl::Square::y
int y() const
将棋としてのY座標を返す.
Definition: basic_type.h:567
osl::isPromoted
bool isPromoted(Ptype ptype)
ptypeがpromote後の型かどうかのチェック
Definition: basic_type.h:137
osl::eval::ml::PiecePairKing::table
static osl::CArray< int16_t, ONE_DIM > table
Definition: piecePairKing.h:69
osl::Piece::square
const Square square() const
Definition: basic_type.h:832
osl::Piece::ptype
Ptype ptype() const
Definition: basic_type.h:821
osl::Move::isPass
bool isPass() const
Definition: basic_type.h:1092
osl::Move::ptype
Ptype ptype() const
Definition: basic_type.h:1155
osl::BLACK
@ BLACK
Definition: basic_type.h:9
osl::PTYPE_EMPTY
@ PTYPE_EMPTY
Definition: basic_type.h:85
osl::Player
Player
Definition: basic_type.h:8
osl::eval::ml::PiecePairKing::evalWithUpdateBang
static void evalWithUpdateBang(const NumEffectState &state, Move moved, CArray< int, 2 > &last_value)
Definition: piecePairKing.cc:214
osl::CArray
Definition: container.h:20
osl::Move::to
const Square to() const
Definition: basic_type.h:1132
osl::eval::ml::PiecePairKing::evalOne
static int evalOne(const NumEffectState &)
Definition: piecePairKing.cc:39
osl::PieceMask
駒番号のビットセット.
Definition: pieceMask.h:21
osl::Square::squareForBlack
const Square squareForBlack(Player player) const
Definition: basic_type.h:598
osl::eval::ml::Weights::dimension
size_t dimension() const
Definition: weights.h:29
osl::PieceMask::clearBit
void clearBit()
unpromote(PTYPE) の駒のbit を消す
Definition: pieceMask.h:74
osl::FixedCapacityVector::push_back
void push_back(const T &e)
Definition: container.h:204
osl::eval::ml::PiecePairKing::add
static int add(const NumEffectState &state, Square to, Ptype ptype)
Definition: piecePairKing.cc:87
osl::Move::player
Player player() const
Definition: basic_type.h:1195
osl
Definition: additionalEffect.h:6