-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.h
158 lines (130 loc) · 4.35 KB
/
search.h
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef search_h
#define search_h
#include <iomanip>
#include "board.h"
#include "hash.h"
#include "move.h"
#include "evaluate.h"
#include "nextmove.h"
#include "parameters.h"
class SEARCH {
public:
SEARCH(int depth, CHESSBOARD * game, HASHTABLE * h, long time, unsigned long long nodes_for_time_check)
{
nodes_for_time_check_ = nodes_for_time_check;
root_depth = depth; root_game = game; q_max_ply = 0; selective_depth = 0;
start_time =0; end_time = 0; allotted_time = time; fTimeOut = false;
node_count =0; best_score = -INFTY;
hashtable = h; side_to_move = game->getActivePlayer();
//move_list = new MOVELIST[root_depth+1];
//for(int i =0; i<=root_depth; i++) {
for(int i =0; i < MAX_PLY; i++) {
move_list[i].linkGame(root_game);
}
for(int i =0; i < Q_CUTOFF; i++ ) {
q_move_list[i].linkGame(root_game);
}
#ifdef SEARCH_STATS
for(int i=0; i <= BAD_CAPTURES; i++)
cutoff_phase[i]=0;
for(int i=0; i < 256; i++) {
cutoff_move[i]=0;
alpha_move[i]=0;
}
#endif
}
~SEARCH()
{
//hashtable->printStats();
#ifdef SEARCH_STATS
if(root_depth >= 8) print_stats();
#endif
//delete move_list;
}
void start(int alpha_bound = -INFTY, int beta_bound = INFTY);
void Hard_PVS_Root();
int Hard_PVS(unsigned char depth, unsigned char ply, CHESSBOARD * game, int alpha, int beta, bool fNulled = false/*, unsigned char parent_moves_searched = 0*/);
int Hard_qSearch(CHESSBOARD * game, int alpha, int beta, int qPly);
bool isOutOfTime() ;
bool TimedOut() const {return fTimeOut;}
void setHasheTable(HASHTABLE * t);
int getNodes() const {return node_count;}
int getNPS() const {return ((int)(node_count/getRunTime()) * 1000);}
int getDepth() const {return root_depth; }
int getSelDepth() const {return selective_depth; }
int getQDepth() const {return q_max_ply;}
double getRunTime() const {return end_time - start_time;}
MOVE getBestMove() const {return best_move;};
int getScore() const {return best_score; }
void collectPV();
std::vector<MOVE> getPV() const {return pv; }
#ifdef SEARCH_STATS
void print_stats() {
std::cout << "SEARCH STATS:\n";
std::cout << std::setw(20) <<" BETA CUTOFFS:";
std::cout << std::setw(50) <<"WINNING ALPHA IMPROVEMENT:\n";
int last_index;
for(last_index = 255; last_index >=0; last_index--) {
if(cutoff_move[last_index] > 0 || alpha_move[last_index] > 0)
break;
}
long long sum_moves =0;
long long sum_alpha_moves =0;
for(int i = 0; i <= last_index; i++) {
sum_moves+=cutoff_move[i];
sum_alpha_moves+=alpha_move[i];
}
for(int i = 0; i <= last_index; i++) {
if(i ==0)
std::cout << std::setw(12) << "(None)";
else if(i < 10)
std::cout << std::setw(10) << "Move " << i << ":";
else
std::cout << std::setw(9) << "Move " << i << ":";
std::cout << std::setw(15) << (double)cutoff_move[i]/sum_moves *100 << "%";
std::cout << std::setw(10) << cutoff_move[i];
std::cout << std::setw(25) << (double)alpha_move[i]/sum_alpha_moves *100<< "%";
std::cout << std::setw(10) << alpha_move[i] << std::endl;
}
std::cout << std::setw(35) << "Total: " << sum_moves;
std::cout << std::setw(35) << "Total: " << sum_alpha_moves << std::endl << std::endl;
long long sum_phases = 0;
long long sum_alpha_phases = 0;
for(int i=0; i <= BAD_CAPTURES; i++) {
sum_phases += cutoff_phase[i];
sum_alpha_phases += alpha_phase[i];
}
for(int i=0; i <= BAD_CAPTURES; i++) {
std::cout << std::setw(15) << "Phase " << i << ":" << std::setw(10) << (double)cutoff_phase[i]/sum_phases *100<< "%" << std::setw(10) << cutoff_phase[i];
std::cout << std::setw(25) << (double)alpha_phase[i]/sum_alpha_phases *100 << "%" << std::setw(10) << alpha_phase[i] << std::endl;
}
system("PAUSE");
}
long long cutoff_phase[BAD_CAPTURES+1];
long long cutoff_move[256];
long long alpha_move[256];
long long alpha_phase[BAD_CAPTURES+1];
#endif
private:
CHESSBOARD * root_game;
bool side_to_move;
int root_depth;
short selective_depth;
int node_count;
int q_max_ply;
long start_time;
long allotted_time;
long end_time;
unsigned long long nodes_for_time_check_;
int root_alpha;
int root_beta;
MOVE best_move;
int best_score;
bool fTimeOut;
HASHTABLE * hashtable;
std::vector<MOVE> pv;
QMOVELIST q_move_list[Q_CUTOFF];
//MOVELIST * move_list;
MOVELIST move_list[MAX_PLY];
};
#endif