Belofte version 2.1.9
A promising chess program using the UCI or Winboard interface
search_abfh.cpp
Go to the documentation of this file.
1/*---------------------------------------------------------------------+
2 * File: search_abfh.cpp
3 * Project: part of belofte - A Promising Chess Program
4 * Author: yves
5 * SPDX-License-Identifier: GPL-2.0-only
6+----------------------------------------------------------------------*/
7
8#include "belofte.h"
9
10//-----------------------------------------------------------------------
11
12/**
13 * Intermediate overwriting procedure for algorithm
14 * @param b board
15 * @param ml movelist of this position
16 * @param nDepth depth to search on
17 * @param alpha value
18 * @param beta beta
19 * @todo check why not test for winning before evaluating moves
20 */
22 bMoveList& ml, depth_t const nDepth,
23 bSearchScore alpha, bSearchScore beta)
24{
25 if (getLevel()->searchDepthReached(nDepth)) {
26 return Quiescence(b, nDepth, alpha, beta, (b.isInCheck() ? 1 : 0));
27 }
28
29 movenum_t n_moves = ml.generateMoves(b);
30
31 bScore terminalScore = RetrieveBoardEvaluation(b);
32
33 DEBUG_sendInfoSearchingNS(b, nDepth, alpha
34 + " " + beta
35 + " static: " + engineInterface::scoreAsString(terminalScore)
36 + (b.isInCheck() ? " check" : ""));
37
38 if (bSearchScore::isDrawScore(terminalScore)) {
40 DEBUG_sendInfoSearchingNS(b, nDepth, "(th. draw)");
42 }
43
44 if (!n_moves) {
46 DEBUG_sendInfoSearching(b, nDepth, "(dead position)", terminalScore);
47 return terminalScore;
48 }
49
51 if (nDepth > 2 && nDepth % 2) CheckIfAbortingSearch();
52
53 depth_t nNewDepth = nDepth + 1;
54 bSearchScore bestscore(-SCORE_INFINITE);
55 ml.sortMoves();
56 for (movenum_t moveid = 1; moveid <= n_moves; ++moveid) {
57 bMove m(ml[moveid]);
58 sendInfoCurrMove(b, nDepth, m, moveid);
59 bBoard chldbrd(b, m);
60 chldbrd.applyMove(m);
61 chldbrd.calcMinorPieces();
62 bMoveList chldML;
63 bSearchScore chldscore(-CalcBestMove(chldbrd, chldML, nNewDepth, -beta, -alpha));
64 chldscore.convergeScore(chldscore.getScore());
65 ml.setScoreOfMove(moveid, chldscore.getScore());
66 if (chldscore.isWinning()) {
67 b.setVariation(chldbrd);
69 DEBUG_sendInfoSearching(b, nDepth, "(winning)", chldscore.getScore());
70 return chldscore.getScore();
71 }
72 if (chldscore.improvesOn(m_nBetaCutOffMargin, beta)) {
74 DEBUG_sendInfoSearchingNS(b, nDepth, "(fail hard) " + chldscore + GTOREQUAL + beta);
75 return beta.getScore();
76 }
77 if (chldscore.improvesOn(alpha)) {
78 b.setVariation(chldbrd);
79 DEBUG_sendInfoSearchingNS(b, nDepth, "alpha & best update " + chldscore + " > " + alpha);
80 bestscore = chldscore.getScore();
81 alpha = chldscore.getScore(); // alpha does not change type
82 } else {
83 DEBUG_sendInfoSearchingNS(b, nDepth, "no update " + chldscore);
84 }
85 }
86
88 DEBUG_sendInfoSearching(b, nDepth, "(return best)", bestscore.getScore());
89 return bestscore.getScore();
90}
91
92//-----------------------------------------------------------------------
93
94// eof
This is the main include file, needs to be included before any other include.
uint_fast8_t movenum_t
Definition belofte.h:100
int_fast8_t depth_t
Definition belofte.h:103
bScore CalcBestMove(bBoard &b, bMoveList &ml, depth_t const nDepth, bSearchScore alpha, bSearchScore beta) override
Intermediate overwriting procedure for algorithm.
bScore m_nBetaCutOffMargin
Definition search_ab.h:43
bScore Quiescence(bBoard &b, depth_t const nDepth, bSearchScore alpha, bSearchScore beta, uint8_t nCheckCount)
Calculate best move from this position considering only non-silent moves.
constexpr bool isInCheck() const
Definition basicboard.h:132
board
Definition board.h:45
void setVariation(bBoard const &chldbrd)
Definition board.cpp:227
boardInfo_t applyMove(bMove const &m) override
modification of board move is kept on previous board newboard does not have move stored and has flag ...
Definition board.cpp:217
void calcMinorPieces(bool const bForceRecalc=false)
Recalculate minor pieces, used for evaluation and end of game condition in case of less than 5 pieces...
Definition board.cpp:68
Definition move.h:13
movenum_t generateMoves(bBasicBoard const &b)
generate moves if not yet generated
Definition movelist.cpp:340
void sortMoves()
sort moves and update bestmove id if less than 5 moves, sort all if more than 5 moves,...
Definition movelist.cpp:293
bool setScoreOfMove(movenum_t const moveid, bScore const score)
Store score of move and update best move.
Definition movelist.cpp:130
void adjustMaxSearchedDepth(depth_t const nDepth)
Definition search.h:132
void sendInfoCurrMove(bBoard const &b, depth_t const nCurDepth, bMove const &m, movenum_t const moveid) const
Definition search.cpp:223
bLevel * getLevel()
Definition search.h:141
int64_t m_nonleafnodes
Definition search.h:125
int64_t m_leafnodes
Definition search.h:124
bScore RetrieveBoardEvaluation(bBoard &b, gameResult_t const gr=GR_UNSET) const
Get score of board, eventually from cache.
Definition search.cpp:242
void CheckIfAbortingSearch() const
Definition search.cpp:168
static constexpr bool isDrawScore(bScore const score)
constexpr bScore getScore() const
Definition searchscore.h:64
bool improvesOn(bSearchScore const &sc)
Definition searchscore.h:72
constexpr bool isWinning() const
Definition searchscore.h:70
bScore convergeScore()
Definition searchscore.h:95
static std::string scoreAsString(bScore const score)
int16_t bScore
Definition eval.h:11
constexpr bScore SCORE_THEORETIC_DRAW
Definition eval.h:17
constexpr bScore SCORE_INFINITE
Definition eval.h:19
#define DEBUG_sendInfoSearchingNS(b, depth, msg)
Definition search.h:36
#define DEBUG_sendInfoSearching(b, depth, msg, sc)
Definition search.h:35