progress.h
Go to the documentation of this file.
/* newProgress.h */
#ifndef PROGRESS_EXPERIMENTAL_NEW_PROGRESS_H
#define PROGRESS_EXPERIMENTAL_NEW_PROGRESS_H
#include "osl/numEffectState.h"
#include "osl/eval/midgame.h"
#include "osl/container.h"
#include <cstdlib>
namespace osl
{
namespace progress
{
template <int N>
class ProgressN
{
int progress;
public:
explicit ProgressN(int value=0) : progress(value)
{
assert(isValid());
}
int value() const { return progress; }
bool isValid() const {
return (progress >= 0) && (progress < N);
}
};
template <int N>
inline bool operator==(ProgressN<N> l, ProgressN<N> r)
{
return l.value() == r.value();
}
template <int N>
inline bool operator!=(ProgressN<N> l, ProgressN<N> r)
{
return ! (l == r);
}
template <int N>
inline bool operator<(ProgressN<N> l, ProgressN<N> r)
{
return l.value() < r.value();
}
typedef ProgressN<16> Progress16;
typedef ProgressN<32> Progress32;
namespace ml
{
struct NewProgressDebugInfo
{
enum Feature
{
ATTACK_5X3,
DEFENSE_5X3,
ATTACK5X5,
STAND,
EFFECT5X5,
KING_RELATIVE_ATTACK,
KING_RELATIVE_DEFENSE,
NON_PAWN_ATTACKED_PAIR,
FEATURE_LIMIT
};
CArray<int, FEATURE_LIMIT> black_values;
CArray<int, FEATURE_LIMIT> white_values;
};
struct NewProgressData
{
CArray<MultiInt,2> non_pawn_ptype_attacked_pair_eval;
MultiInt promotion37_eval;
CArray<int, 2> progresses, attack5x5_progresses, stand_progresses,
effect_progresses, defenses;
CArray<int, 2> rook, bishop, gold, silver, promoted,
king_relative_attack, king_relative_defense, non_pawn_ptype_attacked_pair;
int pawn_facing, promotion37, piecestand7;
};
class NewProgress : private NewProgressData
{
public:
enum { ProgressScale = 2 };
private:
static bool initialized_flag;
static CArray<int, Piece::SIZE> stand_weight;
static CArray<int, 1125> attack5x5_weight;
static CArray<int, 5625> attack5x5_x_weight;
static CArray<int, 10125> attack5x5_y_weight;
static CArray<int, 75> effectstate_weight;
static CArray<int, 81*15*10> attack_relative;
static CArray<int, 81*15*10> defense_relative;
static CArray<int, 4284> king_relative_weight;
static CArray<int, 262144> attacked_ptype_pair_weight;
static CArray<int, 10> pawn_facing_weight;
static CArray<int, 16> promotion37_weight;
static CArray<int, 56> piecestand7_weight;
static int max_progress;
void updatePieceKingRelativeBonus(const NumEffectState &state);
void updateNonPawnAttackedPtypePair(const NumEffectState& state);
template <Player Owner>
void updateNonPawnAttackedPtypePairOne(const NumEffectState& state);
void updatePawnFacing(const NumEffectState& state);
template <Player Attack>
void promotion37One(const NumEffectState& state, int rank);
void updatePromotion37(const NumEffectState& state);
void updatePieceStand7(const NumEffectState& state);
template <Player P>
static void progressOne(const NumEffectState &state,
int &attack, int &defense);
template <Player P>
void updateAttack5x5PiecesAndState(const NumEffectState &state);
template <Player P>
void updateAttack5x5Pieces(PieceMask, const NumEffectState&);
template <Player P>
int attack5x5Value(const NumEffectState &state) const;
template <Player P>
static int index(Square king, Square target)
{
const int x_diff = std::abs(king.x() - target.x()); // [0, 4]
const int y_diff = (P == BLACK ? king.y() - target.y() :
target.y() - king.y()) + 2; // [-2, 2] + 2
return x_diff * 5 + y_diff;
}
template <Player P>
static int indexX(Square king, Square target)
{
int target_x = (king.x() > 5 ? 10 - king.x() : king.x()); // [1, 5]
int x_diff = king.x() - target.x(); // [-4, 4]
if (P == BLACK && king.x() >= 6)
{
x_diff = -x_diff;
}
else if (P == WHITE && king.x() >= 5)
{
x_diff = -x_diff;
}
const int y_diff = (P == BLACK ? king.y() - target.y() :
target.y() - king.y()) + 2; // [-2, 2] + 2
return ((x_diff + 4) * 5 + y_diff) * 5 + target_x - 1;
}
template <Player P>
static int indexY(Square king, Square target)
{
const int x_diff = std::abs(king.x() - target.x()); // [0, 4]
const int y_diff = (P == BLACK ? king.y() - target.y() :
target.y() - king.y()) + 2; // [-2, 2] + 2
const int king_y = (P == BLACK ? king.y() : 10 - king.y()); // [1, 9]
return (x_diff * 5 + y_diff) * 9 + king_y - 1;
}
static int index5x5(int rook, int bishop, int gold, int silver,
int promoted)
{
assert(0 <= promoted && promoted <= 4);
return promoted + 5 * (silver + 5 * (gold + 5 * (bishop + 3 * rook)));
}
static int index5x5x(int rook, int bishop, int gold, int silver,
int promoted, int king_x)
{
assert(0 <= promoted && promoted <= 4);
return king_x - 1 +
5 * (promoted + 5 * (silver + 5 * (gold + 5 * (bishop + 3 * rook))));
}
static int index5x5y(int rook, int bishop, int gold, int silver,
int promoted, int king_y)
{
assert(0 <= promoted && promoted <= 4);
return king_y - 1 +
9 * (promoted + 5 * (silver + 5 * (gold + 5 * (bishop + 3 * rook))));
}
template <Player P>
static int indexPerEffect(Square king, Square target,
int count)
{
const int x_diff = std::abs(king.x() - target.x()); // [0, 4]
const int y_diff = (P == BLACK ? king.y() - target.y() :
target.y() - king.y()) + 2; // [-2, 2] + 2
return x_diff * 5 + y_diff + std::min(8, count) * 25;
}
template <Player P>
static int indexPerEffectY(Square king, Square target,
int count)
{
const int king_y = (P == BLACK ? king.y() : 10 - king.y());
const int x_diff = std::abs(king.x() - target.x()); // [0, 4]
const int y_diff = (P == BLACK ? king.y() - target.y() :
target.y() - king.y()) + 2; // [-2, 2] + 2
return king_y - 1 + 9 * (x_diff * 5 + y_diff + std::min(8, count) * 25);
}
template <Player P>
static int indexPerEffectX(Square king, Square target,
int count)
{
const int king_x = (king.x() > 5 ? 10 - king.x() : king.x());
int x_diff = king.x() - target.x(); // [-4, 4]
if ((P == BLACK && (king.x() > 5)) ||
(P == WHITE && (king.x() >= 5)))
x_diff = -x_diff;
const int y_diff = (P == BLACK ? king.y() - target.y() :
target.y() - king.y()) + 2; // [-2, 2] + 2
return king_x - 1 + 5 * (x_diff + 4 +
9 * (y_diff + 5 * std::min(8, count)));
}
template <Player P>
static int indexRelative(const Square king,
const Ptype ptype, const Square pos)
{
const int x = std::abs(pos.x() - king.x());
const int y = (king.y() - pos.y()) *
(P == osl::BLACK ? 1 : -1) + 8;
return (ptype - osl::PTYPE_PIECE_MIN) * 17 * 9 + (x * 17 + y);
}
static int indexRelative(const Player player, const Square king,
const Piece piece)
{
if (player == BLACK)
{
return indexRelative<BLACK>(king, piece.ptype(),
piece.square());
}
else
{
return indexRelative<WHITE>(king, piece.ptype(),
piece.square());
}
}
public:
NewProgress(const NumEffectState &state);
int progress() const
{
return
std::max(std::min(progresses[0] + progresses[1] +
attack5x5_progresses[0] +
attack5x5_progresses[1] +
stand_progresses[0] + stand_progresses[1] +
effect_progresses[0] + effect_progresses[1] +
defenses[0] + defenses[1] +
king_relative_attack[0] +
king_relative_attack[1] +
king_relative_defense[0] +
king_relative_defense[1] +
non_pawn_ptype_attacked_pair[0] +
non_pawn_ptype_attacked_pair[1] +
pawn_facing + promotion37 + piecestand7,
max_progress-ProgressScale), 0) / ProgressScale;
}
static int maxProgress() { return max_progress / ProgressScale; }
template<Player P>
void updateSub(const NumEffectState &new_state, Move last_move);
void update(const NumEffectState &new_state, Move last_move){
if(new_state.turn()==BLACK)
updateSub<WHITE>(new_state,last_move);
else
updateSub<BLACK>(new_state,last_move);
}
NewProgressDebugInfo debugInfo() const;
private:
template<Player P>
void updateMain(const NumEffectState &new_state, Move last_move);
public:
const Progress16 progress16() const
{
return Progress16(16 * progress() / maxProgress());
}
const Progress16 progress16(Player p) const
{
assert(maxProgress() > 0);
return Progress16(
16 * std::max(
std::min(progresses[playerToIndex(alt(p))] +
attack5x5_progresses[playerToIndex(alt(p))] +
stand_progresses[playerToIndex(alt(p))] +
effect_progresses[playerToIndex(alt(p))] +
defenses[playerToIndex(alt(p))] +
king_relative_attack[playerToIndex(alt(p))] +
king_relative_defense[playerToIndex(p)] +
non_pawn_ptype_attacked_pair[p],
max_progress-ProgressScale), 0)
/ ProgressScale / maxProgress());
}
// p == attack player, alt(p) == king owner
const Progress16 progressAttack(Player p) const
{
assert(maxProgress() > 0);
return Progress16(
8 * std::max(
std::min(progresses[alt(p)] +
attack5x5_progresses[alt(p)] +
stand_progresses[alt(p)] +
effect_progresses[alt(p)] +
king_relative_attack[alt(p)],
max_progress-ProgressScale), -max_progress+ProgressScale)
/ ProgressScale / maxProgress() + 8);
}
// p == king owner (defense player)
const Progress16 progressDefense(Player p) const
{
assert(maxProgress() > 0);
return Progress16(
8 * std::max(
std::min(defenses[alt(p)] +
king_relative_defense[p] +
non_pawn_ptype_attacked_pair[p],
max_progress-ProgressScale),
-max_progress + ProgressScale)
/ ProgressScale / maxProgress() + 8);
}
static bool initialized()
{
return initialized_flag;
}
static bool setUp(const char *filename);
static bool setUp();
static std::string defaultFilename();
const NewProgressData rawData() const { return *this; }
};
bool operator==(const NewProgressData& l, const NewProgressData& r);
inline bool operator==(const NewProgress& l, const NewProgress& r)
{
return l.rawData() == r.rawData();
}
}
using ml::NewProgress;
}
using progress::Progress16;
using progress::Progress32;
using progress::NewProgress;
}
#endif // PROGRESS_EXPERIMENTAL_NEW_PROGRESS_H
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
osl::CArray
Definition container.h:20
osl::Move
圧縮していない moveの表現 .
Definition basic_type.h:1052
osl::NewProgress::NewProgress
NewProgress(const NumEffectState &state)
Definition progress.cc:546
osl::NumEffectState
利きを持つ局面
Definition numEffectState.h:34
osl::PieceMask
駒番号のビットセット.
Definition pieceMask.h:21
osl::Piece
駒.
Definition basic_type.h:788
osl::Piece::ptype
Ptype ptype() const
Definition basic_type.h:821
osl::Piece::square
const Square square() const
Definition basic_type.h:832
osl::SimpleState::turn
Player turn() const
Definition simpleState.h:220
osl::Square
Definition basic_type.h:532
osl::Square::y
int y() const
将棋としてのY座標を返す.
Definition basic_type.h:567
osl::Square::x
int x() const
将棋としてのX座標を返す.
Definition basic_type.h:563
osl::progress::ProgressN
Definition progress.h:15
osl::progress::ProgressN< 16 >::value
int value() const
Definition progress.h:22
osl::progress::ProgressN::ProgressN
ProgressN(int value=0)
Definition progress.h:18
osl::progress::ProgressN::isValid
bool isValid() const
Definition progress.h:23
osl::progress::ProgressN< 16 >::progress
int progress
Definition progress.h:16
osl::progress::ml::NewProgress
Definition progress.h:76
osl::progress::ml::NewProgress::updateMain
void updateMain(const NumEffectState &new_state, Move last_move)
Definition progress.cc:590
osl::progress::ml::NewProgress::attack5x5_x_weight
static CArray< int, 5625 > attack5x5_x_weight
Definition progress.h:83
osl::progress::ml::NewProgress::index5x5x
static int index5x5x(int rook, int bishop, int gold, int silver, int promoted, int king_x)
Definition progress.h:152
osl::progress::ml::NewProgress::progressAttack
const Progress16 progressAttack(Player p) const
Definition progress.h:278
osl::progress::ml::NewProgress::initialized
static bool initialized()
Definition progress.h:304
osl::progress::ml::NewProgress::update
void update(const NumEffectState &new_state, Move last_move)
Definition progress.h:246
osl::progress::ml::NewProgress::indexRelative
static int indexRelative(const Square king, const Ptype ptype, const Square pos)
Definition progress.h:201
osl::progress::ml::NewProgress::updatePawnFacing
void updatePawnFacing(const NumEffectState &state)
Definition progress.cc:476
osl::progress::ml::NewProgress::promotion37One
void promotion37One(const NumEffectState &state, int rank)
Definition progress.cc:492
osl::progress::ml::NewProgress::updateAttack5x5PiecesAndState
void updateAttack5x5PiecesAndState(const NumEffectState &state)
Definition progress.cc:336
osl::progress::ml::NewProgress::effectstate_weight
static CArray< int, 75 > effectstate_weight
Definition progress.h:85
osl::progress::ml::NewProgress::setUp
static bool setUp()
Definition progress.cc:283
osl::progress::ml::NewProgress::progressOne
static void progressOne(const NumEffectState &state, int &attack, int &defense)
Definition progress.cc:296
osl::progress::ml::NewProgress::index5x5y
static int index5x5y(int rook, int bishop, int gold, int silver, int promoted, int king_y)
Definition progress.h:159
osl::progress::ml::NewProgress::updateSub
void updateSub(const NumEffectState &new_state, Move last_move)
Definition progress.cc:684
osl::progress::ml::NewProgress::promotion37_weight
static CArray< int, 16 > promotion37_weight
Definition progress.h:91
osl::progress::ml::NewProgress::indexRelative
static int indexRelative(const Player player, const Square king, const Piece piece)
Definition progress.h:209
osl::progress::ml::NewProgress::progress16
const Progress16 progress16(Player p) const
Definition progress.h:261
osl::progress::ml::NewProgress::updateAttack5x5Pieces
void updateAttack5x5Pieces(PieceMask, const NumEffectState &)
Definition progress.cc:367
osl::progress::ml::NewProgress::pawn_facing_weight
static CArray< int, 10 > pawn_facing_weight
Definition progress.h:90
osl::progress::ml::NewProgress::max_progress
static int max_progress
Definition progress.h:93
osl::progress::ml::NewProgress::progress
int progress() const
Definition progress.h:225
osl::progress::ml::NewProgress::indexPerEffectX
static int indexPerEffectX(Square king, Square target, int count)
Definition progress.h:187
osl::progress::ml::NewProgress::defense_relative
static CArray< int, 81 *15 *10 > defense_relative
Definition progress.h:87
osl::progress::ml::NewProgress::progress16
const Progress16 progress16() const
Definition progress.h:257
osl::progress::ml::NewProgress::NewProgress
NewProgress(const NumEffectState &state)
Definition progress.cc:546
osl::progress::ml::NewProgress::defaultFilename
static std::string defaultFilename()
Definition progress.cc:288
osl::progress::ml::NewProgress::indexY
static int indexY(Square king, Square target)
Definition progress.h:138
osl::progress::ml::NewProgress::attack5x5Value
int attack5x5Value(const NumEffectState &state) const
Definition progress.cc:386
osl::progress::ml::NewProgress::progressDefense
const Progress16 progressDefense(Player p) const
Definition progress.h:292
osl::progress::ml::NewProgress::indexPerEffectY
static int indexPerEffectY(Square king, Square target, int count)
Definition progress.h:177
osl::progress::ml::NewProgress::updateNonPawnAttackedPtypePairOne
void updateNonPawnAttackedPtypePairOne(const NumEffectState &state)
Definition progress.cc:436
osl::progress::ml::NewProgress::indexX
static int indexX(Square king, Square target)
Definition progress.h:121
osl::progress::ml::NewProgress::maxProgress
static int maxProgress()
Definition progress.h:243
osl::progress::ml::NewProgress::index
static int index(Square king, Square target)
Definition progress.h:113
osl::progress::ml::NewProgress::ProgressScale
@ ProgressScale
Definition progress.h:78
osl::progress::ml::NewProgress::piecestand7_weight
static CArray< int, 56 > piecestand7_weight
Definition progress.h:92
osl::progress::ml::NewProgress::updatePromotion37
void updatePromotion37(const NumEffectState &state)
Definition progress.cc:523
osl::progress::ml::NewProgress::index5x5
static int index5x5(int rook, int bishop, int gold, int silver, int promoted)
Definition progress.h:146
osl::progress::ml::NewProgress::attacked_ptype_pair_weight
static CArray< int, 262144 > attacked_ptype_pair_weight
Definition progress.h:89
osl::progress::ml::NewProgress::updatePieceKingRelativeBonus
void updatePieceKingRelativeBonus(const NumEffectState &state)
Definition progress.cc:410
osl::progress::ml::NewProgress::rawData
const NewProgressData rawData() const
Definition progress.h:311
osl::progress::ml::NewProgress::updatePieceStand7
void updatePieceStand7(const NumEffectState &state)
Definition progress.cc:532
osl::progress::ml::NewProgress::indexPerEffect
static int indexPerEffect(Square king, Square target, int count)
Definition progress.h:167
osl::progress::ml::NewProgress::attack5x5_weight
static CArray< int, 1125 > attack5x5_weight
Definition progress.h:82
osl::progress::ml::NewProgress::attack_relative
static CArray< int, 81 *15 *10 > attack_relative
Definition progress.h:86
osl::progress::ml::NewProgress::stand_weight
static CArray< int, Piece::SIZE > stand_weight
Definition progress.h:81
osl::progress::ml::NewProgress::updateNonPawnAttackedPtypePair
void updateNonPawnAttackedPtypePair(const NumEffectState &state)
Definition progress.cc:469
osl::progress::ml::NewProgress::initialized_flag
static bool initialized_flag
Definition progress.h:80
osl::progress::ml::NewProgress::king_relative_weight
static CArray< int, 4284 > king_relative_weight
Definition progress.h:88
osl::progress::ml::NewProgress::attack5x5_y_weight
static CArray< int, 10125 > attack5x5_y_weight
Definition progress.h:84
osl::progress::ml::NewProgress::debugInfo
NewProgressDebugInfo debugInfo() const
Definition progress.cc:726
container.h
midgame.h
osl::progress::ml
Definition progress.cc:756
osl::progress::ml::operator==
bool operator==(const NewProgressData &l, const NewProgressData &r)
Definition progress.cc:12
osl::progress
Definition progress.cc:754
osl::progress::operator!=
bool operator!=(ProgressN< N > l, ProgressN< N > r)
Definition progress.h:33
osl::progress::operator==
bool operator==(ProgressN< N > l, ProgressN< N > r)
Definition progress.h:28
osl::progress::Progress16
ProgressN< 16 > Progress16
Definition progress.h:42
osl::progress::Progress32
ProgressN< 32 > Progress32
Definition progress.h:43
osl::progress::operator<
bool operator<(ProgressN< N > l, ProgressN< N > r)
Definition progress.h:38
osl
Definition additionalEffect.h:6
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
osl::PTYPE_PIECE_MIN
@ PTYPE_PIECE_MIN
Definition basic_type.h:104
osl::playerToIndex
constexpr int playerToIndex(Player player)
Definition basic_type.h:16
osl::MultiInt
QuadInt MultiInt
Definition midgame.h:13
osl::Player
Player
Definition basic_type.h:8
osl::WHITE
@ WHITE
Definition basic_type.h:10
osl::BLACK
@ BLACK
Definition basic_type.h:9
osl::alt
constexpr Player alt(Player player)
Definition basic_type.h:13
numEffectState.h
osl::progress::ml::NewProgressData
Definition progress.h:66
osl::progress::ml::NewProgressData::effect_progresses
CArray< int, 2 > effect_progresses
Definition progress.h:70
osl::progress::ml::NewProgressData::bishop
CArray< int, 2 > bishop
Definition progress.h:71
osl::progress::ml::NewProgressData::promotion37
int promotion37
Definition progress.h:73
osl::progress::ml::NewProgressData::gold
CArray< int, 2 > gold
Definition progress.h:71
osl::progress::ml::NewProgressData::progresses
CArray< int, 2 > progresses
Definition progress.h:69
osl::progress::ml::NewProgressData::non_pawn_ptype_attacked_pair_eval
CArray< MultiInt, 2 > non_pawn_ptype_attacked_pair_eval
Definition progress.h:67
osl::progress::ml::NewProgressData::rook
CArray< int, 2 > rook
Definition progress.h:71
osl::progress::ml::NewProgressData::piecestand7
int piecestand7
Definition progress.h:73
osl::progress::ml::NewProgressData::pawn_facing
int pawn_facing
Definition progress.h:73
osl::progress::ml::NewProgressData::non_pawn_ptype_attacked_pair
CArray< int, 2 > non_pawn_ptype_attacked_pair
Definition progress.h:72
osl::progress::ml::NewProgressData::king_relative_defense
CArray< int, 2 > king_relative_defense
Definition progress.h:72
osl::progress::ml::NewProgressData::promoted
CArray< int, 2 > promoted
Definition progress.h:71
osl::progress::ml::NewProgressData::attack5x5_progresses
CArray< int, 2 > attack5x5_progresses
Definition progress.h:69
osl::progress::ml::NewProgressData::king_relative_attack
CArray< int, 2 > king_relative_attack
Definition progress.h:72
osl::progress::ml::NewProgressData::promotion37_eval
MultiInt promotion37_eval
Definition progress.h:68
osl::progress::ml::NewProgressData::defenses
CArray< int, 2 > defenses
Definition progress.h:70
osl::progress::ml::NewProgressData::silver
CArray< int, 2 > silver
Definition progress.h:71
osl::progress::ml::NewProgressData::stand_progresses
CArray< int, 2 > stand_progresses
Definition progress.h:69
osl::progress::ml::NewProgressDebugInfo
Definition progress.h:48
osl::progress::ml::NewProgressDebugInfo::black_values
CArray< int, FEATURE_LIMIT > black_values
Definition progress.h:61
osl::progress::ml::NewProgressDebugInfo::Feature
Feature
Definition progress.h:50
osl::progress::ml::NewProgressDebugInfo::DEFENSE_5X3
@ DEFENSE_5X3
Definition progress.h:52
osl::progress::ml::NewProgressDebugInfo::FEATURE_LIMIT
@ FEATURE_LIMIT
Definition progress.h:59
osl::progress::ml::NewProgressDebugInfo::ATTACK_5X3
@ ATTACK_5X3
Definition progress.h:51
osl::progress::ml::NewProgressDebugInfo::KING_RELATIVE_DEFENSE
@ KING_RELATIVE_DEFENSE
Definition progress.h:57
osl::progress::ml::NewProgressDebugInfo::ATTACK5X5
@ ATTACK5X5
Definition progress.h:53
osl::progress::ml::NewProgressDebugInfo::NON_PAWN_ATTACKED_PAIR
@ NON_PAWN_ATTACKED_PAIR
Definition progress.h:58
osl::progress::ml::NewProgressDebugInfo::EFFECT5X5
@ EFFECT5X5
Definition progress.h:55
osl::progress::ml::NewProgressDebugInfo::KING_RELATIVE_ATTACK
@ KING_RELATIVE_ATTACK
Definition progress.h:56
osl::progress::ml::NewProgressDebugInfo::STAND
@ STAND
Definition progress.h:54
osl::progress::ml::NewProgressDebugInfo::white_values
CArray< int, FEATURE_LIMIT > white_values
Definition progress.h:62