newProgress.h Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
newProgress.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/state/numEffectState.h"
6 #include "osl/progress/progress16.h"
7 #include "osl/eval/ml/midgame.h"
8 #include "osl/centering5x3.h"
9 #include "osl/misc/carray.h"
10 namespace osl
11 {
12 namespace progress
13 {
14 namespace ml
15 {
16 struct NewProgressDebugInfo
17 {
18 enum Feature
19 {
20 ATTACK_5X3,
21 DEFENSE_5X3,
22 ATTACK5X5,
23 STAND,
24 EFFECT5X5,
25 KING_RELATIVE_ATTACK,
26 KING_RELATIVE_DEFENSE,
27 NON_PAWN_ATTACKED_PAIR,
28 FEATURE_LIMIT
29 };
30 CArray<int, FEATURE_LIMIT> black_values;
31 CArray<int, FEATURE_LIMIT> white_values;
32 };
33
34 struct NewProgressData
35 {
36 CArray<MultiInt,2> non_pawn_ptype_attacked_pair_eval;
37 MultiInt promotion37_eval;
38 CArray<int, 2> progresses, attack5x5_progresses, stand_progresses,
39 effect_progresses, defenses;
40 CArray<int, 2> rook, bishop, gold, silver, promoted,
41 king_relative_attack, king_relative_defense, non_pawn_ptype_attacked_pair;
42 int pawn_facing, promotion37, piecestand7;
43 };
44 class NewProgress : private NewProgressData
45 {
46 public:
47 enum { ProgressScale = 2 };
48 private:
49 static bool initialized_flag;
50 static CArray<int, Piece::SIZE> stand_weight;
51 static CArray<int, 1125> attack5x5_weight;
52 static CArray<int, 5625> attack5x5_x_weight;
53 static CArray<int, 10125> attack5x5_y_weight;
54 static CArray<int, 75> effectstate_weight;
55 static CArray<int, 81*15*10> attack_relative;
56 static CArray<int, 81*15*10> defense_relative;
57 static CArray<int, 4284> king_relative_weight;
58 static CArray<int, 262144> attacked_ptype_pair_weight;
59 static CArray<int, 10> pawn_facing_weight;
60 static CArray<int, 16> promotion37_weight;
61 static CArray<int, 56> piecestand7_weight;
62 static int max_progress;
63 void updatePieceKingRelativeBonus(const NumEffectState &state);
64 void updateNonPawnAttackedPtypePair(const NumEffectState& state);
65 template <Player Owner>
66 void updateNonPawnAttackedPtypePairOne(const NumEffectState& state);
67 void updatePawnFacing(const NumEffectState& state);
68 template <Player Attack>
69 void promotion37One(const NumEffectState& state, int rank);
70 void updatePromotion37(const NumEffectState& state);
71 void updatePieceStand7(const NumEffectState& state);
72 template <Player P>
73 static void progressOne(const NumEffectState &state,
74 int &attack, int &defense);
75 template <Player P>
76 void updateAttack5x5PiecesAndState(const NumEffectState &state);
77 template <Player P>
78 void updateAttack5x5Pieces(PieceMask, const NumEffectState&);
79 template <Player P>
80 int attack5x5Value(const NumEffectState &state) const;
81 template <Player P>
82 static int index(Square king, Square target)
83 {
84 const int x_diff = std::abs(king.x() - target.x()); // [0, 4]
85 const int y_diff = (P == BLACK ? king.y() - target.y() :
86 target.y() - king.y()) + 2; // [-2, 2] + 2
87 return x_diff * 5 + y_diff;
88 }
89 template <Player P>
90 static int indexX(Square king, Square target)
91 {
92 int target_x = (king.x() > 5 ? 10 - king.x() : king.x()); // [1, 5]
93 int x_diff = king.x() - target.x(); // [-4, 4]
94 if (P == BLACK && king.x() >= 6)
95 {
96 x_diff = -x_diff;
97 }
98 else if (P == WHITE && king.x() >= 5)
99 {
100 x_diff = -x_diff;
101 }
102 const int y_diff = (P == BLACK ? king.y() - target.y() :
103 target.y() - king.y()) + 2; // [-2, 2] + 2
104 return ((x_diff + 4) * 5 + y_diff) * 5 + target_x - 1;
105 }
106 template <Player P>
107 static int indexY(Square king, Square target)
108 {
109 const int x_diff = std::abs(king.x() - target.x()); // [0, 4]
110 const int y_diff = (P == BLACK ? king.y() - target.y() :
111 target.y() - king.y()) + 2; // [-2, 2] + 2
112 const int king_y = (P == BLACK ? king.y() : 10 - king.y()); // [1, 9]
113 return (x_diff * 5 + y_diff) * 9 + king_y - 1;
114 }
115 static int index5x5(int rook, int bishop, int gold, int silver,
116 int promoted)
117 {
118 assert(0 <= promoted && promoted <= 4);
119 return promoted + 5 * (silver + 5 * (gold + 5 * (bishop + 3 * rook)));
120 }
121 static int index5x5x(int rook, int bishop, int gold, int silver,
122 int promoted, int king_x)
123 {
124 assert(0 <= promoted && promoted <= 4);
125 return king_x - 1 +
126 5 * (promoted + 5 * (silver + 5 * (gold + 5 * (bishop + 3 * rook))));
127 }
128 static int index5x5y(int rook, int bishop, int gold, int silver,
129 int promoted, int king_y)
130 {
131 assert(0 <= promoted && promoted <= 4);
132 return king_y - 1 +
133 9 * (promoted + 5 * (silver + 5 * (gold + 5 * (bishop + 3 * rook))));
134 }
135 template <Player P>
136 static int indexPerEffect(Square king, Square target,
137 int count)
138 {
139 const int x_diff = std::abs(king.x() - target.x()); // [0, 4]
140 const int y_diff = (P == BLACK ? king.y() - target.y() :
141 target.y() - king.y()) + 2; // [-2, 2] + 2
142 return x_diff * 5 + y_diff + std::min(8, count) * 25;
143 }
144
145 template <Player P>
146 static int indexPerEffectY(Square king, Square target,
147 int count)
148 {
149 const int king_y = (P == BLACK ? king.y() : 10 - king.y());
150 const int x_diff = std::abs(king.x() - target.x()); // [0, 4]
151 const int y_diff = (P == BLACK ? king.y() - target.y() :
152 target.y() - king.y()) + 2; // [-2, 2] + 2
153 return king_y - 1 + 9 * (x_diff * 5 + y_diff + std::min(8, count) * 25);
154 }
155 template <Player P>
156 static int indexPerEffectX(Square king, Square target,
157 int count)
158 {
159 const int king_x = (king.x() > 5 ? 10 - king.x() : king.x());
160 int x_diff = king.x() - target.x(); // [-4, 4]
161 if ((P == BLACK && (king.x() > 5)) ||
162 (P == WHITE && (king.x() >= 5)))
163 x_diff = -x_diff;
164 const int y_diff = (P == BLACK ? king.y() - target.y() :
165 target.y() - king.y()) + 2; // [-2, 2] + 2
166 return king_x - 1 + 5 * (x_diff + 4 +
167 9 * (y_diff + 5 * std::min(8, count)));
168 }
169 template <Player P>
170 static int indexRelative(const Square king,
171 const Ptype ptype, const Square pos)
172 {
173 const int x = std::abs(pos.x() - king.x());
174 const int y = (king.y() - pos.y()) *
175 (P == osl::BLACK ? 1 : -1) + 8;
176 return (ptype - osl::PTYPE_PIECE_MIN) * 17 * 9 + (x * 17 + y);
177 }
178 static int indexRelative(const Player player, const Square king,
179 const Piece piece)
180 {
181 if (player == BLACK)
182 {
183 return indexRelative<BLACK>(king, piece.ptype(),
184 piece.square());
185 }
186 else
187 {
188 return indexRelative<WHITE>(king, piece.ptype(),
189 piece.square());
190 }
191 }
192 public:
193 NewProgress(const NumEffectState &state);
194 int progress() const
195 {
196 return
197 std::max(std::min(progresses[0] + progresses[1] +
198 attack5x5_progresses[0] +
199 attack5x5_progresses[1] +
200 stand_progresses[0] + stand_progresses[1] +
201 effect_progresses[0] + effect_progresses[1] +
202 defenses[0] + defenses[1] +
203 king_relative_attack[0] +
204 king_relative_attack[1] +
205 king_relative_defense[0] +
206 king_relative_defense[1] +
207 non_pawn_ptype_attacked_pair[0] +
208 non_pawn_ptype_attacked_pair[1] +
209 pawn_facing + promotion37 + piecestand7,
210 max_progress-ProgressScale), 0) / ProgressScale;
211 }
212 static int maxProgress() { return max_progress / ProgressScale; }
213 template<Player P>
214 void updateSub(const NumEffectState &new_state, Move last_move);
215 void update(const NumEffectState &new_state, Move last_move){
216 if(new_state.turn()==BLACK)
217 updateSub<WHITE>(new_state,last_move);
218 else
219 updateSub<BLACK>(new_state,last_move);
220 }
221 NewProgressDebugInfo debugInfo() const;
222 private:
223 template<Player P>
224 void updateMain(const NumEffectState &new_state, Move last_move);
225 public:
226 const Progress16 progress16() const
227 {
228 return Progress16(16 * progress() / maxProgress());
229 }
230 const Progress16 progress16(Player p) const
231 {
232 assert(maxProgress() > 0);
233 return Progress16(
234 16 * std::max(
235 std::min(progresses[playerToIndex(alt(p))] +
236 attack5x5_progresses[playerToIndex(alt(p))] +
237 stand_progresses[playerToIndex(alt(p))] +
238 effect_progresses[playerToIndex(alt(p))] +
239 defenses[playerToIndex(alt(p))] +
240 king_relative_attack[playerToIndex(alt(p))] +
241 king_relative_defense[playerToIndex(p)] +
242 non_pawn_ptype_attacked_pair[p],
243 max_progress-ProgressScale), 0)
244 / ProgressScale / maxProgress());
245 }
246 // p == attack player, alt(p) == king owner
247 const Progress16 progressAttack(Player p) const
248 {
249 assert(maxProgress() > 0);
250 return Progress16(
251 8 * std::max(
252 std::min(progresses[alt(p)] +
253 attack5x5_progresses[alt(p)] +
254 stand_progresses[alt(p)] +
255 effect_progresses[alt(p)] +
256 king_relative_attack[alt(p)],
257 max_progress-ProgressScale), -max_progress+ProgressScale)
258 / ProgressScale / maxProgress() + 8);
259 }
260 // p == king owner (defense player)
261 const Progress16 progressDefense(Player p) const
262 {
263 assert(maxProgress() > 0);
264 return Progress16(
265 8 * std::max(
266 std::min(defenses[alt(p)] +
267 king_relative_defense[p] +
268 non_pawn_ptype_attacked_pair[p],
269 max_progress-ProgressScale),
270 -max_progress + ProgressScale)
271 / ProgressScale / maxProgress() + 8);
272 }
273 static bool initialized()
274 {
275 return initialized_flag;
276 }
277 static bool setUp(const char *filename);
278 static bool setUp();
279 static std::string defaultFilename();
280 const NewProgressData rawData() const { return *this; }
281 };
282 bool operator==(const NewProgressData& l, const NewProgressData& r);
283 inline bool operator==(const NewProgress& l, const NewProgress& r)
284 {
285 return l.rawData() == r.rawData();
286 }
287 }
288 using ml::NewProgress;
289 }
290 using progress::NewProgress;
291 }
292
293 #endif // PROGRESS_EXPERIMENTAL_NEW_PROGRESS_H
294 // ;;; Local Variables:
295 // ;;; mode:c++
296 // ;;; c-basic-offset:2
297 // ;;; End: