00001
00002
00003 #include "osl/rating/group.h"
00004 #include "osl/rating/feature/karanari.h"
00005 #include "osl/rating/feature/pinAttack.h"
00006 #include <boost/filesystem/path.hpp>
00007 #include <boost/filesystem/operations.hpp>
00008 #include <iostream>
00009 #include <fstream>
00010 #include <sstream>
00011 #include <iomanip>
00012 #include <cstdio>
00013 #include <cmath>
00014
00015 osl::rating::Group::Group(const std::string& name)
00016 : group_name(name)
00017 {
00018 }
00019
00020 osl::rating::Group::~Group()
00021 {
00022 }
00023
00024 int osl::rating::Group::findMatch(const NumEffectState& state, Move m, const RatingEnv& env) const
00025 {
00026 for (size_t j=0; j<size(); ++j) {
00027 if ((*this)[j].match(state, m, env))
00028 return j;
00029 }
00030 return -1;
00031 }
00032
00033 void osl::rating::Group::saveResult(const std::string& directory, const range_t& range,
00034 const vector<double>& weights) const
00035 {
00036 {
00037 boost::filesystem::path dir(directory);
00038 boost::filesystem::create_directory(dir);
00039 }
00040
00041 std::string filename = directory + "/" + group_name + ".txt";
00042 std::ofstream os(filename.c_str());
00043 for (int i=range.first; i<range.second; ++i)
00044 os << std::setprecision(8) << weights[i] << "\n";
00045 }
00046
00047 bool osl::rating::Group::load(const std::string& directory, const range_t& range,
00048 vector<double>& weights) const
00049 {
00050 std::string filename = directory + "/" + group_name + ".txt";
00051 FILE *fp = fopen(filename.c_str(), "r");
00052 if (! fp)
00053 return false;
00054 for (int i=range.first; i<range.second; ++i) {
00055 if (fscanf(fp, "%lf", &weights[i]) != 1)
00056 return false;
00057 }
00058 fclose(fp);
00059 return true;
00060 }
00061
00062 void osl::rating::Group::show(std::ostream& os, int name_width, const range_t& range,
00063 const vector<double>& weights) const
00064 {
00065 for (size_t f=0; f<size(); ++f) {
00066 os << std::setw(name_width)
00067 << (*this)[f].name()
00068 << " " << 400*log10(weights[f+range.first]) << "\n";
00069 }
00070 }
00071
00072 void osl::rating::Group::showAll(std::ostream& os, int name_width, const range_t& range,
00073 const vector<double>& weights) const
00074 {
00075 showMinMax(os, name_width, range, weights);
00076 for (size_t i=0; i<size(); ++i) {
00077 os << " " << (*this)[i].name() << " " << 400*log10(weights[i+range.first]);
00078 }
00079 os << "\n";
00080 }
00081 void osl::rating::Group::showMinMax(std::ostream& os, int name_width, const range_t& range,
00082 const vector<double>& weights) const
00083 {
00084 double min = 10000000000.0, max = -min;
00085 for (size_t i=0; i<size(); ++i) {
00086 min = std::min(min, 400*log10(weights[i+range.first]));
00087 max = std::max(max, 400*log10(weights[i+range.first]));
00088 }
00089 os << std::setw(name_width)
00090 << group_name
00091 << " [" << min << " -- " << max << "] ";
00092 }
00093
00094 void osl::rating::Group::showTopN(std::ostream& os, int name_width, const range_t& range,
00095 const vector<double>& weights, int n) const
00096 {
00097 if ((int)weights.size() <= n*2)
00098 return showAll(os, name_width, range, weights);
00099 showMinMax(os, name_width, range, weights);
00100 vector<double> w;
00101 w.reserve(size());
00102 for (int i=range.first; i<range.second; ++i)
00103 w.push_back(weights[i]);
00104 std::sort(w.begin(), w.end());
00105 for (int i=0; i<n; ++i) {
00106 double value = w[size()-1-i];
00107 int j=range.first;
00108 for (; j<range.second; ++j)
00109 if (weights[j] == value)
00110 break;
00111 os << " " << (*this)[j-range.first].name() << " " << 400*log10(value);
00112 }
00113 os << " ... ";
00114 for (int i=0; i<n; ++i) {
00115 double value = w[n-1-i];
00116 int j=range.first;
00117 for (; j<range.second; ++j)
00118 if (weights[j] == value)
00119 break;
00120 os << " " << (*this)[j-range.first].name() << " " << 400*log10(value);
00121 }
00122 os << "\n";
00123 }
00124
00125 osl::rating::
00126 ChaseGroup::ChaseGroup() : Group("Chase")
00127 {
00128 for (int o=0; o<4; ++o) {
00129 for (int t=PTYPE_PIECE_MIN; t<=PTYPE_MAX; ++t) {
00130 Ptype target = static_cast<Ptype>(t);
00131 for (int s=PTYPE_PIECE_MIN; s<=PTYPE_MAX; ++s) {
00132 Ptype self = static_cast<Ptype>(s);
00133 push_back(new Chase(self, target, false, static_cast<Chase::OpponentType>(o)));
00134 if (isBasic(self))
00135 push_back(new Chase(self, target, true, static_cast<Chase::OpponentType>(o)));
00136 }
00137 }
00138 }
00139 }
00140
00141 int osl::rating::
00142 ChaseGroup::findMatch(const NumEffectState& state, Move move, const RatingEnv& env) const
00143 {
00144 Move last_move = env.history.lastMove();
00145 if (! last_move.isNormal())
00146 return -1;
00147 if (! state.hasEffectFromTo(move.ptypeO(), move.to(), last_move.to()))
00148 return -1;
00149 int base = 0;
00150 const int unit = (PTYPE_MAX+1 - PTYPE_PIECE_MIN)*(PTYPE_MAX+1-PTYPE_PIECE_MIN+PTYPE_MAX+1-PTYPE_BASIC_MIN);
00151 if (last_move.capturePtype() == PTYPE_EMPTY) {
00152 if (last_move.isDrop()) {
00153 base = unit;
00154 } else {
00155 if (state.hasEffectBy(state.getTurn(), last_move.from()))
00156 base = unit*2;
00157 else
00158 base = unit*3;
00159 }
00160 }
00161 Ptype self = move.ptype();
00162 Ptype target = last_move.ptype();
00163 int index = base + (target - PTYPE_PIECE_MIN)*(PTYPE_MAX+1-PTYPE_PIECE_MIN+PTYPE_MAX+1-PTYPE_BASIC_MIN);
00164 if (isBasic(self)) {
00165 index += (PTYPE_BASIC_MIN - PTYPE_PIECE_MIN);
00166 index += (self - PTYPE_BASIC_MIN)*2;
00167 index += move.isDrop();
00168 } else {
00169 index += (self - PTYPE_PIECE_MIN);
00170 }
00171 assert((*this)[index].match(state, move, env));
00172 return index;
00173 }
00174
00175 osl::rating::KaranariGroup::KaranariGroup() : Group("Karanari")
00176 {
00177 push_back(new Karanari(false, true));
00178 push_back(new Karanari(false, false));
00179 push_back(new Karanari(true, true));
00180 push_back(new Karanari(true, false));
00181 }
00182
00183 int osl::rating::
00184 KaranariGroup::findMatch(const NumEffectState& state, Move move, const RatingEnv&) const
00185 {
00186 return Karanari::index(state, move);
00187 }
00188
00189 osl::rating::
00190 ImmediateAddSupportGroup::ImmediateAddSupportGroup()
00191 : Group("ImmediateAddSupport")
00192 {
00193 for (int s=PTYPE_PIECE_MIN; s<=PTYPE_MAX; ++s) {
00194 for (int a=PTYPE_PIECE_MIN; a<=PTYPE_MAX; ++a) {
00195 for (int p=0; p<8; ++p)
00196 push_back(new ImmediateAddSupport(static_cast<Ptype>(s), static_cast<Ptype>(a)));
00197 }
00198 }
00199 }
00200
00201
00202
00203
00204
00205