Advertisement
Guest User

uci_opties.cpp

a guest
Mar 26th, 2020
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.24 KB | None | 0 0
  1. /*
  2. */
  3.  
  4. #include <ostream>
  5. #include <iostream>
  6.  
  7. #include "houdini.h"
  8. #include "util_tools.h"
  9. #include "zoeken.h"
  10. #include "zoek_smp.h"
  11. #include "zet_hash.h"
  12. #include "uci.h"
  13. #include "syzygy/tbprobe.h"
  14. #include "evaluatie.h"
  15. #ifdef USE_NALIMOV
  16. #   include "egtb_nalimovprobe.h"
  17. #endif
  18.  
  19. using std::string;
  20.  
  21. UCI::UciOptiesMap UciOpties;
  22.  
  23. int TUNE_1, TUNE_2, TUNE_3, TUNE_4, TUNE_5, TUNE_6, TUNE_7, TUNE_8, TUNE_9, TUNE_10;
  24. int TUNE_11, TUNE_12, TUNE_13, TUNE_14, TUNE_15, TUNE_16, TUNE_17, TUNE_18, TUNE_19, TUNE_20;
  25.  
  26. extern void Syzygy_init(const std::string& path);
  27.  
  28. namespace UCI
  29. {
  30.     void on_clear_hash(const UciOptie&)
  31.     {
  32.         Zoeken::wis_alles();
  33.         if (UciOpties["Never Clear Hash"])
  34.             sync_cout << "info string \"Never Clear Hash\" option is enabled" << sync_endl;
  35.         else
  36.             sync_cout << "info string Hash cleared" << sync_endl;
  37.     }
  38.  
  39.     void on_hash_size(const UciOptie& o)
  40.     {
  41.         HoofdHash.verander_grootte(o);
  42.     }
  43.  
  44.     void on_logger(const UciOptie& o)
  45.     {
  46.         start_logger(o);
  47.     }
  48.  
  49.     void on_threads(const UciOptie&)
  50.     {
  51.         Threads.verander_thread_aantal();
  52.     }
  53.  
  54.     void on_syzygy_path(const UciOptie& o)
  55.     {
  56.         Syzygy_init(o);
  57.     }
  58.  
  59. #ifdef USE_NALIMOV
  60.     void on_nalimov_path(const UciOptie& o)
  61.     {
  62.         Nalimov_init(o);
  63.     }
  64.  
  65.     void on_nalimov_cache(const UciOptie& o)
  66.     {
  67.         Nalimov_setCache(o, true);
  68.     }
  69. #endif
  70.  
  71.     void on_50move_distance(const UciOptie& o)
  72.     {
  73.         Stelling::init_hash_move50(o);
  74.     }
  75.  
  76.     void on_save_hash_to_file(const UciOptie& o)
  77.     {
  78.         if (HoofdHash.save_to_file(UciOpties["Hash File"]))
  79.             sync_cout << "info string Hash Saved to File" << sync_endl;
  80.         else
  81.             sync_cout << "info string Failure to Save Hash to File" << sync_endl;
  82.     }
  83.  
  84.     void on_load_hash_from_file(const UciOptie& o)
  85.     {
  86.         if (!UciOpties["Never Clear Hash"])
  87.         {
  88.             UciOpties["Never Clear Hash"] = true;
  89.             sync_cout << "info string Enabling Never Clear Hash option, please enable this option in the Houdini configuration" << sync_endl;
  90.         }
  91.         if (HoofdHash.load_from_file(UciOpties["Hash File"]))
  92.             sync_cout << "info string Hash Loaded from File" << sync_endl;
  93.         else
  94.             sync_cout << "info string Failure to Load Hash from File" << sync_endl;
  95.     }
  96.  
  97.     int sterkte_voor_elo(int elo)
  98.     {
  99.         if (elo <= 800)
  100.             return 0;
  101.         else if (elo <= 2300)
  102.             return (elo - 800) / 30;
  103.         else if (elo <= 3000)
  104.             return (elo - 2300) / 14 + 50;
  105.         else
  106.             return 100;
  107.     }
  108.  
  109.     int elo_voor_sterkte(int sterkte)
  110.     {
  111.         if (sterkte <= 50)
  112.             return 800 + 30 * sterkte;
  113.         else if (sterkte < 100)
  114.             return 2300 + 14 * (sterkte - 50);
  115.         else
  116.             return 3000;
  117.     }
  118.  
  119.     void on_uci_elo(const UciOptie& o)
  120.     {
  121.         int elo = int(o);
  122.         elo = std::min(std::max(elo, 1400), 3200);
  123.         if (elo < 3200)
  124.             sync_cout << "info string UCI_Elo " << elo << " is translated to Strength " << sterkte_voor_elo(elo - 200) << sync_endl;
  125.     }
  126.  
  127.     void on_strength(const UciOptie& o)
  128.     {
  129.         int strength = int(o);
  130.         strength = std::min(std::max(strength, 0), 100);
  131.         if (strength < 100)
  132.             sync_cout << "info string Strength " << strength << " corresponds to approximately " << elo_voor_sterkte(strength) + 200 << " Elo" << sync_endl;
  133.     }
  134.  
  135. #ifdef USE_NUMA
  136.     void on_numa_offset(const UciOptie& o)
  137.     {
  138.         Threads.verander_numa_offset(o);
  139.     }
  140.  
  141.     void on_numa_enabled(const UciOptie& o)
  142.     {
  143.         Threads.verander_numa(o);
  144.     }
  145. #endif
  146.  
  147.     void on_tune1(const UciOptie& o) { TUNE_1 = o; Evaluatie::init_tune(); }
  148.     void on_tune2(const UciOptie& o) { TUNE_2 = o; Evaluatie::init_tune(); }
  149.     void on_tune3(const UciOptie& o) { TUNE_3 = o; Evaluatie::init_tune(); }
  150.     void on_tune4(const UciOptie& o) { TUNE_4 = o; Evaluatie::init_tune(); }
  151.     void on_tune5(const UciOptie& o) { TUNE_5 = o; Evaluatie::init_tune(); }
  152.     void on_tune6(const UciOptie& o) { TUNE_6 = o; Evaluatie::init_tune(); }
  153.     void on_tune7(const UciOptie& o) { TUNE_7 = o; Evaluatie::init_tune(); }
  154.     void on_tune8(const UciOptie& o) { TUNE_8 = o; Evaluatie::init_tune(); }
  155.     void on_tune9(const UciOptie& o) { TUNE_9 = o; Evaluatie::init_tune(); }
  156.     void on_tune10(const UciOptie& o) { TUNE_10 = o; Evaluatie::init_tune(); }
  157.     void on_tune11(const UciOptie& o) { TUNE_11 = o; Evaluatie::init_tune(); }
  158.     void on_tune12(const UciOptie& o) { TUNE_12 = o; Evaluatie::init_tune(); }
  159.     void on_tune13(const UciOptie& o) { TUNE_13 = o; Evaluatie::init_tune(); }
  160.     void on_tune14(const UciOptie& o) { TUNE_14 = o; Evaluatie::init_tune(); }
  161.     void on_tune15(const UciOptie& o) { TUNE_15 = o; Evaluatie::init_tune(); }
  162.     void on_tune16(const UciOptie& o) { TUNE_16 = o; Evaluatie::init_tune(); }
  163.     void on_tune17(const UciOptie& o) { TUNE_17 = o; Evaluatie::init_tune(); }
  164.     void on_tune18(const UciOptie& o) { TUNE_18 = o; Evaluatie::init_tune(); }
  165.     void on_tune19(const UciOptie& o) { TUNE_19 = o; Evaluatie::init_tune(); }
  166.     void on_tune20(const UciOptie& o) { TUNE_20 = o; Evaluatie::init_tune(); }
  167.  
  168.  
  169.     bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const
  170.     {
  171.         return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(),
  172.             [](char c1, char c2) { return tolower(c1) < tolower(c2); });
  173.     }
  174.  
  175. #if defined(IS_64BIT)
  176. #  ifdef PREMIUM
  177.     const int MAX_HASH_MB = 128 * 1024;
  178. #  else
  179.     const int MAX_HASH_MB = 4 * 1024;
  180. #  endif
  181. #else
  182.     const int MAX_HASH_MB = 1024;
  183. #endif
  184.  
  185.     void initialisatie(UciOptiesMap& o)
  186.     {
  187.         o["Threads"] << UciOptie(1, 1, MAX_THREADS, on_threads);
  188.         o["Hash"] << UciOptie(128, 1, MAX_HASH_MB, on_hash_size);
  189.         o["Clear Hash"] << UciOptie(on_clear_hash);
  190.         //o["Tactical Mode"] << UciOptie(false);
  191.         o["Tactical Mode"] << UciOptie(0, 0, MAX_THREADS);
  192.         o["Ponder"] << UciOptie(false);
  193.         o["Contempt"] << UciOptie(2, -10, 10);
  194.         o["Analysis Contempt"] << UciOptie(false);
  195.         o["MultiPV"] << UciOptie(1, 1, 220);
  196.         o["MultiPV_cp"] << UciOptie(0, 0, 999);
  197.         o["SyzygyPath"] << UciOptie("<empty>", on_syzygy_path);
  198.         o["EGTB Probe Depth"] << UciOptie(1, 0, 99);
  199.         o["EGTB Fifty Move Rule"] << UciOptie(true);
  200. #ifdef USE_NALIMOV
  201.         o["NalimovPath"] << UciOptie("<empty>", on_nalimov_path);
  202.         o["NalimovCache"] << UciOptie(32, 4, 1024, on_nalimov_cache);
  203. #endif
  204. #ifdef USE_NUMA
  205.         o["NUMA Offset"] << UciOptie(0, 0, MAX_NUMA_NODES - 1, on_numa_offset);
  206.         o["NUMA Enabled"] << UciOptie(true, on_numa_enabled);
  207. #endif
  208.         o["Strength"] << UciOptie(100, 0, 100, on_strength);
  209.         o["UCI_LimitStrength"] << UciOptie(false);
  210.         o["UCI_Elo"] << UciOptie(3200, 1000, 3200, on_uci_elo);
  211.  
  212.         o["UCI_Chess960"] << UciOptie(false);
  213.         o["Never Clear Hash"] << UciOptie(false);
  214.         o["Hash File"] << UciOptie("<empty>");
  215.         o["Save Hash to File"] << UciOptie(on_save_hash_to_file);
  216.         o["Load Hash from File"] << UciOptie(on_load_hash_from_file);
  217. #ifdef LEARNING
  218.         o["Learning File"] << UciOptie("<empty>");
  219.         o["Learning Threshold"] << UciOptie(10, -100, 200);
  220.         o["Learning"] << UciOptie(false);
  221. #endif
  222.         o["FiftyMoveDistance"] << UciOptie(50, 5, 50, on_50move_distance);
  223.         o["Move Overhead"] << UciOptie(0, 0, 5000);
  224.         o["UCI Log File"] << UciOptie("", on_logger);
  225.         o["Hide Redundant Output"] << UciOptie(false);
  226.  
  227.         o["Own Book"] << UciOptie(false);
  228.         o["Book File"] << UciOptie("<empty>");
  229.         o["Best Book Line"] << UciOptie(false);
  230.  
  231.         //o["Tune1"] << UciOptie(256, -99999, 99999, on_tune1);
  232.         //o["Tune2"] << UciOptie(256, -99999, 99999, on_tune2);
  233.         //o["Tune3"] << UciOptie(256, -99999, 99999, on_tune3);
  234.         //o["Tune4"] << UciOptie(256, -99999, 99999, on_tune4);
  235.         //o["Tune5"] << UciOptie(256, -99999, 99999, on_tune5);
  236.         //o["Tune6"] << UciOptie(256, -99999, 99999, on_tune6);
  237.         //o["Tune7"] << UciOptie(256, -99999, 99999, on_tune7);
  238.         //o["Tune8"] << UciOptie(256, -99999, 99999, on_tune8);
  239.         //o["Tune9"] << UciOptie(256, -99999, 99999, on_tune9);
  240.         //o["Tune10"] << UciOptie(256, -99999, 99999, on_tune10);
  241.         //o["Tune11"] << UciOptie(256, -99999, 99999, on_tune11);
  242.         //o["Tune12"] << UciOptie(256, -99999, 99999, on_tune12);
  243.         //o["Tune13"] << UciOptie(256, -99999, 99999, on_tune13);
  244.         //o["Tune14"] << UciOptie(256, -99999, 99999, on_tune14);
  245.         //o["Tune15"] << UciOptie(256, -99999, 99999, on_tune15);
  246.         //o["Tune16"] << UciOptie(256, -99999, 99999, on_tune16);
  247.         //o["Tune17"] << UciOptie(256, -99999, 99999, on_tune17);
  248.         //o["Tune18"] << UciOptie(256, -99999, 99999, on_tune18);
  249.         //o["Tune19"] << UciOptie(256, -99999, 99999, on_tune19);
  250.         //o["Tune20"] << UciOptie(256, -99999, 99999, on_tune20);
  251.         TUNE_1 = TUNE_2 = TUNE_3 = TUNE_4 = TUNE_5 = TUNE_6 = TUNE_7 = TUNE_8 = TUNE_9 = TUNE_10 = 256;
  252.         TUNE_11 = TUNE_12 = TUNE_13 = TUNE_14 = TUNE_15 = TUNE_16 = TUNE_17 = TUNE_18 = TUNE_19 = TUNE_20 = 256;
  253.     }
  254.  
  255.  
  256.     std::ostream& operator<<(std::ostream& os, const UciOptiesMap& om)
  257.     {
  258.         for (int n = 0; n < (int)om.size(); ++n)
  259.             for (const auto& it : om)
  260.                 if (it.second.index == n)
  261.                 {
  262.                     const UciOptie& o = it.second;
  263.                     os << "option name " << it.first << " type " << o.type;
  264.  
  265.                     if (o.type == "spin")
  266.                         os << " min " << o.min << " max " << o.max;
  267.  
  268.                     if (o.type != "button")
  269.                         os << " default " << o.defaultWaarde;
  270.  
  271.                     os << std::endl;
  272.                     break;
  273.                 }
  274.  
  275.         return os;
  276.     }
  277.  
  278.  
  279.     UciOptie::UciOptie(const char* v, OnChange f) : min(0), max(0), type("string"), on_change(f)
  280.     {
  281.         defaultWaarde = waarde = v;
  282.     }
  283.  
  284.     UciOptie::UciOptie(bool v, OnChange f) : min(0), max(0), type("check"), on_change(f)
  285.     {
  286.         defaultWaarde = waarde = (v ? "true" : "false");
  287.     }
  288.  
  289.     UciOptie::UciOptie(OnChange f) : min(0), max(0), type("button"), on_change(f)
  290.     {}
  291.  
  292.     UciOptie::UciOptie(int v, int minv, int maxv, OnChange f) : min(minv), max(maxv), type("spin"), on_change(f)
  293.     {
  294.         defaultWaarde = waarde = std::to_string(v);
  295.     }
  296.  
  297.     UciOptie::operator int() const
  298.     {
  299.         assert(type == "check" || type == "spin");
  300.         return (type == "spin" ? stoi(waarde) : waarde == "true" || waarde == "yes" || waarde == "1");
  301.     }
  302.  
  303.     UciOptie::operator std::string() const
  304.     {
  305.         assert(type == "string");
  306.         return waarde;
  307.     }
  308.  
  309.     void UciOptie::operator<<(const UciOptie& o)
  310.     {
  311.         static int volgorde = 0;
  312.         *this = o;
  313.         index = volgorde++;
  314.     }
  315.  
  316.     UciOptie& UciOptie::operator=(const string& v)
  317.     {
  318.         assert(!type.empty());
  319.  
  320.         if ((type != "button" && v.empty())
  321.             || (type == "check" && v != "true" && v != "false" && v != "yes" && v != "no" && v != "1" && v != "0")
  322.             || (type == "spin" && (stoi(v) < min || stoi(v) > max)))
  323.             return *this;
  324.  
  325.         if (type != "button")
  326.             waarde = v;
  327.  
  328.         if (on_change)
  329.             on_change(*this);
  330.  
  331.         return *this;
  332.     }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement