1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
//-----------------------------------------------------------------------------
/** @file libpentobi_mcts/Player.cpp
@author Markus Enzenberger
@copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------
#include "Player.h"
#include <fstream>
#include "libboardgame_base/Memory.h"
namespace libpentobi_mcts {
using libpentobi_base::BoardType;
//-----------------------------------------------------------------------------
namespace {
// Rationale for choosing the number of simulations:
// * Level 9, the highest in the desktop version, should be as strong as
// possible on a mid-range PC with reasonable thinking times. The average
// time per game and player is targeted at 2-3 min for the 2-color game
// variants and 5-6 min for the others.
// * Level 7, the highest in the Android version, should be as strong as
// possible on typical mobile hardware. It is set to 4% of level 9.
// * Level 8 is set to 20% of level 9, the middle (on a log scale) between
// level 7 and 9. Since most parameter tuning is done at level 7 or 8, it is
// better for development purposes to define level 8 in terms of time, even
// if it doesn't necessarily correspond to the middle wrt. playing strength.
// * The numbers for level 1 are set to a value that is weak enough for
// beginners without playing silly moves. They are currently chosen depending
// on how strong we estimate Pentobi is in a game variant. It is also taken
// into consideration how much the Elo difference level 1-9 is in self-play
// experiments. After applying the scale factor (see comment in
// Player::get_rating()), we want a range of about 1000 Elo (difference
// between beginner and lower master level).
// * The numbers for level 1-6 are chosen such that they correspond to roughly
// equidistant Elo differences measured in self-play experiments.
// * We only calibrate the numbers for the game variants we care most about.
// For other game variants, we use the numbers of game variants with similar
// playing strength and speed of simulations.
constexpr float counts_classic[Player::max_supported_level] =
{ 3, 30, 90, 181, 667, 5028, 69809, 349044, 1745221 };
constexpr float counts_duo[Player::max_supported_level] =
{ 3, 21, 77, 213, 861, 7280, 221867, 1109339, 5546695 };
constexpr float counts_trigon[Player::max_supported_level] =
{ 100, 246, 457, 876, 1882, 5506, 19819, 99092, 495465 };
constexpr float counts_nexos[Player::max_supported_level] =
{ 250, 347, 625, 1223, 3117, 8270, 20954, 104774, 523877 };
constexpr float counts_callisto_2[Player::max_supported_level] =
{ 30, 87, 300, 1017, 4729, 20435, 122778, 613905, 3069529 };
/** Suggest how much memory to use for the trees depending on the maximum
level used. */
size_t get_memory(unsigned max_level)
{
auto available = libboardgame_base::get_memory();
if (available == 0)
{
LIBBOARDGAME_LOG("WARNING: could not determine system memory"
" (assuming 512MB)");
available = 512000000;
}
// Don't use all of the available memory
size_t reasonable = available / 4;
size_t wanted = 2000000000;
if (max_level < Player::max_supported_level)
{
// We don't need so much memory if m_max_level is smaller than
// max_supported_level. Trigon has the highest relative number of
// simulations on lower levels compared to the highest level. The
// memory used in a search is not proportional to the number of
// simulations (e.g. because the expand threshold increases with the
// depth). We approximate this by adding an exponent to the ratio
// and not taking into account if m_max_level is very small.
static_assert(Player::max_supported_level >= 5);
auto factor = pow(counts_trigon[Player::max_supported_level - 1]
/ counts_trigon[max(max_level, 5u) - 1], 0.8);
wanted = static_cast<size_t>(double(wanted) / factor);
}
size_t memory = min(wanted, reasonable);
LIBBOARDGAME_LOG("Using ", memory / 1000000, " MB of ",
available / 1000000, " MB");
return memory;
}
} // namespace
//-----------------------------------------------------------------------------
Player::Player(Variant initial_variant, unsigned max_level,
const string& books_dir, unsigned nu_threads)
: m_is_book_loaded(false),
m_use_book(true),
m_resign(false),
m_books_dir(books_dir),
m_max_level(max_level),
m_level(4),
m_search(initial_variant, nu_threads, get_memory(max_level)),
m_book(initial_variant)
{
for (unsigned i = 0; i < Board::max_player_moves; ++i)
{
// Hand-tuned such that time per move is more evenly spread among all
// moves than with a fixed number of simulations (because the
// simulations per second increase rapidly with the move number) but
// the average time per game is roughly the same.
m_weight_max_count_duo[i] = 0.7f * exp(0.1f * static_cast<float>(i));
m_weight_max_count_classic[i] = m_weight_max_count_duo[i];
m_weight_max_count_trigon[i] = m_weight_max_count_duo[i];
m_weight_max_count_callisto[i] = m_weight_max_count_duo[i];
m_weight_max_count_callisto_2[i] = m_weight_max_count_duo[i];
// Less weight for the first move(s) because number of legal moves
// is lower and the search applies some pruning rules to reduce the
// branching factor in early moves
if (i == 0)
{
m_weight_max_count_classic[i] *= 0.2f;
m_weight_max_count_trigon[i] *= 0.2f;
m_weight_max_count_duo[i] *= 0.6f;
m_weight_max_count_callisto[i] *= 0.2f;
m_weight_max_count_callisto_2[i] *= 0.2f;
}
else if (i == 1)
{
m_weight_max_count_classic[i] *= 0.2f;
m_weight_max_count_trigon[i] *= 0.5f;
m_weight_max_count_callisto[i] *= 0.6f;
m_weight_max_count_callisto_2[i] *= 0.2f;
}
else if (i == 2)
{
m_weight_max_count_classic[i] *= 0.3f;
m_weight_max_count_trigon[i] *= 0.6f;
}
else if (i == 3)
{
m_weight_max_count_trigon[i] *= 0.8f;
}
}
}
void Player::abort()
{
m_search.abort();
m_was_aborted = true;
}
Move Player::genmove(const Board& bd, Color c)
{
m_resign = false;
m_was_aborted = false;
if (! bd.has_moves(c))
return Move::null();
Move mv;
auto variant = bd.get_variant();
auto board_type = bd.get_board_type();
auto level = min(max(m_level, 1u), m_max_level);
// Don't use more than 2 moves per color from opening book in lower levels
if (m_use_book
&& (level >= 4 || bd.get_nu_moves() < 2u * bd.get_nu_colors()))
{
if (! is_book_loaded(variant))
load_book(m_books_dir
+ "/book_" + to_string_id(variant) + ".blksgf");
if (m_is_book_loaded)
{
mv = m_book.genmove(bd, c);
if (! mv.is_null())
return mv;
}
}
Float max_count = 0;
switch (board_type)
{
case BoardType::classic:
case BoardType::gembloq_2:
max_count = counts_classic[level - 1];
break;
case BoardType::duo:
max_count = counts_duo[level - 1];
break;
case BoardType::trigon:
case BoardType::trigon_3:
case BoardType::callisto:
case BoardType::callisto_3:
case BoardType::gembloq:
case BoardType::gembloq_3:
max_count = counts_trigon[level - 1];
break;
case BoardType::nexos:
max_count = counts_nexos[level - 1];
break;
case BoardType::callisto_2:
max_count = counts_callisto_2[level - 1];
break;
}
// Don't weight max_count in low levels, otherwise it is still too
// strong for beginners (later in the game, the weight becomes much
// greater than 1 because the simulations become very fast)
if (level >= 4)
{
auto player_move = bd.get_nu_onboard_pieces(c);
float weight = 1; // Init to avoid compiler warning
switch (board_type)
{
case BoardType::classic:
weight = m_weight_max_count_classic[player_move];
break;
case BoardType::duo:
case BoardType::gembloq_2:
weight = m_weight_max_count_duo[player_move];
break;
case BoardType::callisto:
case BoardType::callisto_3:
weight = m_weight_max_count_callisto[player_move];
break;
case BoardType::callisto_2:
weight = m_weight_max_count_callisto_2[player_move];
break;
case BoardType::trigon:
case BoardType::trigon_3:
case BoardType::nexos:
case BoardType::gembloq:
case BoardType::gembloq_3:
weight = m_weight_max_count_trigon[player_move];
break;
}
max_count = ceil(max_count * weight);
}
LIBBOARDGAME_LOG("MaxCnt ", fixed, setprecision(0), max_count);
if (! m_search.search(mv, bd, c, max_count, 0, 0, m_time_source))
return Move::null();
m_was_aborted = m_search.was_aborted();
// Resign only in two-player game variants
if (get_nu_players(variant) == 2)
if (m_search.get_root_visit_count() > 500
&& m_search.get_root_val().get_mean() < 0.09f)
m_resign = true;
return mv;
}
Rating Player::get_rating(Variant variant, unsigned level)
{
// The ratings are roughly based on Elo differences measured in self-play
// experiments. The measured values are scaled with a factor smaller than 1
// to take into account that self-play usually overestimates the strength
// against humans. The anchor is set to about 1000 (beginner level) for
// level 1. The exact value for anchor and scale is chosen according to our
// estimate how strong Pentobi plays at level 1 and level 9 in each game
// variant (2000 Elo would be lower expert level). Currently, only 2-player
// variants are calibrated and the ratings are also used for other game
// variants that we assume have comparable strength (e.g. multi-player on
// the same board).
level = min(max(level, 1u), max_supported_level);
Rating result;
switch (get_board_type(variant))
{
case BoardType::classic: // Measured for classic_2
{
// Anchor 1000, scale 0.6
static double elo[max_supported_level] =
{ 1000, 1142, 1283, 1425, 1567, 1708, 1850, 1951, 2030 };
result = Rating(elo[level - 1]);
}
break;
case BoardType::duo:
{
// Anchor 1000, scale 0.74
static double elo[max_supported_level] =
{ 1000, 1189, 1378, 1567, 1755, 1945, 2134, 2185, 2209 };
result = Rating(elo[level - 1]);
}
break;
case BoardType::callisto_2:
{
// Anchor 1000, scale 0.49
static double elo[max_supported_level] =
{ 1000, 1113, 1225, 1338, 1450, 1563, 1675, 1783, 1868 };
result = Rating(elo[level - 1]);
}
break;
case BoardType::trigon: // Measured for trigon_2
case BoardType::trigon_3:
{
// Anchor 1000, scale 0.48
static double elo[max_supported_level] =
{ 1000, 1110, 1220, 1330, 1440, 1550, 1660, 1765, 1897 };
result = Rating(elo[level - 1]);
}
break;
case BoardType::nexos: // Measured for nexos_2
case BoardType::callisto:
case BoardType::callisto_3:
case BoardType::gembloq:
case BoardType::gembloq_2:
case BoardType::gembloq_3:
{
// Anchor 1000, scale 0.60
static double elo[Player::max_supported_level] =
{ 1000, 1101, 1202, 1304, 1406, 1507, 1608, 1698, 1799 };
result = Rating(elo[level - 1]);
}
break;
}
return result;
}
bool Player::is_book_loaded(Variant variant) const
{
return m_is_book_loaded && m_book.get_tree().get_variant() == variant;
}
void Player::load_book(istream& in)
{
m_book.load(in);
m_is_book_loaded = true;
}
bool Player::load_book(const string& filepath)
{
ifstream in(filepath);
if (! in)
{
LIBBOARDGAME_LOG("Could not load book ", filepath);
return false;
}
m_book.load(in);
m_is_book_loaded = true;
LIBBOARDGAME_LOG("Loaded book ", filepath);
return true;
}
bool Player::resign() const
{
return m_resign;
}
//-----------------------------------------------------------------------------
} // namespace libpentobi_mcts
|