All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
mobility.cc
Go to the documentation of this file.
1 #include "osl/eval/ml/mobility.h"
5 
6 using osl::MultiInt;
7 
22 
27 
30 
31 void osl::eval::ml::
32 RookMobility::setUp(const Weights &weights,int stage)
33 {
34  for (size_t i = 0; i < 9; ++i)
35  {
36  RookMobilityAll::rook_vertical_table[i][stage] = weights.value(i);
37  }
38  for (size_t i = 0; i < 9; ++i)
39  {
40  RookMobilityAll::rook_horizontal_table[i][stage] = weights.value(i + 9);
41  }
42  for (size_t i = 0; i < 9; ++i)
43  {
44  RookMobilityAll::rook_vertical_table[i+9][stage] = weights.value(i + 18);
45  }
46  for (size_t i = 0; i < 9; ++i)
47  {
48  RookMobilityAll::rook_horizontal_table[i+9][stage] = weights.value(i + 27);
49  }
50 }
51 
52 void osl::eval::ml::
54 {
55  for (size_t i = 0; i < ONE_DIM; ++i)
56  {
57  for (int s=0; s<NStages; ++s)
58  RookMobilityAll::sum_table[i][s] = weights.value(i + ONE_DIM*s);
59  }
60 }
61 
62 void osl::eval::ml::
64 {
65  for (size_t i = 0; i < ONE_DIM; ++i)
66  {
67  for (int s=0; s<NStages; ++s)
68  RookMobilityAll::x_table[i][s] = weights.value(i + ONE_DIM*s);
69  }
70 }
71 
72 void osl::eval::ml::
74 {
75  for (size_t i = 0; i < ONE_DIM; ++i)
76  {
77  for (int s=0; s<NStages; ++s)
78  RookMobilityAll::y_table[i][s] = weights.value(i + ONE_DIM*s);
79  }
80 }
81 
82 void osl::eval::ml::
84 {
85  for (size_t i = 0; i < ONE_DIM; ++i)
86  {
87  for (int s=0; s<NStages; ++s)
88  RookMobilityAll::xkingx_table[i][s] = weights.value(i + ONE_DIM*s);
89  }
90 }
91 void osl::eval::ml::
93 {
94  for (size_t i = 0; i < ONE_DIM; ++i)
95  {
96  for (int s=0; s<NStages; ++s)
97  RookMobilityAll::sumkingx_table[i][s] = weights.value(i + ONE_DIM*s);
98  }
99 }
100 
101 template <int Sign> inline
102 void osl::eval::ml::
103 RookMobilityAll::adjust(const NumEffectState& state,
104  bool promoted, int vertical, int horizontal,
105  Square position, MultiInt& value)
106 {
107  int offset=0;
108  if(promoted) offset=9;
109  if(Sign>0)
110  {
111  if (! promoted) {
112  const Square king = state.kingSquare<BLACK>();
113  value += (xkingx_table[indexXKingX<Sign>(position, king, vertical, true)]
114  + xkingx_table[indexXKingX<Sign>(position, king, horizontal, false)]
115  + sumkingx_table[vertical + horizontal + 17*std::abs(king.x()-position.x())]
116  );
117  }
118  value+= (rook_vertical_table[vertical+offset]+
119  rook_horizontal_table[horizontal+offset] +
120  sum_table[vertical+horizontal+(promoted ? 17 : 0)] +
121  x_table[indexX(position, promoted, vertical, true)] +
122  x_table[indexX(position, promoted, horizontal, false)] +
123  y_table[indexY<Sign>(position, promoted, vertical, true)] +
124  y_table[indexY<Sign>(position, promoted, horizontal, false)]);
125  }
126  else{ // Sign<0
127  if (! promoted) {
128  const Square king = state.kingSquare<WHITE>();
129  value -= (xkingx_table[indexXKingX<Sign>(position, king, vertical, true)]
130  + xkingx_table[indexXKingX<Sign>(position, king, horizontal, false)]
131  + sumkingx_table[vertical + horizontal + 17*std::abs(king.x()-position.x())]
132  );
133  }
134  value-= (rook_vertical_table[vertical+offset]+
135  rook_horizontal_table[horizontal+offset] +
136  sum_table[vertical+horizontal+(promoted ? 17 : 0)] +
137  x_table[indexX(position, promoted, vertical, true)] +
138  x_table[indexX(position, promoted, horizontal, false)] +
139  y_table[indexY<Sign>(position, promoted, vertical, true)] +
140  y_table[indexY<Sign>(position, promoted, horizontal, false)]);
141  }
142 }
143 
144 void osl::eval::ml::
145 RookMobilityAll::eval(const NumEffectState& state, MultiInt& out)
146 {
147  out.clear();
148  for (int i = PtypeTraits<ROOK>::indexMin;
149  i < PtypeTraits<ROOK>::indexLimit;
150  ++i)
151  {
152  const Piece rook = state.pieceOf(i);
153  if (! rook.isOnBoard())
154  continue;
155  if (rook.owner() == BLACK)
156  {
157  const int vertical = osl::mobility::RookMobility::countVerticalAll<BLACK>(state,i);
158  const int horizontal = osl::mobility::RookMobility::countHorizontalAll<BLACK>(
159  state, i);
160  adjust<1>(state, rook.isPromoted(), vertical, horizontal, rook.square(), out);
161  }
162  else
163  {
164  const int vertical = osl::mobility::RookMobility::countVerticalAll<WHITE>(state,i);
165  const int horizontal = osl::mobility::RookMobility::countHorizontalAll<WHITE>(
166  state, i);
167  adjust<-1>(state, rook.isPromoted(), vertical, horizontal, rook.square(), out);
168  }
169  }
170 }
171 
172 
173 
174 
175 void osl::eval::ml::
176 BishopMobility::setUp(const Weights &weights,int stage)
177 {
178  for (size_t i = 0; i < 18; ++i)
179  {
180  BishopMobilityAll::bishop_table[i][stage] = weights.value(i);
181  }
182  for (size_t i = 0; i < 18; ++i)
183  {
184  BishopMobilityAll::bishop_table[i+18][stage] = weights.value(i + 18);
185  }
186 }
187 
188 void osl::eval::ml::
190 {
191  for (size_t i = 0; i < ONE_DIM; ++i)
192  {
193  for (int s=0; s<NStages; ++s)
194  BishopMobilityAll::each_table[i][s] = weights.value(i + ONE_DIM*s);
195  }
196 }
197 
198 template <int Sign> inline
199 void osl::eval::ml::
200 BishopMobilityAll::adjust(bool promoted, int mobility1, int mobility2,
201  MultiInt& value)
202 {
203  int count=0;
204  int each_offset = 0;
205  if(promoted)
206  {
207  count=18;
208  each_offset = 9;
209  }
210  if(Sign>0)
211  {
212  value += (bishop_table[mobility1 + mobility2 + count] +
213  each_table[mobility1 + each_offset] +
214  each_table[mobility2 + each_offset]);
215  }
216  else
217  {
218  value -= (bishop_table[mobility1 + mobility2 + count] +
219  each_table[mobility1 + each_offset] +
220  each_table[mobility2 + each_offset]);
221  }
222 }
223 
224 void osl::eval::ml::
225 BishopMobilityAll::eval(const NumEffectState& state, MultiInt& out)
226 {
227  out.clear();
228  for (int i = PtypeTraits<BISHOP>::indexMin;
229  i < PtypeTraits<BISHOP>::indexLimit;
230  ++i)
231  {
232  const Piece bishop = state.pieceOf(i);
233  if (! bishop.isOnBoard())
234  continue;
235  if (bishop.owner() == BLACK)
236  {
237  const int mobility1 =
238  mobility::BishopMobility::countAllDir<BLACK, UL>(state, bishop) +
239  mobility::BishopMobility::countAllDir<BLACK, DR>(state, bishop);
240  const int mobility2 =
241  mobility::BishopMobility::countAllDir<BLACK, UR>(state, bishop) +
242  mobility::BishopMobility::countAllDir<BLACK, DL>(state, bishop);
243  adjust<1>(bishop.isPromoted(), mobility1, mobility2, out);
244  }
245  else
246  {
247  const int mobility1 =
248  mobility::BishopMobility::countAllDir<WHITE, UL>(state, bishop) +
249  mobility::BishopMobility::countAllDir<WHITE, DR>(state, bishop);
250  const int mobility2 =
251  mobility::BishopMobility::countAllDir<WHITE, UR>(state, bishop) +
252  mobility::BishopMobility::countAllDir<WHITE, DL>(state, bishop);
253  adjust<-1>(bishop.isPromoted(), mobility1, mobility2, out);
254  }
255  }
256 }
257 
258 
259 
260 void osl::eval::ml::
261 LanceMobility::setUp(const Weights &weights,int stage)
262 {
263  for (size_t i = 0; i < 9; ++i)
264  {
265  lance_table[i][stage] = weights.value(i);
266  }
267 }
268 
269 template <int Sign> inline
270 void osl::eval::ml::
271 LanceMobilityAll::adjust(int mobility, MultiInt& value)
272 {
273  if(Sign>0)
274  value += LanceMobility::lance_table[mobility];
275  else
276  value -= LanceMobility::lance_table[mobility];
277 }
278 
279 void osl::eval::ml::
280 LanceMobilityAll::eval(const NumEffectState &state, MultiInt& out)
281 {
282  out.clear();
283  for (int i = PtypeTraits<LANCE>::indexMin;
284  i < PtypeTraits<LANCE>::indexLimit;
285  ++i)
286  {
287  const Piece lance = state.pieceOf(i);
288  if (!lance.isOnBoardNotPromoted())
289  continue;
290  if (lance.pieceIsBlack())
291  {
292  const int mobility = osl::mobility::LanceMobility::countAll<BLACK>(
293  state, lance.square(),i);
294  adjust<1>(mobility, out);
295  }
296  else
297  {
298  const int mobility = osl::mobility::LanceMobility::countAll<WHITE>(
299  state, lance.square(),i);
300  adjust<-1>(mobility, out);
301  }
302  }
303 }
304 
305 // ;;; Local Variables:
306 // ;;; mode:c++
307 // ;;; c-basic-offset:2
308 // ;;; End: