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
10PiecePairKing::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
37template <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
85template <osl::Player King>
87PiecePairKing::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}
125template <osl::Player King>
127PiecePairKing::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}
163template <osl::Player King>
165PiecePairKing::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
212template <osl::Player P>
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
268namespace 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:
size_t size() const
Definition: container.h:243
void push_back(const T &e)
Definition: container.h:204
圧縮していない moveの表現 .
Definition: basic_type.h:1052
Ptype ptype() const
Definition: basic_type.h:1155
Player player() const
Definition: basic_type.h:1195
bool isDrop() const
Definition: basic_type.h:1150
bool isPass() const
Definition: basic_type.h:1092
Ptype capturePtype() const
Definition: basic_type.h:1180
Ptype oldPtype() const
移動前のPtype, i.e., 成る手だった場合成る前
Definition: basic_type.h:1174
const Square to() const
Definition: basic_type.h:1132
const Square from() const
Definition: basic_type.h:1125
利きを持つ局面
const PieceMask & piecesOnBoard(Player p) const
駒番号のビットセット.
Definition: pieceMask.h:21
void clearBit()
unpromote(PTYPE) の駒のbit を消す
Definition: pieceMask.h:74
Ptype ptype() const
Definition: basic_type.h:821
const Square square() const
Definition: basic_type.h:832
const Piece pieceOf(int num) const
Definition: simpleState.h:76
Square kingSquare() const
Definition: simpleState.h:94
int y() const
将棋としてのY座標を返す.
Definition: basic_type.h:567
const Square squareForBlack(Player player) const
Definition: basic_type.h:598
static int add(const NumEffectState &state, Square to, Ptype ptype)
static void setUp(const Weights &weights)
static int addSub(const NumEffectState &state, Square to, Ptype ptype, Square from)
static void evalWithUpdateBang(const NumEffectState &state, Move moved, CArray< int, 2 > &last_value)
static int composeIndex(int king, int i0, int i1)
Definition: piecePairKing.h:36
static int evalOne(const NumEffectState &)
static osl::CArray< int16_t, ONE_DIM > table
Definition: piecePairKing.h:69
static int sub(const NumEffectState &state, Square from, Ptype ptype)
static int indexKing(Player owner, Square king, bool &flipx)
Definition: piecePairKing.h:44
static CArray< int, 2 > eval(const NumEffectState &)
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:84
@ KING
Definition: basic_type.h:93
@ PTYPE_EMPTY
Definition: basic_type.h:85
bool isPromoted(Ptype ptype)
ptypeがpromote後の型かどうかのチェック
Definition: basic_type.h:137
Player
Definition: basic_type.h:8
@ WHITE
Definition: basic_type.h:10
@ BLACK
Definition: basic_type.h:9
constexpr Player alt(Player player)
Definition: basic_type.h:13
PtypeO captured(PtypeO ptypeO)
unpromoteすると共に,ownerを反転する.
Definition: basic_type.h:264
size_t dimension() const
Definition: weights.h:29
int value(size_t index) const
Definition: weights.h:27