Subversion Repositories Games.Chess Giants

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
99 pmbaty 1
/*
2
    Texel - A UCI chess engine.
3
    Copyright (C) 2012-2014  Peter Ă–sterlund, peterosterlund2@gmail.com
4
 
5
    This program is free software: you can redistribute it and/or modify
6
    it under the terms of the GNU General Public License as published by
7
    the Free Software Foundation, either version 3 of the License, or
8
    (at your option) any later version.
9
 
10
    This program is distributed in the hope that it will be useful,
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    GNU General Public License for more details.
14
 
15
    You should have received a copy of the GNU General Public License
16
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18
 
19
/*
20
 * enginecontrol.hpp
21
 *
22
 *  Created on: Mar 4, 2012
23
 *      Author: petero
24
 */
25
 
26
#ifndef ENGINECONTROL_HPP_
27
#define ENGINECONTROL_HPP_
28
 
29
#include "search.hpp"
30
#include "transpositionTable.hpp"
31
#include "position.hpp"
32
#include "move.hpp"
33
 
34
#include <vector>
35
#include <iosfwd>
36
#include <thread>
37
#include <mutex>
38
#include <memory>
39
#include <atomic>
40
 
41
class SearchParams;
42
 
43
 
44
/**
45
 * Control the search thread.
46
 */
47
class EngineControl {
48
public:
49
    EngineControl(std::ostream& o);
50
    ~EngineControl();
51
 
52
    void startSearch(const Position& pos, const std::vector<Move>& moves, const SearchParams& sPar);
53
 
54
    void startPonder(const Position& pos, const std::vector<Move>& moves, const SearchParams& sPar);
55
 
56
    void ponderHit();
57
 
58
    void stopSearch();
59
 
60
    void newGame();
61
 
62
    /**
63
     * Compute thinking time for current search.
64
     */
65
    void computeTimeLimit(const SearchParams& sPar);
66
 
67
    static void printOptions(std::ostream& os);
68
 
69
    void setOption(const std::string& optionName, const std::string& optionValue,
70
                   bool deferIfBusy);
71
 
72
private:
73
    /**
74
     * This class is responsible for sending "info" strings during search.
75
     */
76
    class SearchListener : public Search::Listener {
77
    public:
78
        SearchListener(std::ostream& os0);
79
 
80
        void notifyDepth(int depth) override;
81
 
82
        void notifyCurrMove(const Move& m, int moveNr) override;
83
 
84
        void notifyPV(int depth, int score, int time, U64 nodes, int nps, bool isMate,
85
                      bool upperBound, bool lowerBound, const std::vector<Move>& pv,
86
                      int multiPVIndex, U64 tbHits) override;
87
 
88
        void notifyStats(U64 nodes, int nps, U64 tbHits, int time) override;
89
 
90
    private:
91
        std::ostream& os;
92
    };
93
 
94
    void startThread(int minTimeLimit, int maxTimeLimit, int maxDepth, int maxNodes);
95
 
96
    void stopThread();
97
 
98
    void setupTT();
99
 
100
    void setupPosition(Position pos, const std::vector<Move>& moves);
101
 
102
    /**
103
     * Try to find a move to ponder from the transposition table.
104
     */
105
    Move getPonderMove(Position pos, const Move& m);
106
 
107
    static std::string moveToString(const Move& m);
108
 
109
 
110
    std::ostream& os;
111
 
112
    int hashParListenerId;
113
    int clearHashParListenerId;
114
    std::map<std::string, std::string> pendingOptions;
115
 
116
    std::shared_ptr<std::thread> engineThread;
117
    std::mutex threadMutex;
118
    std::atomic<bool> shouldDetach;
119
    std::shared_ptr<Search> sc;
120
    TranspositionTable tt;
121
    ParallelData pd;
122
    KillerTable kt;
123
    History ht;
124
    std::shared_ptr<Evaluate::EvalHashTables> et;
125
    TreeLogger treeLog;
126
 
127
    Position pos;
128
    std::vector<U64> posHashList;
129
    int posHashListSize;
130
    std::atomic<bool> ponder;     // True if currently doing pondering
131
    bool onePossibleMove;
132
    std::atomic<bool> infinite;
133
 
134
    int minTimeLimit;
135
    int maxTimeLimit;
136
    int maxDepth;
137
    int maxNodes;
138
    std::vector<Move> searchMoves;
139
 
140
    // Random seed for reduced strength
141
    U64 randomSeed;
142
};
143
 
144
 
145
#endif /* ENGINECONTROL_HPP_ */