group.cc
Go to the documentation of this file.
/* group.cc
*/
#include "osl/rating/group.h"
#include "osl/rating/feature/karanari.h"
#include "osl/rating/feature/pinAttack.h"
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
osl::rating::Group::Group(const std::string& name)
: group_name(name)
{
}
osl::rating::Group::~Group()
{
}
int osl::rating::Group::findMatch(const NumEffectState& state, Move m, const RatingEnv& env) const
{
for (size_t j=0; j<size(); ++j) {
if ((*this)[j].match(state, m, env))
return j;
}
return -1;
}
void osl::rating::Group::saveResult(const std::string& directory, const range_t& range,
const std::vector<double>& weights) const
{
{
boost::filesystem::path dir(directory);
boost::filesystem::create_directory(dir);
}
std::string filename = directory + "/" + group_name + ".txt";
std::ofstream os(filename.c_str());
for (int i=range.first; i<range.second; ++i)
os << std::setprecision(8) << weights[i] << "\n";
}
bool osl::rating::Group::load(const std::string& directory, const range_t& range,
std::vector<double>& weights) const
{
std::string filename = directory + "/" + group_name + ".txt";
FILE *fp = fopen(filename.c_str(), "r");
if (! fp)
return false;
for (int i=range.first; i<range.second; ++i) {
if (fscanf(fp, "%lf", &weights[i]) != 1)
return false;
}
fclose(fp);
return true;
}
void osl::rating::Group::show(std::ostream& os, int name_width, const range_t& range,
const std::vector<double>& weights) const
{
#ifndef MINIMAL
for (size_t f=0; f<size(); ++f) {
os << std::setw(name_width)
<< (*this)[f].name()
<< " " << 400*log10(weights[f+range.first]) << "\n";
}
#endif
}
void osl::rating::Group::showAll(std::ostream& os, int name_width, const range_t& range,
const std::vector<double>& weights) const
{
#ifndef MINIMAL
showMinMax(os, name_width, range, weights);
for (size_t i=0; i<size(); ++i) {
os << " " << (*this)[i].name() << " " << 400*log10(weights[i+range.first]);
}
os << "\n";
#endif
}
void osl::rating::Group::showMinMax(std::ostream& os, int name_width, const range_t& range,
const std::vector<double>& weights) const
{
#ifndef MINIMAL
double min = 10000000000.0, max = -min;
for (size_t i=0; i<size(); ++i) {
min = std::min(min, 400*log10(weights[i+range.first]));
max = std::max(max, 400*log10(weights[i+range.first]));
}
os << std::setw(name_width)
<< group_name
<< " [" << min << " -- " << max << "] ";
#endif
}
void osl::rating::Group::showTopN(std::ostream& os, int name_width, const range_t& range,
const std::vector<double>& weights, int n) const
{
#ifndef MINIMAL
if ((int)weights.size() <= n*2)
return showAll(os, name_width, range, weights);
showMinMax(os, name_width, range, weights);
std::vector<double> w;
w.reserve(size());
for (int i=range.first; i<range.second; ++i)
w.push_back(weights[i]);
std::sort(w.begin(), w.end());
for (int i=0; i<n; ++i) {
double value = w[size()-1-i];
int j=range.first;
for (; j<range.second; ++j)
if (weights[j] == value)
break;
os << " " << (*this)[j-range.first].name() << " " << 400*log10(value);
}
os << " ... ";
for (int i=0; i<n; ++i) {
double value = w[n-1-i];
int j=range.first;
for (; j<range.second; ++j)
if (weights[j] == value)
break;
os << " " << (*this)[j-range.first].name() << " " << 400*log10(value);
}
os << "\n";
#endif
}
osl::rating::
ChaseGroup::ChaseGroup() : Group("Chase")
{
for (int o=0; o<4; ++o) {
for (int t=PTYPE_PIECE_MIN; t<=PTYPE_MAX; ++t) {
Ptype target = static_cast<Ptype>(t);
for (int s=PTYPE_PIECE_MIN; s<=PTYPE_MAX; ++s) {
Ptype self = static_cast<Ptype>(s);
push_back(new Chase(self, target, false, static_cast<Chase::OpponentType>(o)));
if (isBasic(self))
push_back(new Chase(self, target, true, static_cast<Chase::OpponentType>(o)));
}
}
}
}
int osl::rating::
ChaseGroup::findMatch(const NumEffectState& state, Move move, const RatingEnv& env) const
{
Move last_move = env.history.lastMove();
if (! last_move.isNormal())
return -1;
if (! state.hasEffectIf(move.ptypeO(), move.to(), last_move.to()))
return -1;
int base = 0;
const int unit = (PTYPE_MAX+1 - PTYPE_PIECE_MIN)*(PTYPE_MAX+1-PTYPE_PIECE_MIN+PTYPE_MAX+1-PTYPE_BASIC_MIN);
if (last_move.capturePtype() == PTYPE_EMPTY) {
if (last_move.isDrop()) {
base = unit;
} else {
if (state.hasEffectAt(state.turn(), last_move.from()))
base = unit*2;
else
base = unit*3;
}
}
Ptype self = move.ptype();
Ptype target = last_move.ptype();
int index = base + (target - PTYPE_PIECE_MIN)*(PTYPE_MAX+1-PTYPE_PIECE_MIN+PTYPE_MAX+1-PTYPE_BASIC_MIN);
if (isBasic(self)) {
index += (PTYPE_BASIC_MIN - PTYPE_PIECE_MIN);
index += (self - PTYPE_BASIC_MIN)*2;
index += move.isDrop();
} else {
index += (self - PTYPE_PIECE_MIN);
}
assert((*this)[index].match(state, move, env));
return index;
}
osl::rating::KaranariGroup::KaranariGroup() : Group("Karanari")
{
push_back(new Karanari(false, true));
push_back(new Karanari(false, false));
push_back(new Karanari(true, true));
push_back(new Karanari(true, false));
}
int osl::rating::
KaranariGroup::findMatch(const NumEffectState& state, Move move, const RatingEnv&) const
{
return Karanari::index(state, move);
}
osl::rating::
ImmediateAddSupportGroup::ImmediateAddSupportGroup()
: Group("ImmediateAddSupport")
{
for (int s=PTYPE_PIECE_MIN; s<=PTYPE_MAX; ++s) {
for (int a=PTYPE_PIECE_MIN; a<=PTYPE_MAX; ++a) {
for (int p=0; p<8; ++p) // progress8
push_back(new ImmediateAddSupport(static_cast<Ptype>(s), static_cast<Ptype>(a)));
}
}
}
/* ------------------------------------------------------------------------- */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
osl::Move
圧縮していない moveの表現 .
Definition basic_type.h:1052
osl::Move::ptypeO
PtypeO ptypeO() const
移動後のPtype, i.e., 成る手だった場合成った後
Definition basic_type.h:1162
osl::Move::ptype
Ptype ptype() const
Definition basic_type.h:1155
osl::Move::isDrop
bool isDrop() const
Definition basic_type.h:1150
osl::Move::capturePtype
Ptype capturePtype() const
Definition basic_type.h:1180
osl::Move::isNormal
bool isNormal() const
INVALID でも PASS でもない.
Definition basic_type.h:1088
osl::Move::to
const Square to() const
Definition basic_type.h:1132
osl::Move::from
const Square from() const
Definition basic_type.h:1125
osl::NumEffectState
利きを持つ局面
Definition numEffectState.h:34
osl::NumEffectState::hasEffectAt
bool hasEffectAt(Square target) const
対象とするマスにあるプレイヤーの利きがあるかどうか.
Definition numEffectState.h:324
osl::NumEffectState::hasEffectIf
bool hasEffectIf(PtypeO ptypeo, Square attacker, Square target) const
attackerにptypeoの駒がいると仮定した場合にtargetに利きがあるかどうか を stateをupdateしないで確かめる.
Definition numEffectState.h:465
osl::SimpleState::turn
Player turn() const
Definition simpleState.h:220
osl::container::MoveStack::lastMove
const Move lastMove(size_t last=1) const
Definition moveStack.h:28
osl::rating::Chase
Definition rating/feature.h:140
osl::rating::Chase::OpponentType
OpponentType
Definition rating/feature.h:142
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::saveResult
void saveResult(const std::string &directory, const range_t &range, const std::vector< double > &weights) const
Definition group.cc:33
osl::rating::Group::findMatch
virtual int findMatch(const NumEffectState &state, Move m, const RatingEnv &env) const
Definition group.cc:24
osl::rating::Group::load
bool load(const std::string &directory, const range_t &range, std::vector< double > &weights) const
Definition group.cc:47
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::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::rating::Group::group_name
std::string group_name
Definition group.h:19
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::Group::Group
Group(const std::string &name)
Definition group.cc:15
osl::rating::Group::~Group
virtual ~Group()
Definition group.cc:20
osl::rating::ImmediateAddSupport
Definition rating/feature.h:184
osl::rating::Karanari
Definition karanari.h:13
osl::rating::Karanari::index
static int index(const NumEffectState &state, Move move)
Definition karanari.h:40
osl::rating::RatingEnv
Definition ratingEnv.h:16
osl::rating::RatingEnv::history
MoveStack history
Definition ratingEnv.h:18
group.h
karanari.h
osl::rating::range_t
std::pair< int, int > range_t
Definition range.h:10
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
osl::PTYPE_PIECE_MIN
@ PTYPE_PIECE_MIN
Definition basic_type.h:104
osl::PTYPE_MAX
@ PTYPE_MAX
Definition basic_type.h:105
osl::PTYPE_EMPTY
@ PTYPE_EMPTY
Definition basic_type.h:85
osl::PTYPE_BASIC_MIN
@ PTYPE_BASIC_MIN
Definition basic_type.h:103
osl::isBasic
bool isBasic(Ptype ptype)
ptypeが基本型(promoteしていない)かのチェック
Definition basic_type.h:128
pinAttack.h
osl::rating::ChaseGroup::findMatch
int findMatch(const NumEffectState &state, Move move, const RatingEnv &env) const
Definition group.cc:150
osl::rating::ChaseGroup::ChaseGroup
ChaseGroup()
Definition group.cc:134
osl::rating::ImmediateAddSupportGroup::ImmediateAddSupportGroup
ImmediateAddSupportGroup()
Definition group.cc:198
osl::rating::KaranariGroup::findMatch
int findMatch(const NumEffectState &state, Move move, const RatingEnv &) const
Definition group.cc:192
osl::rating::KaranariGroup::KaranariGroup
KaranariGroup()
Definition group.cc:183