group.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
group.cc
Go to the documentation of this file.
1 /* group.cc
2 */
3 #include "osl/rating/group.h"
4 #include "osl/rating/feature/karanari.h"
5 #include "osl/rating/feature/pinAttack.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
20 osl::rating::Group::~Group()
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 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 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 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 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 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 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 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
133 osl::rating::
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
149 int osl::rating::
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;
158 const int unit = (PTYPE_MAX+1 - PTYPE_PIECE_MIN)*(PTYPE_MAX+1-PTYPE_PIECE_MIN+PTYPE_MAX+1-PTYPE_BASIC_MIN);
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
183 osl::rating::KaranariGroup::KaranariGroup() : Group("Karanari")
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
191 int osl::rating::
192 KaranariGroup::findMatch(const NumEffectState& state, Move move, const RatingEnv&) const
193 {
194 return Karanari::index(state, move);
195 }
196
197 osl::rating::
198 ImmediateAddSupportGroup::ImmediateAddSupportGroup()
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: