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
15osl::rating::Group::Group(const std::string& name)
16 : group_name(name)
17{
18}
19
21{
22}
23
24int 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
33void 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
47bool 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
62void 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
74void 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}
85void 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
100void 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
134ChaseGroup::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
150ChaseGroup::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
192KaranariGroup::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:
圧縮していない moveの表現 .
Definition: basic_type.h:1052
PtypeO ptypeO() const
移動後のPtype, i.e., 成る手だった場合成った後
Definition: basic_type.h:1162
Ptype ptype() const
Definition: basic_type.h:1155
bool isDrop() const
Definition: basic_type.h:1150
Ptype capturePtype() const
Definition: basic_type.h:1180
bool isNormal() const
INVALID でも PASS でもない.
Definition: basic_type.h:1088
const Square to() const
Definition: basic_type.h:1132
const Square from() const
Definition: basic_type.h:1125
利きを持つ局面
bool hasEffectAt(Square target) const
対象とするマスにあるプレイヤーの利きがあるかどうか.
bool hasEffectIf(PtypeO ptypeo, Square attacker, Square target) const
attackerにptypeoの駒がいると仮定した場合にtargetに利きがあるかどうか を stateをupdateしないで確かめる.
Player turn() const
Definition: simpleState.h:220
const Move lastMove(size_t last=1) const
Definition: moveStack.h:28
mutually exclusive set of features
Definition: group.h:17
void showAll(std::ostream &os, int name_width, const range_t &range, const std::vector< double > &weights) const
Definition: group.cc:74
void saveResult(const std::string &directory, const range_t &range, const std::vector< double > &weights) const
Definition: group.cc:33
virtual int findMatch(const NumEffectState &state, Move m, const RatingEnv &env) const
Definition: group.cc:24
bool load(const std::string &directory, const range_t &range, std::vector< double > &weights) const
Definition: group.cc:47
void showTopN(std::ostream &os, int name_width, const range_t &range, const std::vector< double > &weights, int n) const
Definition: group.cc:100
void showMinMax(std::ostream &os, int name_width, const range_t &range, const std::vector< double > &weights) const
Definition: group.cc:85
virtual void show(std::ostream &, int name_width, const range_t &range, const std::vector< double > &weights) const
Definition: group.cc:62
Group(const std::string &name)
Definition: group.cc:15
virtual ~Group()
Definition: group.cc:20
static int index(const NumEffectState &state, Move move)
Definition: karanari.h:40
int max(Player p, int v1, int v2)
Definition: evalTraits.h:84
int min(Player p, int v1, int v2)
Definition: evalTraits.h:92
std::pair< int, int > range_t
Definition: range.h:10
Ptype
駒の種類を4ビットでコード化する
Definition: basic_type.h:84
@ PTYPE_PIECE_MIN
Definition: basic_type.h:104
@ PTYPE_MAX
Definition: basic_type.h:105
@ PTYPE_EMPTY
Definition: basic_type.h:85
@ PTYPE_BASIC_MIN
Definition: basic_type.h:103
bool isBasic(Ptype ptype)
ptypeが基本型(promoteしていない)かのチェック
Definition: basic_type.h:128
int findMatch(const NumEffectState &state, Move move, const RatingEnv &env) const
Definition: group.cc:150
int findMatch(const NumEffectState &state, Move move, const RatingEnv &) const
Definition: group.cc:192