Skip to content

Normalize book weights #1109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config.yml.default
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ engine: # Engine settings.
# - engines/atomicbook2.bin
# etc.
# Use the same pattern for 'chess960', 'giveaway' (antichess), 'crazyhouse', 'horde', 'kingofthehill', 'racingkings' and '3check' as well.
min_weight: 1 # Does not select moves with weight below min_weight (min 0, max: 65535).
min_weight: 1 # Does not select moves with weight below min_weight (min 0, max: 100 if normalization isn't "none" else 65535).
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).
max_depth: 20 # How many moves from the start to take from the book.
normalization: "none" # Normalization method for the book weights. One of "none", "sum", or "max".

draw_or_resign:
resign_enabled: false # Whether or not the bot should resign.
Expand Down
6 changes: 6 additions & 0 deletions lib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def insert_default_values(CONFIG: CONFIG_DICT_TYPE) -> None:
set_config_default(CONFIG, "engine", "polyglot", key="max_depth", default=8)
set_config_default(CONFIG, "engine", "polyglot", key="selection", default="weighted_random")
set_config_default(CONFIG, "engine", "polyglot", key="min_weight", default=1)
set_config_default(CONFIG, "engine", "polyglot", key="normalization", default="none")
set_config_default(CONFIG, "challenge", key="concurrency", default=1)
set_config_default(CONFIG, "challenge", key="sort_by", default="best")
set_config_default(CONFIG, "challenge", key="preference", default="none")
Expand Down Expand Up @@ -387,6 +388,11 @@ def has_valid_list(name: str) -> bool:
f"`{selection}` is not a valid `engine:{select}` value. "
f"Please choose from {valid_selections}.")

polyglot_section = CONFIG["engine"].get("polyglot") or {}
config_assert(polyglot_section.get("normalization") in ["none", "max", "sum"],
f"`{polyglot_section.get('normalization')}` is not a valid choice for "
f"`engine:polyglot:normalization`. Please choose from ['none', 'max', 'sum'].")

lichess_tbs_config = CONFIG["engine"].get("lichess_bot_tbs") or {}
quality_selections = ["best", "suggest"]
for tb in ["syzygy", "gaviota"]:
Expand Down
6 changes: 6 additions & 0 deletions lib/engine_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,12 @@ def get_book_move(board: chess.Board, game: model.Game,
try:
selection = polyglot_cfg.selection
min_weight = polyglot_cfg.min_weight
normalization = polyglot_cfg.normalization
weights = [entry.weight for entry in reader.find_all(board)]
scalar = (sum(weights) if normalization == "sum" and weights else
max(weights) if normalization == "max" and weights else 100)
min_weight = min_weight * scalar / 100

if selection == "weighted_random":
move = reader.weighted_choice(board).move
elif selection == "uniform_random":
Expand Down
3 changes: 2 additions & 1 deletion wiki/Configure-lichess-bot.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ will precede the `go` command to start thinking with `sd 5`. The other `go_comma
- `polyglot`: Tell lichess-bot whether your bot should use an opening book. Multiple books can be specified for each chess variant.
- `enabled`: Whether to use the book at all.
- `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.
- `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.
- `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.
- `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.
- `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.
- `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.
- `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:
1. `chessdb_book`: Consults a [Chinese chess position database](https://www.chessdb.cn/), which also hosts a xiangqi database.
2. `lichess_cloud_analysis`: Consults [Lichess's own position analysis database](https://lichess.org/api#operation/apiCloudEval).
Expand Down
Loading