My Project
patternGroup.cc
Go to the documentation of this file.
1/* patternGroup.cc
2 */
4#include <sstream>
5
7 : Group(name(d, d2)), direction(d), direction2(d2)
8{
9 for (int attack=0; attack<3; ++attack) {
10 for (int defense=0; defense<3; ++defense) {
11 for (int s=PTYPE_PIECE_MIN; s<= PTYPE_MAX; ++s) {
12 for (int t=PTYPE_PIECE_MIN; t<= PTYPE_MAX; ++t) {
13 push_back(new Pattern(d, d2, static_cast<Ptype>(s), static_cast<Ptype>(t), true, attack, defense));
14 push_back(new Pattern(d, d2, static_cast<Ptype>(s), static_cast<Ptype>(t), false, attack, defense));
15 }
16 push_back(new Pattern(d, d2, static_cast<Ptype>(s), PTYPE_EMPTY, true, attack, defense));
17 push_back(new Pattern(d, d2, static_cast<Ptype>(s), PTYPE_EDGE, true, attack, defense)); // redundant
18 }
19 }
20 }
21 target_table.fill(0);
22 for (int x=1; x<=9; ++x) {
23 for (int y=1; y<=9; ++y) {
24 const Square src(x,y);
25 const Square target_b = Pattern::nextSquare(BLACK, src, direction, direction2);
26 const Square target_w = Pattern::nextSquare(WHITE, src, direction, direction2);
27 target_table[playerToIndex(BLACK)][src.index()] = target_b.uintValue();
28 target_table[playerToIndex(WHITE)][src.index()] = target_w.uintValue();
29 }
30 }
31}
32
33int osl::rating::PatternGroup::findMatch(const NumEffectState& state, Move move, const RatingEnv& env) const
34{
35 const Ptype self = move.ptype();
36 const Square position
37 = Square::makeDirect(target_table[playerToIndex(move.player())][move.to().index()]);
38 assert(position == Pattern::nextSquare(move, direction, direction2));
39 const Piece p = (position == move.from()) ? Piece::EMPTY() : state.pieceAt(position);
40 const Ptype target = p.ptype();
41 if (env.pattern_cache[position.index()] < 0)
42 env.pattern_cache[position.index()] = CountEffect2::index(state, position, env)
44 const int base = env.pattern_cache[position.index()];
45
46 int index = base + (self - PTYPE_PIECE_MIN)*((PTYPE_MAX+1 - PTYPE_PIECE_MIN)*2 +2);
47 if (!isPiece(target)) {
48 index += (PTYPE_MAX+1-PTYPE_PIECE_MIN)*2 + (target == PTYPE_EMPTY ? 0 : 1);
49 }
50 else {
51 index += (target - PTYPE_PIECE_MIN)*2 + (p.owner() != move.player());
52 }
53 return index;
54}
55
56std::string osl::rating::PatternGroup::name(Direction direction, Direction direction2)
57{
58 std::ostringstream ss;
59 ss << "Pattern" << direction;
60 if (direction2 != Pattern::INVALID)
61 ss << direction2;
62 return ss.str();
63}
64
67
68std::string osl::rating::PatternLongGroup::name(int direction_id)
69{
70 std::ostringstream ss;
71 ss << "PatLong" << direction_id;
72 return ss.str();
73}
74
76 : Group(name(d)), direction_id(d)
77{
78 const CArray<Ptype,5> self_list = {{ ROOK, PROOK, BISHOP, PBISHOP, LANCE }};
79 for (int s=0; s<((d == 0) ? 5 : 4); ++s) {
80 const Ptype self = self_list[s];
81 const Direction direction = makeDirection(self);
82 for (int attack=0; attack<3; ++attack) {
83 for (int defense=0; defense<3; ++defense) {
84 for (int t=PTYPE_PIECE_MIN; t<= PTYPE_MAX; ++t) {
85 push_back(new PatternLong(direction, self, LongTarget(static_cast<Ptype>(t), true, true, attack, defense)));
86 push_back(new PatternLong(direction, self, LongTarget(static_cast<Ptype>(t), true, false, attack, defense)));
87 push_back(new PatternLong(direction, self, LongTarget(static_cast<Ptype>(t), false, true, attack, defense)));
88 push_back(new PatternLong(direction, self, LongTarget(static_cast<Ptype>(t), false, false, attack, defense)));
89 }
90 push_back(new PatternLong(direction, self, LongTarget(PTYPE_EMPTY, true, true, attack, defense)));
91 push_back(new PatternLong(direction, self, LongTarget(PTYPE_EMPTY, false, true, attack, defense)));
92 }
93 }
94 push_back(new PatternLong(direction, self, LongTarget(PTYPE_EDGE, true, true, 0, 0)));
95 }
96}
97
99{
100 const size_t unit = ((PTYPE_MAX+1-PTYPE_PIECE_MIN)*4+2)*9+1;
101 const Ptype self = move.ptype();
102 int base = 0;
103 switch (self) {
104 case ROOK:
105 break;
106 case PROOK:
107 base += unit; break;
108 case BISHOP:
109 base += unit*2; break;
110 case PBISHOP:
111 base += unit*3; break;
112 case LANCE:
113 if (direction_id != 0)
114 return -1;
115 base += unit*4; break;
116 default:
117 return -1;
118 }
119 const Direction direction = makeDirection(self);
120 const PieceSquare pp = PatternLong::find(state, move, direction);
121
122 int index = base;
123 if (pp.first.isEdge()) {
124 index += unit - 1;
125 } else {
126 index += ((PTYPE_MAX+1-PTYPE_PIECE_MIN)*4+2)*CountEffect2::index(state, pp.second, env);
127 if (pp.first.isEmpty()) {
128 index += (PTYPE_MAX+1-PTYPE_PIECE_MIN)*4;
129 index += ! LongTarget::isPromotable(move, pp.second);
130 }
131 else {
132 assert(pp.first.isPiece());
133 index += (pp.first.ptype()-PTYPE_PIECE_MIN)*4;
134 index += (! LongTarget::isPromotable(move, pp.second))*2;
135 index += (pp.first.owner() != move.player());
136 }
137 }
138 return index;
139}
140
141std::string osl::rating::PatternLongGroup2::name(int direction_id)
142{
143 std::ostringstream ss;
144 ss << "PatLong2" << direction_id;
145 return ss.str();
146}
147
149 : Group(name(d)), direction_id(d)
150{
151 const CArray<Ptype,5> self_list = {{ ROOK, PROOK, BISHOP, PBISHOP, LANCE }};
152 for (int s=0; s<((d == 0) ? 5 : 4); ++s) {
153 const Ptype self = self_list[s];
154 const Direction direction = makeDirection(self);
155 for (int t=PTYPE_PIECE_MIN; t<= PTYPE_MAX; ++t) {
156 push_back(new PatternLong2(direction, self, LongTarget2(static_cast<Ptype>(t), true)));
157 push_back(new PatternLong2(direction, self, LongTarget2(static_cast<Ptype>(t), false)));
158 }
159 push_back(new PatternLong2(direction, self, LongTarget2(PTYPE_EDGE, true)));
160 }
161}
162
164{
165 const size_t unit = (PTYPE_MAX+1-PTYPE_PIECE_MIN)*2+1;
166 const Ptype self = move.ptype();
167 int base = 0;
168 switch (self) {
169 case ROOK:
170 break;
171 case PROOK:
172 base += unit; break;
173 case BISHOP:
174 base += unit*2; break;
175 case PBISHOP:
176 base += unit*3; break;
177 case LANCE:
178 if (direction_id != 0)
179 return -1;
180 base += unit*4; break;
181 default:
182 return -1;
183 }
184 const Direction direction = makeDirection(self);
185 const Piece p = PatternLong2::find(state, move, direction);
186
187 int index = base;
188 if (! p.isPiece()) {
189 index += unit - 1;
190 } else {
191 assert(p.isPiece());
192 index += (p.ptype()-PTYPE_PIECE_MIN)*2;
193 index += (p.owner() != move.player());
194 }
195 return index;
196}
197
200 : Group(std::string("PatternBlock")+Ptype_Table.getCsaName(a)), attacker(a)
201{
202 assert(a == LANCE || a == ROOK || a == BISHOP);
203 for (int s=PTYPE_PIECE_MIN; s<=PTYPE_MAX; ++s) {
204 const Ptype self = static_cast<Ptype>(s);
205 for (int attack=0; attack<3; ++attack) {
206 for (int defense=0; defense<3; ++defense) {
207 for (int t=PTYPE_PIECE_MIN; t<= PTYPE_MAX; ++t) {
208 push_back(new PatternBlock(self, a, LongTarget(static_cast<Ptype>(t), true, true, attack, defense)));
209 push_back(new PatternBlock(self, a, LongTarget(static_cast<Ptype>(t), true, false, attack, defense)));
210 push_back(new PatternBlock(self, a, LongTarget(static_cast<Ptype>(t), false, true, attack, defense)));
211 push_back(new PatternBlock(self, a, LongTarget(static_cast<Ptype>(t), false, false, attack, defense)));
212 }
213 push_back(new PatternBlock(self, a, LongTarget(PTYPE_EMPTY, true, true, attack, defense)));
214 push_back(new PatternBlock(self, a, LongTarget(PTYPE_EMPTY, false, true, attack, defense)));
215 }
216 }
217 }
218}
219
221PatternBlockGroup::findMatch(const NumEffectState& state, Move move, const RatingEnv& env) const
222{
223 const size_t unit = ((PTYPE_MAX+1-PTYPE_PIECE_MIN)*4+2)*9;
224 const PieceSquare pp = PatternBlock::find(state, move, attacker);
225 if (pp.first.isEdge())
226 return -1;
227
228 int index = (move.ptype() - PTYPE_PIECE_MIN)*unit;
229 index += ((PTYPE_MAX+1-PTYPE_PIECE_MIN)*4+2)*CountEffect2::index(state, pp.second, env);
230 if (pp.first.isEmpty()) {
231 index += (PTYPE_MAX+1-PTYPE_PIECE_MIN)*4;
232 index += ! pp.second.canPromote(alt(state.turn()));
233 }
234 else {
235 assert(pp.first.isPiece());
236 index += (pp.first.ptype()-PTYPE_PIECE_MIN)*4;
237 index += (! pp.second.canPromote(pp.first.isPiece() ? alt(pp.first.owner()) : alt(move.player())))*2;
238 index += (pp.first.owner() != move.player());
239 }
240 return index;
241}
242
243/* ------------------------------------------------------------------------- */
244// ;;; Local Variables:
245// ;;; mode:c++
246// ;;; c-basic-offset:2
247// ;;; End:
圧縮していない moveの表現 .
Ptype ptype() const
Player player() const
const Square to() const
const Square from() const
利きを持つ局面
Ptype ptype() const
Definition basic_type.h:821
Player owner() const
Definition basic_type.h:963
bool isPiece() const
Definition basic_type.h:953
static const Piece EMPTY()
Definition basic_type.h:797
Player turn() const
const Piece pieceAt(Square sq) const
unsigned int index() const
Definition basic_type.h:572
unsigned int uintValue() const
Definition basic_type.h:539
static const Square makeDirect(int value)
Definition basic_type.h:538
mutually exclusive set of features
Definition group.h:17
int findMatch(const NumEffectState &state, Move m, const RatingEnv &env) const
static const PieceSquare find(const NumEffectState &state, Move move, Ptype attacker_ptype)
Definition pattern.cc:105
static const Piece find(const NumEffectState &state, Move move, Direction direction)
Definition pattern.h:157
static std::string name(int direction_id)
int findMatch(const NumEffectState &state, Move m, const RatingEnv &env) const
Direction makeDirection(Ptype ptype) const
int findMatch(const NumEffectState &state, Move m, const RatingEnv &env) const
Direction makeDirection(Ptype ptype) const
static const CArray< Direction, 4 > bishop_direction4
static const CArray< Direction, 4 > rook_direction4
static std::string name(int direction_id)
static const PieceSquare find(const NumEffectState &state, Move move, Direction direction)
Definition pattern.cc:66
static Square nextSquare(Player player, Square start, Direction direction, Direction direction2)
Definition pattern.h:33
static const Direction INVALID
Definition pattern.h:16
CArray< int, Square::SIZE > pattern_cache
Definition ratingEnv.h:24
std::pair< Piece, Square > PieceSquare
Definition pattern.h:60
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
@ PTYPE_PIECE_MIN
Definition basic_type.h:104
@ PTYPE_MAX
Definition basic_type.h:105
@ ROOK
Definition basic_type.h:100
@ BISHOP
Definition basic_type.h:99
@ PROOK
Definition basic_type.h:92
@ PTYPE_EDGE
Definition basic_type.h:86
@ PTYPE_EMPTY
Definition basic_type.h:85
@ PBISHOP
Definition basic_type.h:91
@ LANCE
Definition basic_type.h:96
const PtypeTable Ptype_Table
Definition tables.cc:97
constexpr int playerToIndex(Player player)
Definition basic_type.h:16
Direction
Definition basic_type.h:310
@ WHITE
Definition basic_type.h:10
@ BLACK
Definition basic_type.h:9
constexpr bool isPiece(Ptype ptype)
ptypeが空白やEDGEでないかのチェック
Definition basic_type.h:120
constexpr Player alt(Player player)
Definition basic_type.h:13
static int index(const NumEffectState &state, Square position, const RatingEnv &env)
static bool isPromotable(Move move, Square position)
Definition pattern.h:69
CArray2d< unsigned char, 2, Square::SIZE > target_table
int findMatch(const NumEffectState &state, Move m, const RatingEnv &) const
PatternGroup(Direction d, Direction d2=Pattern::INVALID)
static std::string name(Direction direction, Direction direction2)