progress.h
Go to the documentation of this file.
1 /* newProgress.h */
2 #ifndef PROGRESS_EXPERIMENTAL_NEW_PROGRESS_H
3 #define PROGRESS_EXPERIMENTAL_NEW_PROGRESS_H
4 
5 #include "osl/numEffectState.h"
6 #include "osl/eval/midgame.h"
7 #include "osl/container.h"
8 #include <cstdlib>
9 namespace osl
10 {
11  namespace progress
12  {
13  template <int N>
14  class ProgressN
15  {
16  int progress;
17  public:
18  explicit ProgressN(int value=0) : progress(value)
19  {
20  assert(isValid());
21  }
22  int value() const { return progress; }
23  bool isValid() const {
24  return (progress >= 0) && (progress < N);
25  }
26  };
27  template <int N>
29  {
30  return l.value() == r.value();
31  }
32  template <int N>
34  {
35  return ! (l == r);
36  }
37  template <int N>
38  inline bool operator<(ProgressN<N> l, ProgressN<N> r)
39  {
40  return l.value() < r.value();
41  }
44 
45  namespace ml
46  {
48  {
49  enum Feature
50  {
59  FEATURE_LIMIT
60  };
63  };
64 
66  {
69  CArray<int, 2> progresses, attack5x5_progresses, stand_progresses,
70  effect_progresses, defenses;
71  CArray<int, 2> rook, bishop, gold, silver, promoted,
72  king_relative_attack, king_relative_defense, non_pawn_ptype_attacked_pair;
73  int pawn_facing, promotion37, piecestand7;
74  };
75  class NewProgress : private NewProgressData
76  {
77  public:
78  enum { ProgressScale = 2 };
79  private:
80  static bool initialized_flag;
93  static int max_progress;
94  void updatePieceKingRelativeBonus(const NumEffectState &state);
95  void updateNonPawnAttackedPtypePair(const NumEffectState& state);
96  template <Player Owner>
97  void updateNonPawnAttackedPtypePairOne(const NumEffectState& state);
98  void updatePawnFacing(const NumEffectState& state);
99  template <Player Attack>
100  void promotion37One(const NumEffectState& state, int rank);
101  void updatePromotion37(const NumEffectState& state);
102  void updatePieceStand7(const NumEffectState& state);
103  template <Player P>
104  static void progressOne(const NumEffectState &state,
105  int &attack, int &defense);
106  template <Player P>
107  void updateAttack5x5PiecesAndState(const NumEffectState &state);
108  template <Player P>
109  void updateAttack5x5Pieces(PieceMask, const NumEffectState&);
110  template <Player P>
111  int attack5x5Value(const NumEffectState &state) const;
112  template <Player P>
113  static int index(Square king, Square target)
114  {
115  const int x_diff = std::abs(king.x() - target.x()); // [0, 4]
116  const int y_diff = (P == BLACK ? king.y() - target.y() :
117  target.y() - king.y()) + 2; // [-2, 2] + 2
118  return x_diff * 5 + y_diff;
119  }
120  template <Player P>
121  static int indexX(Square king, Square target)
122  {
123  int target_x = (king.x() > 5 ? 10 - king.x() : king.x()); // [1, 5]
124  int x_diff = king.x() - target.x(); // [-4, 4]
125  if (P == BLACK && king.x() >= 6)
126  {
127  x_diff = -x_diff;
128  }
129  else if (P == WHITE && king.x() >= 5)
130  {
131  x_diff = -x_diff;
132  }
133  const int y_diff = (P == BLACK ? king.y() - target.y() :
134  target.y() - king.y()) + 2; // [-2, 2] + 2
135  return ((x_diff + 4) * 5 + y_diff) * 5 + target_x - 1;
136  }
137  template <Player P>
138  static int indexY(Square king, Square target)
139  {
140  const int x_diff = std::abs(king.x() - target.x()); // [0, 4]
141  const int y_diff = (P == BLACK ? king.y() - target.y() :
142  target.y() - king.y()) + 2; // [-2, 2] + 2
143  const int king_y = (P == BLACK ? king.y() : 10 - king.y()); // [1, 9]
144  return (x_diff * 5 + y_diff) * 9 + king_y - 1;
145  }
146  static int index5x5(int rook, int bishop, int gold, int silver,
147  int promoted)
148  {
149  assert(0 <= promoted && promoted <= 4);
150  return promoted + 5 * (silver + 5 * (gold + 5 * (bishop + 3 * rook)));
151  }
152  static int index5x5x(int rook, int bishop, int gold, int silver,
153  int promoted, int king_x)
154  {
155  assert(0 <= promoted && promoted <= 4);
156  return king_x - 1 +
157  5 * (promoted + 5 * (silver + 5 * (gold + 5 * (bishop + 3 * rook))));
158  }
159  static int index5x5y(int rook, int bishop, int gold, int silver,
160  int promoted, int king_y)
161  {
162  assert(0 <= promoted && promoted <= 4);
163  return king_y - 1 +
164  9 * (promoted + 5 * (silver + 5 * (gold + 5 * (bishop + 3 * rook))));
165  }
166  template <Player P>
167  static int indexPerEffect(Square king, Square target,
168  int count)
169  {
170  const int x_diff = std::abs(king.x() - target.x()); // [0, 4]
171  const int y_diff = (P == BLACK ? king.y() - target.y() :
172  target.y() - king.y()) + 2; // [-2, 2] + 2
173  return x_diff * 5 + y_diff + std::min(8, count) * 25;
174  }
175 
176  template <Player P>
177  static int indexPerEffectY(Square king, Square target,
178  int count)
179  {
180  const int king_y = (P == BLACK ? king.y() : 10 - king.y());
181  const int x_diff = std::abs(king.x() - target.x()); // [0, 4]
182  const int y_diff = (P == BLACK ? king.y() - target.y() :
183  target.y() - king.y()) + 2; // [-2, 2] + 2
184  return king_y - 1 + 9 * (x_diff * 5 + y_diff + std::min(8, count) * 25);
185  }
186  template <Player P>
187  static int indexPerEffectX(Square king, Square target,
188  int count)
189  {
190  const int king_x = (king.x() > 5 ? 10 - king.x() : king.x());
191  int x_diff = king.x() - target.x(); // [-4, 4]
192  if ((P == BLACK && (king.x() > 5)) ||
193  (P == WHITE && (king.x() >= 5)))
194  x_diff = -x_diff;
195  const int y_diff = (P == BLACK ? king.y() - target.y() :
196  target.y() - king.y()) + 2; // [-2, 2] + 2
197  return king_x - 1 + 5 * (x_diff + 4 +
198  9 * (y_diff + 5 * std::min(8, count)));
199  }
200  template <Player P>
201  static int indexRelative(const Square king,
202  const Ptype ptype, const Square pos)
203  {
204  const int x = std::abs(pos.x() - king.x());
205  const int y = (king.y() - pos.y()) *
206  (P == osl::BLACK ? 1 : -1) + 8;
207  return (ptype - osl::PTYPE_PIECE_MIN) * 17 * 9 + (x * 17 + y);
208  }
209  static int indexRelative(const Player player, const Square king,
210  const Piece piece)
211  {
212  if (player == BLACK)
213  {
214  return indexRelative<BLACK>(king, piece.ptype(),
215  piece.square());
216  }
217  else
218  {
219  return indexRelative<WHITE>(king, piece.ptype(),
220  piece.square());
221  }
222  }
223  public:
224  NewProgress(const NumEffectState &state);
225  int progress() const
226  {
227  return
228  std::max(std::min(progresses[0] + progresses[1] +
229  attack5x5_progresses[0] +
230  attack5x5_progresses[1] +
231  stand_progresses[0] + stand_progresses[1] +
232  effect_progresses[0] + effect_progresses[1] +
233  defenses[0] + defenses[1] +
234  king_relative_attack[0] +
235  king_relative_attack[1] +
236  king_relative_defense[0] +
237  king_relative_defense[1] +
238  non_pawn_ptype_attacked_pair[0] +
239  non_pawn_ptype_attacked_pair[1] +
240  pawn_facing + promotion37 + piecestand7,
241  max_progress-ProgressScale), 0) / ProgressScale;
242  }
243  static int maxProgress() { return max_progress / ProgressScale; }
244  template<Player P>
245  void updateSub(const NumEffectState &new_state, Move last_move);
246  void update(const NumEffectState &new_state, Move last_move){
247  if(new_state.turn()==BLACK)
248  updateSub<WHITE>(new_state,last_move);
249  else
250  updateSub<BLACK>(new_state,last_move);
251  }
252  NewProgressDebugInfo debugInfo() const;
253  private:
254  template<Player P>
255  void updateMain(const NumEffectState &new_state, Move last_move);
256  public:
257  const Progress16 progress16() const
258  {
259  return Progress16(16 * progress() / maxProgress());
260  }
261  const Progress16 progress16(Player p) const
262  {
263  assert(maxProgress() > 0);
264  return Progress16(
265  16 * std::max(
266  std::min(progresses[playerToIndex(alt(p))] +
267  attack5x5_progresses[playerToIndex(alt(p))] +
268  stand_progresses[playerToIndex(alt(p))] +
269  effect_progresses[playerToIndex(alt(p))] +
270  defenses[playerToIndex(alt(p))] +
271  king_relative_attack[playerToIndex(alt(p))] +
272  king_relative_defense[playerToIndex(p)] +
273  non_pawn_ptype_attacked_pair[p],
274  max_progress-ProgressScale), 0)
275  / ProgressScale / maxProgress());
276  }
277  // p == attack player, alt(p) == king owner
278  const Progress16 progressAttack(Player p) const
279  {
280  assert(maxProgress() > 0);
281  return Progress16(
282  8 * std::max(
283  std::min(progresses[alt(p)] +
284  attack5x5_progresses[alt(p)] +
285  stand_progresses[alt(p)] +
286  effect_progresses[alt(p)] +
287  king_relative_attack[alt(p)],
288  max_progress-ProgressScale), -max_progress+ProgressScale)
289  / ProgressScale / maxProgress() + 8);
290  }
291  // p == king owner (defense player)
292  const Progress16 progressDefense(Player p) const
293  {
294  assert(maxProgress() > 0);
295  return Progress16(
296  8 * std::max(
297  std::min(defenses[alt(p)] +
298  king_relative_defense[p] +
299  non_pawn_ptype_attacked_pair[p],
300  max_progress-ProgressScale),
301  -max_progress + ProgressScale)
302  / ProgressScale / maxProgress() + 8);
303  }
304  static bool initialized()
305  {
306  return initialized_flag;
307  }
308  static bool setUp(const char *filename);
309  static bool setUp();
310  static std::string defaultFilename();
311  const NewProgressData rawData() const { return *this; }
312  };
313  bool operator==(const NewProgressData& l, const NewProgressData& r);
314  inline bool operator==(const NewProgress& l, const NewProgress& r)
315  {
316  return l.rawData() == r.rawData();
317  }
318  }
319  using ml::NewProgress;
320  }
321  using progress::Progress16;
322  using progress::Progress32;
323  using progress::NewProgress;
324 }
325 
326 #endif // PROGRESS_EXPERIMENTAL_NEW_PROGRESS_H
327 // ;;; Local Variables:
328 // ;;; mode:c++
329 // ;;; c-basic-offset:2
330 // ;;; End:
int max(Player p, int v1, int v2)
Definition: evalTraits.h:84
static int index(Square king, Square target)
Definition: progress.h:113
static int index5x5y(int rook, int bishop, int gold, int silver, int promoted, int king_y)
Definition: progress.h:159
static CArray< int, 56 > piecestand7_weight
Definition: progress.h:92
static int indexPerEffectY(Square king, Square target, int count)
Definition: progress.h:177
constexpr Player alt(Player player)
Definition: basic_type.h:13
Ptype ptype() const
Definition: basic_type.h:821
ProgressN< 32 > Progress32
Definition: progress.h:43
int min(Player p, int v1, int v2)
Definition: evalTraits.h:92
bool isValid() const
Definition: progress.h:23
int y() const
将棋としてのY座標を返す.
Definition: basic_type.h:567
CArray< int, 2 > stand_progresses
Definition: progress.h:69
CArray< int, FEATURE_LIMIT > white_values
Definition: progress.h:62
bool operator!=(ProgressN< N > l, ProgressN< N > r)
Definition: progress.h:33
static CArray< int, Piece::SIZE > stand_weight
Definition: progress.h:81
int x() const
将棋としてのX座標を返す.
Definition: basic_type.h:563
static int indexX(Square king, Square target)
Definition: progress.h:121
static int indexRelative(const Player player, const Square king, const Piece piece)
Definition: progress.h:209
const Progress16 progressAttack(Player p) const
Definition: progress.h:278
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:83
ProgressN< 16 > Progress16
Definition: progress.h:42
static int indexPerEffect(Square king, Square target, int count)
Definition: progress.h:167
static int index5x5x(int rook, int bishop, int gold, int silver, int promoted, int king_x)
Definition: progress.h:152
static CArray< int, 4284 > king_relative_weight
Definition: progress.h:88
CArray< int, FEATURE_LIMIT > black_values
Definition: progress.h:61
bool operator==(ProgressN< N > l, ProgressN< N > r)
Definition: progress.h:28
static CArray< int, 75 > effectstate_weight
Definition: progress.h:85
圧縮していない moveの表現 .
Definition: basic_type.h:1051
void update(const NumEffectState &new_state, Move last_move)
Definition: progress.h:246
駒番号のビットセット.
Definition: pieceMask.h:20
static CArray< int, 81 *15 *10 > defense_relative
Definition: progress.h:87
constexpr int playerToIndex(Player player)
Definition: basic_type.h:16
static int indexRelative(const Square king, const Ptype ptype, const Square pos)
Definition: progress.h:201
Player turn() const
Definition: simpleState.h:220
CArray< MultiInt, 2 > non_pawn_ptype_attacked_pair_eval
Definition: progress.h:67
利きを持つ局面
const Progress16 progress16(Player p) const
Definition: progress.h:261
const Square square() const
Definition: basic_type.h:832
static int index5x5(int rook, int bishop, int gold, int silver, int promoted)
Definition: progress.h:146
const Progress16 progress16() const
Definition: progress.h:257
static CArray< int, 5625 > attack5x5_x_weight
Definition: progress.h:83
static CArray< int, 16 > promotion37_weight
Definition: progress.h:91
static CArray< int, 262144 > attacked_ptype_pair_weight
Definition: progress.h:89
ProgressN(int value=0)
Definition: progress.h:18
static CArray< int, 10125 > attack5x5_y_weight
Definition: progress.h:84
Player
Definition: basic_type.h:8
static CArray< int, 81 *15 *10 > attack_relative
Definition: progress.h:86
const Progress16 progressDefense(Player p) const
Definition: progress.h:292
static int indexPerEffectX(Square king, Square target, int count)
Definition: progress.h:187
static int indexY(Square king, Square target)
Definition: progress.h:138
static CArray< int, 10 > pawn_facing_weight
Definition: progress.h:90
const NewProgressData rawData() const
Definition: progress.h:311
static CArray< int, 1125 > attack5x5_weight
Definition: progress.h:82