Belofte version 2.2.0
A promising chess program using the UCI or Winboard interface
search_mm.cpp
Go to the documentation of this file.
1/*---------------------------------------------------------------------+
2 * File: search_mm.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 * Root search for algorithm
14 * @param b board
15 * @param ml movelist of this position
16 */
18{
19 bBoard searchBoard(b);
20 bScore minimizing = searchBoard.minimizing();
21 bScore score = CalcBestMove(searchBoard, ml, 0, minimizing) * minimizing;
22 return score;
23}
24
25//-----------------------------------------------------------------------
26
27/**
28 * Calculate best move from this position at given depth, iterative call
29 * @param b Board
30 * @param ml movelist of this position
31 * @param nDepth depth to search for, increasing until searchDepth
32 * @param minimizing search from minimizing view
33 */
35 bMoveList& ml, depth_t const nDepth, bScore const minimizing)
36{
38 if (isNoBench() && gr != GR_UNKNOWN) {
39 ++m_leafnodes; // leaf node because of final position
40
42 DEBUG_sendInfoSearchingNS(b, nDepth, "(draw)");
44 }
45 // mate
47 DEBUG_sendInfoSearching(b, nDepth, "(final score)", terminalScore);
48 return terminalScore;
49 }
50
52
53 if (getLevel()->searchDepthReached(nDepth)) {
55 bScore terminalScore = RetrieveBoardEvaluationForWhite(b, gr, true);
56 DEBUG_sendInfoSearching(b, nDepth, "Depth reached", terminalScore);
57 return terminalScore;
58 }
59
60 // in bruteforce, we do not need moves to be ordered
61 ml.clearNeedSorted();
62 // in bruteforce, we do not need score during move-generation
63 ml.clearKeepScores();
64 movenum_t n_moves = ml.generateMoves(b);
65
66 if (nDepth) ++m_nonleafnodes;
67
68 if (!n_moves && isBench()) {
69 bScore terminalScore = RetrieveBoardEvaluationForWhite(b, gr, true);
70 DEBUG_sendInfoSearching(b, nDepth, "Dead position", terminalScore);
71 return terminalScore;
72 }
73
74 // going forward, we set scores and keep scores
75 ml.clearScores();
76 ml.setKeepScores();
77
78 if (nDepth > 2 && nDepth % 2) CheckIfAbortingSearch();
79
80 ml.curmoveid = 1;
81 if (minimizing == 1) {
82 // maximize result
83 for ( ; ml.curmoveid <= n_moves; ++ml.curmoveid) {
84 bMove m(ml[ml.curmoveid]);
85 sendInfoCurrMove(b, nDepth, m, ml.curmoveid);
86 boardInfo_t bi = b.bBasicBoard::applyMove(m);
87 bMoveList chldML;
88 bScore chldScore = CalcBestMove(b, chldML, nDepth + 1, -1);
89 ml.setScoreOfMoveUnsorted(ml.curmoveid, chldScore);
90 b.bBasicBoard::unApplyMove(m, bi);
91 // consider forced draw
92 if (chldScore > SCORE_INFINITE) break;
93 }
94 } else {
95 // minimize result
96 for ( ; ml.curmoveid <= n_moves; ++ml.curmoveid) {
97 bMove m(ml[ml.curmoveid]);
98 sendInfoCurrMove(b, nDepth, m, ml.curmoveid);
99 boardInfo_t bi = b.bBasicBoard::applyMove(m);
100 bMoveList chldML;
101 bScore chldScore = CalcBestMove(b, chldML, nDepth + 1, 1);
102 ml.setMinScoreOfMoveUnsorted(ml.curmoveid, chldScore);
103 b.bBasicBoard::unApplyMove(m, bi);
104 // consider forced draw
105 if (chldScore < -SCORE_INFINITE) break;
106 }
107 }
108
110 ml.setBestMoveScore(bestScore);
111 DEBUG_sendInfoSearching(b, nDepth, "(return best)", bestScore);
112 return bestScore;
113}
114
115//-----------------------------------------------------------------------
116
117// eof
union boardInfo boardInfo_t
This is the main include file, needs to be included before any other include.
uint_fast8_t movenum_t
Definition belofte.h:103
int_fast8_t depth_t
Definition belofte.h:106
bScore CalcBestMove(bBoard &b, bMoveList &ml) override
Root search for algorithm.
Definition search_mm.cpp:17
constexpr bScore minimizing() const
Definition basicboard.h:172
board
Definition board.h:45
Definition move.h:13
void clearScores()
Definition movelist.h:67
void clearNeedSorted()
Definition movelist.h:96
movenum_t generateMoves(bBasicBoard const &b)
generate moves if not yet generated
Definition movelist.cpp:417
void setScoreOfMoveUnsorted(movenum_t const moveid, bScore const score)
Store score of move and update best move.
Definition movelist.cpp:210
constexpr bScore getBestMoveScore() const
Definition movelist.h:78
void setMinScoreOfMoveUnsorted(movenum_t const moveid, bScore const score)
Store score of move and update best move.
Definition movelist.cpp:230
void clearKeepScores()
Definition movelist.h:100
movenum_t curmoveid
Definition movelist.h:104
void setKeepScores()
Definition movelist.h:98
void setBestMoveScore(bScore const score)
Definition movelist.h:80
static bScore resultToScoreFlag(gameResult_t const gr)
Class static function convert all draw scores to SCORE_THEORETIC_DRAW.
Definition eval.cpp:74
static gameResult_t gameEndedResult(bBoard const &b)
Class static function See if board is in finite state, meaning game is ended.
Definition eval.cpp:42
static bool isDrawResult(gameResult_t const gr)
Definition eval.h:67
void adjustMaxSearchedDepth(depth_t const nDepth)
Definition search.h:152
void sendInfoCurrMove(bBoard const &b, depth_t const nCurDepth, bMove const &m, movenum_t const moveid) const
Definition search.cpp:282
bScore RetrieveBoardEvaluationForWhite(bBoard &b, gameResult_t const gr, bool const bRecalcFirst) const
Get score of board, eventually from cache, from whites view.
Definition search.cpp:323
constexpr bool isNoBench() const
Definition search.h:132
bLevel * getLevel()
Definition search.h:161
int64_t m_nonleafnodes
Definition search.h:145
constexpr bool isBench() const
Definition search.h:134
int64_t m_leafnodes
Definition search.h:144
void CheckIfAbortingSearch() const
Definition search.cpp:223
bScore convergeScore()
@ GR_UNKNOWN
Definition eval.h:33
enum gameResult gameResult_t
Definition eval.h:40
int16_t bScore
Definition eval.h:11
constexpr bScore SCORE_THEORETIC_DRAW
Definition eval.h:16
constexpr bScore SCORE_INFINITE
Definition eval.h:20
#define DEBUG_sendInfoSearchingNS(b, depth, msg)
Definition search.h:54
#define DEBUG_sendInfoSearching(b, depth, msg, sc)
Definition search.h:53