Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 99 | pmbaty | 1 | /* |
| 2 | Texel - A UCI chess engine. |
||
| 3 | Copyright (C) 2012-2013 Peter Ă–sterlund, peterosterlund2@gmail.com |
||
| 4 | |||
| 5 | This program is free software: you can redistribute it and/or modify |
||
| 6 | it under the terms of the GNU General Public License as published by |
||
| 7 | the Free Software Foundation, either version 3 of the License, or |
||
| 8 | (at your option) any later version. |
||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, |
||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 13 | GNU General Public License for more details. |
||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License |
||
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
| 17 | */ |
||
| 18 | |||
| 19 | /* |
||
| 20 | * history.hpp |
||
| 21 | * |
||
| 22 | * Created on: Feb 25, 2012 |
||
| 23 | * Author: petero |
||
| 24 | */ |
||
| 25 | |||
| 26 | #ifndef HISTORY_HPP_ |
||
| 27 | #define HISTORY_HPP_ |
||
| 28 | |||
| 29 | #include "piece.hpp" |
||
| 30 | #include "position.hpp" |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Implements the relative history heuristic. |
||
| 34 | */ |
||
| 35 | class History { |
||
| 36 | public: |
||
| 37 | History(); |
||
| 38 | |||
| 39 | /** Clear all history information. */ |
||
| 40 | void init(); |
||
| 41 | |||
| 42 | /** Rescale the history counters, so that future updates have more weight. */ |
||
| 43 | void reScale(); |
||
| 44 | |||
| 45 | /** Record move as a success. */ |
||
| 46 | void addSuccess(const Position& pos, const Move& m, int depth); |
||
| 47 | |||
| 48 | /** Record move as a failure. */ |
||
| 49 | void addFail(const Position& pos, const Move& m, int depth); |
||
| 50 | |||
| 51 | /** Get a score between 0 and 49, depending of the success/fail ratio of the move. */ |
||
| 52 | int getHistScore(const Position& pos, const Move& m) const; |
||
| 53 | |||
| 54 | private: |
||
| 55 | static int depthWeight(int depth); |
||
| 56 | |||
| 57 | static int depthTable[6]; |
||
| 58 | |||
| 59 | struct Entry { |
||
| 60 | RelaxedShared<int> countSuccess; |
||
| 61 | RelaxedShared<int> countFail; |
||
| 62 | mutable RelaxedShared<int> score; |
||
| 63 | }; |
||
| 64 | Entry ht[Piece::nPieceTypes][64]; |
||
| 65 | }; |
||
| 66 | |||
| 67 | |||
| 68 | inline |
||
| 69 | History::History() { |
||
| 70 | init(); |
||
| 71 | } |
||
| 72 | |||
| 73 | inline int |
||
| 74 | History::depthWeight(int depth) { |
||
| 75 | return depthTable[clamp(depth, 0, (int)COUNT_OF(depthTable)-1)]; |
||
| 76 | } |
||
| 77 | |||
| 78 | inline void |
||
| 79 | History::addSuccess(const Position& pos, const Move& m, int depth) { |
||
| 80 | int p = pos.getPiece(m.from()); |
||
| 81 | int cnt = depthWeight(depth); |
||
| 82 | Entry& e = ht[p][m.to()]; |
||
| 83 | int val = e.countSuccess + cnt; |
||
| 84 | if (val + e.countFail > 1300) { |
||
| 85 | val /= 2; |
||
| 86 | e.countFail = e.countFail / 2; |
||
| 87 | } |
||
| 88 | e.countSuccess = val; |
||
| 89 | e.score = -1; |
||
| 90 | } |
||
| 91 | |||
| 92 | inline void |
||
| 93 | History::addFail(const Position& pos, const Move& m, int depth) { |
||
| 94 | int p = pos.getPiece(m.from()); |
||
| 95 | int cnt = depthWeight(depth); |
||
| 96 | Entry& e = ht[p][m.to()]; |
||
| 97 | int val = e.countFail + cnt; |
||
| 98 | if (val + e.countSuccess > 1300) { |
||
| 99 | val /= 2; |
||
| 100 | e.countSuccess = e.countSuccess / 2; |
||
| 101 | } |
||
| 102 | e.countFail = val; |
||
| 103 | e.score = -1; |
||
| 104 | } |
||
| 105 | |||
| 106 | inline int |
||
| 107 | History::getHistScore(const Position& pos, const Move& m) const { |
||
| 108 | int p = pos.getPiece(m.from()); |
||
| 109 | const Entry& e = ht[p][m.to()]; |
||
| 110 | int ret = e.score; |
||
| 111 | if (ret >= 0) |
||
| 112 | return ret; |
||
| 113 | int succ = e.countSuccess; |
||
| 114 | int fail = e.countFail; |
||
| 115 | if (succ + fail > 0) { |
||
| 116 | ret = succ * 49 / (succ + fail); |
||
| 117 | } else |
||
| 118 | ret = 0; |
||
| 119 | e.score = ret; |
||
| 120 | return ret; |
||
| 121 | } |
||
| 122 | |||
| 123 | #endif /* HISTORY_HPP_ */ |