refactor: MovePicker 类增加 moves[MAX_MOVES] 成员并使用

This commit is contained in:
Calcitem 2020-07-08 01:14:27 +08:00
parent f6a9f40e78
commit 1472f78bc8
7 changed files with 53 additions and 47 deletions

View File

@ -289,7 +289,7 @@ void MoveList::shuffle()
/// generate<LEGAL> generates all the legal moves in the given position /// generate<LEGAL> generates all the legal moves in the given position
//template<> //template<>
ExtMove *generateMoves(/* TODO: const */ Position *position, ExtMove *moveList) ExtMove *generate(/* TODO: const */ Position *position, ExtMove *moveList)
{ {
Square square; Square square;

View File

@ -23,8 +23,6 @@
#include <array> #include <array>
#include "config.h" #include "config.h"
#include "position.h"
#include "search.h"
#include "types.h" #include "types.h"
using namespace std; using namespace std;
@ -37,9 +35,8 @@ enum GenType
LEGAL LEGAL
}; };
class ExtMove struct ExtMove
{ {
public:
Move move; Move move;
Value value; Value value;
Rating rating; Rating rating;
@ -66,7 +63,7 @@ inline bool operator < (const ExtMove &first, const ExtMove &second)
} }
//template <GenType> //template <GenType>
ExtMove *generateMoves(Position *pos, ExtMove *moveList); ExtMove *generate(Position *pos, ExtMove *moveList);
/// The MoveList struct is a simple wrapper around generate(). It sometimes comes /// The MoveList struct is a simple wrapper around generate(). It sometimes comes
/// in handy to use this class instead of the low level generate() function. /// in handy to use this class instead of the low level generate() function.

View File

@ -36,16 +36,18 @@ void partial_insertion_sort(ExtMove *begin, ExtMove *end, int limit)
} }
} }
MovePicker::MovePicker(Position *pos, ExtMove *extMove) MovePicker::MovePicker(Position *pos)
{ {
position = pos; position = pos;
cur = extMove; cur = moves;
#ifdef HOSTORY_HEURISTIC #ifdef HOSTORY_HEURISTIC
clearHistoryScore(); clearHistoryScore();
#endif #endif
} }
/// MovePicker::score() assigns a numerical value to each move in a list, used
/// for sorting.
void MovePicker::score() void MovePicker::score()
{ {
while (cur++->move != MOVE_NONE) { while (cur++->move != MOVE_NONE) {
@ -152,6 +154,15 @@ void MovePicker::score()
} }
} }
/// MovePicker::next_move() is the most important method of the MovePicker class. It
/// returns a new pseudo legal move every time it is called until there are no more
/// moves left, picking the move with the highest score from a list of generated moves.
Move MovePicker::next_move()
{
// TODO
return MOVE_NONE;
}
#ifdef HOSTORY_HEURISTIC #ifdef HOSTORY_HEURISTIC
Score MovePicker::getHistoryScore(Move move) Score MovePicker::getHistoryScore(Move move)
{ {

View File

@ -20,13 +20,15 @@
#ifndef MOVEPICK_H #ifndef MOVEPICK_H
#define MOVEPICK_H #define MOVEPICK_H
#include "stack.h" #include <array>
#include "types.h" #include <limits>
#include <type_traits>
#include "movegen.h" #include "movegen.h"
#include "position.h" #include "position.h"
class Position; class Position;
class ExtMove; struct ExtMove;
void partial_insertion_sort(ExtMove *begin, ExtMove *end, int limit); void partial_insertion_sort(ExtMove *begin, ExtMove *end, int limit);
@ -38,10 +40,28 @@ class MovePicker
}; };
public: public:
MovePicker(Position *position, ExtMove *cur);
MovePicker(const MovePicker &) = delete; MovePicker(const MovePicker &) = delete;
MovePicker &operator=(const MovePicker &) = delete; MovePicker &operator=(const MovePicker &) = delete;
// Move nextMove(bool skipQuiets = false); MovePicker(Position *position);
Move next_move();
//private:
void score();
ExtMove *begin()
{
return cur;
}
ExtMove *end()
{
return endMoves;
}
Position *position;
ExtMove *cur, *endMoves;
ExtMove moves[MAX_MOVES] { MOVE_NONE };
#ifdef HOSTORY_HEURISTIC #ifdef HOSTORY_HEURISTIC
// TODO: Fix size // TODO: Fix size
@ -53,22 +73,6 @@ public:
void setHistoryScore(Move move, Depth depth); void setHistoryScore(Move move, Depth depth);
void clearHistoryScore(); void clearHistoryScore();
#endif // HOSTORY_HEURISTIC #endif // HOSTORY_HEURISTIC
public:
void score();
ExtMove *begin()
{
return cur;
}
// ExtMove *end()
// {
// return endMoves;
// }
Position *position;
ExtMove *cur;
}; };
#endif // MOVEPICK_H #endif // #ifndef MOVEPICK_H

View File

@ -19,7 +19,7 @@
#include <algorithm> #include <algorithm>
#include <climits> #include <climits>
#include "position.h"
#include "search.h" #include "search.h"
#include "movegen.h" #include "movegen.h"
#include "option.h" #include "option.h"

View File

@ -24,7 +24,6 @@
#include "search.h" #include "search.h"
#include "evaluate.h" #include "evaluate.h"
#include "movegen.h"
#include "hashmap.h" #include "hashmap.h"
#include "tt.h" #include "tt.h"
#include "endgame.h" #include "endgame.h"
@ -405,7 +404,7 @@ Value AIAlgorithm::search(Depth depth, Value alpha, Value beta)
st->generateNullMove(moves); st->generateNullMove(moves);
st->generateChildren(moves, this, node); st->generateChildren(moves, this, node);
do_null_move(); do_null_move();
int moveCount = st->generateMoves(moves); int moveCount = st->generate(moves);
if (moveCount) if (moveCount)
{ {
st->generateChildren(moves, this, node->children[0]); st->generateChildren(moves, this, node->children[0]);
@ -435,19 +434,17 @@ Value AIAlgorithm::search(Depth depth, Value alpha, Value beta)
return bestValue; return bestValue;
} }
ExtMove extMoves[MAX_MOVES]; MovePicker mp(pos);
memset(extMoves, 0, sizeof(extMoves)); mp.endMoves = generate(pos, mp.moves);
ExtMove *end = generateMoves(pos, extMoves);
MovePicker mp(pos, extMoves);
mp.score(); mp.score();
partial_insertion_sort(extMoves, end, -100); partial_insertion_sort(mp.moves, mp.endMoves, -100);
ExtMove *cur = extMoves; ExtMove *cur = mp.moves;
int nchild = end - cur; int nchild = mp.endMoves - cur;
if (nchild == 1 && depth == originDepth) { if (nchild == 1 && depth == originDepth) {
bestMove = extMoves[0].move; bestMove = mp.moves[0].move;
bestValue = VALUE_UNIQUE; bestValue = VALUE_UNIQUE;
return bestValue; return bestValue;
} }
@ -455,7 +452,7 @@ Value AIAlgorithm::search(Depth depth, Value alpha, Value beta)
#ifdef TRANSPOSITION_TABLE_ENABLE #ifdef TRANSPOSITION_TABLE_ENABLE
#ifdef PREFETCH_SUPPORT #ifdef PREFETCH_SUPPORT
for (int i = 0; i < nchild; i++) { for (int i = 0; i < nchild; i++) {
TranspositionTable::prefetch(pos->next_primary_key(extMoves[i].move)); TranspositionTable::prefetch(pos->next_primary_key(mp.moves[i].move));
} }
#ifdef PREFETCH_DEBUG #ifdef PREFETCH_DEBUG
@ -470,7 +467,7 @@ Value AIAlgorithm::search(Depth depth, Value alpha, Value beta)
for (int i = 0; i < nchild; i++) { for (int i = 0; i < nchild; i++) {
stashPosition(); stashPosition();
Color before = pos->sideToMove; Color before = pos->sideToMove;
Move move = extMoves[i].move; Move move = mp.moves[i].move;
do_move(move); do_move(move);
Color after = pos->sideToMove; Color after = pos->sideToMove;

View File

@ -27,14 +27,12 @@
#include <array> #include <array>
#include "stack.h" #include "stack.h"
#include "position.h"
#include "tt.h" #include "tt.h"
#include "hashmap.h" #include "hashmap.h"
#include "endgame.h" #include "endgame.h"
#include "types.h" #include "types.h"
#include "misc.h" #include "misc.h"
#include "movepick.h"
#include "movegen.h"
#ifdef CYCLE_STAT #ifdef CYCLE_STAT
#include "stopwatch.h" #include "stopwatch.h"
#endif #endif
@ -43,7 +41,6 @@ class AIAlgorithm;
class Node; class Node;
class Position; class Position;
class MovePicker; class MovePicker;
class ExtMove;
using namespace std; using namespace std;
using namespace CTSL; using namespace CTSL;