From 786564068bb22c750c41649733d4aa6733accaec Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Thu, 14 Apr 2011 15:59:49 +0200 Subject: [PATCH] Promote OptionsMap to a class And add a bit of documentation too. No functional change. Signed-off-by: Marco Costalba --- src/main.cpp | 1 - src/uci.cpp | 2 +- src/ucioption.cpp | 103 +++++++++++++++++++++++----------------------- src/ucioption.h | 39 ++++++++++-------- 4 files changed, 75 insertions(+), 70 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 02f2b87d..4b716dde 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -50,7 +50,6 @@ int main(int argc, char* argv[]) { // Startup initializations init_bitboards(); - init_uci_options(); Position::init_zobrist(); Position::init_piece_square_tables(); init_eval(1); diff --git a/src/uci.cpp b/src/uci.cpp index 683aac6d..8b90c227 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -78,7 +78,7 @@ bool execute_uci_command(const string& cmd) { else if (token == "uci") cout << "id name " << engine_name() << "\nid author " << engine_authors() - << "\n" << options_to_uci() + << "\n" << Options.print_all() << "\nuciok" << endl; else if (token == "ucinewgame") diff --git a/src/ucioption.cpp b/src/ucioption.cpp index 97cbe1a6..89e47058 100644 --- a/src/ucioption.cpp +++ b/src/ucioption.cpp @@ -60,53 +60,55 @@ static string stringify(const T& v) { } -/// init_uci_options() initializes the UCI options to their hard coded default +/// OptionsMap c'tor initializes the UCI options to their hard coded default /// values and initializes the default value of "Threads" and "Minimum Split Depth" /// parameters according to the number of CPU cores. -void init_uci_options() { - - Options["Use Search Log"] = Option(false); - Options["Search Log Filename"] = Option("SearchLog.txt"); - Options["Book File"] = Option("book.bin"); - Options["Best Book Move"] = Option(false); - Options["Mobility (Middle Game)"] = Option(100, 0, 200); - Options["Mobility (Endgame)"] = Option(100, 0, 200); - Options["Pawn Structure (Middle Game)"] = Option(100, 0, 200); - Options["Pawn Structure (Endgame)"] = Option(100, 0, 200); - Options["Passed Pawns (Middle Game)"] = Option(100, 0, 200); - Options["Passed Pawns (Endgame)"] = Option(100, 0, 200); - Options["Space"] = Option(100, 0, 200); - Options["Aggressiveness"] = Option(100, 0, 200); - Options["Cowardice"] = Option(100, 0, 200); - Options["Check Extension (PV nodes)"] = Option(2, 0, 2); - Options["Check Extension (non-PV nodes)"] = Option(1, 0, 2); - Options["Pawn Push to 7th Extension (PV nodes)"] = Option(1, 0, 2); - Options["Pawn Push to 7th Extension (non-PV nodes)"] = Option(1, 0, 2); - Options["Passed Pawn Extension (PV nodes)"] = Option(1, 0, 2); - Options["Passed Pawn Extension (non-PV nodes)"] = Option(0, 0, 2); - Options["Pawn Endgame Extension (PV nodes)"] = Option(2, 0, 2); - Options["Pawn Endgame Extension (non-PV nodes)"] = Option(2, 0, 2); - Options["Minimum Split Depth"] = Option(4, 4, 7); - Options["Maximum Number of Threads per Split Point"] = Option(5, 4, 8); - Options["Threads"] = Option(1, 1, MAX_THREADS); - Options["Use Sleeping Threads"] = Option(true); - Options["Hash"] = Option(32, 4, 8192); - Options["Clear Hash"] = Option(false, "button"); - Options["Ponder"] = Option(true); - Options["OwnBook"] = Option(true); - Options["MultiPV"] = Option(1, 1, 500); - Options["Skill level"] = Option(20, 0, 20); - Options["Emergency Move Horizon"] = Option(40, 0, 50); - Options["Emergency Base Time"] = Option(200, 0, 30000); - Options["Emergency Move Time"] = Option(70, 0, 5000); - Options["Minimum Thinking Time"] = Option(20, 0, 5000); - Options["UCI_Chess960"] = Option(false); - Options["UCI_AnalyseMode"] = Option(false); +OptionsMap::OptionsMap() { + + OptionsMap& o = *this; + + o["Use Search Log"] = Option(false); + o["Search Log Filename"] = Option("SearchLog.txt"); + o["Book File"] = Option("book.bin"); + o["Best Book Move"] = Option(false); + o["Mobility (Middle Game)"] = Option(100, 0, 200); + o["Mobility (Endgame)"] = Option(100, 0, 200); + o["Pawn Structure (Middle Game)"] = Option(100, 0, 200); + o["Pawn Structure (Endgame)"] = Option(100, 0, 200); + o["Passed Pawns (Middle Game)"] = Option(100, 0, 200); + o["Passed Pawns (Endgame)"] = Option(100, 0, 200); + o["Space"] = Option(100, 0, 200); + o["Aggressiveness"] = Option(100, 0, 200); + o["Cowardice"] = Option(100, 0, 200); + o["Check Extension (PV nodes)"] = Option(2, 0, 2); + o["Check Extension (non-PV nodes)"] = Option(1, 0, 2); + o["Pawn Push to 7th Extension (PV nodes)"] = Option(1, 0, 2); + o["Pawn Push to 7th Extension (non-PV nodes)"] = Option(1, 0, 2); + o["Passed Pawn Extension (PV nodes)"] = Option(1, 0, 2); + o["Passed Pawn Extension (non-PV nodes)"] = Option(0, 0, 2); + o["Pawn Endgame Extension (PV nodes)"] = Option(2, 0, 2); + o["Pawn Endgame Extension (non-PV nodes)"] = Option(2, 0, 2); + o["Minimum Split Depth"] = Option(4, 4, 7); + o["Maximum Number of Threads per Split Point"] = Option(5, 4, 8); + o["Threads"] = Option(1, 1, MAX_THREADS); + o["Use Sleeping Threads"] = Option(true); + o["Hash"] = Option(32, 4, 8192); + o["Clear Hash"] = Option(false, "button"); + o["Ponder"] = Option(true); + o["OwnBook"] = Option(true); + o["MultiPV"] = Option(1, 1, 500); + o["Skill level"] = Option(20, 0, 20); + o["Emergency Move Horizon"] = Option(40, 0, 50); + o["Emergency Base Time"] = Option(200, 0, 30000); + o["Emergency Move Time"] = Option(70, 0, 5000); + o["Minimum Thinking Time"] = Option(20, 0, 5000); + o["UCI_Chess960"] = Option(false); + o["UCI_AnalyseMode"] = Option(false); // Set some SMP parameters accordingly to the detected CPU count - Option& thr = Options["Threads"]; - Option& msd = Options["Minimum Split Depth"]; + Option& thr = o["Threads"]; + Option& msd = o["Minimum Split Depth"]; thr.defaultValue = thr.currentValue = stringify(cpu_count()); @@ -115,15 +117,15 @@ void init_uci_options() { } -/// options_to_uci() returns a string with all the UCI options in chronological +/// OptionsMap::print_all() returns a string with all the UCI options in chronological /// insertion order (the idx field) and in the format defined by the UCI protocol. -string options_to_uci() { +string OptionsMap::print_all() const { std::stringstream s; - for (size_t i = 0; i <= Options.size(); i++) - for (OptionsMap::const_iterator it = Options.begin(); it != Options.end(); ++it) + for (size_t i = 0; i <= size(); i++) + for (OptionsMap::const_iterator it = begin(); it != end(); ++it) if (it->second.idx == i) { const Option& o = it->second; @@ -143,13 +145,13 @@ string options_to_uci() { /// Option class c'tors -Option::Option(const char* def) : type("string"), idx(Options.size()), minValue(0), maxValue(0) +Option::Option(const char* def) : type("string"), minValue(0), maxValue(0), idx(Options.size()) { defaultValue = currentValue = def; } -Option::Option(bool def, string t) : type(t), idx(Options.size()), minValue(0), maxValue(0) +Option::Option(bool def, string t) : type(t), minValue(0), maxValue(0), idx(Options.size()) { defaultValue = currentValue = (def ? "true" : "false"); } -Option::Option(int def, int minv, int maxv) : type("spin"), idx(Options.size()), minValue(minv), maxValue(maxv) +Option::Option(int def, int minv, int maxv) : type("spin"), minValue(minv), maxValue(maxv), idx(Options.size()) { defaultValue = currentValue = stringify(def); } @@ -164,8 +166,7 @@ void Option::set_value(const string& v) { if (v.empty()) return; - if ( (type == "check" || type == "button") - != (v == "true" || v == "false")) + if ((type == "check" || type == "button") != (v == "true" || v == "false")) return; if (type == "spin") diff --git a/src/ucioption.h b/src/ucioption.h index b9c16cd9..a5da2e44 100644 --- a/src/ucioption.h +++ b/src/ucioption.h @@ -27,7 +27,7 @@ class Option { public: - Option() {} // To allow insertion in a std::map + Option() {} // To be used in a std::map Option(const char* defaultValue); Option(bool defaultValue, std::string type = "check"); Option(int defaultValue, int minValue, int maxValue); @@ -36,16 +36,33 @@ public: template T value() const; private: - friend void init_uci_options(); - friend std::string options_to_uci(); + friend class OptionsMap; std::string defaultValue, currentValue, type; - size_t idx; int minValue, maxValue; + size_t idx; +}; + + +/// Custom comparator because UCI options should not be case sensitive +struct CaseInsensitiveLess { + bool operator() (const std::string&, const std::string&) const; +}; + + +/// Our options container is actually a map with a customized c'tor +class OptionsMap : public std::map { +public: + OptionsMap(); + std::string print_all() const; }; +extern OptionsMap Options; + + +/// Option::value() definition and specializations template -inline T Option::value() const { +T Option::value() const { assert(type == "spin"); return T(atoi(currentValue.c_str())); @@ -65,16 +82,4 @@ inline bool Option::value() const { return currentValue == "true"; } - -// Custom comparator because UCI options should not be case sensitive -struct CaseInsensitiveLess { - bool operator() (const std::string&, const std::string&) const; -}; - -typedef std::map OptionsMap; - -extern OptionsMap Options; -extern void init_uci_options(); -extern std::string options_to_uci(); - #endif // !defined(UCIOPTION_H_INCLUDED) -- 2.39.2