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
//template<>
ExtMove *generateMoves(/* TODO: const */ Position *position, ExtMove *moveList)
ExtMove *generate(/* TODO: const */ Position *position, ExtMove *moveList)
{
Square square;

View File

@ -23,8 +23,6 @@
#include <array>
#include "config.h"
#include "position.h"
#include "search.h"
#include "types.h"
using namespace std;
@ -37,9 +35,8 @@ enum GenType
LEGAL
};
class ExtMove
struct ExtMove
{
public:
Move move;
Value value;
Rating rating;
@ -66,7 +63,7 @@ inline bool operator < (const ExtMove &first, const ExtMove &second)
}
//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
/// 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;
cur = extMove;
cur = moves;
#ifdef HOSTORY_HEURISTIC
clearHistoryScore();
#endif
}
/// MovePicker::score() assigns a numerical value to each move in a list, used
/// for sorting.
void MovePicker::score()
{
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
Score MovePicker::getHistoryScore(Move move)
{

View File

@ -20,13 +20,15 @@
#ifndef MOVEPICK_H
#define MOVEPICK_H
#include "stack.h"
#include "types.h"
#include <array>
#include <limits>
#include <type_traits>
#include "movegen.h"
#include "position.h"
class Position;
class ExtMove;
struct ExtMove;
void partial_insertion_sort(ExtMove *begin, ExtMove *end, int limit);
@ -38,10 +40,28 @@ class MovePicker
};
public:
MovePicker(Position *position, ExtMove *cur);
MovePicker(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
// TODO: Fix size
@ -53,22 +73,6 @@ public:
void setHistoryScore(Move move, Depth depth);
void clearHistoryScore();
#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 <climits>
#include "position.h"
#include "search.h"
#include "movegen.h"
#include "option.h"

View File

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

View File

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