progressEval.h
Go to the documentation of this file.
00001 /* progressEval.h
00002  */
00003 #ifndef EVAL_PROGRESSEVAL_H
00004 #define EVAL_PROGRESSEVAL_H
00005 
00006 // #define EXPERIMENTAL_EVAL
00007 
00008 #include "osl/effect_util/pin.h"
00009 #include "osl/eval/pieceEval.h"
00010 #include "osl/eval/minorPieceBonus.h"
00011 
00012 #include "osl/eval/ppair/piecePairPieceEval.h"
00013 #include "osl/eval/endgame/attackDefense.h"
00014 #include "osl/eval/mobilityTable.h"
00015 #include "osl/progress/effect5x3.h"
00016 #include "osl/progress/effect5x3d.h"
00017 #include "osl/progress/progress32.h"
00018 #include "osl/mobility/rookMobility.h"
00019 #include "osl/mobility/bishopMobility.h"
00020 #include "osl/mobility/lanceMobility.h"
00021 #include "osl/state/numEffectState.h"
00022 #include <boost/static_assert.hpp>
00023 
00024 namespace osl
00025 {
00026   namespace eval
00027   {
00028     struct ProgressDebugInfo
00029     {
00030       int eval, opening, endgame;
00031       int progress, progress_bonus;
00032       int progress_independent_bonus, progress_dependent_bonus;
00033       int minor_piece_bonus;
00034 
00035       // break down of progress bonus
00036       int black_danger, black_defense, white_danger, white_defense;
00037       // progress independent bonus
00038       int mobility_bonus, two_rook_bonus, knight_check_bonus, rook_rank_bonus,
00039         enter_king_bonus, middle_king_bonus, silver_penalty, gold_penalty;
00040       // progress dependent bonus
00041       int king8_attack_bonus, pin_bonus;
00042 
00043       MinorPieceDebugInfo minor_piece_bonus_info;
00044     };
00045 
00049     template <class OpeningEval>
00050     class ProgressEvalGeneral
00051     {
00052     public:
00053       typedef OpeningEval opening_eval_t;
00054       typedef endgame::AttackDefense endgame_eval_t;
00055       typedef progress::Effect5x3WithBonus progress_t;
00056       typedef progress::Effect5x3d defense_t;
00057     private:
00058       opening_eval_t opening_eval;
00059       endgame_eval_t endgame_eval;
00060       progress_t current_progress;
00061       defense_t defense_effect;
00062       MinorPieceBonus minor_piece_bonus;
00063       // Records pinned pieces of a player, not the opponent's
00064       // (owner of pieces == player)
00065       mutable CArray<PieceMask, 2> pin_mask;
00066       CArray2d<int, 2, SHORT8_DIRECTION_MAX+1> can_check_pieces;
00067       int progress_independent_bonus;
00068       int progress_dependent_bonus;
00069       int major_pieces;
00070       CArray<int,2> attack_bonus; // index is king (i.e., defense)
00071       int rook_mobility, bishop_mobility, lance_mobility;
00072       enum{
00073         INVALID=EvalTraits<BLACK>::MAX_VALUE+1,
00074           };
00075       mutable int cache;
00076 
00077       static CArray<int, PTYPEO_SIZE> capture_values;
00078 
00079       template<Player P, Ptype PTYPE, Direction Dir, Direction LongDir>
00080       void initializeCheckPieceDir(const NumEffectState &state, int count);
00081       template <Player P, Ptype PTYPE>
00082       void initializeCheckPiece(const NumEffectState &state);
00083       static void setUpInternal(const char *filename=0);
00084     public:
00085       ProgressEvalGeneral(const NumEffectState& state);
00086       void changeTurn() {}
00087       static bool initialized() 
00088       { 
00089         return opening_eval_t::initialized();
00090       } 
00091       static bool setUp(const char *filename)
00092       {
00093         if (! opening_eval_t::setUp())
00094           return false;
00095         setUpInternal(filename);
00096         return true;
00097       }
00098       static bool setUp()
00099       {
00100         if (! opening_eval_t::setUp())
00101           return false;
00102         setUpInternal();
00103         return true;
00104       }
00105 
00107       static const int ROUND_UP = 64;
00109       static int attackDefenseBonusT16(Progress16 black, Progress16 white,
00110                                        Progress16 black_defense,
00111                                        Progress16 white_defense) 
00112       {
00113         return 
00114           (white.value() * 2 - white_defense.value()
00115            - black.value() * 2 + black_defense.value())
00116           * 3200 / 2;
00117       }
00118       static int composeValue(int value_opening, int value_endgame, 
00119                               Progress16 progress16,
00120                               Progress16 black,
00121                               Progress16 white,
00122                               Progress16 black_defense,
00123                               Progress16 white_defense,
00124                               int minor_piece_bonus,
00125                               int progress_independent_bonus,
00126                               int progress_dependent_bonus)
00127       {
00128         BOOST_STATIC_ASSERT(((PtypeEvalTraits<BISHOP>::val * 2 + PtypeEvalTraits<PAWN>::val) %  16) == 0);
00129         BOOST_STATIC_ASSERT((PtypeEvalTraits<PAWN>::val % 32) == 0);
00130         assert(progress16.isValid());
00131         assert(black.isValid());
00132         assert(white.isValid());
00133         /*
00134           value_opening * (16 - progress16) + value_endgame * progress16
00135           + bonus * (white_progress - black_progress) * prorgress16 / 16 */
00136         int result = value_opening*16 + 
00137           progress16.value() * (value_endgame-value_opening +
00138                                 (attackDefenseBonusT16(black, white,
00139                                                        black_defense,
00140                                                        white_defense))
00141                                 / 16);
00142 
00143         result += progress_independent_bonus;
00144         result += minor_piece_bonus;
00145         result += progress_dependent_bonus;
00146         result &= ~(ROUND_UP-1);
00147         assert(result % 2 == 0);
00148         return result;
00149       }
00150       const Progress16 progress16() const { return current_progress.progress16(); }
00151       const Progress16 progress16bonus(Player p) const {
00152         return current_progress.progress16bonus(p);
00153       }
00154       void invalidateCache(){ 
00155         cache=INVALID;
00156       }
00157       int value() const
00158       {
00159         if(cache==INVALID)
00160           cache = composeValue(
00161             openingValue(), 
00162             endgame_eval.value(), 
00163             progress16(),
00164             current_progress.progress16bonus(BLACK),
00165             current_progress.progress16bonus(WHITE),
00166             defense_effect.progress16(BLACK),
00167             defense_effect.progress16(WHITE),
00168             minor_piece_bonus.value(
00169               progress16(),
00170               progress16bonus(BLACK),
00171               progress16bonus(WHITE)),
00172             progress_independent_bonus,
00173             progress_dependent_bonus);
00174 
00175         return cache;
00176       }
00177       const Progress32 progress32() const { 
00178         return Progress32(current_progress.progress16(BLACK).value()
00179                           + current_progress.progress16(WHITE).value()); 
00180       }
00181       static void setValues(const SimpleState&, Progress16 progress16,
00182                             container::PieceValues&);
00183       static void setValues(const SimpleState& s, container::PieceValues& o);
00184 
00185       int expect(const NumEffectState& state, Move move) const;
00186       Move suggestMove(const NumEffectState&) const 
00187       {
00188         return Move();
00189       }
00190       void update(const NumEffectState& new_state, Move last_move);
00191       template <Player p>
00192       int calculateAttackBonusEach(const NumEffectState& state) const;
00193       template <Player Attack, Direction Dir>
00194       int calculateAttackBonusOne(const NumEffectState& state) const;
00195       int calculateKnightCheck(const NumEffectState& state) const;
00196       template <osl::Player P>
00197       int calculateKnightCheckEach(const NumEffectState& state) const;
00198       template <Player p>
00199       int calculateEnterKingBonus(const NumEffectState& state) const ;
00200       template <Player p>
00201       int calculateMiddleKingBonus(const NumEffectState& state) const;
00202       int calculateRookRankBonus(const NumEffectState& state) const;
00203 
00204     public:
00205       static int infty()
00206       {
00207         assert(endgame_eval_t::infty() <= opening_eval_t::infty());
00208         // 序盤を使用
00209         return composeValue(opening_eval_t::infty(), 0, Progress16(0),
00210                             Progress16(0), Progress16(0), Progress16(0),
00211                             Progress16(0), 0, 0, 0);
00212       }
00213 
00214       static int captureValue(PtypeO ptypeO)
00215       {
00216         return capture_values[ptypeOIndex(ptypeO)];
00217       }
00218       static int seeScale()
00219       {
00220         return captureValue(newPtypeO(WHITE,PAWN))
00221           / PieceEval::captureValue(newPtypeO(WHITE,PAWN));
00222       }
00223 
00224       const PieceMask pins(Player player) const { 
00225         return pin_mask[player]; 
00226       }
00227       // for debug
00228       int minorPieceValue() const { 
00229         return minor_piece_bonus.value(progress16(),
00230                                        progress16bonus(BLACK),
00231                                        progress16bonus(WHITE)); 
00232       }
00233       int openingValue() const { return opening_eval.value(); }
00234       int endgameValue() const { return endgame_eval.value(); }
00235       ProgressDebugInfo debugInfo(const NumEffectState& state) const;
00236 
00237       // public for debugging purpose only
00238       // also updates pin_mask
00239       int calculatePinBonus(const NumEffectState& state) const;
00240       int calculateMobilityBonus() const;
00241       static int calculateMobilityBonusRook(const NumEffectState& state);
00242       static int calculateMobilityBonusBishop(const NumEffectState& state);
00243       static int calculateMobilityBonusLance(const NumEffectState& state);
00244       int calculateAttackRooks(const NumEffectState& state) const;
00245       int calculateAttackBonus(const NumEffectState& state) const;
00246       int calculateSilverPenalty(const NumEffectState& state) const;
00247       int calculateGoldPenalty(const NumEffectState& state) const;
00248 
00249       int attackDefenseBonus() const
00250       {
00251         return progress16().value()
00252           * attackDefenseBonusT16(current_progress.progress16bonus(BLACK),
00253                                   current_progress.progress16bonus(WHITE),
00254                                   defense_effect.progress16(BLACK),
00255                                   defense_effect.progress16(WHITE))
00256           / 16;
00257       }
00258       int attackBonusScale(int val, Player attack) const 
00259       {
00260         return val * current_progress.progress16(alt(attack)).value() / 16 * 4;
00261       }
00262       void debug() const {}
00263       enum { AdjustableDimension
00264              = PTYPE_SIZE + endgame::KingPieceTable::EffectiveDimension*2 };
00265       static void resetWeights(const int *weight);
00266     };
00267 
00268     typedef PiecePairPieceEval progress_eval_opening_t;
00269     class ProgressEval : public ProgressEvalGeneral<progress_eval_opening_t> 
00270     {
00271     public:
00272       explicit ProgressEval(const NumEffectState& state) 
00273         : ProgressEvalGeneral<progress_eval_opening_t>(state)
00274       {
00275       }
00276       static const PtypeEvalTable Piece_Value;
00277     };
00278   } // namespace eval
00279 } // namespace osl
00280 
00281 #endif /* EVAL_PROGRESSEVAL_H */
00282 // ;;; Local Variables:
00283 // ;;; mode:c++
00284 // ;;; c-basic-offset:2
00285 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines