Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle flooding of UCI-commands (close #81) #167

Merged
merged 1 commit into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions engine/src/agents/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void Agent::set_best_move(size_t moveCounter)

Agent::Agent(NeuralNetAPI* net, PlaySettings* playSettings, bool verbose):
NeuralNetAPIUser(net),
playSettings(playSettings), verbose(verbose)
playSettings(playSettings), verbose(verbose), isRunning(false)
{
}

Expand All @@ -70,8 +70,19 @@ Action Agent::get_best_action()
return evalInfo->bestMove;
}

void Agent::lock()
{
runnerMutex.lock();
}

void Agent::unlock()
{
runnerMutex.unlock();
}

void Agent::perform_action()
{
isRunning = true;
evalInfo->start = chrono::steady_clock::now();
this->evaluate_board_state();
evalInfo->end = chrono::steady_clock::now();
Expand All @@ -83,9 +94,8 @@ void Agent::perform_action()
#else
info_bestmove(StateConstants::action_to_uci(evalInfo->bestMove, state->is_chess960()));
#endif



isRunning = false;
runnerMutex.unlock();
}

void run_agent_thread(Agent* agent)
Expand Down
8 changes: 8 additions & 0 deletions engine/src/agents/agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ class Agent : public NeuralNetAPIUser
PlaySettings* playSettings;
StateObj* state;
EvalInfo* evalInfo;
// protect the isRunning attribute and makes sure that the stop() command can only be called after the search has actually been started
mutex runnerMutex;
bool verbose;
// boolean which can be triggered by "stop" from std-in to stop the current search
bool isRunning;

public:
Agent(NeuralNetAPI* net, PlaySettings* playSettings, bool verbose);
Expand Down Expand Up @@ -100,6 +104,10 @@ class Agent : public NeuralNetAPIUser
* @return Action
*/
Action get_best_action();

void lock();

void unlock();
};
}

Expand Down
8 changes: 4 additions & 4 deletions engine/src/agents/mctsagent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ MCTSAgent::MCTSAgent(NeuralNetAPI *netSingle, vector<unique_ptr<NeuralNetAPI>>&
opponentsNextRoot(nullptr),
lastValueEval(-1.0f),
reusedFullTree(false),
isRunning(false),
overallNPS(0.0f),
nbNPSentries(0),
threadManager(nullptr),
Expand Down Expand Up @@ -340,26 +339,27 @@ void MCTSAgent::run_mcts_search()
ThreadManagerParams tParams(curMovetime, 250, is_game_sceneario(searchLimits), can_prolong_search(rootNode->plies_from_null()/2, timeManager->get_thresh_move()));
threadManager = make_unique<ThreadManager>(&tData, &tInfo, &tParams);
unique_ptr<thread> tManager = make_unique<thread>(run_thread_manager, threadManager.get());
isRunning = true;

runnerMutex.unlock();
for (size_t i = 0; i < searchSettings->threads; ++i) {
threads[i]->join();
}
threadManager->kill();
tManager->join();
delete[] threads;
isRunning = false;
}

void MCTSAgent::stop()
{
runnerMutex.lock();
if (!isRunning) {
runnerMutex.unlock();
return;
}
if (threadManager != nullptr) {
threadManager->stop_search();
}
isRunning = false;
runnerMutex.unlock();
}

void MCTSAgent::print_root_node()
Expand Down
3 changes: 0 additions & 3 deletions engine/src/agents/mctsagent.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ class MCTSAgent : public Agent
// boolean which indicates if the same node was requested twice for analysis
bool reusedFullTree;

// boolean which can be triggered by "stop" from std-in to stop the current search
bool isRunning;

// saves the overall nps for each move during the game
float overallNPS;
size_t avgDepth;
Expand Down
12 changes: 7 additions & 5 deletions engine/src/uci/crazyara.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,10 @@ void CrazyAra::prepare_search_config_structs()
}
}

void CrazyAra::go(StateObj* state, istringstream &is, EvalInfo& evalInfo) {

void CrazyAra::go(StateObj* state, istringstream &is, EvalInfo& evalInfo)
{
wait_to_finish_last_search();
ongoingSearch = true;
prepare_search_config_structs();

string token;
Expand All @@ -186,15 +188,15 @@ void CrazyAra::go(StateObj* state, istringstream &is, EvalInfo& evalInfo) {
else if (token == "movetime") is >> searchLimits.movetime;
else if (token == "infinite") searchLimits.infinite = true;
}
wait_to_finish_last_search();

ongoingSearch = true;
if (useRawNetwork) {
rawAgent->set_search_settings(state, &searchLimits, &evalInfo);
rawAgent->lock(); // lock() rawAgent to avoid calling stop() immediatly
mainSearchThread = thread(run_agent_thread, rawAgent.get());
}
else {
mctsAgent->set_search_settings(state, &searchLimits, &evalInfo);
mctsAgent->lock(); // lock() mctsAgent to avoid calling stop() immediatly
mainSearchThread = thread(run_agent_thread, mctsAgent.get());
}
}
Expand All @@ -211,7 +213,6 @@ void CrazyAra::go(const string& fen, string goCommand, EvalInfo& evalInfo)
position(state.get(), is);
istringstream isGoCommand(goCommand);
go(state.get(), isGoCommand, evalInfo);
wait_to_finish_last_search();
}

void CrazyAra::wait_to_finish_last_search()
Expand All @@ -226,6 +227,7 @@ void CrazyAra::stop_search()
{
if (mctsAgent != nullptr) {
mctsAgent->stop();
wait_to_finish_last_search();
}
}

Expand Down