piecePairEval.tcc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
piecePairEval.tcc
Go to the documentation of this file.
1 /* piecePairEval.tcc
2 */
3 #ifndef EVAL_PIECE_PAIR_EVAL_TCC
4 #define EVAL_PIECE_PAIR_EVAL_TCC
5
6 #include "osl/eval/ppair/piecePairEval.h"
7 #include "osl/eval/ppair/piecePairAssert.h"
8 #include "osl/container/pieceValues.h"
9 #include "osl/pieceTable.h"
10 #include "osl/oslConfig.h"
11
12 template <class Table>
13 osl::eval::ppair::
14 PiecePairEvalTableBase<Table>::PiecePairEvalTableBase(const SimpleState& state)
15 {
16 for (int i=0; i<Piece::SIZE; i++) {
17 for (int j=i; j<Piece::SIZE; j++) {
18 val += Table::Table.valueOf
19 (PiecePairIndex::indexOfPieceNum(state, i),
20 PiecePairIndex::indexOfPieceNum(state, j));
21 }
22 }
23 }
24
25 template <class Table>
26 bool& osl::eval::ppair::
27 PiecePairEvalTableBase<Table>::initializationFlag()
28 {
29 static bool flag = false;
30 return flag;
31 }
32
33 template <class Table>
34 bool osl::eval::ppair::
35 PiecePairEvalTableBase<Table>::setUp(const char *filename)
36 {
37 bool& result = initializationFlag();
38 result = Table::Table.setUp(filename);
39 return result;
40 }
41
42 template <class Table>
43 bool osl::eval::ppair::
44 PiecePairEvalTableBase<Table>::setUp()
45 {
46 std::string filename = OslConfig::home();
47 filename += "/data/sibling-attack.pair";
48 return setUp(filename.c_str());
49 }
50
51 template <class Table>
52 int osl::eval::ppair::PiecePairEvalTableBase<Table>::
53 adjustPairs(const SimpleState& state,
54 unsigned int new_index)
55 {
56 int diff = 0;
57 for (int i=0; i<Piece::SIZE; i++)
58 {
59 const Piece p=state.pieceOf(i);
60 if(p.isOnBoard()){
61 const unsigned int target =
62 PiecePairIndex::indexOf(p);
63 // 以下は target==old_index で1回以上(駒台)マッチ
64 diff += Table::Table.valueOf(target, new_index);
65 }
66 }
67 diff += Table::Table.valueOf(new_index, new_index);
68 return diff;
69 }
70
71 template <class Table>
72 int osl::eval::ppair::PiecePairEvalTableBase<Table>::
73 adjustPairs(const SimpleState& state,
74 unsigned int old_index, unsigned int new_index)
75 {
76 int diff = 0;
77 for (int i=0; i<Piece::SIZE; i++)
78 {
79 const Piece p=state.pieceOf(i);
80 if(p.isOnBoard()){
81 const unsigned int target =
82 PiecePairIndex::indexOf(p);
83 diff -= Table::Table.valueOf(target, old_index);
84 // 以下は target==old_index で1回以上(駒台)マッチ
85 diff += Table::Table.valueOf(target, new_index);
86 }
87 }
88 diff -= Table::Table.valueOf(old_index, new_index); // 足しすぎた分
89 diff += Table::Table.valueOf(new_index, new_index);
90 return diff;
91 }
92
93 template <class Table>
94 int osl::eval::ppair::PiecePairEvalTableBase<Table>::
95 adjustPairs(const SimpleState& state,
96 unsigned int old_index, unsigned int old_index2,
97 unsigned int new_index)
98 {
99 int diff = 0;
100 for (int i=0;i<Piece::SIZE; i++)
101 {
102 const Piece p=state.pieceOf(i);
103 if(p.isOnBoard()){
104 const unsigned int target =
105 PiecePairIndex::indexOf(p);
106 diff += Table::Table.valueOf(target, new_index);
107 diff -= Table::Table.valueOf(target, old_index);
108 diff -= Table::Table.valueOf(target, old_index2);
109 }
110 }
111
112 diff -= Table::Table.valueOf(old_index, new_index);
113 diff -= Table::Table.valueOf(old_index2, new_index);
114 diff += Table::Table.valueOf(old_index, old_index2);
115
116 diff += Table::Table.valueOf(new_index, new_index);
117 return diff;
118 }
119
120 // update
121 template <class Table>
122 int osl::eval::ppair::PiecePairEvalTableBase<Table>::
123 adjustPairsAfterMove(const SimpleState& state,
124 unsigned int new_index)
125 {
126 int diff = 0;
127 for (int i=0; i<Piece::SIZE; i++)
128 {
129 const Piece p=state.pieceOf(i);
130 if (p.isOnBoard()) {
131 const unsigned int target =
132 PiecePairIndex::indexOf(p);
133 diff += Table::Table.valueOf(target, new_index);
134 }
135 }
136 return diff;
137 }
138
139 template <class Table>
140 int osl::eval::ppair::PiecePairEvalTableBase<Table>::
141 adjustPairsAfterMove(const SimpleState& state,
142 unsigned int old_index, unsigned int new_index)
143 {
144 int diff = 0;
145 for (int i=0; i<Piece::SIZE; i++)
146 {
147 const Piece p=state.pieceOf(i);
148 if(p.isOnBoard()){
149 const unsigned int target =
150 PiecePairIndex::indexOf(p);
151 diff -= Table::Table.valueOf(target, old_index);
152 diff += Table::Table.valueOf(target, new_index);
153 }
154 }
155 diff -= Table::Table.valueOf(old_index, old_index);
156 diff += Table::Table.valueOf(new_index, old_index); // 引きすぎた分
157 return diff;
158 }
159
160 template <class Table>
161 int osl::eval::ppair::PiecePairEvalTableBase<Table>::
162 adjustPairsAfterMove(const SimpleState& state,
163 unsigned int old_index, unsigned int old_index2,
164 unsigned int new_index)
165 {
166 int diff = 0;
167 for (int i=0;i<Piece::SIZE; i++)
168 {
169 const Piece p=state.pieceOf(i);
170 if(p.isOnBoard()){
171 const unsigned int target =
172 PiecePairIndex::indexOf(p);
173 diff += Table::Table.valueOf(target, new_index);
174 diff -= Table::Table.valueOf(target, old_index);
175 diff -= Table::Table.valueOf(target, old_index2);
176 }
177 }
178
179 diff += Table::Table.valueOf(new_index, old_index);
180 diff += Table::Table.valueOf(new_index, old_index2);
181
182 diff -= Table::Table.valueOf(old_index, old_index2);
183 diff -= Table::Table.valueOf(old_index, old_index);
184 diff -= Table::Table.valueOf(old_index2, old_index2);
185 return diff;
186 }
187
188 template <class Table>
189 void osl::eval::ppair::PiecePairEvalTableBase<Table>::
190 setValues(const SimpleState& state, container::PieceValues& values)
191 {
192 values.fill(0);
193 // 速度は無視
194 for (int i=0; i<Piece::SIZE; i++) {
195 const bool i_is_king = (Piece_Table.getPtypeOf(i) == KING);
196 for (int j=0; j<Piece::SIZE; j++) {
197 if (i==j)
198 continue;
199 const bool j_is_king = (Piece_Table.getPtypeOf(j) == KING);
200
201 const int relation_value = Table::Table.valueOf
202 (PiecePairIndex::indexOfPieceNum(state, i),
203 PiecePairIndex::indexOfPieceNum(state, j));
204 if (i_is_king && (! j_is_king))
205 {
206 values[j] += relation_value;
207 }
208 else
209 {
210 values[i] += relation_value;
211 }
212 }
213 }
214 for (int i=0; i<Piece::SIZE; i++)
215 {
216 values[i] /= 2;
217
218 const unsigned int index = PiecePairIndex::indexOfPieceNum(state, i);
219 const int self_value = Table::Table.valueOf(index, index);
220 values[i] += self_value;
221 }
222 }
223
224 /* ------------------------------------------------------------------------- */
225
226 template <class Eval, class Table>
227 osl::eval::ppair::
228 PiecePairEval<Eval,Table>::PiecePairEval(const SimpleState& state)
229 : PiecePairEvalTableBase<Table>(state)
230 {
231 }
232
233 template <class Eval, class Table>
234 int osl::eval::ppair::PiecePairEval<Eval,Table>::
235 expect(const SimpleState& state, Move m) const
236 {
237 const Ptype ptype = m.ptype();
238 const Square to = m.to();
239 const Player player = state.turn();
240 if (m.isDrop()) {
241 piece_pair_assert(state.pieceAt(to) == Piece::EMPTY());
242 return this->roundUp(this->val + Eval::diffAfterDropMove(state, to, newPtypeO(player, ptype)));
243 }
244
245 const Square from = m.from();
246 piece_pair_assert(state.pieceAt(from) != Piece::EMPTY());
247 if (m.capturePtype() == PTYPE_EMPTY) {
248 piece_pair_assert(state.pieceAt(to) == Piece::EMPTY());
249 return this->roundUp(this->val + Eval::diffAfterSimpleMove(state, from, to, m.promoteMask()));
250 }
251 piece_pair_assert(state.pieceAt(to) != Piece::EMPTY());
252 return this->roundUp(this->val + Eval::diffAfterCaptureMove(state, from, to, m.capturePtypeO(), m.promoteMask()));
253 }
254
255 #endif /* EVAL_PIECE_PAIR_EVAL_TCC */
256 // ;;; Local Variables:
257 // ;;; mode:c++
258 // ;;; c-basic-offset:2
259 // ;;; End: