All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
piecePairKing.cc
Go to the documentation of this file.
1 /* piecePairKing.cc
2  */
4 #include "osl/eval/ml/weights.h"
6 #include "osl/misc/cstdint.h"
7 
8 osl::CArray<osl::int16_t, 1488375> osl::eval::ml::PiecePairKing::table;
9 
10 void osl::eval::ml::
12 {
13  for (size_t i=0; i<weights.dimension(); ++i)
14  table[i] = weights.value(i);
15 
16  for (int x=1; x<=5; ++x)
17  {
18  for (int y=1; y<=3; ++y)
19  {
20  bool flipx;
21  const int king = indexKing(WHITE, Square(x,y), flipx);
22  for (int i=0; i<45*7; ++i)
23  for (int j=i+1; j<45*7; ++j)
24  table[composeIndex(king, j, i)] = table[composeIndex(king, i, j)];
25  }
26  }
27 }
28 
29 osl::CArray<int,2> osl::eval::ml::
30 PiecePairKing::eval(const NumEffectState& state)
31 {
32  CArray<int,2> ret;
33  ret[BLACK] = evalOne<BLACK>(state);
34  ret[WHITE] = evalOne<WHITE>(state);
35  return ret;
36 }
37 
38 template <osl::Player King>
40 PiecePairKing::evalOne(const NumEffectState& state)
41 {
42  FixedCapacityVector<Piece,38> pieces;
43  if (state.template kingSquare<King>().template squareForBlack<King>().y() < 7)
44  return 0;
45 
46  PieceMask bitset = state.piecesOnBoard(King) & ~state.promotedPieces();
47  bitset.clearBit<KING>();
48  while (! bitset.none())
49  {
50  const Piece p = state.pieceOf(bitset.takeOneBit());
51  if (p.square().squareForBlack<King>().y() >= 5)
52  pieces.push_back(p);
53  }
54  int sum = 0;
55  bool flipx;
56  const int index_king = indexKing(King, state.kingSquare(King), flipx);
57  if (flipx)
58  {
59  for (size_t i=0; i<pieces.size(); ++i)
60  {
61  const unsigned int i0 = indexPiece<true>(King, pieces[i].square(), pieces[i].ptype());
62  for (size_t j=i+1; j<pieces.size(); ++j)
63  {
64  const unsigned int i1 = indexPiece<true>(King, pieces[j].square(), pieces[j].ptype());
65  const unsigned int index = composeIndex(index_king, i0, i1);
66  sum += table[index];
67  }
68  }
69  }
70  else
71  {
72  for (size_t i=0; i<pieces.size(); ++i)
73  {
74  const unsigned int i0 = indexPiece<false>(King, pieces[i].square(), pieces[i].ptype());
75  for (size_t j=i+1; j<pieces.size(); ++j)
76  {
77  const unsigned int i1 = indexPiece<false>(King, pieces[j].square(), pieces[j].ptype());
78  const unsigned int index = composeIndex(index_king, i0, i1);
79  sum += table[index];
80  }
81  }
82  }
83  return (King == BLACK) ? sum : -sum;
84 }
85 
86 template <osl::Player King>
88 PiecePairKing::add(const NumEffectState& state, Square to, Ptype ptype)
89 {
90  const Square king = state.kingSquare(King);
91  bool flipx;
92  const int index_king = indexKing(King, king, flipx);
93  int sum = 0;
94  PieceMask bitset = state.piecesOnBoard(King) & ~state.promotedPieces();
95  bitset.clearBit<KING>();
96  unsigned int i0;
97  if (flipx)
98  {
99  i0 = indexPiece<true>(King, to, ptype);
100  while (! bitset.none())
101  {
102  const Piece p = state.pieceOf(bitset.takeOneBit());
103  if (p.square().squareForBlack(King).y() < 5)
104  continue;
105  const unsigned int i1 = indexPiece<true>(King, p.square(), p.ptype());
106  const unsigned int index = composeIndex(index_king, i0, i1);
107  sum += table[index];
108  }
109  }
110  else
111  {
112  i0 = indexPiece<false>(King, to, ptype);
113  while (! bitset.none())
114  {
115  const Piece p = state.pieceOf(bitset.takeOneBit());
116  if (p.square().squareForBlack(King).y() < 5)
117  continue;
118  const unsigned int i1 = indexPiece<false>(King, p.square(), p.ptype());
119  const unsigned int index = composeIndex(index_king, i0, i1);
120  sum += table[index];
121  }
122  }
123  sum -= table[composeIndex(index_king, i0, i0)];
124  return (King == BLACK) ? sum : -sum;
125 }
126 template <osl::Player King>
127 int osl::eval::ml::
128 PiecePairKing::sub(const NumEffectState& state, Square from, Ptype ptype)
129 {
130  const Square king = state.kingSquare(King);
131  bool flipx;
132  const int index_king = indexKing(King, king, flipx);
133  int sum = 0;
134  PieceMask bitset = state.piecesOnBoard(King) & ~state.promotedPieces();
135  bitset.clearBit<KING>();
136  if (flipx)
137  {
138  const unsigned int i0 = indexPiece<true>(King, from, ptype);
139  while (! bitset.none())
140  {
141  const Piece p = state.pieceOf(bitset.takeOneBit());
142  if (p.square().squareForBlack(King).y() < 5)
143  continue;
144  const unsigned int i1 = indexPiece<true>(King, p.square(), p.ptype());
145  const unsigned int index = composeIndex(index_king, i0, i1);
146  sum -= table[index];
147  }
148  }
149  else
150  {
151  const unsigned int i0 = indexPiece<false>(King, from, ptype);
152  while (! bitset.none())
153  {
154  const Piece p = state.pieceOf(bitset.takeOneBit());
155  if (p.square().squareForBlack(King).y() < 5)
156  continue;
157  const unsigned int i1 = indexPiece<false>(King, p.square(), p.ptype());
158  const unsigned int index = composeIndex(index_king, i0, i1);
159  sum -= table[index];
160  }
161  }
162  return (King == BLACK) ? sum : -sum;
163 }
164 template <osl::Player King>
165 int osl::eval::ml::
166 PiecePairKing::addSub(const NumEffectState& state, Square to, Ptype ptype, Square from)
167 {
168  const Square king = state.kingSquare(King);
169  bool flipx;
170  const int index_king = indexKing(King, king, flipx);
171  unsigned int i0, s0;
172  int sum = 0;
173  PieceMask bitset = state.piecesOnBoard(King) & ~state.promotedPieces();
174  bitset.clearBit<KING>();
175  FixedCapacityVector<Piece,38> pieces;
176  if (flipx)
177  {
178  i0 = indexPiece<true>(King, to, ptype);
179  s0 = indexPiece<true>(King, from, ptype);
180  while (! bitset.none())
181  {
182  const Piece p = state.pieceOf(bitset.takeOneBit());
183  if (p.square().squareForBlack(King).y() < 5)
184  continue;
185  const unsigned int i1 = indexPiece<true>(King, p.square(), p.ptype());
186  const unsigned int index = composeIndex(index_king, i0, i1);
187  sum += table[index];
188  const unsigned int sub_index = composeIndex(index_king, s0, i1);
189  sum -= table[sub_index];
190  }
191  }
192  else
193  {
194  i0 = indexPiece<false>(King, to, ptype);
195  s0 = indexPiece<false>(King, from, ptype);
196  while (! bitset.none())
197  {
198  const Piece p = state.pieceOf(bitset.takeOneBit());
199  if (p.square().squareForBlack(King).y() < 5)
200  continue;
201  const unsigned int i1 = indexPiece<false>(King, p.square(), p.ptype());
202  const unsigned int index = composeIndex(index_king, i0, i1);
203  sum += table[index];
204  const unsigned int sub_index = composeIndex(index_king, s0, i1);
205  sum -= table[sub_index];
206  }
207  }
208  sum -= table[composeIndex(index_king, i0, i0)];
209  sum += table[composeIndex(index_king, s0, i0)];
210  return (King == BLACK) ? sum : -sum;
211 }
212 
213 template <osl::Player P>
214 void osl::eval::ml::
215 PiecePairKing::evalWithUpdateBang(const NumEffectState& state, Move moved, CArray<int,2>& last_value)
216 {
217  assert(P == moved.player());
218  if (moved.isPass())
219  return;
220  const Player Opponent = PlayerTraits<P>::opponent;
221  const Ptype captured = moved.capturePtype();
222  bool adjust_capture = (captured != PTYPE_EMPTY)
223  && ! isPromoted(captured)
224  && moved.to().squareForBlack(alt(P)).y() >= 5;
225  if (adjust_capture)
226  {
227  const Square roking = state.kingSquare(alt(P)).squareForBlack(alt(P));
228  adjust_capture = roking.y() >= 7;
229  }
230  if (moved.ptype() == KING)
231  {
232  last_value[P] = evalOne<P>(state);
233  if (adjust_capture)
234  last_value[alt(P)] += sub<Opponent>(state, moved.to(), captured);
235  return;
236  }
237  const Square rking = state.kingSquare(P).squareForBlack(P);
238  if (rking.y() < 7)
239  {
240  if (adjust_capture)
241  last_value[alt(P)] += sub<Opponent>(state, moved.to(), captured);
242  return;
243  }
244  const Square rto = moved.to().squareForBlack(P);
245  if (moved.isDrop())
246  {
247  if (rto.y() >= 5)
248  last_value[P] += add<P>(state, moved.to(), moved.ptype());
249  return;
250  }
251  const Square rfrom = moved.from().squareForBlack(P);
252  if (adjust_capture)
253  last_value[alt(P)] += sub<Opponent>(state, moved.to(), captured);
254 
255  if (isPromoted(moved.oldPtype()))
256  return;
257  if (rfrom.y() < 5)
258  {
259  if (rto.y() >= 5 && ! isPromoted(moved.ptype()))
260  last_value[P] += add<P>(state, moved.to(), moved.ptype());
261  return;
262  }
263  if (rto.y() < 5 || isPromoted(moved.ptype()))
264  last_value[P] += sub<P>(state, moved.from(), moved.oldPtype());
265  else
266  last_value[P] += addSub<P>(state, moved.to(), moved.ptype(), moved.from());
267 }
268 
269 namespace osl
270 {
271  namespace eval
272  {
273  namespace ml
274  {
275  template void PiecePairKing::evalWithUpdateBang<BLACK>(const NumEffectState&, Move, CArray<int,2>&);
276  template void PiecePairKing::evalWithUpdateBang<WHITE>(const NumEffectState&, Move, CArray<int,2>&);
277  }
278  }
279 }
280 
281 // ;;; Local Variables:
282 // ;;; mode:c++
283 // ;;; c-basic-offset:2
284 // ;;; End: