Skip to content

Commit f358074

Browse files
Normalize book weights (#1109)
1 parent 58d6eb7 commit f358074

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

config.yml.default

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ engine: # Engine settings.
2323
# - engines/atomicbook2.bin
2424
# etc.
2525
# Use the same pattern for 'chess960', 'giveaway' (antichess), 'crazyhouse', 'horde', 'kingofthehill', 'racingkings' and '3check' as well.
26-
min_weight: 1 # Does not select moves with weight below min_weight (min 0, max: 65535).
26+
min_weight: 1 # Does not select moves with weight below min_weight (min 0, max: 100 if normalization isn't "none" else 65535).
2727
selection: "weighted_random" # Move selection is one of "weighted_random", "uniform_random" or "best_move" (but not below the min_weight in the 2nd and 3rd case).
2828
max_depth: 20 # How many moves from the start to take from the book.
29+
normalization: "none" # Normalization method for the book weights. One of "none", "sum", or "max".
2930

3031
draw_or_resign:
3132
resign_enabled: false # Whether or not the bot should resign.

lib/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ def insert_default_values(CONFIG: CONFIG_DICT_TYPE) -> None:
198198
set_config_default(CONFIG, "engine", "polyglot", key="max_depth", default=8)
199199
set_config_default(CONFIG, "engine", "polyglot", key="selection", default="weighted_random")
200200
set_config_default(CONFIG, "engine", "polyglot", key="min_weight", default=1)
201+
set_config_default(CONFIG, "engine", "polyglot", key="normalization", default="none")
201202
set_config_default(CONFIG, "challenge", key="concurrency", default=1)
202203
set_config_default(CONFIG, "challenge", key="sort_by", default="best")
203204
set_config_default(CONFIG, "challenge", key="preference", default="none")
@@ -387,6 +388,11 @@ def has_valid_list(name: str) -> bool:
387388
f"`{selection}` is not a valid `engine:{select}` value. "
388389
f"Please choose from {valid_selections}.")
389390

391+
polyglot_section = CONFIG["engine"].get("polyglot") or {}
392+
config_assert(polyglot_section.get("normalization") in ["none", "max", "sum"],
393+
f"`{polyglot_section.get('normalization')}` is not a valid choice for "
394+
f"`engine:polyglot:normalization`. Please choose from ['none', 'max', 'sum'].")
395+
390396
lichess_tbs_config = CONFIG["engine"].get("lichess_bot_tbs") or {}
391397
quality_selections = ["best", "suggest"]
392398
for tb in ["syzygy", "gaviota"]:

lib/engine_wrapper.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,12 @@ def get_book_move(board: chess.Board, game: model.Game,
754754
try:
755755
selection = polyglot_cfg.selection
756756
min_weight = polyglot_cfg.min_weight
757+
normalization = polyglot_cfg.normalization
758+
weights = [entry.weight for entry in reader.find_all(board)]
759+
scalar = (sum(weights) if normalization == "sum" and weights else
760+
max(weights) if normalization == "max" and weights else 100)
761+
min_weight = min_weight * scalar / 100
762+
757763
if selection == "weighted_random":
758764
move = reader.weighted_choice(board).move
759765
elif selection == "uniform_random":

wiki/Configure-lichess-bot.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ will precede the `go` command to start thinking with `sd 5`. The other `go_comma
8888
- `polyglot`: Tell lichess-bot whether your bot should use an opening book. Multiple books can be specified for each chess variant.
8989
- `enabled`: Whether to use the book at all.
9090
- `book`: A nested list of books. The next indented line should list a chess variant (`standard`, `3check`, `horde`, etc.) followed on succeeding indented lines with paths to the book files. See `config.yml.default` for examples.
91-
- `min_weight`: The minimum weight or quality a move must have if it is to have a chance of being selected. If a move cannot be found that has at least this weight, no move will be selected.
91+
- `min_weight`: The minimum weight or quality a move must have if it is to have a chance of being selected (doesn't apply for `selection="weighted_random"`). If a move cannot be found that has at least this weight, no move will be selected. If `normalization` is `"none"` then `min_weight` takes values from 0 to 65535, but if `normalization` is `sum` or `max` then it takes values from 0 to 100.
9292
- `selection`: The method for selecting a move. The choices are: `"weighted_random"` where moves with a higher weight/quality have a higher probability of being chosen, `"uniform_random"` where all moves of sufficient quality have an equal chance of being chosen, and `"best_move"` where the move with the highest weight is always chosen.
9393
- `max_depth`: The maximum number of moves a bot plays before it stops consulting the book. If `max_depth` is 3, then the bot will stop consulting the book after its third move.
94+
- `normalization`: The normalization method applied to the weights of the moves. The choices are: `"none"` where no normalization is applied, `"sum"` where the weights are normalized to sum up to 100, and `"max"` where the weights are normalized so that the maximum weight is 100.
9495
- `online_moves`: This section gives your bot access to various online resources for choosing moves like opening books and endgame tablebases. This can be a supplement or a replacement for chess databases stored on your computer. There are four sections that correspond to four different online databases:
9596
1. `chessdb_book`: Consults a [Chinese chess position database](https://www.chessdb.cn/), which also hosts a xiangqi database.
9697
2. `lichess_cloud_analysis`: Consults [Lichess's own position analysis database](https://lichess.org/api#operation/apiCloudEval).

0 commit comments

Comments
 (0)