All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
group.h
Go to the documentation of this file.
1 /* group.h
2  */
3 #ifndef _GROUP_H
4 #define _GROUP_H
5 
6 #include "osl/rating/feature.h"
7 #include "osl/rating/range.h"
8 #include "osl/stl/vector.h"
9 #include <boost/ptr_container/ptr_vector.hpp>
10 
11 namespace osl
12 {
13  namespace rating
14  {
16  class Group : public boost::ptr_vector<Feature>
17  {
18  public:
19  std::string group_name;
20 
21  Group(const std::string& name);
22  Group(Feature *f) : group_name(f->name()) { push_back(f); }
23  virtual ~Group();
24  virtual void show(std::ostream&, int name_width, const range_t& range,
25  const vector<double>& weights) const;
26 
28  virtual int findMatch(const NumEffectState& state, Move m, const RatingEnv& env) const;
29  void showMinMax(std::ostream& os, int name_width, const range_t& range,
30  const vector<double>& weights) const;
31  void showAll(std::ostream& os, int name_width, const range_t& range,
32  const vector<double>& weights) const;
33  void showTopN(std::ostream& os, int name_width, const range_t& range,
34  const vector<double>& weights, int n) const;
35  void saveResult(const std::string& directory, const range_t& range,
36  const vector<double>& weights) const;
37  bool load(const std::string& directory, const range_t& range,
38  vector<double>& weights) const;
39  virtual bool effectiveInCheck() const { return (*this)[0].effectiveInCheck(); }
40  };
41 
42  struct TakeBackGroup : public Group
43  {
44  TakeBackGroup() : Group("TakeBack")
45  {
46  push_back(new TakeBack());
47  push_back(new TakeBack2());
48  }
49 #ifndef MINIMAL
50  void show(std::ostream& os, int name_width, const range_t& range,
51  const vector<double>& weights) const
52  {
53  showAll(os, name_width, range, weights);
54  }
55 #endif
56  int findMatch(const NumEffectState&, Move move, const RatingEnv& env) const
57  {
58  const Square to = move.to();
59  if (! env.history.hasLastMove() || env.history.lastMove().to() != to)
60  return -1;
61  if (! env.history.hasLastMove(2) || env.history.lastMove(2).to() != to)
62  return 0;
63  return 1;
64  }
65  bool effectiveInCheck() const { return true; }
66  };
67 
68  struct CheckGroup : public Group
69  {
70  CheckGroup() : Group("Check")
71  {
72  for (int i=0; i<4; ++i)
73  for (int p=0; p<8; ++p) // progress8
74  push_back(new Check(i));
75  }
76  void show(std::ostream& os, int name_width, const range_t& range,
77  const vector<double>& weights) const
78  {
79  showAll(os, name_width, range, weights);
80  }
81  int findMatch(const NumEffectState& state, Move move, const RatingEnv& env) const
82  {
83  using namespace osl::move_classifier;
86  int index = -1;
87  if (direct && !open)
88  index = Check::openLong(state, move);
89  else if (open)
90  index = direct + 2;
91  const int progress8 = env.progress.value()/2;
92  return index*8 + progress8;
93  }
94  bool effectiveInCheck() const { return true; }
95  };
96 
97  class SendOffGroup : public Group
98  {
99  public:
100  SendOffGroup() : Group("SendOff")
101  {
102  for (int p=0; p<8; ++p) // progress8
103  push_back(new SendOff(0));
104  for (int p=0; p<8; ++p) // progress8
105  push_back(new SendOff(1));
106  }
107  void show(std::ostream& os, int name_width, const range_t& range,
108  const vector<double>& weights) const
109  {
110  showAll(os, name_width, range, weights);
111  }
112  int findMatch(const NumEffectState&, Move move, const RatingEnv& env) const
113  {
114  if (! env.sendoffs.isMember(move.to()))
115  return -1;
116  const int progress8 = env.progress.value()/2;
117  return (move.capturePtype() != PTYPE_EMPTY)*8 + progress8;
118  }
119  };
120 
121  struct BlockGroup : public Group
122  {
123  BlockGroup() : Group("Block")
124  {
125  for (int s=0; s<=3; ++s) {
126  for (int o=0; o<=3; ++o) {
127  push_back(new Block(s, o));
128  }
129  }
130  }
131  void show(std::ostream& os, int name_width, const range_t& range,
132  const vector<double>& weights) const
133  {
134  showAll(os, name_width, range, weights);
135  }
136  int findMatch(const NumEffectState& state, Move move, const RatingEnv& ) const
137  {
138  const int index = Block::count(state, move.to(), state.turn())*4
139  + Block::count(state, move.to(), alt(state.turn()));
140  return index;
141  }
142  bool effectiveInCheck() const { return true; }
143  };
144 
145  struct OpenGroup : public Group
146  {
147  OpenGroup() : Group("Open")
148  {
149  for (int i=0; i<16; ++i)
150  push_back(new Open(i));
151  }
152  void show(std::ostream& os, int name_width, const range_t& range,
153  const vector<double>& weights) const
154  {
155  showTopN(os, name_width, range, weights, 3);
156  }
157  int findMatch(const NumEffectState& state, Move move, const RatingEnv& ) const
158  {
159  const int index = Open::index(state, move);
160  return index;
161  }
162  bool effectiveInCheck() const { return true; }
163  };
164 
165  struct ChaseGroup : public Group
166  {
167  ChaseGroup();
168  void show(std::ostream& os, int name_width, const range_t& range,
169  const vector<double>& weights) const
170  {
171  showTopN(os, name_width, range, weights, 3);
172  }
173  int findMatch(const NumEffectState& state, Move move, const RatingEnv& env) const;
174  };
175 
176  struct KaranariGroup : public Group
177  {
178  KaranariGroup();
179  void show(std::ostream& os, int name_width, const range_t& range,
180  const vector<double>& weights) const
181  {
182  showAll(os, name_width, range, weights);
183  }
184  int findMatch(const NumEffectState& state, Move move, const RatingEnv&) const;
185  };
186 
188  {
190  void show(std::ostream& os, int name_width, const range_t& range,
191  const vector<double>& weights) const
192  {
193  showTopN(os, name_width, range, weights, 3);
194  }
195  int findMatch(const NumEffectState& state, Move move, const RatingEnv& env) const
196  {
197  const int index = ImmediateAddSupport::index(state, move, env);
198  if (index < 0)
199  return index;
200  const int progress8 = env.progress.value()/2;
201  return index*8 + progress8;
202  }
203  };
204 
205  struct BadLanceGroup : public Group
206  {
207  BadLanceGroup() : Group("BadLance")
208  {
209  push_back(new BadLance(false));
210  push_back(new BadLance(true));
211  }
212  void show(std::ostream& os, int name_width, const range_t& range,
213  const vector<double>& weights) const
214  {
215  showAll(os, name_width, range, weights);
216  }
217  int findMatch(const NumEffectState& state, Move move, const RatingEnv&) const
218  {
219  const Square front = Board_Table.nextSquare(move.player(), move.to(), U);
220  if (! BadLance::basicMatch(state, move, front))
221  return -1;
222  const int index = state.hasEffectAt(alt(move.player()), front);
223  return index;
224  }
225  };
226 
227  struct PawnAttackGroup : public Group
228  {
229  PawnAttackGroup() : Group("PawnAttack")
230  {
231  for (int p=0; p<8; ++p) // progress8
232  push_back(new PawnAttack());
233  }
234  void show(std::ostream& os, int name_width, const range_t& range,
235  const vector<double>& weights) const
236  {
237  showAll(os, name_width, range, weights);
238  }
239  int findMatch(const NumEffectState& state, Move move, const RatingEnv& env) const
240  {
241  if (! (*this)[0].match(state, move, env))
242  return -1;
243  const int progress8 = env.progress.value()/2;
244  return progress8;
245  }
246  };
247  }
248 }
249 
250 #endif /* _GROUP_H */
251 // ;;; Local Variables:
252 // ;;; mode:c++
253 // ;;; c-basic-offset:2
254 // ;;; End: