Skip to content

Commit

Permalink
Allow UCI parameters to be double
Browse files Browse the repository at this point in the history
Change the operators of the Option type in uci.h to accept floating
point numbers in double precision on input as the numerical type for
the "spin" values of the UCI protocol.

The output of Stockfish after the "uci" command is unaffected.

This change is compatible with all the existing GUI (as they will
continue sending integers that we can interpret as doubles in SF),
and allows us to pass double parameters to Stockfish in the console
via the "setoption" command. This will be useful if we implement
another tuner as an alternative for SPSA.

Closes #1556

No functional change.

---------------------

A example of the new functionality in action in the branch `tune_float2'`:
snicolet/Stockfish@876c322

I have added the following lines in ucioptions.cpp:

```C++

void on_pi(const Option& o)
{
      double x = Options["PI"];  // or double x = o;
      std::cerr << "received value is x = " << x << std::endl;
}

...

o["PI"]   << Option(3.1415926, -10000000, 10000000, on_pi);
```

Then I can change the value of Pi in Stockfish via the command line, and
check that Stockfish understands a floating point:

````
> ./stockfish
> setoption name PI value 2.7182818284

received value is x = 2.71828
````

On output, the default value of Pi is truncated to 3 (to remain compatible
with the UCI protocol and GUIs):

````
> uci

[...]
option name SyzygyProbeLimit type spin default 6 min 0 max 6
option name PI type spin default 3 min -10000000 max 10000000
uciok
````
  • Loading branch information
snicolet committed Apr 23, 2018
1 parent f7cc002 commit 82f7d50
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/search.cpp
Expand Up @@ -317,7 +317,7 @@ void Thread::search() {

multiPV = std::min(multiPV, rootMoves.size());

int ct = Options["Contempt"] * PawnValueEg / 100; // From centipawns
int ct = int(Options["Contempt"]) * PawnValueEg / 100; // From centipawns

// In analysis mode, adjust contempt in accordance with user preference
if (Limits.infinite || Options["UCI_AnalyseMode"])
Expand Down Expand Up @@ -1632,9 +1632,9 @@ bool RootMove::extract_ponder_from_tt(Position& pos) {
void Tablebases::rank_root_moves(Position& pos, Search::RootMoves& rootMoves) {

RootInTB = false;
UseRule50 = Options["Syzygy50MoveRule"];
ProbeDepth = Options["SyzygyProbeDepth"] * ONE_PLY;
Cardinality = Options["SyzygyProbeLimit"];
UseRule50 = bool(Options["Syzygy50MoveRule"]);
ProbeDepth = int(Options["SyzygyProbeDepth"]) * ONE_PLY;
Cardinality = int(Options["SyzygyProbeLimit"]);
bool dtz_available = true;

// Tables with fewer pieces than SyzygyProbeLimit are searched with
Expand Down
4 changes: 2 additions & 2 deletions src/uci.h
Expand Up @@ -49,12 +49,12 @@ class Option {
Option(OnChange = nullptr);
Option(bool v, OnChange = nullptr);
Option(const char* v, OnChange = nullptr);
Option(int v, int minv, int maxv, OnChange = nullptr);
Option(double v, int minv, int maxv, OnChange = nullptr);
Option(const char* v, const char *cur, OnChange = nullptr);

Option& operator=(const std::string&);
void operator<<(const Option&);
operator int() const;
operator double() const;
operator std::string() const;
bool operator==(const char*);

Expand Down
14 changes: 8 additions & 6 deletions src/ucioption.cpp
Expand Up @@ -92,11 +92,13 @@ std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
const Option& o = it.second;
os << "\noption name " << it.first << " type " << o.type;

if (o.type != "button")
if (o.type == "string" || o.type == "check" || o.type == "combo")
os << " default " << o.defaultValue;

if (o.type == "spin")
os << " min " << o.min << " max " << o.max;
os << " default " << int(stof(o.defaultValue))
<< " min " << o.min
<< " max " << o.max;

break;
}
Expand All @@ -116,15 +118,15 @@ Option::Option(bool v, OnChange f) : type("check"), min(0), max(0), on_change(f)
Option::Option(OnChange f) : type("button"), min(0), max(0), on_change(f)
{}

Option::Option(int v, int minv, int maxv, OnChange f) : type("spin"), min(minv), max(maxv), on_change(f)
Option::Option(double v, int minv, int maxv, OnChange f) : type("spin"), min(minv), max(maxv), on_change(f)
{ defaultValue = currentValue = std::to_string(v); }

Option::Option(const char* v, const char* cur, OnChange f) : type("combo"), min(0), max(0), on_change(f)
{ defaultValue = v; currentValue = cur; }

Option::operator int() const {
Option::operator double() const {
assert(type == "check" || type == "spin");
return (type == "spin" ? stoi(currentValue) : currentValue == "true");
return (type == "spin" ? stof(currentValue) : currentValue == "true");
}

Option::operator std::string() const {
Expand Down Expand Up @@ -160,7 +162,7 @@ Option& Option::operator=(const string& v) {

if ( (type != "button" && v.empty())
|| (type == "check" && v != "true" && v != "false")
|| (type == "spin" && (stoi(v) < min || stoi(v) > max)))
|| (type == "spin" && (stof(v) < min || stof(v) > max)))
return *this;

if (type != "button")
Expand Down

2 comments on commit 82f7d50

@lucasart
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thumbs down to this patch.

@mstembera
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch introduces the following warnings under MSVC:
\src\main.cpp(47): warning C4244: 'argument': conversion from 'double' to '::size_t', possible loss of data
\src\main.cpp(48): warning C4244: 'argument': conversion from 'double' to '::size_t', possible loss of data
\src\timeman.cpp(86): warning C4244: 'initializing': conversion from 'double' to 'TimePoint', possible loss of data
\src\timeman.cpp(87): warning C4244: 'initializing': conversion from 'double' to 'TimePoint', possible loss of data
\src\timeman.cpp(88): warning C4244: 'initializing': conversion from 'double' to 'TimePoint', possible loss of data
\src\timeman.cpp(89): warning C4244: 'initializing': conversion from 'double' to 'TimePoint', possible loss of data
\src\ucioption.cpp(40): warning C4244: 'argument': conversion from 'double' to '::size_t', possible loss of data
\src\ucioption.cpp(42): warning C4244: 'argument': conversion from 'double' to '::size_t', possible loss of data
\src\search.cpp(256): warning C4244: 'argument': conversion from 'double' to 'int', possible loss of data
\src\search.cpp(310): warning C4244: 'initializing': conversion from 'double' to '::size_t', possible loss of data
\src\search.cpp(311): warning C4244: 'argument': conversion from 'double' to 'int', possible loss of data

Please sign in to comment.