-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.cpp
executable file
·141 lines (110 loc) · 3.32 KB
/
search.cpp
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
#include <vector>
#include <stdexcept>
#include <memory>
#include <iostream>
#include <future>
#include <thread>
#include "extern/CTPL/ctpl_stl.h"
#include "search.hpp"
#include "evaluate.hpp"
#include "bitboard.hpp"
long int explored_moves;
struct ThreadInput
{
checkers::Checkers state;
checkers::Move last_move;
int depth;
};
struct ThreadOutput
{
checkers::Move move;
float score;
};
float search::negamax(checkers::Checkers &state, int depth, float alpha, float beta)
{
//std::cout << "Negamax:" << std::endl;
//std::cout << state.toString() << std::endl;
explored_moves++;
std::vector<checkers::Move> moves = state.generateLegalMoves();
float score = MIN_VAL;
if (state.isWon(moves))
{
return MIN_VAL;
}
if (depth <= 0)
{
return state.getCurrentPlayer() == bitboard::Color::Black
? evaluate::evaluateBoard(state.getBoard())
: -evaluate::evaluateBoard(state.getBoard());
}
for (const checkers::Move &move : moves)
{
state.step(move);
float value = -negamax(state, --depth, -beta, -alpha);
state.undoStep();
if (value > score)
{
score = value;
}
if (score > alpha)
{
alpha = score;
}
if (alpha >= beta)
{
return alpha;
}
}
return score;
}
ThreadOutput negamax_thread(int id, ThreadInput input)
{
ThreadOutput out;
out.move = input.last_move;
out.score = -search::negamax(input.state, input.depth, search::MIN_VAL, search::MAX_VAL);
return out;
}
checkers::Move search::searchTo(const checkers::Checkers &game, int iterations, unsigned int thread_amount)
{
explored_moves = 0;
std::vector<checkers::Move> moves = game.generateLegalMoves();
if (moves.empty())
{
throw std::runtime_error("Search called on position with no possible moves.");
}
if (moves.size() == 1)
{
std::cout << "Only one move possible" << std::endl;
return moves[0];
}
if (thread_amount > moves.size())
{
thread_amount = moves.size();
}
ctpl::thread_pool pool(thread_amount);
std::vector<std::future<ThreadOutput>> results(moves.size());
checkers::Checkers game_copy = game;
std::cout << "Spawning " << thread_amount << " threads " << std::endl;
for (unsigned int i = 0; i < moves.size(); i++)
{
game_copy.step(moves[i]);
results[i] = pool.push(negamax_thread, ThreadInput{game_copy, moves[i], iterations});
game_copy.undoStep();
}
std::cout << "Fetching thread results" << std::endl;
float best_move_score = MIN_VAL;
checkers::Move best_move = moves[0];
for (unsigned int i = 0; i < moves.size(); i++)
{
ThreadOutput result = results[i].get();
std::cout << "Found move: " << result.score << std::endl;
if (result.score > best_move_score)
{
best_move_score = result.score;
best_move = result.move;
}
}
std::cout << "Score: " << best_move_score << std::endl;
std::cout << "Explored " << explored_moves << " moves" << std::endl;
return best_move;
}