newProgress.h
Go to the documentation of this file.
00001 /* newProgress.h */
00002 #ifndef PROGRESS_EXPERIMENTAL_NEW_PROGRESS_H
00003 #define PROGRESS_EXPERIMENTAL_NEW_PROGRESS_H
00004 
00005 #include "osl/state/numEffectState.h"
00006 #include "osl/progress/progress16.h"
00007 #include "osl/eval/ml/midgame.h"
00008 #include "osl/centering5x3.h"
00009 #include "osl/misc/carray.h"
00010 namespace osl
00011 {
00012   namespace progress
00013   {
00014     namespace ml
00015     {
00016       struct NewProgressData
00017       {
00018         CArray<MultiInt,2> non_pawn_ptype_attacked_pair_eval;
00019         CArray<int, 2> progresses, attack5x5_progresses, stand_progresses,
00020           effect_progresses, defenses;
00021         CArray<int, 2> rook, bishop, gold, silver, promoted,
00022           king_relative_attack, king_relative_defense, non_pawn_ptype_attacked_pair;
00023       };
00024       class NewProgress : private NewProgressData
00025       {
00026       public:
00027         enum { ProgressScale = 2 };
00028       private:
00029         static bool initialized_flag;
00030         static CArray<int, Piece::SIZE> stand_weight;
00031         static CArray<int, 1125> attack5x5_weight;
00032         static CArray<int, 5625> attack5x5_x_weight;
00033         static CArray<int, 10125> attack5x5_y_weight;
00034         static CArray<int, 75> effectstate_weight;
00035         static CArray<int, 81*15*10> attack_relative;
00036         static CArray<int, 81*15*10> defense_relative;
00037         static CArray<int, 4284> king_relative_weight;
00038         static CArray<int, 262144> attacked_ptype_pair_weight;
00039         static int max_progress;
00040         void updatePieceKingRelativeBonus(const NumEffectState &state);
00041         void updateNonPawnAttackedPtypePair(const NumEffectState& state);
00042         template <Player Owner>
00043         void updateNonPawnAttackedPtypePairOne(const NumEffectState& state);
00044         template <Player P>
00045         static void progressOne(const NumEffectState &state,
00046                                 int &attack, int &defense);
00047         template <Player P>
00048         void updateAttack5x5PiecesAndState(const NumEffectState &state);
00049         template <Player P>
00050         void updateAttack5x5Pieces(PieceMask, const NumEffectState&);
00051         template <Player P>
00052         int attack5x5Value(const NumEffectState &state) const;
00053         template <Player P>
00054         static int index(Square king, Square target)
00055         {
00056           const int x_diff = std::abs(king.x() - target.x()); // [0, 4]
00057           const int y_diff = (P == BLACK ? king.y() - target.y() :
00058                               target.y() - king.y()) + 2; // [-2, 2] + 2
00059           return x_diff * 5 + y_diff;
00060         }
00061         template <Player P>
00062         static int indexX(Square king, Square target)
00063         {
00064           int target_x = (king.x() > 5 ? 10 - king.x() : king.x()); // [1, 5]
00065           int x_diff = king.x() - target.x(); // [-4, 4]
00066           if (P == BLACK && king.x() >= 6)
00067           {
00068             x_diff = -x_diff;
00069           }
00070           else if (P == WHITE && king.x() >= 5)
00071           {
00072             x_diff = -x_diff;
00073           }
00074           const int y_diff = (P == BLACK ? king.y() - target.y() :
00075                               target.y() - king.y()) + 2; // [-2, 2] + 2
00076           return ((x_diff + 4) * 5 + y_diff) * 5 + target_x - 1;
00077         }
00078         template <Player P>
00079         static int indexY(Square king, Square target)
00080         {
00081           const int x_diff = std::abs(king.x() - target.x()); // [0, 4]
00082           const int y_diff = (P == BLACK ? king.y() - target.y() :
00083                               target.y() - king.y()) + 2; // [-2, 2] + 2
00084           const int king_y = (P == BLACK ? king.y() : 10 - king.y()); // [1, 9]
00085           return (x_diff * 5 + y_diff) * 9 + king_y - 1;
00086         }
00087         static int index5x5(int rook, int bishop, int gold, int silver,
00088                             int promoted)
00089         {
00090           assert(0 <= promoted && promoted <= 4);
00091           return promoted + 5 * (silver + 5 * (gold + 5 * (bishop + 3 * rook)));
00092         }
00093         static int index5x5x(int rook, int bishop, int gold, int silver,
00094                              int promoted, int king_x)
00095         {
00096           assert(0 <= promoted && promoted <= 4);
00097           return king_x - 1 +
00098             5 * (promoted + 5 * (silver + 5 * (gold + 5 * (bishop + 3 * rook))));
00099         }
00100         static int index5x5y(int rook, int bishop, int gold, int silver,
00101                              int promoted, int king_y)
00102         {
00103           assert(0 <= promoted && promoted <= 4);
00104           return king_y - 1 +
00105             9 * (promoted + 5 * (silver + 5 * (gold + 5 * (bishop + 3 * rook))));
00106         }
00107         template <Player P>
00108         static int indexPerEffect(Square king, Square target,
00109                                   int count)
00110         {
00111           const int x_diff = std::abs(king.x() - target.x()); // [0, 4]
00112           const int y_diff = (P == BLACK ? king.y() - target.y() :
00113                               target.y() - king.y()) + 2; // [-2, 2] + 2
00114           return x_diff * 5 + y_diff + std::min(8, count) * 25;
00115         }
00116 
00117         template <Player P>
00118         static int indexPerEffectY(Square king, Square target,
00119                                    int count)
00120         {
00121           const int king_y = (P == BLACK ? king.y() : 10 - king.y());
00122           const int x_diff = std::abs(king.x() - target.x()); // [0, 4]
00123           const int y_diff = (P == BLACK ? king.y() - target.y() :
00124                               target.y() - king.y()) + 2; // [-2, 2] + 2
00125           return king_y - 1 + 9 * (x_diff * 5 + y_diff + std::min(8, count) * 25);
00126         }
00127         template <Player P>
00128         static int indexPerEffectX(Square king, Square target,
00129                                    int count)
00130         {
00131           const int king_x = (king.x() > 5 ? 10 - king.x() : king.x());
00132           int x_diff = king.x() - target.x(); // [-4, 4]
00133           if ((P == BLACK && (king.x() > 5)) ||
00134               (P == WHITE && (king.x() >= 5)))
00135             x_diff = -x_diff;
00136           const int y_diff = (P == BLACK ? king.y() - target.y() :
00137                               target.y() - king.y()) + 2; // [-2, 2] + 2
00138           return king_x - 1 + 5 * (x_diff + 4 +
00139                                    9 * (y_diff + 5 *  std::min(8, count)));
00140         }
00141         template <Player P>
00142         static int indexRelative(const Square king,
00143                                  const Ptype ptype, const Square pos)
00144         {
00145           const int x = std::abs(pos.x() - king.x());
00146           const int y = (king.y() - pos.y()) *
00147             (P == osl::BLACK ? 1 : -1) + 8;
00148           return (ptype - osl::PTYPE_PIECE_MIN) * 17 * 9 + (x * 17 + y);
00149         }
00150         static int indexRelative(const Player player, const Square king,
00151                                  const Piece piece)
00152         {
00153           if (player == BLACK)
00154           {
00155             return indexRelative<BLACK>(king, piece.ptype(),
00156                                         piece.square());
00157           }
00158           else
00159           {
00160             return indexRelative<WHITE>(king, piece.ptype(),
00161                                         piece.square());
00162           }
00163         }
00164       public:
00165         NewProgress(const NumEffectState &state);
00166         int progress() const
00167         {
00168           return
00169             std::max(std::min(progresses[0] + progresses[1] +
00170                               attack5x5_progresses[0] +
00171                               attack5x5_progresses[1] +
00172                               stand_progresses[0] + stand_progresses[1] +
00173                               effect_progresses[0] + effect_progresses[1] +
00174                               defenses[0] + defenses[1] +
00175                               king_relative_attack[0] +
00176                               king_relative_attack[1] +
00177                               king_relative_defense[0] +
00178                               king_relative_defense[1] +
00179                               non_pawn_ptype_attacked_pair[0] +
00180                               non_pawn_ptype_attacked_pair[1],
00181                               max_progress-ProgressScale), 0) / ProgressScale;
00182         }
00183         static int maxProgress() { return max_progress / ProgressScale; }
00184         template<Player P>
00185         void updateSub(const NumEffectState &new_state, Move last_move);
00186         void update(const NumEffectState &new_state, Move last_move){
00187           if(new_state.turn()==BLACK)
00188             updateSub<WHITE>(new_state,last_move);
00189           else
00190             updateSub<BLACK>(new_state,last_move);
00191         }
00192       private:
00193         template<Player P>
00194         void updateMain(const NumEffectState &new_state, Move last_move);
00195       public:
00196         const Progress16 progress16() const
00197         {
00198           return Progress16(16 * progress() / maxProgress());
00199         }
00200         const Progress16 progress16(Player p) const
00201         {
00202           assert(maxProgress() > 0);
00203           return Progress16(
00204             16 * std::max(
00205               std::min(progresses[playerToIndex(alt(p))] +
00206                        attack5x5_progresses[playerToIndex(alt(p))] +
00207                        stand_progresses[playerToIndex(alt(p))] +
00208                        effect_progresses[playerToIndex(alt(p))] +
00209                        defenses[playerToIndex(alt(p))] +
00210                        king_relative_attack[playerToIndex(alt(p))] +
00211                        king_relative_defense[playerToIndex(p)] +
00212                        non_pawn_ptype_attacked_pair[p],
00213                        max_progress-ProgressScale), 0)
00214             / ProgressScale / maxProgress());
00215         }
00216         // p == attack player, alt(p) == king owner
00217         const Progress16 progressAttack(Player p) const
00218         {
00219           assert(maxProgress() > 0);
00220           return Progress16(
00221             8 * std::max(
00222               std::min(progresses[alt(p)] +
00223                        attack5x5_progresses[alt(p)] +
00224                        stand_progresses[alt(p)] +
00225                        effect_progresses[alt(p)] +
00226                        king_relative_attack[alt(p)],
00227                        max_progress-ProgressScale), -max_progress+ProgressScale)
00228             / ProgressScale / maxProgress() + 8);
00229         }
00230         // p == king owner (defense player)
00231         const Progress16 progressDefense(Player p) const
00232         {
00233           assert(maxProgress() > 0);
00234           return Progress16(
00235             8 * std::max(
00236               std::min(defenses[alt(p)] +
00237                        king_relative_defense[p] +
00238                        non_pawn_ptype_attacked_pair[p],
00239                        max_progress-ProgressScale),
00240               -max_progress + ProgressScale)
00241             / ProgressScale / maxProgress() + 8);
00242         }
00243         static bool initialized()
00244         {
00245           return initialized_flag;
00246         }
00247         static bool setUp(const char *filename);
00248         static bool setUp();
00249         const NewProgressData rawData() const { return *this; }
00250       };
00251       bool operator==(const NewProgressData& l, const NewProgressData& r);
00252       inline bool operator==(const NewProgress& l, const NewProgress& r) 
00253       {
00254         return l.rawData() == r.rawData();
00255       }
00256     }
00257   }
00258 }
00259 
00260 #endif // PROGRESS_EXPERIMENTAL_NEW_PROGRESS_H
00261 // ;;; Local Variables:
00262 // ;;; mode:c++
00263 // ;;; c-basic-offset:2
00264 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines