My Project
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>
39  {
40  return l.value() < r.value();
41  }
44 
45  namespace ml
46  {
48  {
49  enum Feature
50  {
60  };
63  };
64 
66  {
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;
96  template <Player Owner>
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>
108  template <Player P>
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
233  defenses[0] + defenses[1] +
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  }
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(
270  defenses[playerToIndex(alt(p))] +
275  / ProgressScale / maxProgress());
276  }
277  // p == attack player, alt(p) == king owner
279  {
280  assert(maxProgress() > 0);
281  return Progress16(
282  8 * std::max(
283  std::min(progresses[alt(p)] +
285  stand_progresses[alt(p)] +
286  effect_progresses[alt(p)] +
289  / ProgressScale / maxProgress() + 8);
290  }
291  // p == king owner (defense player)
293  {
294  assert(maxProgress() > 0);
295  return Progress16(
296  8 * std::max(
297  std::min(defenses[alt(p)] +
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:
osl::progress::ml::NewProgressDebugInfo::white_values
CArray< int, FEATURE_LIMIT > white_values
Definition: progress.h:62
osl::progress::ml::NewProgress::effectstate_weight
static CArray< int, 75 > effectstate_weight
Definition: progress.h:85
osl::progress::ml::NewProgressData::promotion37
int promotion37
Definition: progress.h:73
osl::progress::Progress16
ProgressN< 16 > Progress16
Definition: progress.h:42
osl::PTYPE_PIECE_MIN
@ PTYPE_PIECE_MIN
Definition: basic_type.h:104
osl::Square
Definition: basic_type.h:532
osl::progress::ml::NewProgressData::king_relative_defense
CArray< int, 2 > king_relative_defense
Definition: progress.h:72
osl::progress::ml::NewProgressDebugInfo::ATTACK_5X3
@ ATTACK_5X3
Definition: progress.h:51
osl::progress::ml::NewProgress::maxProgress
static int maxProgress()
Definition: progress.h:243
osl::eval::min
int min(Player p, int v1, int v2)
Definition: evalTraits.h:92
osl::container::QuadInt
Definition: quadInt.h:43
osl::WHITE
@ WHITE
Definition: basic_type.h:10
osl::progress::ml::NewProgress::updatePieceKingRelativeBonus
void updatePieceKingRelativeBonus(const NumEffectState &state)
Definition: progress.cc:410
osl::progress::ml::NewProgress::index5x5y
static int index5x5y(int rook, int bishop, int gold, int silver, int promoted, int king_y)
Definition: progress.h:159
osl::progress::ml::NewProgressDebugInfo::Feature
Feature
Definition: progress.h:50
osl::progress::ml::NewProgress::promotion37One
void promotion37One(const NumEffectState &state, int rank)
Definition: progress.cc:492
osl::progress::ml::NewProgress::attack_relative
static CArray< int, 81 *15 *10 > attack_relative
Definition: progress.h:86
osl::progress::ml::NewProgress::progress
int progress() const
Definition: progress.h:225
osl::progress::ml::NewProgress::attack5x5_y_weight
static CArray< int, 10125 > attack5x5_y_weight
Definition: progress.h:84
osl::alt
constexpr Player alt(Player player)
Definition: basic_type.h:13
osl::progress::ml::NewProgress::update
void update(const NumEffectState &new_state, Move last_move)
Definition: progress.h:246
osl::progress::ml::NewProgress::updateSub
void updateSub(const NumEffectState &new_state, Move last_move)
Definition: progress.cc:684
osl::progress::ml::NewProgress::initialized
static bool initialized()
Definition: progress.h:304
osl::progress::ml::NewProgressData::promoted
CArray< int, 2 > promoted
Definition: progress.h:71
osl::progress::ml::NewProgress::indexPerEffectX
static int indexPerEffectX(Square king, Square target, int count)
Definition: progress.h:187
osl::progress::ml::NewProgressData::bishop
CArray< int, 2 > bishop
Definition: progress.h:71
osl::progress::ml::NewProgress::rawData
const NewProgressData rawData() const
Definition: progress.h:311
osl::progress::ml::NewProgressDebugInfo::NON_PAWN_ATTACKED_PAIR
@ NON_PAWN_ATTACKED_PAIR
Definition: progress.h:58
osl::progress::operator==
bool operator==(ProgressN< N > l, ProgressN< N > r)
Definition: progress.h:28
osl::Move
圧縮していない moveの表現 .
Definition: basic_type.h:1052
osl::progress::ml::NewProgress::progress16
const Progress16 progress16() const
Definition: progress.h:257
osl::progress::ProgressN
Definition: progress.h:15
osl::progress::operator<
bool operator<(ProgressN< N > l, ProgressN< N > r)
Definition: progress.h:38
osl::progress::ml::NewProgress::updatePromotion37
void updatePromotion37(const NumEffectState &state)
Definition: progress.cc:523
osl::progress::ml::NewProgress::indexPerEffect
static int indexPerEffect(Square king, Square target, int count)
Definition: progress.h:167
osl::progress::ml::NewProgress::NewProgress
NewProgress(const NumEffectState &state)
Definition: progress.cc:546
osl::progress::ml::NewProgress::updateAttack5x5Pieces
void updateAttack5x5Pieces(PieceMask, const NumEffectState &)
Definition: progress.cc:367
osl::progress::ml::NewProgress::promotion37_weight
static CArray< int, 16 > promotion37_weight
Definition: progress.h:91
osl::progress::ml::NewProgressDebugInfo::DEFENSE_5X3
@ DEFENSE_5X3
Definition: progress.h:52
osl::progress::ml::NewProgressData::effect_progresses
CArray< int, 2 > effect_progresses
Definition: progress.h:70
osl::progress::ml::NewProgress::pawn_facing_weight
static CArray< int, 10 > pawn_facing_weight
Definition: progress.h:90
osl::progress::ml::NewProgress::progress16
const Progress16 progress16(Player p) const
Definition: progress.h:261
osl::progress::ProgressN::ProgressN
ProgressN(int value=0)
Definition: progress.h:18
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:84
osl::progress::ml::NewProgressData::stand_progresses
CArray< int, 2 > stand_progresses
Definition: progress.h:69
osl::progress::ml::NewProgress::progressDefense
const Progress16 progressDefense(Player p) const
Definition: progress.h:292
osl::Piece
駒.
Definition: basic_type.h:788
osl::progress::ml::NewProgress::stand_weight
static CArray< int, Piece::SIZE > stand_weight
Definition: progress.h:81
osl::progress::ml::NewProgress::indexRelative
static int indexRelative(const Player player, const Square king, const Piece piece)
Definition: progress.h:209
osl::eval::max
int max(Player p, int v1, int v2)
Definition: evalTraits.h:84
osl::progress::ml::NewProgress
Definition: progress.h:76
osl::progress::ml::NewProgress::debugInfo
NewProgressDebugInfo debugInfo() const
Definition: progress.cc:726
osl::progress::ProgressN::progress
int progress
Definition: progress.h:16
osl::progress::ml::NewProgressData::promotion37_eval
MultiInt promotion37_eval
Definition: progress.h:68
osl::progress::ml::operator==
bool operator==(const NewProgressData &l, const NewProgressData &r)
Definition: progress.cc:12
osl::progress::ml::NewProgressDebugInfo::KING_RELATIVE_ATTACK
@ KING_RELATIVE_ATTACK
Definition: progress.h:56
container.h
midgame.h
osl::progress::ml::NewProgressData::pawn_facing
int pawn_facing
Definition: progress.h:73
osl::progress::ml::NewProgress::attacked_ptype_pair_weight
static CArray< int, 262144 > attacked_ptype_pair_weight
Definition: progress.h:89
osl::progress::ml::NewProgress::updatePieceStand7
void updatePieceStand7(const NumEffectState &state)
Definition: progress.cc:532
osl::progress::ml::NewProgress::attack5x5Value
int attack5x5Value(const NumEffectState &state) const
Definition: progress.cc:386
osl::progress::ml::NewProgressData::rook
CArray< int, 2 > rook
Definition: progress.h:71
osl::progress::ml::NewProgress::updatePawnFacing
void updatePawnFacing(const NumEffectState &state)
Definition: progress.cc:476
osl::progress::ml::NewProgress::index5x5x
static int index5x5x(int rook, int bishop, int gold, int silver, int promoted, int king_x)
Definition: progress.h:152
osl::progress::ml::NewProgressData::non_pawn_ptype_attacked_pair_eval
CArray< MultiInt, 2 > non_pawn_ptype_attacked_pair_eval
Definition: progress.h:67
osl::progress::ml::NewProgress::max_progress
static int max_progress
Definition: progress.h:93
osl::progress::ml::NewProgress::updateMain
void updateMain(const NumEffectState &new_state, Move last_move)
Definition: progress.cc:590
osl::progress::ml::NewProgress::indexY
static int indexY(Square king, Square target)
Definition: progress.h:138
osl::progress::ml::NewProgress::updateAttack5x5PiecesAndState
void updateAttack5x5PiecesAndState(const NumEffectState &state)
Definition: progress.cc:336
osl::progress::ml::NewProgress::index
static int index(Square king, Square target)
Definition: progress.h:113
osl::progress::ml::NewProgressData::non_pawn_ptype_attacked_pair
CArray< int, 2 > non_pawn_ptype_attacked_pair
Definition: progress.h:72
osl::progress::ml::NewProgressDebugInfo
Definition: progress.h:48
osl::progress::ml::NewProgress::defense_relative
static CArray< int, 81 *15 *10 > defense_relative
Definition: progress.h:87
osl::Square::x
int x() const
将棋としてのX座標を返す.
Definition: basic_type.h:563
osl::NumEffectState
利きを持つ局面
Definition: numEffectState.h:34
osl::progress::ml::NewProgress::defaultFilename
static std::string defaultFilename()
Definition: progress.cc:288
osl::progress::ml::NewProgressData::attack5x5_progresses
CArray< int, 2 > attack5x5_progresses
Definition: progress.h:69
osl::playerToIndex
constexpr int playerToIndex(Player player)
Definition: basic_type.h:16
osl::progress::ml::NewProgressData
Definition: progress.h:66
osl::progress::ml::NewProgressData::king_relative_attack
CArray< int, 2 > king_relative_attack
Definition: progress.h:72
osl::Square::y
int y() const
将棋としてのY座標を返す.
Definition: basic_type.h:567
osl::progress::ProgressN::isValid
bool isValid() const
Definition: progress.h:23
osl::progress::ml::NewProgress::king_relative_weight
static CArray< int, 4284 > king_relative_weight
Definition: progress.h:88
osl::Piece::square
const Square square() const
Definition: basic_type.h:832
osl::progress::ml::NewProgress::attack5x5_weight
static CArray< int, 1125 > attack5x5_weight
Definition: progress.h:82
osl::progress::ml::NewProgressDebugInfo::EFFECT5X5
@ EFFECT5X5
Definition: progress.h:55
osl::Piece::ptype
Ptype ptype() const
Definition: basic_type.h:821
osl::progress::ml::NewProgressData::defenses
CArray< int, 2 > defenses
Definition: progress.h:70
osl::progress::ml::NewProgress::indexRelative
static int indexRelative(const Square king, const Ptype ptype, const Square pos)
Definition: progress.h:201
osl::progress::ml::NewProgress::updateNonPawnAttackedPtypePairOne
void updateNonPawnAttackedPtypePairOne(const NumEffectState &state)
Definition: progress.cc:436
osl::BLACK
@ BLACK
Definition: basic_type.h:9
osl::progress::ml::NewProgress::initialized_flag
static bool initialized_flag
Definition: progress.h:80
osl::progress::ml::NewProgress::setUp
static bool setUp()
Definition: progress.cc:283
osl::progress::ml::NewProgressData::silver
CArray< int, 2 > silver
Definition: progress.h:71
osl::progress::ml::NewProgress::piecestand7_weight
static CArray< int, 56 > piecestand7_weight
Definition: progress.h:92
osl::progress::ml::NewProgressDebugInfo::FEATURE_LIMIT
@ FEATURE_LIMIT
Definition: progress.h:59
osl::progress::ml::NewProgressDebugInfo::black_values
CArray< int, FEATURE_LIMIT > black_values
Definition: progress.h:61
osl::progress::ml::NewProgress::progressOne
static void progressOne(const NumEffectState &state, int &attack, int &defense)
Definition: progress.cc:296
osl::progress::ml::NewProgressData::piecestand7
int piecestand7
Definition: progress.h:73
osl::SimpleState::turn
Player turn() const
Definition: simpleState.h:220
osl::progress::ml::NewProgressData::progresses
CArray< int, 2 > progresses
Definition: progress.h:69
osl::progress::ml::NewProgressDebugInfo::STAND
@ STAND
Definition: progress.h:54
osl::progress::ml::NewProgress::progressAttack
const Progress16 progressAttack(Player p) const
Definition: progress.h:278
osl::Player
Player
Definition: basic_type.h:8
numEffectState.h
osl::progress::ml::NewProgress::indexPerEffectY
static int indexPerEffectY(Square king, Square target, int count)
Definition: progress.h:177
osl::progress::ml::NewProgress::indexX
static int indexX(Square king, Square target)
Definition: progress.h:121
osl::progress::ml::NewProgressData::gold
CArray< int, 2 > gold
Definition: progress.h:71
osl::progress::ml::NewProgressDebugInfo::ATTACK5X5
@ ATTACK5X5
Definition: progress.h:53
osl::CArray< int, FEATURE_LIMIT >
osl::progress::ml::NewProgress::ProgressScale
@ ProgressScale
Definition: progress.h:78
osl::progress::ml::NewProgressDebugInfo::KING_RELATIVE_DEFENSE
@ KING_RELATIVE_DEFENSE
Definition: progress.h:57
osl::progress::Progress32
ProgressN< 32 > Progress32
Definition: progress.h:43
osl::progress::ml::NewProgress::updateNonPawnAttackedPtypePair
void updateNonPawnAttackedPtypePair(const NumEffectState &state)
Definition: progress.cc:469
osl::progress::ProgressN::value
int value() const
Definition: progress.h:22
osl::PieceMask
駒番号のビットセット.
Definition: pieceMask.h:21
osl::progress::operator!=
bool operator!=(ProgressN< N > l, ProgressN< N > r)
Definition: progress.h:33
osl::progress::ml::NewProgress::index5x5
static int index5x5(int rook, int bishop, int gold, int silver, int promoted)
Definition: progress.h:146
osl::progress::ml::NewProgress::attack5x5_x_weight
static CArray< int, 5625 > attack5x5_x_weight
Definition: progress.h:83
osl
Definition: additionalEffect.h:6