summaryrefslogtreecommitdiff
path: root/Userland/Services/ChessEngine/MCTSTree.h
blob: 4241980822ead8fa0f8e04a27a8329a27c96d967 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
 * Copyright (c) 2020, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Function.h>
#include <AK/NonnullOwnPtrVector.h>
#include <AK/OwnPtr.h>
#include <LibChess/Chess.h>
#include <math.h>

class MCTSTree {
public:
    enum EvalMethod {
        Simulation,
        Heuristic,
    };

    MCTSTree(const Chess::Board& board, MCTSTree* parent = nullptr);

    MCTSTree& select_leaf();
    MCTSTree& expand();
    int simulate_game() const;
    int heuristic() const;
    void apply_result(int game_score);
    void do_round();

    Chess::Move best_move() const;
    double expected_value() const;
    double uct(Chess::Color color) const;
    bool expanded() const;

private:
    // While static parameters are less configurable, they don't take up any
    // memory in the tree, which I believe to be a worthy tradeoff.
    static constexpr double s_exploration_parameter { sqrt(2) };
    // FIXME: Optimize simulations enough for use.
    static constexpr EvalMethod s_eval_method { EvalMethod::Heuristic };

    NonnullOwnPtrVector<MCTSTree> m_children;
    MCTSTree* m_parent { nullptr };
    int m_white_points { 0 };
    int m_simulations { 0 };
    OwnPtr<Chess::Board> m_board;
    Optional<Chess::Move> m_last_move;
    Chess::Color m_turn : 2;
    bool m_moves_generated : 1 { false };
};