Skip to content

Commit

Permalink
Merge pull request #13 from megalodon-chess/search
Browse files Browse the repository at this point in the history
Search
  • Loading branch information
phuang1024 authored Mar 23, 2021
2 parents 9d5500e + cc78164 commit 089bd32
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 92 deletions.
137 changes: 50 additions & 87 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "search.hpp"
#include "eval.hpp"
#include "options.hpp"
#include "utils.hpp"

using std::cin;
using std::cout;
Expand All @@ -35,7 +36,8 @@ using std::string;
SearchInfo::SearchInfo() {
}

SearchInfo::SearchInfo(int _depth, int _seldepth, bool _is_mate, float _score, int _nodes, int _nps, int _time, Move _move) {
SearchInfo::SearchInfo(int _depth, int _seldepth, bool _is_mate, float _score, int _nodes, int _nps,
double _time, Move _move, float _alpha, float _beta) {
depth = _depth;
seldepth = _seldepth;
is_mate_score = _is_mate;
Expand All @@ -44,6 +46,8 @@ SearchInfo::SearchInfo(int _depth, int _seldepth, bool _is_mate, float _score, i
nps = _nps;
time = _time;
move = _move;
alpha = _alpha;
beta = _beta;
}

string SearchInfo::as_string() {
Expand All @@ -53,8 +57,8 @@ string SearchInfo::as_string() {
str += (is_mate_score ? "mate" : "cp");
str += " " + std::to_string((is_mate_score ? (int)score : (int)(100*score)));
str += " nodes " + std::to_string(nodes) + " nps " + std::to_string(nps);
str += " tbhits 0 time " + std::to_string(time);
str += " pv ";// + Bitboard::move_str(move);
str += " tbhits 0 time " + std::to_string((int)(1000*time));
str += " pv " + Bitboard::move_str(move);
return str;
}

Expand Down Expand Up @@ -85,96 +89,55 @@ float move_time(const Options& options, const Position& pos, const float& time,
}


SearchInfo search(const Options& options, Position pos, const int& depth, const double& max_time) {
pos.alpha = MIN;
pos.beta = MAX;
vector<vector<Position*>> nodes = {{&pos}};
Move best_move;
int num_nodes = 1;
SearchInfo dfs(const Options& options, const Position& pos, const int& depth, float alpha, float beta) {
U64 o_attacks = Bitboard::attacked(pos, !pos.turn);
vector<Move> moves = Bitboard::legal_moves(pos, o_attacks);

while (true) {
int curr_depth = nodes.size() - 1;
int depth_done = true;

if (nodes.size() < depth) {
for (const auto& node: nodes[curr_depth]) {
if (!node->done) {
depth_done = false;
break;
}
}
}

if (depth_done) {
Position* target_node;
bool found = false;
for (const auto& node: nodes[curr_depth-1]) {
if (!node->done) {
target_node = node;
found = true;
break;
}
}

if (found) {
target_node->eval = target_node->turn ? MIN : MAX;
if (curr_depth != 1) {
target_node->alpha = target_node->parent->alpha;
target_node->beta = target_node->parent->beta;
}
for (const auto& node: nodes[curr_depth]) {
if (curr_depth == depth-1) {
U64 o_attacks = Bitboard::attacked(*node, !node->turn);
vector<Move> moves = Bitboard::legal_moves(*node, o_attacks);
node->eval = eval(options, *node, (moves.size()!=0), curr_depth, o_attacks);
}
if (target_node->turn) {
if (node->eval > target_node->eval) {
target_node->eval = node->eval;
if (curr_depth == 1) best_move = node->move_stack.back();
}
if (node->eval > target_node->alpha) target_node->alpha = node->eval;
if (target_node->beta <= target_node->alpha) break;
} else {
if (node->eval < target_node->eval) {
target_node->eval = node->eval;
if (curr_depth == 1) best_move = node->move_stack.back();
}
if (node->eval < target_node->beta) target_node->beta = node->eval;
if (target_node->beta <= target_node->alpha) break;
}
}
if (curr_depth != 1) {
if (target_node->alpha > target_node->parent->alpha) target_node->parent->alpha = target_node->alpha;
if (target_node->beta < target_node->parent->beta) target_node->parent->beta = target_node->beta;
}
target_node->done = true;
if (depth == 0 || moves.size() == 0) {
const float score = eval(options, pos, moves.size()!=0, depth, o_attacks);
return SearchInfo(depth, depth, false, score, 1, 0, 0, Move(), alpha, beta);
}
int nodes = 1;
int best_ind = 0;
float best_eval = pos.turn ? MIN : MAX;

for (auto i = 0; i < moves.size(); i++) {
Position new_pos = Bitboard::push(pos, moves[i]);
SearchInfo result = dfs(options, new_pos, depth-1, alpha, beta);
nodes += result.nodes;

if (pos.turn) {
if (result.score > best_eval) {
best_ind = i;
best_eval = result.score;
}
nodes.erase(nodes.end()-1);

if (result.score > alpha) alpha = result.score;
if (beta <= alpha) break;
} else {
vector<Position*> new_depth;
Position* target_node;
for (const auto& node: nodes[curr_depth]) {
if (!node->done) {
target_node = node;
break;
}
}

U64 o_attacks = Bitboard::attacked(*target_node, !target_node->turn);
vector<Move> moves = Bitboard::legal_moves(*target_node, o_attacks);
for (const auto& move: moves) {
Position new_node = Bitboard::push(*target_node, move);
new_node.parent = target_node;
new_depth.push_back(&new_node);
if (result.score < best_eval) {
best_ind = i;
best_eval = result.score;
}
nodes.push_back(new_depth);
num_nodes += moves.size();
if (result.score < beta) beta = result.score;
if (beta <= alpha) break;
}
}
return SearchInfo(depth, depth, false, best_eval, nodes, 0, 0, moves[best_ind], alpha, beta);
}

SearchInfo search(const Options& options, Position pos, const int& depth) {
// Iterative deepening doesn't have any improvements yet.
SearchInfo result;
double start = get_time();

for (auto d = 1; d <= depth; d++) {
result = dfs(options, pos, d, MIN, MAX);
double elapse = get_time() - start;

if (nodes[0][0]->done) break;
result.time = elapse;
result.nps = result.nodes / elapse;
cout << result.as_string() << endl;
}

return SearchInfo(depth, depth, false, nodes[0][0]->eval, num_nodes, 0, 0, best_move);
return result;
}
8 changes: 5 additions & 3 deletions src/search.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ using std::string;

struct SearchInfo {
SearchInfo();
SearchInfo(int, int, bool, float, int, int, int, Move);
SearchInfo(int, int, bool, float, int, int, double, Move, float, float);
string as_string();

int depth;
Expand All @@ -42,8 +42,10 @@ struct SearchInfo {
float score;
int nodes;
int nps;
int time;
double time;
Move move;
float alpha;
float beta;
};

constexpr float MAX = 1000000;
Expand All @@ -52,4 +54,4 @@ constexpr float MIN = -1000000;
float moves_left(const Options&, const Position&);
float move_time(const Options&, const Position&, const float&, const float&);

SearchInfo search(const Options&, Position, const int&, const double&);
SearchInfo search(const Options&, Position, const int&);
4 changes: 2 additions & 2 deletions src/uci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ float go(Options& options, Position& pos, vector<string> parts, float prev_eval)
if (total < 10) depth++;

double start = get_time();
SearchInfo result = search(options, pos, depth, 0);
SearchInfo result = search(options, pos, 5);
double elapse = get_time() - start;

float score = result.score;
result.time = 1000 * (elapse);
result.time = elapse;
result.nps = result.nodes / (elapse);
if (!pos.turn) result.score *= -1;
cout << result.as_string() << endl;
Expand Down

0 comments on commit 089bd32

Please sign in to comment.