My Project
group.cc
Go to the documentation of this file.
1 /* group.cc
2  */
3 #include "osl/rating/group.h"
6 #include <boost/filesystem/path.hpp>
7 #include <boost/filesystem/operations.hpp>
8 #include <iostream>
9 #include <fstream>
10 #include <sstream>
11 #include <iomanip>
12 #include <cstdio>
13 #include <cmath>
14 
15 osl::rating::Group::Group(const std::string& name)
16  : group_name(name)
17 {
18 }
19 
21 {
22 }
23 
24 int osl::rating::Group::findMatch(const NumEffectState& state, Move m, const RatingEnv& env) const
25 {
26  for (size_t j=0; j<size(); ++j) {
27  if ((*this)[j].match(state, m, env))
28  return j;
29  }
30  return -1;
31 }
32 
33 void osl::rating::Group::saveResult(const std::string& directory, const range_t& range,
34  const std::vector<double>& weights) const
35 {
36  {
37  boost::filesystem::path dir(directory);
38  boost::filesystem::create_directory(dir);
39  }
40 
41  std::string filename = directory + "/" + group_name + ".txt";
42  std::ofstream os(filename.c_str());
43  for (int i=range.first; i<range.second; ++i)
44  os << std::setprecision(8) << weights[i] << "\n";
45 }
46 
47 bool osl::rating::Group::load(const std::string& directory, const range_t& range,
48  std::vector<double>& weights) const
49 {
50  std::string filename = directory + "/" + group_name + ".txt";
51  FILE *fp = fopen(filename.c_str(), "r");
52  if (! fp)
53  return false;
54  for (int i=range.first; i<range.second; ++i) {
55  if (fscanf(fp, "%lf", &weights[i]) != 1)
56  return false;
57  }
58  fclose(fp);
59  return true;
60 }
61 
62 void osl::rating::Group::show(std::ostream& os, int name_width, const range_t& range,
63  const std::vector<double>& weights) const
64 {
65 #ifndef MINIMAL
66  for (size_t f=0; f<size(); ++f) {
67  os << std::setw(name_width)
68  << (*this)[f].name()
69  << " " << 400*log10(weights[f+range.first]) << "\n";
70  }
71 #endif
72 }
73 
74 void osl::rating::Group::showAll(std::ostream& os, int name_width, const range_t& range,
75  const std::vector<double>& weights) const
76 {
77 #ifndef MINIMAL
78  showMinMax(os, name_width, range, weights);
79  for (size_t i=0; i<size(); ++i) {
80  os << " " << (*this)[i].name() << " " << 400*log10(weights[i+range.first]);
81  }
82  os << "\n";
83 #endif
84 }
85 void osl::rating::Group::showMinMax(std::ostream& os, int name_width, const range_t& range,
86  const std::vector<double>& weights) const
87 {
88 #ifndef MINIMAL
89  double min = 10000000000.0, max = -min;
90  for (size_t i=0; i<size(); ++i) {
91  min = std::min(min, 400*log10(weights[i+range.first]));
92  max = std::max(max, 400*log10(weights[i+range.first]));
93  }
94  os << std::setw(name_width)
95  << group_name
96  << " [" << min << " -- " << max << "] ";
97 #endif
98 }
99 
100 void osl::rating::Group::showTopN(std::ostream& os, int name_width, const range_t& range,
101  const std::vector<double>& weights, int n) const
102 {
103 #ifndef MINIMAL
104  if ((int)weights.size() <= n*2)
105  return showAll(os, name_width, range, weights);
106  showMinMax(os, name_width, range, weights);
107  std::vector<double> w;
108  w.reserve(size());
109  for (int i=range.first; i<range.second; ++i)
110  w.push_back(weights[i]);
111  std::sort(w.begin(), w.end());
112  for (int i=0; i<n; ++i) {
113  double value = w[size()-1-i];
114  int j=range.first;
115  for (; j<range.second; ++j)
116  if (weights[j] == value)
117  break;
118  os << " " << (*this)[j-range.first].name() << " " << 400*log10(value);
119  }
120  os << " ... ";
121  for (int i=0; i<n; ++i) {
122  double value = w[n-1-i];
123  int j=range.first;
124  for (; j<range.second; ++j)
125  if (weights[j] == value)
126  break;
127  os << " " << (*this)[j-range.first].name() << " " << 400*log10(value);
128  }
129  os << "\n";
130 #endif
131 }
132 
134 ChaseGroup::ChaseGroup() : Group("Chase")
135 {
136  for (int o=0; o<4; ++o) {
137  for (int t=PTYPE_PIECE_MIN; t<=PTYPE_MAX; ++t) {
138  Ptype target = static_cast<Ptype>(t);
139  for (int s=PTYPE_PIECE_MIN; s<=PTYPE_MAX; ++s) {
140  Ptype self = static_cast<Ptype>(s);
141  push_back(new Chase(self, target, false, static_cast<Chase::OpponentType>(o)));
142  if (isBasic(self))
143  push_back(new Chase(self, target, true, static_cast<Chase::OpponentType>(o)));
144  }
145  }
146  }
147 }
148 
150 ChaseGroup::findMatch(const NumEffectState& state, Move move, const RatingEnv& env) const
151 {
152  Move last_move = env.history.lastMove();
153  if (! last_move.isNormal())
154  return -1;
155  if (! state.hasEffectIf(move.ptypeO(), move.to(), last_move.to()))
156  return -1;
157  int base = 0;
159  if (last_move.capturePtype() == PTYPE_EMPTY) {
160  if (last_move.isDrop()) {
161  base = unit;
162  } else {
163  if (state.hasEffectAt(state.turn(), last_move.from()))
164  base = unit*2;
165  else
166  base = unit*3;
167  }
168  }
169  Ptype self = move.ptype();
170  Ptype target = last_move.ptype();
171  int index = base + (target - PTYPE_PIECE_MIN)*(PTYPE_MAX+1-PTYPE_PIECE_MIN+PTYPE_MAX+1-PTYPE_BASIC_MIN);
172  if (isBasic(self)) {
173  index += (PTYPE_BASIC_MIN - PTYPE_PIECE_MIN);
174  index += (self - PTYPE_BASIC_MIN)*2;
175  index += move.isDrop();
176  } else {
177  index += (self - PTYPE_PIECE_MIN);
178  }
179  assert((*this)[index].match(state, move, env));
180  return index;
181 }
182 
184 {
185  push_back(new Karanari(false, true));
186  push_back(new Karanari(false, false));
187  push_back(new Karanari(true, true));
188  push_back(new Karanari(true, false));
189 }
190 
192 KaranariGroup::findMatch(const NumEffectState& state, Move move, const RatingEnv&) const
193 {
194  return Karanari::index(state, move);
195 }
196 
199  : Group("ImmediateAddSupport")
200 {
201  for (int s=PTYPE_PIECE_MIN; s<=PTYPE_MAX; ++s) {
202  for (int a=PTYPE_PIECE_MIN; a<=PTYPE_MAX; ++a) {
203  for (int p=0; p<8; ++p) // progress8
204  push_back(new ImmediateAddSupport(static_cast<Ptype>(s), static_cast<Ptype>(a)));
205  }
206  }
207 }
208 
209 /* ------------------------------------------------------------------------- */
210 // ;;; Local Variables:
211 // ;;; mode:c++
212 // ;;; c-basic-offset:2
213 // ;;; End:
osl::rating::Group::load
bool load(const std::string &directory, const range_t &range, std::vector< double > &weights) const
Definition: group.cc:47
osl::PTYPE_PIECE_MIN
@ PTYPE_PIECE_MIN
Definition: basic_type.h:104
osl::rating::Group::saveResult
void saveResult(const std::string &directory, const range_t &range, const std::vector< double > &weights) const
Definition: group.cc:33
osl::rating::Group::showMinMax
void showMinMax(std::ostream &os, int name_width, const range_t &range, const std::vector< double > &weights) const
Definition: group.cc:85
osl::eval::min
int min(Player p, int v1, int v2)
Definition: evalTraits.h:92
group.h
osl::rating::Karanari::index
static int index(const NumEffectState &state, Move move)
Definition: karanari.h:40
osl::rating::KaranariGroup::KaranariGroup
KaranariGroup()
Definition: group.cc:183
osl::Move
圧縮していない moveの表現 .
Definition: basic_type.h:1052
osl::rating::Group
mutually exclusive set of features
Definition: group.h:17
pinAttack.h
osl::container::MoveStack::lastMove
const Move lastMove(size_t last=1) const
Definition: moveStack.h:28
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:84
osl::rating::range_t
std::pair< int, int > range_t
Definition: range.h:10
osl::NumEffectState::hasEffectIf
bool hasEffectIf(PtypeO ptypeo, Square attacker, Square target) const
attackerにptypeoの駒がいると仮定した場合にtargetに利きがあるかどうか を stateをupdateしないで確かめる.
Definition: numEffectState.h:465
osl::rating::Chase
Definition: rating/feature.h:140
osl::NumEffectState::hasEffectAt
bool hasEffectAt(Square target) const
対象とするマスにあるプレイヤーの利きがあるかどうか.
Definition: numEffectState.h:324
osl::rating::Group::showAll
void showAll(std::ostream &os, int name_width, const range_t &range, const std::vector< double > &weights) const
Definition: group.cc:74
osl::rating::Group::~Group
virtual ~Group()
Definition: group.cc:20
osl::eval::max
int max(Player p, int v1, int v2)
Definition: evalTraits.h:84
karanari.h
osl::rating::RatingEnv
Definition: ratingEnv.h:16
osl::PTYPE_MAX
@ PTYPE_MAX
Definition: basic_type.h:105
osl::rating::Group::Group
Group(const std::string &name)
Definition: group.cc:15
osl::Move::capturePtype
Ptype capturePtype() const
Definition: basic_type.h:1180
osl::Move::isDrop
bool isDrop() const
Definition: basic_type.h:1150
osl::Move::ptypeO
PtypeO ptypeO() const
移動後のPtype, i.e., 成る手だった場合成った後
Definition: basic_type.h:1162
osl::isBasic
bool isBasic(Ptype ptype)
ptypeが基本型(promoteしていない)かのチェック
Definition: basic_type.h:128
osl::rating::RatingEnv::history
MoveStack history
Definition: ratingEnv.h:18
osl::Move::isNormal
bool isNormal() const
INVALID でも PASS でもない.
Definition: basic_type.h:1088
osl::Move::from
const Square from() const
Definition: basic_type.h:1125
osl::rating::ImmediateAddSupportGroup::ImmediateAddSupportGroup
ImmediateAddSupportGroup()
Definition: group.cc:198
osl::NumEffectState
利きを持つ局面
Definition: numEffectState.h:34
osl::rating::ChaseGroup::ChaseGroup
ChaseGroup()
Definition: group.cc:134
osl::rating::Chase::OpponentType
OpponentType
Definition: rating/feature.h:142
osl::rating::Group::findMatch
virtual int findMatch(const NumEffectState &state, Move m, const RatingEnv &env) const
Definition: group.cc:24
osl::Move::ptype
Ptype ptype() const
Definition: basic_type.h:1155
osl::PTYPE_BASIC_MIN
@ PTYPE_BASIC_MIN
Definition: basic_type.h:103
osl::rating::KaranariGroup::findMatch
int findMatch(const NumEffectState &state, Move move, const RatingEnv &) const
Definition: group.cc:192
osl::rating::Karanari
Definition: karanari.h:13
osl::PTYPE_EMPTY
@ PTYPE_EMPTY
Definition: basic_type.h:85
osl::SimpleState::turn
Player turn() const
Definition: simpleState.h:220
osl::rating::ImmediateAddSupport
Definition: rating/feature.h:184
osl::rating::Group::showTopN
void showTopN(std::ostream &os, int name_width, const range_t &range, const std::vector< double > &weights, int n) const
Definition: group.cc:100
osl::Move::to
const Square to() const
Definition: basic_type.h:1132
osl::rating::Group::show
virtual void show(std::ostream &, int name_width, const range_t &range, const std::vector< double > &weights) const
Definition: group.cc:62
osl::rating::ChaseGroup::findMatch
int findMatch(const NumEffectState &state, Move move, const RatingEnv &env) const
Definition: group.cc:150