Subversion Repositories Games.Chess Giants

Rev

Rev 154 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
96 pmbaty 1
/*
2
  Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3
  Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4
  Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
169 pmbaty 5
  Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
96 pmbaty 6
 
7
  Stockfish is free software: you can redistribute it and/or modify
8
  it under the terms of the GNU General Public License as published by
9
  the Free Software Foundation, either version 3 of the License, or
10
  (at your option) any later version.
11
 
12
  Stockfish is distributed in the hope that it will be useful,
13
  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
  GNU General Public License for more details.
16
 
17
  You should have received a copy of the GNU General Public License
18
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
*/
20
 
21
#ifndef MOVEPICK_H_INCLUDED
22
#define MOVEPICK_H_INCLUDED
23
 
169 pmbaty 24
#include <array>
25
#include <limits>
96 pmbaty 26
 
27
#include "movegen.h"
28
#include "position.h"
29
#include "types.h"
30
 
169 pmbaty 31
/// StatBoards is a generic 2-dimensional array used to store various statistics
32
template<int Size1, int Size2, typename T = int16_t>
33
struct StatBoards : public std::array<std::array<T, Size2>, Size1> {
96 pmbaty 34
 
169 pmbaty 35
  void fill(const T& v) {
36
    T* p = &(*this)[0][0];
37
    std::fill(p, p + sizeof(*this) / sizeof(*p), v);
38
  }
96 pmbaty 39
 
169 pmbaty 40
  void update(T& entry, int bonus, const int D) {
96 pmbaty 41
 
169 pmbaty 42
    assert(abs(bonus) <= D); // Ensure range is [-32 * D, 32 * D]
43
    assert(abs(32 * D) < (std::numeric_limits<T>::max)()); // Ensure we don't overflow
96 pmbaty 44
 
169 pmbaty 45
    entry += bonus * 32 - entry * abs(bonus) / D;
96 pmbaty 46
 
169 pmbaty 47
    assert(abs(entry) <= 32 * D);
96 pmbaty 48
  }
169 pmbaty 49
};
96 pmbaty 50
 
169 pmbaty 51
/// StatCubes is a generic 3-dimensional array used to store various statistics
52
template<int Size1, int Size2, int Size3, typename T = int16_t>
53
struct StatCubes : public std::array<std::array<std::array<T, Size3>, Size2>, Size1> {
54
 
55
  void fill(const T& v) {
56
    T* p = &(*this)[0][0][0];
57
    std::fill(p, p + sizeof(*this) / sizeof(*p), v);
58
  }
59
 
60
  void update(T& entry, int bonus, const int D, const int W) {
61
 
62
    assert(abs(bonus) <= D); // Ensure range is [-W * D, W * D]
63
    assert(abs(W * D) < (std::numeric_limits<T>::max)()); // Ensure we don't overflow
64
 
65
    entry += bonus * W - entry * abs(bonus) / D;
66
 
67
    assert(abs(entry) <= W * D);
68
  }
96 pmbaty 69
};
70
 
169 pmbaty 71
/// ButterflyBoards are 2 tables (one for each color) indexed by the move's from
72
/// and to squares, see chessprogramming.wikispaces.com/Butterfly+Boards
73
typedef StatBoards<COLOR_NB, int(SQUARE_NB) * int(SQUARE_NB)> ButterflyBoards;
96 pmbaty 74
 
169 pmbaty 75
/// PieceToBoards are addressed by a move's [piece][to] information
76
typedef StatBoards<PIECE_NB, SQUARE_NB> PieceToBoards;
96 pmbaty 77
 
169 pmbaty 78
/// CapturePieceToBoards are addressed by a move's [piece][to][captured piece type] information
79
typedef StatCubes<PIECE_NB, SQUARE_NB, PIECE_TYPE_NB> CapturePieceToBoards;
154 pmbaty 80
 
169 pmbaty 81
/// ButterflyHistory records how often quiet moves have been successful or
82
/// unsuccessful during the current search, and is used for reduction and move
83
/// ordering decisions. It uses ButterflyBoards as backing store.
84
struct ButterflyHistory : public ButterflyBoards {
154 pmbaty 85
 
169 pmbaty 86
  void update(Color c, Move m, int bonus) {
87
    StatBoards::update((*this)[c][from_to(m)], bonus, 324);
88
  }
89
};
154 pmbaty 90
 
169 pmbaty 91
/// PieceToHistory is like ButterflyHistory, but is based on PieceToBoards
92
struct PieceToHistory : public PieceToBoards {
93
 
94
  void update(Piece pc, Square to, int bonus) {
95
    StatBoards::update((*this)[pc][to], bonus, 936);
154 pmbaty 96
  }
169 pmbaty 97
};
154 pmbaty 98
 
169 pmbaty 99
/// CapturePieceToHistory is like PieceToHistory, but is based on CapturePieceToBoards
100
struct CapturePieceToHistory : public CapturePieceToBoards {
101
 
102
  void update(Piece pc, Square to, PieceType captured, int bonus) {
103
    StatCubes::update((*this)[pc][to][captured], bonus, 324, 2);
104
  }
154 pmbaty 105
};
106
 
169 pmbaty 107
/// CounterMoveHistory stores counter moves indexed by [piece][to] of the previous
108
/// move, see chessprogramming.wikispaces.com/Countermove+Heuristic
109
typedef StatBoards<PIECE_NB, SQUARE_NB, Move> CounterMoveHistory;
154 pmbaty 110
 
169 pmbaty 111
/// ContinuationHistory is the history of a given pair of moves, usually the
112
/// current one given a previous one. History table is based on PieceToBoards
113
/// instead of ButterflyBoards.
114
typedef StatBoards<PIECE_NB, SQUARE_NB, PieceToHistory> ContinuationHistory;
115
 
116
 
96 pmbaty 117
/// MovePicker class is used to pick one pseudo legal move at a time from the
118
/// current position. The most important method is next_move(), which returns a
119
/// new pseudo legal move each time it is called, until there are no moves left,
120
/// when MOVE_NONE is returned. In order to improve the efficiency of the alpha
121
/// beta algorithm, MovePicker attempts to return the moves which are most likely
122
/// to get a cut-off first.
123
 
124
class MovePicker {
125
public:
126
  MovePicker(const MovePicker&) = delete;
127
  MovePicker& operator=(const MovePicker&) = delete;
169 pmbaty 128
  MovePicker(const Position&, Move, Value, const CapturePieceToHistory*);
129
  MovePicker(const Position&, Move, Depth, const ButterflyHistory*,  const CapturePieceToHistory*, Square);
130
  MovePicker(const Position&, Move, Depth, const ButterflyHistory*, const CapturePieceToHistory*, const PieceToHistory**, Move, Move*);
131
  Move next_move(bool skipQuiets = false);
96 pmbaty 132
 
133
private:
134
  template<GenType> void score();
154 pmbaty 135
  ExtMove* begin() { return cur; }
96 pmbaty 136
  ExtMove* end() { return endMoves; }
137
 
138
  const Position& pos;
169 pmbaty 139
  const ButterflyHistory* mainHistory;
140
  const CapturePieceToHistory* captureHistory;
141
  const PieceToHistory** contHistory;
142
  Move ttMove, countermove, killers[2];
143
  ExtMove *cur, *endMoves, *endBadCaptures;
144
  int stage;
96 pmbaty 145
  Square recaptureSquare;
146
  Value threshold;
169 pmbaty 147
  Depth depth;
154 pmbaty 148
  ExtMove moves[MAX_MOVES];
96 pmbaty 149
};
150
 
151
#endif // #ifndef MOVEPICK_H_INCLUDED