Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f0b4e42f17 | ||
|
|
4e95413c4d | ||
|
|
19ea29b902 | ||
|
|
9fcef9c3c8 | ||
|
|
7ab07c8c71 | ||
|
|
7e3287be71 | ||
|
|
9642d606dc | ||
|
|
dc52b7846a | ||
|
|
0b18805f00 | ||
|
|
469ff7237c | ||
|
|
0f410353d1 | ||
|
|
b2eb9ca81a | ||
|
|
e4d114a763 | ||
|
|
68d7dcbccb | ||
|
|
7db4fcf59c | ||
|
|
00f0d06381 | ||
|
|
0f849c1b7f | ||
|
|
4c4e6d15c8 | ||
|
|
d1bcec29ce | ||
|
|
c4886db1fe | ||
|
|
caac437db7 | ||
|
|
de6b96bbf8 | ||
|
|
34018414df | ||
|
|
d4ed1a3f40 | ||
|
|
f9160084a7 | ||
|
|
04b1187963 | ||
|
|
f69da36c7c | ||
|
|
e3184752c0 | ||
|
|
cfcd24318b | ||
|
|
13cb768c67 | ||
|
|
372ab43e8b | ||
|
|
65b809d115 | ||
|
|
b239ce6bf0 | ||
|
|
e69294dfa3 | ||
|
|
454d9e61c1 | ||
|
|
1f2bb52418 | ||
|
|
e6ec17090b | ||
|
|
67c9e59939 | ||
|
|
f1df87a22d | ||
|
|
9c5335b4ef | ||
|
|
e98241cc7f | ||
|
|
740be80922 | ||
|
|
eb133efba1 | ||
|
|
cfe58552f2 | ||
|
|
36640bfe7e | ||
|
|
a6549e256a | ||
|
|
21a64294cf | ||
|
|
ba4ffca56f | ||
|
|
081d54dc55 | ||
|
|
64981dad3e | ||
|
|
a6472893a8 | ||
|
|
81a9a89ac8 | ||
|
|
f9e0f5db1a | ||
|
|
aafc6af697 | ||
|
|
82737f243f | ||
|
|
0250f9e954 | ||
|
|
b3ffc1f32f | ||
|
|
5f8e143cf8 | ||
|
|
2e84a1e3ab | ||
|
|
9d7567dfea | ||
|
|
c9f6ac3849 | ||
|
|
feb441c380 | ||
|
|
0a7be07d0f | ||
|
|
61da1c4d58 | ||
|
|
2bde715482 | ||
|
|
41b421ded5 | ||
|
|
baf0741e0d | ||
|
|
d99a35fdf0 | ||
|
|
2e952b0f92 | ||
|
|
b3c3182fec | ||
|
|
20196cb545 | ||
|
|
e33b88277b | ||
|
|
9885596770 | ||
|
|
83c9a2c51c | ||
|
|
ad7041c0a4 | ||
|
|
73d2945e95 | ||
|
|
b1b7b8234d | ||
|
|
8a2ada7165 | ||
|
|
506487f396 | ||
|
|
05ce50e647 | ||
|
|
c2f2730114 | ||
|
|
e06e1cdac1 | ||
|
|
94eced67f6 | ||
|
|
b5a34eda5e | ||
|
|
8b2c55eae4 | ||
|
|
aea6985817 | ||
|
|
e7731361cd | ||
|
|
7586608aaa | ||
|
|
1814cbc755 | ||
|
|
020132ea3c | ||
|
|
1bdedebf96 | ||
|
|
692ec62aac | ||
|
|
ce3ed3b6d1 | ||
|
|
345c6d21d9 | ||
|
|
0d126aa6a7 | ||
|
|
5ca125e336 | ||
|
|
855e5c2377 | ||
|
|
7e621311e6 | ||
|
|
b2a475ff5f | ||
|
|
4b3e8020fa | ||
|
|
194c3d3bc4 | ||
|
|
b929963f91 | ||
|
|
079959e6d4 | ||
|
|
64b2e3d9b1 | ||
|
|
eac21b424a | ||
|
|
cce6f5dd0c | ||
|
|
7e4d5889a8 | ||
|
|
71d3b3309a | ||
|
|
d662594814 | ||
|
|
9be74da127 | ||
|
|
85cdb7e077 | ||
|
|
a3d2f352d3 | ||
|
|
4dcf0d04a0 | ||
|
|
91b5062549 | ||
|
|
a7ef4ee4a9 | ||
|
|
2e802d7dfe | ||
|
|
81bd4648e3 | ||
|
|
48bbfa7114 | ||
|
|
320c43cdd3 | ||
|
|
a9b51e384f | ||
|
|
1fb235fb3f | ||
|
|
0f748da56c | ||
|
|
fd2168d529 | ||
|
|
41dabc2150 | ||
|
|
f114a6d0bb | ||
|
|
7cf8b381ce | ||
|
|
1f0b36764d | ||
|
|
2fd3cf0d2a | ||
|
|
931b6fa999 | ||
|
|
ba3505afc8 | ||
|
|
6ed9e882f8 | ||
|
|
0df0921c47 | ||
|
|
865eac5782 | ||
|
|
9421d254f7 | ||
|
|
d71e264bb9 | ||
|
|
031b5285ed | ||
|
|
9e5f131105 | ||
|
|
f4bf94eb98 | ||
|
|
75022da950 | ||
|
|
5bc4459111 | ||
|
|
65f7b3f38a |
No files matched your search
@@ -1,51 +1,52 @@
|
||||
# Define directories
|
||||
SRC_DIR := source
|
||||
OBJ_DIR := obj
|
||||
FMT_DIR := external/fmt
|
||||
|
||||
CXX := clang++
|
||||
EXE ?= Eleanor
|
||||
EVALFILE := ./nnue.bin
|
||||
|
||||
ifeq ($(OS),Windows_NT)
|
||||
UNAME_S := Windows
|
||||
EXE_EXT := .exe
|
||||
MKDIR := mkdir
|
||||
RM := rmdir /s /q
|
||||
DEL := del /q
|
||||
PLATFORM := windows
|
||||
UNAME_S := Windows
|
||||
EXE_EXT := .exe
|
||||
MKDIR := mkdir
|
||||
RM := rmdir /s /q
|
||||
DEL := del /q
|
||||
PLATFORM := windows
|
||||
CXX := g++
|
||||
else
|
||||
UNAME_S := $(shell uname -s)
|
||||
ARCH := $(shell uname -m)
|
||||
EXE_EXT :=
|
||||
MKDIR := mkdir -p
|
||||
RM := rm -rf
|
||||
DEL := rm -f
|
||||
ifeq ($(UNAME_S),Darwin)
|
||||
PLATFORM := mac
|
||||
else
|
||||
PLATFORM := unix
|
||||
endif
|
||||
UNAME_S := $(shell uname -s)
|
||||
ARCH := $(shell uname -m)
|
||||
EXE_EXT :=
|
||||
MKDIR := mkdir -p
|
||||
RM := rm -rf
|
||||
DEL := rm -f
|
||||
CXX := clang++
|
||||
ifeq ($(UNAME_S),Darwin)
|
||||
PLATFORM := mac
|
||||
else
|
||||
PLATFORM := unix
|
||||
endif
|
||||
endif
|
||||
|
||||
CXXFLAGS := -std=c++20 -O3 -flto -Wall
|
||||
LDFLAGS := -O3 -flto
|
||||
|
||||
ifeq ($(PLATFORM),mac)
|
||||
ifeq ($(ARCH),arm64)
|
||||
# Apple Silicon
|
||||
ARCH_FLAGS := -mcpu=apple-m1
|
||||
else
|
||||
ARCH_FLAGS := -march=native
|
||||
endif
|
||||
ifeq ($(ARCH),arm64)
|
||||
ARCH_FLAGS := -mcpu=apple-m1
|
||||
else
|
||||
ARCH_FLAGS := -march=native
|
||||
endif
|
||||
else ifeq ($(PLATFORM),windows)
|
||||
ARCH_FLAGS := -march=native
|
||||
LDFLAGS += -static -Wl,--stack,8388608
|
||||
else
|
||||
ifeq ($(ARCH),aarch64)
|
||||
# Generic Linux ARM64
|
||||
ARCH_FLAGS := -mcpu=native
|
||||
else
|
||||
ARCH_FLAGS := -march=native
|
||||
LDFLAGS += -fuse-ld=lld -static
|
||||
endif
|
||||
ifeq ($(ARCH),aarch64)
|
||||
ARCH_FLAGS := -mcpu=native
|
||||
else
|
||||
ARCH_FLAGS := -march=native
|
||||
LDFLAGS += -fuse-ld=lld -static
|
||||
endif
|
||||
endif
|
||||
|
||||
CXXFLAGS += $(ARCH_FLAGS)
|
||||
@@ -56,18 +57,12 @@ MAKEFLAGS += -j
|
||||
SRCS := $(wildcard $(SRC_DIR)/*.cpp)
|
||||
OBJS := $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(SRCS))
|
||||
|
||||
FMT_SRC := $(FMT_DIR)/format.cc
|
||||
FMT_OBJ := $(OBJ_DIR)/format.o
|
||||
|
||||
$(EXE)$(EXE_EXT): $(OBJS) $(FMT_OBJ)
|
||||
$(EXE)$(EXE_EXT): $(OBJS)
|
||||
$(CXX) $(CXXFLAGS) -DEVALFILE=\"$(EVALFILE)\" $^ -o $@ $(LDFLAGS)
|
||||
|
||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR)
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
$(FMT_OBJ): $(FMT_SRC) | $(OBJ_DIR)
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJ_DIR):
|
||||
$(MKDIR) $(OBJ_DIR)
|
||||
|
||||
@@ -80,4 +75,3 @@ else
|
||||
@$(RM) $(OBJ_DIR)
|
||||
@$(DEL) $(EXE)$(EXE_EXT)
|
||||
endif
|
||||
$(MAKE)
|
||||
@@ -14,7 +14,8 @@
|
||||
|
||||
| Version | Release Date | [CCRL 40/15](https://www.computerchess.org.uk/ccrl/4040/cgi/compare_engines.cgi?family=Eleanor&print=Rating+list&print=Results+table&print=LOS+table&print=Ponder+hit+table&print=Eval+difference+table&print=Comopp+gamenum+table&print=Overlap+table&print=Score+with+common+opponents) | [CCRL Blitz](https://www.computerchess.org.uk/ccrl/404/cgi/compare_engines.cgi?family=Eleanor&print=Rating+list&print=Results+table&print=LOS+table&print=Ponder+hit+table&print=Eval+difference+table&print=Comopp+gamenum+table&print=Overlap+table&print=Score+with+common+opponents) |
|
||||
| --- | --- | --- | --- |
|
||||
| 3.0 | 2025-12-05 | ? | ?
|
||||
| 4.0 | 2026-04-18 | - | -
|
||||
| 3.0 | 2025-12-05 | 3405 (#84) | 3495 (#79-80)
|
||||
| 2.0 | 2025-08-23 | 3312 (#115) | 3392 (#105)
|
||||
| 1.0 | 2025-06-02 | 2856 (#244) | 2759 (#256)
|
||||
|
||||
@@ -25,13 +26,13 @@
|
||||
|
||||
Eleanor uses a **fully neural network-based evaluation function**, entirely trained on self-play data from an initially simple HCE using the [bullet](https://github.com/jw1912/bullet) trainer.
|
||||
|
||||
**Architecture:** `(768hm -> 1536)x2 -> 1x8`
|
||||
A 1536-hidden-layer *perspective net* with 8 output buckets and horizontal mirroring.
|
||||
**Architecture:** `(768x8hm -> 1536)x2 -> 1x8`
|
||||
A 1536-hidden-layer *perspective net* with 8 input buckets, 8 output buckets and horizontal mirroring.
|
||||
|
||||
## Installation
|
||||
|
||||
> **Prerequisites:**
|
||||
> - `clang++` compiler
|
||||
> - `clang++` on linux, `g++` on windows
|
||||
> - Access to `make` command
|
||||
|
||||
1. Clone the repository
|
||||
@@ -70,4 +71,4 @@ The engine can be used in any GUI with **UCI support**.
|
||||
|
||||
**The project is a work in progress and actively developed. Contributions are highly appreciated.**
|
||||
|
||||
###### Last updated: 2025-12-05
|
||||
###### Last updated: 2026-04-18
|
||||
@@ -0,0 +1,96 @@
|
||||
#!/usr/bin/env python3
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
SCRIPT_DIR = Path(__file__).resolve().parent
|
||||
DEFAULT_INPUT = SCRIPT_DIR / "spsa_output.txt"
|
||||
DEFAULT_TARGET = SCRIPT_DIR.parent / "source" / "tunables.h"
|
||||
|
||||
VALUE_RE = re.compile(r"^\s*([A-Za-z0-9_]+)\s*,\s*([^\s,]+)\s*$")
|
||||
LINE_RE = re.compile(
|
||||
r"^(?P<indent>\s*)X_(?P<kind>INT|DOUBLE)\("
|
||||
r"(?P<name>[A-Za-z0-9_]+),\s*"
|
||||
r"(?P<default>[^,]+),\s*"
|
||||
r"(?P<rest>.+)\)\s*(?P<continuation>\\)?$"
|
||||
)
|
||||
|
||||
|
||||
def update_line(line: str, updated_values: dict[str, str]) -> tuple[str, bool]:
|
||||
match = LINE_RE.match(line)
|
||||
if not match:
|
||||
return line, False
|
||||
|
||||
name = match.group("name")
|
||||
if name not in updated_values:
|
||||
return line, False
|
||||
|
||||
continuation = " \\" if match.group("continuation") else ""
|
||||
new_line = (
|
||||
f"{match.group('indent')}X_{match.group('kind')}("
|
||||
f"{name}, {updated_values[name]}, {match.group('rest')}){continuation}\n"
|
||||
)
|
||||
return new_line, True
|
||||
|
||||
|
||||
def load_values(path: Path) -> dict[str, str]:
|
||||
values = {}
|
||||
|
||||
for line in path.read_text().splitlines():
|
||||
match = VALUE_RE.match(line)
|
||||
if not match:
|
||||
continue
|
||||
values[match.group(1)] = match.group(2)
|
||||
|
||||
return values
|
||||
|
||||
|
||||
def apply_updates(text: str, updated_values: dict[str, str]) -> tuple[str, set[str]]:
|
||||
updated_lines: list[str] = []
|
||||
seen: set[str] = set()
|
||||
|
||||
for line in text.splitlines(keepends=True):
|
||||
new_line, changed = update_line(line, updated_values)
|
||||
if changed:
|
||||
match = LINE_RE.match(line)
|
||||
if match:
|
||||
seen.add(match.group("name"))
|
||||
updated_lines.append(new_line)
|
||||
|
||||
return "".join(updated_lines), seen
|
||||
|
||||
|
||||
def main() -> int:
|
||||
input_path = Path(sys.argv[1]) if len(sys.argv) > 1 else DEFAULT_INPUT
|
||||
target = Path(sys.argv[2]) if len(sys.argv) > 2 else DEFAULT_TARGET
|
||||
|
||||
if not input_path.exists():
|
||||
print(f"Input file not found: {input_path}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
updated_values = load_values(input_path)
|
||||
if not updated_values:
|
||||
print(f"No tunable values found in {input_path}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if not target.exists():
|
||||
print(f"Target file not found: {target}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
text = target.read_text()
|
||||
updated_text, seen = apply_updates(text, updated_values)
|
||||
|
||||
missing = sorted(set(updated_values) - seen)
|
||||
if missing:
|
||||
print("Missing tunables:", ", ".join(missing), file=sys.stderr)
|
||||
return 1
|
||||
|
||||
target.write_text(updated_text)
|
||||
print(f"Updated {len(seen)} tunables in {target} from {input_path}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
+68
-53
@@ -1,59 +1,74 @@
|
||||
[Event "?"]
|
||||
[Site "?"]
|
||||
[Date "2025.09.04"]
|
||||
[Date "2026.03.26"]
|
||||
[Round "1"]
|
||||
[White "Eleanor-base"]
|
||||
[Black "Eleanor-dev"]
|
||||
[Result "1-0"]
|
||||
[FEN "rn1qk1nr/ppp2pRp/3bp3/3p4/3P4/2P2P1P/PP2PP2/RNBQKB2 b Qkq - 0 1"]
|
||||
[GameDuration "00:01:31"]
|
||||
[GameEndTime "2025-09-04T22:39:24.808 CEST"]
|
||||
[GameStartTime "2025-09-04T22:37:53.089 CEST"]
|
||||
[PlyCount "84"]
|
||||
[White "Eleanor-dev"]
|
||||
[Black "Eleanor-base"]
|
||||
[Result "0-1"]
|
||||
[FEN "rn1qk1nr/pbp1ppbp/1p1p2p1/3P4/5P2/2N1P3/PPPB2PP/R2QKBNR b KQkq - 0 1"]
|
||||
[GameDuration "00:00:14"]
|
||||
[GameEndTime "2026-03-26T20:42:15.476 CET"]
|
||||
[GameStartTime "2026-03-26T20:42:00.864 CET"]
|
||||
[PlyCount "115"]
|
||||
[SetUp "1"]
|
||||
[Termination "abandoned"]
|
||||
[TimeControl "37.56+0.38"]
|
||||
[TimeControl "5.17+0.05"]
|
||||
|
||||
1... Ne7 {-1.65 14/26 1861 1544983} 2. Rg1 {+1.64 12/23 2087 934687}
|
||||
c5 {-1.74 15/32 1565 1167061} 3. f4 {+1.61 13/27 2002 772197}
|
||||
Nd7 {-1.59 15/31 1455 1090717} 4. e3 {+1.84 14/31 1753 1387350}
|
||||
Qb6 {-1.65 14/31 1385 1273463} 5. a4 {+1.98 14/28 1505 1375155}
|
||||
Qc7 {-2.11 14/29 1849 830961} 6. dxc5 {+1.98 15/30 1571 1101192}
|
||||
Nxc5 {-2.16 15/34 1778 545375} 7. a5 {+1.90 14/29 1555 1021903}
|
||||
a6 {-2.22 15/33 1706 667700} 8. Nd2 {+2.38 14/29 1267 922229}
|
||||
O-O-O {-2.34 14/31 1641 580655} 9. b4 {+2.62 14/31 1463 975771}
|
||||
Ne4 {-2.37 15/39 1336 932258} 10. Nxe4 {+2.55 13/31 1579 228776}
|
||||
dxe4 {-2.85 15/31 1004 694847} 11. Bd2 {+3.02 13/30 1247 753167}
|
||||
Ng6 {-2.40 14/35 1118 862794} 12. Qb1 {+2.40 12/26 1475 339681}
|
||||
f5 {-1.97 13/31 1465 304169} 13. Qb3 {+2.25 12/34 957 597544}
|
||||
Nh4 {-2.21 14/38 1409 432849} 14. Be2 {+2.30 13/35 1343 843064}
|
||||
Nf3+ {-2.18 14/40 1355 318067} 15. Bxf3 {+2.49 14/39 1346 645681}
|
||||
exf3 {-2.59 15/30 1115 864898} 16. O-O-O {+2.85 14/35 1297 425564}
|
||||
Rhe8 {-2.11 13/34 1269 275858} 17. c4 {+2.35 13/30 1249 86929}
|
||||
e5 {-1.53 14/40 1188 1007892} 18. Bc3 {+2.71 12/37 1207 154226}
|
||||
exf4 {-0.71 13/45 1186 280646} 19. c5 {+0.71 13/34 883 618110}
|
||||
Be5 {-0.52 13/65 1145 440206} 20. Rxd8+ {+0.56 13/36 861 757974}
|
||||
Qxd8 {-0.55 15/56 1106 373775} 21. Bxe5 {+0.98 14/49 845 689690}
|
||||
Rxe5 {-0.87 14/83 799 511973} 22. exf4 {+0.98 13/36 1092 370730}
|
||||
Re2 {-0.87 13/89 751 367978} 23. Rg8 {+1.05 13/43 1056 334030}
|
||||
Re8 {-0.87 13/79 1030 242199} 24. Rxe8 {+1.13 12/34 1023 141620}
|
||||
Qxe8 {-0.87 13/41 998 22345} 25. Qd1 {+1.46 11/36 991 227470}
|
||||
Qe4 {-0.89 12/59 968 192246} 26. Qd2 {+1.17 13/42 856 592181}
|
||||
Qc4+ {-0.90 11/95 891 510827} 27. Kb2 {+1.46 12/38 937 154015}
|
||||
h5 {-1.01 11/66 913 404234} 28. Qc3 {+1.38 13/37 909 437481}
|
||||
Qe2+ {-0.73 10/76 886 55851} 29. Qc2 {+1.42 14/34 882 272787}
|
||||
Qe4 {-1.19 10/49 860 32225} 30. Kb3 {+1.27 12/37 824 598253}
|
||||
Qxf4 {-1.13 10/98 838 184366} 31. c6 {+1.24 12/51 734 627843}
|
||||
Qe4 {-0.96 10/103 814 271816} 32. cxb7+ {+1.24 13/50 817 324130}
|
||||
Kxb7 {-0.96 9/16 792 1604} 33. Qc3 {+1.24 14/30 796 171391}
|
||||
Qd5+ {-1.20 8/35 772 15385} 34. Ka3 {+1.23 11/40 525 374116}
|
||||
Qe4 {-1.25 8/85 752 9999} 35. Qd2 {+1.19 12/37 767 281732}
|
||||
Kc7 {-1.09 10/65 733 175302} 36. Qb2 {+1.21 12/40 748 316739}
|
||||
h4 {-1.09 7/48 715 39908} 37. Qc1+ {+1.19 11/38 529 523703}
|
||||
Kd8 {-0.65 10/83 700 298167} 38. Qc3 {+1.26 12/49 723 322911}
|
||||
Kd7 {-0.64 9/44 684 18906} 39. Qg7+ {+1.23 11/37 537 395773}
|
||||
Kc8 {-1.04 8/14 667 847} 40. Qf7 {+1.23 11/45 572 501521}
|
||||
Qd3+ {-1.04 7/81 653 28141} 41. Qb3 {+1.23 11/36 689 130089}
|
||||
Qe2 {-1.28 8/54 639 10579} 42. Qc3+ {+1.08 11/38 581 477548}
|
||||
Kd7 {-0.67 8/40 627 10552} 43. Qb2 {+1.08 11/33 663 104343, Black disconnects}
|
||||
1-0
|
||||
1... Nf6 {+0.25 16/24 256 274740} 2. Bb5+ {-0.31 14/28 286 222469}
|
||||
c6 {+0.30 14/30 154 175813} 3. dxc6 {-0.31 15/29 214 240246}
|
||||
Nxc6 {+0.29 15/25 271 56301} 4. Nf3 {-0.25 16/25 174 198800}
|
||||
O-O {+0.25 14/29 260 157287} 5. O-O {-0.28 15/26 245 287541}
|
||||
a6 {+0.24 17/35 224 246704} 6. Bd3 {-0.22 14/27 136 153559}
|
||||
b5 {+0.24 17/31 241 178951} 7. Qe1 {-0.24 14/28 245 274939}
|
||||
Qb6 {+0.25 16/25 230 190364} 8. a3 {-0.25 14/25 207 226160}
|
||||
e5 {+0.26 14/27 221 86623} 9. fxe5 {-0.10 14/27 158 172124}
|
||||
Nxe5 {+0.28 13/27 214 129429} 10. Nxe5 {-0.39 13/24 170 177476}
|
||||
dxe5 {+0.26 15/27 100 113069} 11. Kh1 {-0.18 13/23 121 136389}
|
||||
e4 {+0.32 13/18 202 26644} 12. Be2 {-0.19 15/28 182 213316}
|
||||
Rfc8 {+0.19 13/23 194 212538} 13. a4 {-0.12 13/24 207 126167}
|
||||
b4 {+0.22 13/25 187 192683} 14. a5 {-0.18 14/26 101 112261}
|
||||
Qd8 {+0.15 14/31 111 123537} 15. Na4 {-0.19 14/34 196 210792}
|
||||
Nd5 {+0.23 15/29 157 177113} 16. Qf2 {-0.22 14/25 188 135679}
|
||||
f5 {+0.22 15/28 130 130850} 17. c4 {-0.23 13/25 182 139155}
|
||||
bxc3 {+0.24 15/25 98 105292} 18. bxc3 {-0.24 13/25 175 94316}
|
||||
Bc6 {+0.23 14/17 166 18894} 19. Bc4 {-0.20 14/29 97 108937}
|
||||
Bxa4 {+0.20 13/24 81 86007} 20. Rxa4 {-0.21 16/30 85 101741}
|
||||
h5 {+0.25 13/20 159 95557} 21. Qe2 {-0.25 12/30 165 163762}
|
||||
Kh7 {+0.36 12/21 98 103093} 22. Rxf5 {-0.39 12/27 159 75820}
|
||||
gxf5 {+0.34 12/26 101 110999} 23. Qxh5+ {-0.45 13/38 107 119594}
|
||||
Kg8 {+0.33 13/23 149 16142} 24. Qxf5 {-0.39 14/26 74 84084}
|
||||
Rc5 {+0.40 13/23 144 140578} 25. Qe6+ {-0.37 12/36 150 138869}
|
||||
Kf8 {+0.53 14/27 138 120201} 26. Bc1 {-0.45 14/30 144 132855}
|
||||
Rb8 {+0.59 14/33 74 90173} 27. Ra1 {-0.55 14/36 130 123759}
|
||||
Qf6 {+0.63 14/35 82 98374} 28. Qxf6+ {-0.21 14/39 132 146058}
|
||||
Bxf6 {+0.61 15/35 100 125452} 29. Ba3 {-0.59 14/29 86 103103}
|
||||
Nb4 {+0.57 16/29 129 51381} 30. Bxb4 {-0.57 15/25 64 80650}
|
||||
Rxb4 {+0.53 14/29 91 125806} 31. cxb4 {-0.49 15/23 129 48919}
|
||||
Rxc4 {+0.47 15/27 122 47840} 32. Rb1 {-0.45 15/30 67 91596}
|
||||
Be7 {+0.46 14/24 92 126553} 33. g4 {-0.44 15/26 124 72397}
|
||||
Rxb4 {+0.49 14/25 73 87941} 34. Rf1+ {-0.49 14/28 121 72810}
|
||||
Ke8 {+0.45 15/36 89 113698} 35. Rf5 {-0.45 13/22 117 70248}
|
||||
Kd7 {+0.45 14/30 115 74784} 36. Re5 {-0.45 13/22 82 101380}
|
||||
Bd6 {+0.50 14/33 87 110879} 37. Rh5 {-0.45 12/34 113 72228}
|
||||
Rb1+ {+0.50 15/32 56 64801} 38. Kg2 {-0.46 14/29 74 94136}
|
||||
Rb2+ {+0.50 14/17 108 4246} 39. Kh1 {-0.48 15/25 108 48523}
|
||||
Re2 {+0.53 14/27 78 96242} 40. Rh7+ {-0.56 13/28 105 70703}
|
||||
Ke6 {+0.59 13/25 67 86840} 41. Rh6+ {-0.61 14/29 87 110057}
|
||||
Kd5 {+0.59 15/27 91 103932} 42. Rh5+ {-0.65 14/28 100 108417}
|
||||
Be5 {+0.70 14/33 94 110182} 43. Rh3 {-0.67 14/30 86 100550}
|
||||
Kc4 {+0.83 13/27 100 123700} 44. Rh5 {-0.75 12/24 96 56083}
|
||||
Bc3 {+0.88 12/26 47 63602} 45. Rh6 {-0.73 12/23 95 70966}
|
||||
Rxe3 {+0.92 12/31 96 115497} 46. h4 {-0.83 10/24 91 46076}
|
||||
Re1+ {+0.98 12/25 94 124212} 47. Kg2 {-0.92 13/29 45 57555}
|
||||
Bd4 {+0.94 14/28 86 116606} 48. Rxa6 {-0.93 13/26 90 42707}
|
||||
e3 {+0.95 13/23 90 44547} 49. Re6 {-0.92 12/40 88 86679}
|
||||
e2 {+0.99 13/23 88 36470} 50. a6 {-0.96 13/40 86 89628}
|
||||
Kd5 {+0.97 12/27 86 80236} 51. a7 {-0.97 12/30 84 17470}
|
||||
Bxa7 {+1.08 13/30 84 33077} 52. Re7 {-1.12 12/28 82 71784}
|
||||
Rg1+ {+1.17 13/28 53 70853} 53. Kf3 {-1.21 13/30 81 51340}
|
||||
e1=Q {+1.21 13/30 83 51082} 54. Rxe1 {-1.51 14/31 59 80154}
|
||||
Rxe1 {+1.25 13/29 41 59171} 55. h5 {-1.31 14/29 40 55864}
|
||||
Bd4 {+1.29 12/29 81 48179} 56. g5 {-1.23 11/26 80 30276}
|
||||
Rf1+ {+1.33 12/20 80 18531} 57. Kg2 {-3.99 12/39 78 76309}
|
||||
Rf5 {+1.56 12/26 79 58979} 58. g6 {-M18 12/38 76 76733}
|
||||
Ke4 {+M19 13/35 46 55731, White disconnects} 0-1
|
||||
@@ -0,0 +1,42 @@
|
||||
lmrBaseQuiet, 1.1029723689888005
|
||||
lmrDivisorQuiet, 2.3063513397139945
|
||||
lmrBaseNoisy, 0.451805556082857
|
||||
lmrDivisorNoisy, 3.1355330830404338
|
||||
lmrCutnode, 2005
|
||||
lmrIsPV, 982
|
||||
lmrTTPV, 936
|
||||
lmrHistoryDivisor, 8377
|
||||
lmrImproving, 1131
|
||||
lmrCorrplexity, 830
|
||||
lmrTTPVFailLow, 913
|
||||
lmrDeeperBase, 46
|
||||
rfpBase, 21
|
||||
rfpMargin, 97
|
||||
fpMargin, 86
|
||||
probcutBetaMargin, 190
|
||||
razoringScalar, 195
|
||||
doubleExtMargin, 32
|
||||
seeQuietThreshold, -81
|
||||
seeNoisyThreshold, -32
|
||||
aspInitialDelta, 26
|
||||
nmpBetaMargin, 25
|
||||
seePawnValue, 98
|
||||
seeKnightValue, 301
|
||||
seeBishopValue, 346
|
||||
seeRookValue, 507
|
||||
seeQueenValue, 924
|
||||
seeQsThreshold, -13
|
||||
seeOrderingThreshold, -102
|
||||
ldseMargin, 26
|
||||
historyBonusMultiplier, 306
|
||||
historyBonusSub, 252
|
||||
historyMalusMultiplier, 301
|
||||
historyMalusSub, 250
|
||||
contHistoryBonusMultiplier, 300
|
||||
contHistoryBonusSub, 252
|
||||
contHistoryMalusMultiplier, 300
|
||||
contHistoryMalusSub, 249
|
||||
captHistoryBonusMultiplier, 302
|
||||
captHistoryBonusSub, 249
|
||||
captHistoryMalusMultiplier, 300
|
||||
captHistoryMalusSub, 249
|
||||
Vendored
-2985
File diff suppressed because it is too large.
Load diff
Vendored
-1949
File diff suppressed because it is too large.
Load diff
Vendored
-4244
File diff suppressed because it is too large.
Load diff
Vendored
-46
@@ -1,46 +0,0 @@
|
||||
// Formatting library for C++
|
||||
//
|
||||
// Copyright (c) 2012 - 2016, Victor Zverovich
|
||||
// All rights reserved.
|
||||
//
|
||||
// For the license information refer to format.h.
|
||||
|
||||
#include "fmt/format-inl.h"
|
||||
|
||||
FMT_BEGIN_NAMESPACE
|
||||
namespace detail {
|
||||
|
||||
template FMT_API auto dragonbox::to_decimal(float x) noexcept
|
||||
-> dragonbox::decimal_fp<float>;
|
||||
template FMT_API auto dragonbox::to_decimal(double x) noexcept
|
||||
-> dragonbox::decimal_fp<double>;
|
||||
|
||||
#if FMT_USE_LOCALE
|
||||
// DEPRECATED! locale_ref in the detail namespace
|
||||
template FMT_API locale_ref::locale_ref(const std::locale& loc);
|
||||
template FMT_API auto locale_ref::get<std::locale>() const -> std::locale;
|
||||
#endif
|
||||
|
||||
// Explicit instantiations for char.
|
||||
|
||||
template FMT_API auto thousands_sep_impl(locale_ref)
|
||||
-> thousands_sep_result<char>;
|
||||
template FMT_API auto decimal_point_impl(locale_ref) -> char;
|
||||
|
||||
// DEPRECATED!
|
||||
template FMT_API void buffer<char>::append(const char*, const char*);
|
||||
|
||||
// DEPRECATED!
|
||||
template FMT_API void vformat_to(buffer<char>&, string_view,
|
||||
typename vformat_args<>::type, locale_ref);
|
||||
|
||||
// Explicit instantiations for wchar_t.
|
||||
|
||||
template FMT_API auto thousands_sep_impl(locale_ref)
|
||||
-> thousands_sep_result<wchar_t>;
|
||||
template FMT_API auto decimal_point_impl(locale_ref) -> wchar_t;
|
||||
|
||||
template FMT_API void buffer<wchar_t>::append(const wchar_t*, const wchar_t*);
|
||||
|
||||
} // namespace detail
|
||||
FMT_END_NAMESPACE
|
||||
+21
-21
@@ -18,7 +18,7 @@ int CalculateIndex(bool perspective, bool side, int pieceType, int square, bool
|
||||
}
|
||||
|
||||
// All friendly, for quiets
|
||||
void AccumulatorPair::addSub(bool stm, int add, int addPT, int sub, int subPT) {
|
||||
void AccumulatorPair::addSub(bool stm, int add, int addPT, int sub, int subPT, BucketPair bp) {
|
||||
int addW = CalculateIndex(White, stm, addPT, add, mirroredWhite);
|
||||
int addB = CalculateIndex(Black, stm, addPT, add, mirroredBlack);
|
||||
|
||||
@@ -26,16 +26,16 @@ void AccumulatorPair::addSub(bool stm, int add, int addPT, int sub, int subPT) {
|
||||
int subB = CalculateIndex(Black, stm, subPT, sub, mirroredBlack);
|
||||
|
||||
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
||||
white[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[subW * NNUE::HL_SIZE + i];
|
||||
white[i] += NNUE::net.accumulator_weights[bp.white][addW * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[bp.white][subW * NNUE::HL_SIZE + i];
|
||||
|
||||
black[i] += NNUE::net.accumulator_weights[addB * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[subB * NNUE::HL_SIZE + i];
|
||||
black[i] += NNUE::net.accumulator_weights[bp.black][addB * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[bp.black][subB * NNUE::HL_SIZE + i];
|
||||
}
|
||||
}
|
||||
|
||||
// Captures
|
||||
void AccumulatorPair::addSubSub(bool stm, int add, int addPT, int sub1, int subPT1, int sub2, int subPT2) {
|
||||
void AccumulatorPair::addSubSub(bool stm, int add, int addPT, int sub1, int subPT1, int sub2, int subPT2, BucketPair bp) {
|
||||
int addW = CalculateIndex(White, stm, addPT, add, mirroredWhite);
|
||||
int addB = CalculateIndex(Black, stm, addPT, add, mirroredBlack);
|
||||
|
||||
@@ -46,18 +46,18 @@ void AccumulatorPair::addSubSub(bool stm, int add, int addPT, int sub1, int subP
|
||||
int subB2 = CalculateIndex(Black, !stm, subPT2, sub2, mirroredBlack);
|
||||
|
||||
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
||||
white[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[subW1 * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[subW2 * NNUE::HL_SIZE + i];
|
||||
white[i] += NNUE::net.accumulator_weights[bp.white][addW * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[bp.white][subW1 * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[bp.white][subW2 * NNUE::HL_SIZE + i];
|
||||
|
||||
black[i] += NNUE::net.accumulator_weights[addB * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i];
|
||||
black[i] += NNUE::net.accumulator_weights[bp.black][addB * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[bp.black][subB1 * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[bp.black][subB2 * NNUE::HL_SIZE + i];
|
||||
}
|
||||
}
|
||||
|
||||
// Castling
|
||||
void AccumulatorPair::addAddSubSub(bool stm, int add1, int addPT1, int add2, int addPT2, int sub1, int subPT1, int sub2, int subPT2) {
|
||||
void AccumulatorPair::addAddSubSub(bool stm, int add1, int addPT1, int add2, int addPT2, int sub1, int subPT1, int sub2, int subPT2, BucketPair bp) {
|
||||
int addW1 = CalculateIndex(White, stm, addPT1, add1, mirroredWhite);
|
||||
int addB1 = CalculateIndex(Black, stm, addPT1, add1, mirroredBlack);
|
||||
|
||||
@@ -71,15 +71,15 @@ void AccumulatorPair::addAddSubSub(bool stm, int add1, int addPT1, int add2, int
|
||||
int subB2 = CalculateIndex(Black, stm, subPT2, sub2, mirroredBlack);
|
||||
|
||||
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
||||
white[i] += NNUE::net.accumulator_weights[addW1 * NNUE::HL_SIZE + i]
|
||||
+ NNUE::net.accumulator_weights[addW2 * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[subW1 * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[subW2 * NNUE::HL_SIZE + i];
|
||||
white[i] += NNUE::net.accumulator_weights[bp.white][addW1 * NNUE::HL_SIZE + i]
|
||||
+ NNUE::net.accumulator_weights[bp.white][addW2 * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[bp.white][subW1 * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[bp.white][subW2 * NNUE::HL_SIZE + i];
|
||||
|
||||
black[i] += NNUE::net.accumulator_weights[addB1 * NNUE::HL_SIZE + i]
|
||||
+ NNUE::net.accumulator_weights[addB2 * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i];
|
||||
black[i] += NNUE::net.accumulator_weights[bp.black][addB1 * NNUE::HL_SIZE + i]
|
||||
+ NNUE::net.accumulator_weights[bp.black][addB2 * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[bp.black][subB1 * NNUE::HL_SIZE + i]
|
||||
- NNUE::net.accumulator_weights[bp.black][subB2 * NNUE::HL_SIZE + i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,11 @@
|
||||
|
||||
namespace ACC {
|
||||
|
||||
struct BucketPair {
|
||||
int white;
|
||||
int black;
|
||||
};
|
||||
|
||||
using Accumulator = std::array<int16_t, NNUE::HL_SIZE>;
|
||||
|
||||
struct AccumulatorPair {
|
||||
@@ -13,9 +18,9 @@ struct AccumulatorPair {
|
||||
bool mirroredBlack = false;
|
||||
|
||||
// yoinked from Quinniboi - Prelude
|
||||
void addSub(bool stm, int add, int addPT, int sub, int subPT);
|
||||
void addSubSub(bool stm, int add, int addPT, int sub1, int subPT1, int sub2, int subPT2);
|
||||
void addAddSubSub(bool stm, int add1, int addPT1, int add2, int addPT2, int sub1, int subPT1, int sub2, int subPT2);
|
||||
void addSub(bool stm, int add, int addPT, int sub, int subPT, BucketPair bp);
|
||||
void addSubSub(bool stm, int add, int addPT, int sub1, int subPT1, int sub2, int subPT2, BucketPair bp);
|
||||
void addAddSubSub(bool stm, int add1, int addPT1, int add2, int addPT2, int sub1, int subPT1, int sub2, int subPT2, BucketPair bp);
|
||||
};
|
||||
|
||||
int CalculateIndex(bool perspective, bool side, int pieceType, int square, bool mirrored);
|
||||
|
||||
@@ -64,7 +64,9 @@ constexpr std::array<std::string_view, 50> fenPositions = {
|
||||
void RunBenchmark() {
|
||||
Board board;
|
||||
|
||||
TTable BenchTT;
|
||||
auto ctx = std::make_unique<SearchContext>();
|
||||
ctx->TT = &BenchTT;
|
||||
|
||||
Stopwatch sw;
|
||||
for (int i = 0; i < fenPositions.size(); i++) {
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
constexpr int BENCH_DEPTH = 13;
|
||||
constexpr int BENCH_DEPTH = 11;
|
||||
|
||||
void RunBenchmark();
|
||||
+465
-51
@@ -1,9 +1,13 @@
|
||||
#include "board.h"
|
||||
#include "accumulator.h"
|
||||
#include "movegen.h"
|
||||
#include "nnue.h"
|
||||
#include "tt.h"
|
||||
#include <iostream>
|
||||
#include <ranges>
|
||||
#include <string_view>
|
||||
#include <cassert>
|
||||
#include "types.h"
|
||||
#include "utils.h"
|
||||
|
||||
void Board::Reset() {
|
||||
@@ -16,6 +20,13 @@ void Board::Reset() {
|
||||
sideToMove = White;
|
||||
occupied = 0ULL;
|
||||
|
||||
checkers = 0ULL;
|
||||
|
||||
mailbox = std::array<int, 64>();
|
||||
mailbox.fill(nullPieceType);
|
||||
|
||||
pinned = std::array<Bitboard, 2>();
|
||||
|
||||
pieces = std::array<Bitboard, 6>();
|
||||
colors = std::array<Bitboard, 2>();
|
||||
|
||||
@@ -33,6 +44,9 @@ void Board::Reset() {
|
||||
hashKey = 0ULL;
|
||||
pawnKey = 0ULL;
|
||||
nonPawnKey = 0ULL;
|
||||
majorKey = 0ULL;
|
||||
|
||||
checkZones = std::array<Bitboard, 4>();
|
||||
}
|
||||
|
||||
void Board::SetByFen(std::string_view fen) {
|
||||
@@ -56,8 +70,7 @@ void Board::SetByFen(std::string_view fen) {
|
||||
bool side = std::islower(piece);
|
||||
int pieceType = pieceTypes.find(std::tolower(piece));
|
||||
|
||||
colors[side].SetBit(currSquare);
|
||||
pieces[pieceType].SetBit(currSquare);
|
||||
SetPiece(pieceType, currSquare, side);
|
||||
|
||||
currSquare++;
|
||||
}
|
||||
@@ -81,8 +94,15 @@ void Board::SetByFen(std::string_view fen) {
|
||||
fullMoves = std::stoi(tokens[5]);
|
||||
}
|
||||
|
||||
pawnKey = 0ULL;
|
||||
nonPawnKey = 0ULL;
|
||||
majorKey = 0ULL;
|
||||
|
||||
occupied = colors[White] | colors[Black];
|
||||
hashKey = UTILS::GetHashKey(*this);
|
||||
checkers = CalcCheckers();
|
||||
pinned = {CalcPinned(White), CalcPinned(Black)};
|
||||
CalcCheckers();
|
||||
MOVEGEN::GenThreatMaps(*this);
|
||||
MOVEGEN::GenerateMoves<All>(*this, true);
|
||||
|
||||
@@ -246,11 +266,11 @@ void Board::ResetMoves() {
|
||||
void Board::ListMoves() {
|
||||
int moveCount = 1;
|
||||
for (int i = 0; i < currentMoveIndex; i++) {
|
||||
Board copy = *this;
|
||||
bool isLegal = copy.MakeMove(moveList[i]);
|
||||
if (!IsLegal(moveList[i])) continue;
|
||||
|
||||
Board copy = *this;
|
||||
copy.MakeMove(moveList[i]);
|
||||
|
||||
if (!isLegal)
|
||||
continue;
|
||||
|
||||
std::cout << moveCount << ". ";
|
||||
moveList[i].PrintMove();
|
||||
@@ -261,11 +281,7 @@ void Board::ListMoves() {
|
||||
}
|
||||
|
||||
int Board::GetPieceType(int square) {
|
||||
for (int piece = Pawn; piece <= King; piece++) {
|
||||
if (pieces[piece].IsSet(square)) return piece;
|
||||
}
|
||||
|
||||
return nullPieceType;
|
||||
return mailbox[square];
|
||||
}
|
||||
|
||||
int Board::GetPieceColor(int square) {
|
||||
@@ -283,12 +299,18 @@ void Board::SetPiece(int piece, int square, bool color) {
|
||||
colors[color].SetBit(square);
|
||||
occupied.SetBit(square);
|
||||
|
||||
mailbox[square] = piece;
|
||||
|
||||
hashKey ^= UTILS::zKeys[color][piece][square];
|
||||
|
||||
if (piece == Pawn) {
|
||||
pawnKey ^= UTILS::zKeys[color][Pawn][square];
|
||||
} else {
|
||||
nonPawnKey ^= UTILS::zKeys[color][piece][square];
|
||||
|
||||
if (piece == King || piece == Rook || piece == Queen) {
|
||||
majorKey ^= UTILS::zKeys[color][piece][square];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -297,12 +319,18 @@ void Board::RemovePiece(int piece, int square, bool color) {
|
||||
colors[color].PopBit(square);
|
||||
occupied.PopBit(square);
|
||||
|
||||
mailbox[square] = nullPieceType;
|
||||
|
||||
hashKey ^= UTILS::zKeys[color][piece][square];
|
||||
|
||||
if (piece == Pawn) {
|
||||
pawnKey ^= UTILS::zKeys[color][Pawn][square];
|
||||
} else {
|
||||
nonPawnKey ^= UTILS::zKeys[color][piece][square];
|
||||
|
||||
if (piece == King || piece == Rook || piece == Queen) {
|
||||
majorKey ^= UTILS::zKeys[color][piece][square];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -342,8 +370,7 @@ void Board::Promote(int square, int pieceType, int color, bool isCapture) {
|
||||
SetPiece(pieceType, square, color);
|
||||
}
|
||||
|
||||
bool Board::MakeMove(Move move) {
|
||||
Board save = *this;
|
||||
void Board::MakeMove(Move move) {
|
||||
// Null Move
|
||||
if (!move) {
|
||||
int newEpTarget = noEPTarget;
|
||||
@@ -357,7 +384,7 @@ bool Board::MakeMove(Move move) {
|
||||
|
||||
enPassantTarget = newEpTarget;
|
||||
|
||||
return true;
|
||||
return;
|
||||
}
|
||||
|
||||
int newEpTarget = noEPTarget;
|
||||
@@ -370,30 +397,54 @@ bool Board::MakeMove(Move move) {
|
||||
|
||||
int endPiece = attackerPiece;
|
||||
|
||||
bool fullRecalc = false;
|
||||
const auto bucketPair = GetBuckets();
|
||||
|
||||
if (attackerPiece == King) {
|
||||
const int toFile = move.MoveTo() % 8;
|
||||
|
||||
if (attackerColor == White) {
|
||||
if ((toFile > 3) != accPair.mirroredWhite) {
|
||||
accPair.mirroredWhite = !accPair.mirroredWhite;
|
||||
fullRecalc = true;
|
||||
}
|
||||
} else {
|
||||
if ((toFile > 3) != accPair.mirroredBlack) {
|
||||
accPair.mirroredBlack = !accPair.mirroredBlack;
|
||||
fullRecalc = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (NNUE::kingBuckets[attackerColor][move.MoveFrom()] != NNUE::kingBuckets[attackerColor][move.MoveTo()]) {
|
||||
fullRecalc = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Removing attacker piece from old position
|
||||
RemovePiece(attackerPiece, move.MoveFrom(), attackerColor);
|
||||
|
||||
if (move.IsPromo()) {
|
||||
endPiece = move.GetPromoPiece();
|
||||
if (move.IsCapture()) {
|
||||
endPiece = move.GetFlags() - 9;
|
||||
Promote(move.MoveTo(), endPiece, sideToMove, true);
|
||||
} else {
|
||||
endPiece = move.GetFlags() - 5;
|
||||
Promote(move.MoveTo(), endPiece, sideToMove, false);
|
||||
}
|
||||
}
|
||||
|
||||
if (move.IsCapture()) {
|
||||
if (move.GetFlags() != epCapture) {
|
||||
accPair.addSubSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece, move.MoveTo(), targetPiece);
|
||||
} else {
|
||||
accPair.addSubSub(sideToMove, enPassantTarget, endPiece, move.MoveFrom(), attackerPiece, move.MoveTo() - direction, Pawn);
|
||||
}
|
||||
} else {
|
||||
if (move.GetFlags() != kingCastle && move.GetFlags() != queenCastle) {
|
||||
accPair.addSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece);
|
||||
}
|
||||
}
|
||||
if (!fullRecalc) {
|
||||
if (move.IsCapture()) {
|
||||
if (move.GetFlags() != epCapture) {
|
||||
accPair.addSubSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece, move.MoveTo(), targetPiece, bucketPair);
|
||||
} else {
|
||||
accPair.addSubSub(sideToMove, enPassantTarget, endPiece, move.MoveFrom(), attackerPiece, move.MoveTo() - direction, Pawn, bucketPair);
|
||||
}
|
||||
} else {
|
||||
if (move.GetFlags() != kingCastle && move.GetFlags() != queenCastle) {
|
||||
accPair.addSub(sideToMove, move.MoveTo(), endPiece, move.MoveFrom(), attackerPiece, bucketPair);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch (move.GetFlags())
|
||||
{
|
||||
@@ -427,7 +478,10 @@ bool Board::MakeMove(Move move) {
|
||||
|
||||
SetPiece(attackerPiece, move.MoveTo(), attackerColor);
|
||||
|
||||
accPair.addAddSubSub(sideToMove, move.MoveTo(), King, rookSquare - 2, Rook, move.MoveFrom(), King, rookSquare, Rook);
|
||||
if (!fullRecalc) {
|
||||
accPair.addAddSubSub(sideToMove, move.MoveTo(), King, rookSquare - 2, Rook, move.MoveFrom(), King, rookSquare, Rook, bucketPair);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case queenCastle:
|
||||
@@ -441,27 +495,19 @@ bool Board::MakeMove(Move move) {
|
||||
SetPiece(Rook, rookSquare + 3, attackerColor);
|
||||
|
||||
SetPiece(attackerPiece, move.MoveTo(), attackerColor);
|
||||
accPair.addAddSubSub(sideToMove, move.MoveTo(), King, rookSquare + 3, Rook, move.MoveFrom(), King, rookSquare, Rook);
|
||||
break;
|
||||
|
||||
if (!fullRecalc) {
|
||||
accPair.addAddSubSub(sideToMove, move.MoveTo(), King, rookSquare + 3, Rook, move.MoveFrom(), King, rookSquare, Rook, bucketPair);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (attackerPiece == King) {
|
||||
const int toFile = move.MoveTo() % 8;
|
||||
|
||||
if (attackerColor == White) {
|
||||
if ((toFile > 3) != accPair.mirroredWhite) {
|
||||
accPair.mirroredWhite = !accPair.mirroredWhite;
|
||||
ResetAccPair();
|
||||
}
|
||||
} else {
|
||||
if ((toFile > 3) != accPair.mirroredBlack) {
|
||||
accPair.mirroredBlack = !accPair.mirroredBlack;
|
||||
ResetAccPair();
|
||||
}
|
||||
}
|
||||
if (fullRecalc) {
|
||||
ResetAccPair();
|
||||
}
|
||||
|
||||
// Removing the right to castle on king and rook movement
|
||||
@@ -481,13 +527,17 @@ bool Board::MakeMove(Move move) {
|
||||
enPassantTarget = newEpTarget;
|
||||
|
||||
MOVEGEN::GenThreatMaps(*this);
|
||||
pinned = {CalcPinned(White), CalcPinned(Black)};
|
||||
checkers = CalcCheckers();
|
||||
CalcCheckZones();
|
||||
|
||||
/*
|
||||
sideToMove = !sideToMove;
|
||||
if (InCheck()) {
|
||||
*this = save;
|
||||
return false;
|
||||
}
|
||||
sideToMove = !sideToMove;
|
||||
*/
|
||||
|
||||
if (attackerColor == Black) fullMoves++;
|
||||
if (attackerPiece == Pawn || move.IsCapture()) {
|
||||
@@ -497,8 +547,6 @@ bool Board::MakeMove(Move move) {
|
||||
}
|
||||
|
||||
positionIndex++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Board::InPossibleZug() {
|
||||
@@ -512,6 +560,13 @@ bool Board::InPossibleZug() {
|
||||
return !toCheck;
|
||||
}
|
||||
|
||||
ACC::BucketPair Board::GetBuckets() {
|
||||
int wKingSq = (pieces[King] & colors[White]).getLS1BIndex();
|
||||
int bKingSq = (pieces[King] & colors[Black]).getLS1BIndex();
|
||||
|
||||
return {NNUE::kingBuckets[White][wKingSq],NNUE::kingBuckets[Black][bKingSq]};
|
||||
}
|
||||
|
||||
void Board::ResetAccPair() {
|
||||
Bitboard whitePieces = colors[White];
|
||||
Bitboard blackPieces = colors[Black];
|
||||
@@ -519,6 +574,8 @@ void Board::ResetAccPair() {
|
||||
accPair.white = NNUE::net.accumulator_biases;
|
||||
accPair.black = NNUE::net.accumulator_biases;
|
||||
|
||||
const auto bucketPair = GetBuckets();
|
||||
|
||||
while (whitePieces) {
|
||||
int square = whitePieces.getLS1BIndex();
|
||||
|
||||
@@ -526,8 +583,8 @@ void Board::ResetAccPair() {
|
||||
int bInput = ACC::CalculateIndex(Black, White, GetPieceType(square), square, accPair.mirroredBlack);
|
||||
|
||||
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
||||
accPair.white[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i];
|
||||
accPair.black[i] += NNUE::net.accumulator_weights[bInput * NNUE::HL_SIZE + i];
|
||||
accPair.white[i] += NNUE::net.accumulator_weights[bucketPair.white][wInput * NNUE::HL_SIZE + i];
|
||||
accPair.black[i] += NNUE::net.accumulator_weights[bucketPair.black][bInput * NNUE::HL_SIZE + i];
|
||||
}
|
||||
|
||||
whitePieces.PopBit(square);
|
||||
@@ -540,8 +597,8 @@ void Board::ResetAccPair() {
|
||||
int bInput = ACC::CalculateIndex(Black, Black, GetPieceType(square), square, accPair.mirroredBlack);
|
||||
|
||||
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
||||
accPair.white[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i];
|
||||
accPair.black[i] += NNUE::net.accumulator_weights[bInput * NNUE::HL_SIZE + i];
|
||||
accPair.white[i] += NNUE::net.accumulator_weights[bucketPair.white][wInput * NNUE::HL_SIZE + i];
|
||||
accPair.black[i] += NNUE::net.accumulator_weights[bucketPair.black][bInput * NNUE::HL_SIZE + i];
|
||||
}
|
||||
|
||||
blackPieces.PopBit(square);
|
||||
@@ -577,6 +634,363 @@ Bitboard Board::AttacksTo(int square, Bitboard occupancy) {
|
||||
return attacks;
|
||||
}
|
||||
|
||||
static Bitboard rayBetween(int sq1, int sq2) {
|
||||
if (sq1 == sq2) return 0ULL;
|
||||
|
||||
int f1 = sq1 % 8, r1 = sq1 / 8;
|
||||
int f2 = sq2 % 8, r2 = sq2 / 8;
|
||||
|
||||
int df = (f2 > f1) - (f2 < f1);
|
||||
int dr = (r2 > r1) - (r2 < r1);
|
||||
|
||||
// must be same rank, file, or diagonal
|
||||
if (df != 0 && dr != 0 && std::abs(f2 - f1) != std::abs(r2 - r1))
|
||||
return 0ULL;
|
||||
|
||||
Bitboard ray = 0ULL;
|
||||
|
||||
int f = f1 + df;
|
||||
int r = r1 + dr;
|
||||
|
||||
while (f != f2 || r != r2) {
|
||||
ray |= 1ULL << (r * 8 + f);
|
||||
f += df;
|
||||
r += dr;
|
||||
}
|
||||
|
||||
ray &= ~((1ULL << sq1) | (1ULL << sq2));
|
||||
|
||||
return ray;
|
||||
}
|
||||
|
||||
// Returns the full ray from `from` in the direction of `to`, to the edge of the board
|
||||
static Bitboard fullRay(int from, int to) {
|
||||
int df = (to % 8) - (from % 8);
|
||||
int dr = (to / 8) - (from / 8);
|
||||
|
||||
if (df != 0) df /= std::abs(df);
|
||||
if (dr != 0) dr /= std::abs(dr);
|
||||
|
||||
Bitboard ray = 0ULL;
|
||||
|
||||
int f = from % 8 + df;
|
||||
int r = from / 8 + dr;
|
||||
while (f >= 0 && f < 8 && r >= 0 && r < 8) {
|
||||
ray |= 1ULL << (r * 8 + f);
|
||||
f += df;
|
||||
r += dr;
|
||||
}
|
||||
|
||||
f = from % 8 - df;
|
||||
r = from / 8 - dr;
|
||||
while (f >= 0 && f < 8 && r >= 0 && r < 8) {
|
||||
ray |= 1ULL << (r * 8 + f);
|
||||
f -= df;
|
||||
r -= dr;
|
||||
}
|
||||
|
||||
ray |= 1ULL << from;
|
||||
return ray;
|
||||
}
|
||||
|
||||
bool Board::IsSquareThreatened(bool side, int square) {
|
||||
return colorThreats[!side].IsSet(square);
|
||||
}
|
||||
|
||||
Bitboard Board::CalcCheckers() {
|
||||
return AttacksTo((colors[sideToMove] & pieces[King]).getLS1BIndex(), occupied) & colors[!sideToMove];
|
||||
}
|
||||
|
||||
Bitboard Board::CalcPinned(bool color) {
|
||||
Bitboard pinned;
|
||||
|
||||
int kingSquare = (pieces[King] & colors[color]).getLS1BIndex();
|
||||
|
||||
assert(kingSquare != -1);
|
||||
|
||||
Bitboard oppQueens = pieces[Queen] & colors[!color];
|
||||
|
||||
Bitboard potentialAttackers = MOVEGEN::getBishopAttack(kingSquare, colors[!color]) & (oppQueens | (pieces[Bishop] & colors[!color]))
|
||||
| MOVEGEN::getRookAttack(kingSquare, colors[!color]) & (oppQueens | (pieces[Rook] & colors[!color]));
|
||||
|
||||
while (potentialAttackers) {
|
||||
int attackerSquare = potentialAttackers.getLS1BIndex();
|
||||
|
||||
Bitboard isPinned = colors[color] & rayBetween(attackerSquare, kingSquare);
|
||||
|
||||
if (isPinned.PopCount() == 1)
|
||||
pinned |= isPinned;
|
||||
|
||||
potentialAttackers.PopBit(attackerSquare);
|
||||
}
|
||||
|
||||
return pinned;
|
||||
}
|
||||
|
||||
bool Board::IsLegal(Move &move) {
|
||||
assert(move != 0);
|
||||
|
||||
int us = sideToMove;
|
||||
int them = !us;
|
||||
|
||||
int from = move.MoveFrom();
|
||||
int to = move.MoveTo();
|
||||
|
||||
Bitboard king = pieces[King] & colors[us];
|
||||
int kingSquare = king.getLS1BIndex();
|
||||
|
||||
int moveType = move.GetFlags();
|
||||
|
||||
if (moveType == epCapture) {
|
||||
int epCapturedSquare = to + (us == White ? -8 : 8);
|
||||
Bitboard occAfterEP = occupied ^ Bitboard::GetSquare(from) ^ Bitboard::GetSquare(to)
|
||||
^ Bitboard::GetSquare(epCapturedSquare);
|
||||
|
||||
Bitboard theirQueens = pieces[Queen] & colors[them];
|
||||
|
||||
return (MOVEGEN::getBishopAttack(kingSquare, occAfterEP) & (theirQueens | (pieces[Bishop] & colors[them]))).PopCount() < 1
|
||||
&& (MOVEGEN::getRookAttack(kingSquare, occAfterEP) & (theirQueens | (pieces[Rook] & colors[them]))).PopCount() < 1;
|
||||
}
|
||||
|
||||
int movingPiece = GetPieceType(from);
|
||||
|
||||
if (movingPiece == King) {
|
||||
Bitboard kinglessOcc = occupied ^ king;
|
||||
Bitboard theirQueens = pieces[Queen] & colors[them];
|
||||
|
||||
return !colorThreats[them].IsSet(to)
|
||||
&& (MOVEGEN::getBishopAttack(to, kinglessOcc) & (theirQueens | (pieces[Bishop] & colors[them]))).PopCount() == 0
|
||||
&& (MOVEGEN::getRookAttack(to, kinglessOcc) & (theirQueens | (pieces[Rook] & colors[them]))).PopCount() == 0;
|
||||
}
|
||||
|
||||
if (checkers.PopCount() > 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pinned[us].IsSet(from) && !fullRay(kingSquare, from).IsSet(to))
|
||||
return false;
|
||||
|
||||
if (checkers.PopCount() < 1)
|
||||
return true;
|
||||
|
||||
int checkerSquare = checkers.getLS1BIndex();
|
||||
|
||||
return (rayBetween(kingSquare, checkerSquare) | checkers).IsSet(to);
|
||||
}
|
||||
|
||||
bool Board::IsPseudoLegal(Move &move) {
|
||||
if (!move) return true;
|
||||
|
||||
const int from = move.MoveFrom();
|
||||
const int to = move.MoveTo();
|
||||
const int flag = move.GetFlags();
|
||||
|
||||
// basic bounds
|
||||
if ((unsigned)from >= 64u || (unsigned)to >= 64u) return false;
|
||||
|
||||
const int us = sideToMove;
|
||||
const int them = !sideToMove;
|
||||
|
||||
const int movingPiece = GetPieceType(from);
|
||||
if (movingPiece == nullPieceType) return false;
|
||||
if (!colors[us].IsSet(from)) return false;
|
||||
if (colors[us].IsSet(to)) return false;
|
||||
|
||||
const bool isCapture = move.IsCapture();
|
||||
const int targetPiece = GetPieceType(to);
|
||||
|
||||
// capture consistency (except EP which captures "behind")
|
||||
if (flag == capture) {
|
||||
if (targetPiece == nullPieceType) return false;
|
||||
if (!colors[them].IsSet(to)) return false;
|
||||
} else if (isCapture && flag != epCapture) {
|
||||
// promo-capture also lands on occupied enemy square
|
||||
if (targetPiece == nullPieceType) return false;
|
||||
if (!colors[them].IsSet(to)) return false;
|
||||
} else if (!isCapture) {
|
||||
// quiet moves must not land on occupied square
|
||||
if (occupied.IsSet(to)) {
|
||||
// except castling (king lands on empty anyway)
|
||||
if (flag != kingCastle && flag != queenCastle) return false;
|
||||
}
|
||||
}
|
||||
|
||||
const int fromFile = from & 7;
|
||||
const int toFile = to & 7;
|
||||
const int fromRank = from >> 3;
|
||||
const int toRank = to >> 3;
|
||||
|
||||
const int dir = us ? south : north;
|
||||
|
||||
auto isOneStepPawnPush = [&] {
|
||||
return to == from + dir && !occupied.IsSet(to);
|
||||
};
|
||||
|
||||
auto isPawnCaptureTo = [&] {
|
||||
// diagonal one step forward
|
||||
return (to == from + dir - 1 && toFile == fromFile - 1)
|
||||
|| (to == from + dir + 1 && toFile == fromFile + 1);
|
||||
};
|
||||
|
||||
// handle specials by flag first
|
||||
switch (flag) {
|
||||
case quiet:
|
||||
case capture:
|
||||
break;
|
||||
|
||||
case doublePawnPush: {
|
||||
if (movingPiece != Pawn) return false;
|
||||
// from must be on starting rank
|
||||
if (!us) { // white
|
||||
if (fromRank != 1) return false;
|
||||
} else { // black
|
||||
if (fromRank != 6) return false;
|
||||
}
|
||||
const int mid = from + dir;
|
||||
if (to != from + 2 * dir) return false;
|
||||
if (occupied.IsSet(mid) || occupied.IsSet(to)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
case epCapture: {
|
||||
if (movingPiece != Pawn) return false;
|
||||
if (enPassantTarget == noEPTarget) return false;
|
||||
if (to != enPassantTarget) return false;
|
||||
if (!isPawnCaptureTo()) return false;
|
||||
|
||||
// target square is empty, captured pawn is behind to
|
||||
const int capSq = to - dir;
|
||||
if (!colors[them].IsSet(capSq)) return false;
|
||||
if (GetPieceType(capSq) != Pawn) return false;
|
||||
if (occupied.IsSet(to)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
case kingCastle:
|
||||
case queenCastle: {
|
||||
if (movingPiece != King) return false;
|
||||
|
||||
const int kingSquare = (colors[us] & pieces[King]).getLS1BIndex();
|
||||
if (from != kingSquare) return false;
|
||||
|
||||
const int kingRight = us ? blackKingRight : whiteKingRight;
|
||||
const int queenRight = us ? blackQueenRight : whiteQueenRight;
|
||||
|
||||
if (flag == kingCastle) {
|
||||
if (!(castlingRights & kingRight)) return false;
|
||||
const U64 KingSide = us ? 0xf000000000000000ULL : 0xf0ULL;
|
||||
const U64 mask = us ? 0x7000000000000000ULL : 0x70ULL;
|
||||
const int targetSq = us ? g8 : g1;
|
||||
if (to != targetSq) return false;
|
||||
|
||||
// must be exactly king+rook on that side and no attacked transit squares
|
||||
if ((Bitboard(KingSide) & occupied).PopCount() != 2) return false;
|
||||
if (mask & colorThreats[them]) return false;
|
||||
return true;
|
||||
} else {
|
||||
if (!(castlingRights & queenRight)) return false;
|
||||
const U64 QueenSide = us ? 0x1f00000000000000ULL : 0x1fULL;
|
||||
const U64 mask = us ? 0x1c00000000000000ULL : 0x1cULL;
|
||||
const int targetSq = us ? c8 : c1;
|
||||
if (to != targetSq) return false;
|
||||
|
||||
if ((Bitboard(QueenSide) & occupied).PopCount() != 2) return false;
|
||||
if (mask & colorThreats[them]) return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
// promotions (quiet + capture)
|
||||
if (!move.IsPromo()) return false;
|
||||
if (movingPiece != Pawn) return false;
|
||||
|
||||
// must land on last rank
|
||||
if (!us) {
|
||||
if (toRank != 7) return false;
|
||||
} else {
|
||||
if (toRank != 0) return false;
|
||||
}
|
||||
|
||||
if (move.IsCapture()) {
|
||||
if (!isPawnCaptureTo()) return false;
|
||||
if (!colors[them].IsSet(to)) return false;
|
||||
} else {
|
||||
if (!isOneStepPawnPush()) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// normal piece movement checks for quiet/capture (non-special)
|
||||
switch (movingPiece) {
|
||||
case Pawn: {
|
||||
// non-promo pawns cannot land on last rank
|
||||
if (!us) { if (toRank == 7) return false; }
|
||||
else { if (toRank == 0) return false; }
|
||||
|
||||
if (move.IsCapture()) {
|
||||
if (!isPawnCaptureTo()) return false;
|
||||
// (regular capture already checked occupancy/enemy above)
|
||||
return true;
|
||||
} else {
|
||||
return isOneStepPawnPush();
|
||||
}
|
||||
}
|
||||
|
||||
case Knight:
|
||||
return (MOVEGEN::knightAttacks[from] & Bitboard::GetSquare(to)) != 0;
|
||||
|
||||
case Bishop: {
|
||||
Bitboard att = MOVEGEN::getBishopAttack(from, occupied);
|
||||
return (att & Bitboard::GetSquare(to)) != 0;
|
||||
}
|
||||
|
||||
case Rook: {
|
||||
Bitboard att = MOVEGEN::getRookAttack(from, occupied);
|
||||
return (att & Bitboard::GetSquare(to)) != 0;
|
||||
}
|
||||
|
||||
case Queen: {
|
||||
Bitboard att = MOVEGEN::getBishopAttack(from, occupied) | MOVEGEN::getRookAttack(from, occupied);
|
||||
return (att & Bitboard::GetSquare(to)) != 0;
|
||||
}
|
||||
|
||||
case King:
|
||||
return (MOVEGEN::kingAttacks[from] & Bitboard::GetSquare(to)) != 0;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void Board::CalcCheckZones() {
|
||||
int oppKingSquare = (colors[!sideToMove] & pieces[King]).getLS1BIndex();
|
||||
|
||||
checkZones[0] = MOVEGEN::pawnAttacks[!sideToMove][oppKingSquare];
|
||||
checkZones[1] = MOVEGEN::knightAttacks[oppKingSquare];
|
||||
checkZones[2] = MOVEGEN::getBishopAttack(oppKingSquare, occupied);
|
||||
checkZones[3] = MOVEGEN::getRookAttack(oppKingSquare, occupied);
|
||||
}
|
||||
|
||||
bool Board::GivesDirectCheck(Move &move) {
|
||||
int attackerType = nullPieceType;
|
||||
|
||||
if (move.IsPromo()) {
|
||||
attackerType = move.GetPromoPiece();
|
||||
} else {
|
||||
attackerType = GetPieceType(move.MoveFrom());
|
||||
}
|
||||
|
||||
if (attackerType == King)
|
||||
return false;
|
||||
|
||||
Bitboard checkZone = 0ULL;
|
||||
|
||||
if (attackerType == Queen) {
|
||||
checkZone = checkZones[Bishop] | checkZones[Rook];
|
||||
} else {
|
||||
checkZone = checkZones[attackerType];
|
||||
}
|
||||
|
||||
return checkZone.IsSet(move.MoveTo());
|
||||
}
|
||||
+20
-2
@@ -16,11 +16,16 @@ public:
|
||||
SetByFen(StartingFen);
|
||||
}
|
||||
|
||||
std::array<int, 64> mailbox;
|
||||
|
||||
std::array<Bitboard, 6> pieces;
|
||||
std::array<Bitboard, 2> colors;
|
||||
|
||||
Bitboard occupied;
|
||||
|
||||
std::array<Bitboard, 2> pinned;
|
||||
Bitboard checkers;
|
||||
|
||||
ACC::AccumulatorPair accPair;
|
||||
|
||||
std::array<Bitboard, 6> pieceThreats;
|
||||
@@ -42,6 +47,9 @@ public:
|
||||
U64 hashKey = 0ULL;
|
||||
U64 pawnKey = 0ULL;
|
||||
U64 nonPawnKey = 0ULL;
|
||||
U64 majorKey = 0ULL;
|
||||
|
||||
std::array<Bitboard, 4> checkZones;
|
||||
|
||||
void Reset();
|
||||
void SetByFen(std::string_view fen);
|
||||
@@ -54,6 +62,7 @@ public:
|
||||
void ResetMoves();
|
||||
void ListMoves();
|
||||
|
||||
ACC::BucketPair GetBuckets();
|
||||
void ResetAccPair();
|
||||
|
||||
int GetPieceType(int square);
|
||||
@@ -65,11 +74,20 @@ public:
|
||||
void RemovePiece(int piece, int square, bool color);
|
||||
|
||||
void Promote(int square, int pieceType, int color, bool isCapture);
|
||||
bool MakeMove(Move move);
|
||||
void MakeMove(Move move);
|
||||
|
||||
bool InPossibleZug();
|
||||
|
||||
Bitboard AttacksTo(int square, Bitboard occupancy);
|
||||
|
||||
bool IsSquareThreatened(bool side, int square);
|
||||
};
|
||||
|
||||
Bitboard CalcCheckers();
|
||||
Bitboard CalcPinned(bool color);
|
||||
|
||||
void CalcCheckZones();
|
||||
bool GivesDirectCheck(Move &move);
|
||||
|
||||
bool IsLegal(Move &move);
|
||||
bool IsPseudoLegal(Move &move);
|
||||
};
|
||||
+262
-181
@@ -6,29 +6,61 @@
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <csignal>
|
||||
#include <cstdlib>
|
||||
#include <cerrno>
|
||||
|
||||
#include "datagen.h"
|
||||
#include "movegen.h"
|
||||
#include "search.h"
|
||||
#include "movegen.h"
|
||||
#include "utils.h"
|
||||
|
||||
// OS-dependent threading includes
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <io.h> // for _commit
|
||||
#include <io.h>
|
||||
#include <process.h>
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#include <unistd.h> // for sync
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
static std::filesystem::path MakeTempPath(const std::string& prefix, const std::string& ext) {
|
||||
namespace fs = std::filesystem;
|
||||
fs::path dir = fs::temp_directory_path();
|
||||
|
||||
std::random_device rd;
|
||||
std::mt19937_64 gen(rd());
|
||||
std::uniform_int_distribution<uint64_t> dist;
|
||||
auto name = prefix + std::to_string(dist(gen)) + ext;
|
||||
|
||||
return dir / name;
|
||||
}
|
||||
|
||||
static std::string ShellQuote(const std::string& s) {
|
||||
#ifdef _WIN32
|
||||
std::string out = "\"";
|
||||
for (char c : s) {
|
||||
if (c == '"') out += "\\\"";
|
||||
else out += c;
|
||||
}
|
||||
out += "\"";
|
||||
return out;
|
||||
#else
|
||||
std::string out = "'";
|
||||
for (char c : s) {
|
||||
if (c == '\'') out += "'\\''";
|
||||
else out += c;
|
||||
}
|
||||
out += "'";
|
||||
return out;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
static void HideCursor() {
|
||||
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
@@ -46,63 +78,75 @@ static void ShowCursor() {
|
||||
SetConsoleCursorInfo(hConsole, &cursorInfo);
|
||||
}
|
||||
#else
|
||||
static void HideCursor() {
|
||||
std::cout << "\033[?25l";
|
||||
}
|
||||
|
||||
static void ShowCursor() {
|
||||
std::cout << "\033[?25h";
|
||||
}
|
||||
static void HideCursor() { std::cout << "\033[?25l"; }
|
||||
static void ShowCursor() { std::cout << "\033[?25h"; }
|
||||
#endif
|
||||
|
||||
namespace DATAGEN {
|
||||
|
||||
// ============================================================================
|
||||
// HTTP CLIENT FUNCTIONS (using system curl commands)
|
||||
// ============================================================================
|
||||
|
||||
static bool PostJSON(const std::string& url, const std::string& jsonBody, std::string& response) {
|
||||
std::string tempFile = "/tmp/datagen_request.json";
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
fs::path tempFile = MakeTempPath("datagen_request_", ".json");
|
||||
fs::path responseFile = MakeTempPath("datagen_response_", ".txt");
|
||||
|
||||
std::ofstream out(tempFile);
|
||||
if (!out) {
|
||||
std::cerr << "Failed to create temp file for request" << std::endl;
|
||||
std::cerr << "Failed to create temp file for request: " << tempFile.string() << "\n";
|
||||
return false;
|
||||
}
|
||||
out << jsonBody;
|
||||
out.close();
|
||||
|
||||
std::string responseFile = "/tmp/datagen_response.txt";
|
||||
std::ostringstream cmd;
|
||||
cmd << "curl -s -X POST \"" << url << "\" "
|
||||
<< "-H \"Content-Type: application/json\" "
|
||||
<< "-d @" << tempFile << " "
|
||||
<< "-o " << responseFile << " "
|
||||
<< "2>/dev/null";
|
||||
cmd << "curl -s -X POST "
|
||||
<< ShellQuote(url) << " "
|
||||
<< "-H " << ShellQuote("Content-Type: application/json") << " "
|
||||
<< "-d " << ShellQuote("@" + tempFile.string()) << " "
|
||||
<< "-o " << ShellQuote(responseFile.string()) << " ";
|
||||
|
||||
#ifdef _WIN32
|
||||
cmd << "2>nul";
|
||||
#else
|
||||
cmd << "2>/dev/null";
|
||||
#endif
|
||||
|
||||
int result = system(cmd.str().c_str());
|
||||
|
||||
|
||||
std::ifstream responseIn(responseFile);
|
||||
if (responseIn) {
|
||||
std::stringstream buffer;
|
||||
buffer << responseIn.rdbuf();
|
||||
response = buffer.str();
|
||||
responseIn.close();
|
||||
} else {
|
||||
response.clear();
|
||||
}
|
||||
|
||||
std::remove(tempFile.c_str());
|
||||
std::remove(responseFile.c_str());
|
||||
std::error_code ec;
|
||||
fs::remove(tempFile, ec);
|
||||
fs::remove(responseFile, ec);
|
||||
|
||||
return (result == 0);
|
||||
}
|
||||
|
||||
static bool UploadFile(const std::string& url, const std::string& filepath,
|
||||
const std::string& username, int positions) {
|
||||
static bool UploadFile(const std::string& url,
|
||||
const std::string& filepath,
|
||||
const std::string& username,
|
||||
int positions) {
|
||||
std::ostringstream cmd;
|
||||
cmd << "curl -s -X POST \"" << url << "\" "
|
||||
<< "-F \"file=@" << filepath << "\" "
|
||||
<< "-F \"user=" << username << "\" "
|
||||
<< "-F \"positions=" << positions << "\" "
|
||||
<< "2>/dev/null";
|
||||
|
||||
cmd << "curl -s -X POST "
|
||||
<< ShellQuote(url) << " "
|
||||
<< "-F " << ShellQuote("file=@" + filepath) << " "
|
||||
<< "-F " << ShellQuote("user=" + username) << " "
|
||||
<< "-F " << ShellQuote("positions=" + std::to_string(positions)) << " ";
|
||||
|
||||
#ifdef _WIN32
|
||||
cmd << "2>nul";
|
||||
#else
|
||||
cmd << "2>/dev/null";
|
||||
#endif
|
||||
|
||||
int result = system(cmd.str().c_str());
|
||||
return (result == 0);
|
||||
@@ -126,30 +170,24 @@ static std::string BuildHeartbeatJSON(const std::string& username, int threads)
|
||||
return json.str();
|
||||
}
|
||||
|
||||
// Simple JSON value extractor
|
||||
static std::string ExtractJSONValue(const std::string& json, const std::string& key) {
|
||||
// Look for "key":"value" or "key":value pattern
|
||||
std::string searchKey = "\"" + key + "\":";
|
||||
size_t pos = json.find(searchKey);
|
||||
if (pos == std::string::npos) return "";
|
||||
|
||||
|
||||
pos += searchKey.length();
|
||||
|
||||
// Skip whitespace
|
||||
|
||||
while (pos < json.length() && (json[pos] == ' ' || json[pos] == '\t' || json[pos] == '\n')) {
|
||||
pos++;
|
||||
}
|
||||
|
||||
if (pos >= json.length()) return "";
|
||||
|
||||
// Check if value is quoted
|
||||
|
||||
if (json[pos] == '"') {
|
||||
pos++; // Skip opening quote
|
||||
pos++;
|
||||
size_t endPos = json.find("\"", pos);
|
||||
if (endPos == std::string::npos) return "";
|
||||
return json.substr(pos, endPos - pos);
|
||||
} else {
|
||||
// Unquoted value (number, boolean, etc.)
|
||||
size_t endPos = pos;
|
||||
while (endPos < json.length() && json[endPos] != ',' && json[endPos] != '}' && json[endPos] != ']') {
|
||||
endPos++;
|
||||
@@ -158,29 +196,22 @@ static std::string ExtractJSONValue(const std::string& json, const std::string&
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// HEARTBEAT THREAD
|
||||
// ============================================================================
|
||||
|
||||
static void HeartbeatThread(const std::string& username, int threads,
|
||||
static void HeartbeatThread(const std::string& username, int threads,
|
||||
std::atomic<bool>& stopFlag, const std::string& serverUrl) {
|
||||
while (!stopFlag) {
|
||||
for (int i = 0; i < 15 && !stopFlag; ++i) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
|
||||
if (stopFlag) break;
|
||||
|
||||
|
||||
std::string response;
|
||||
std::string heartbeatJSON = BuildHeartbeatJSON(username, threads);
|
||||
PostJSON(serverUrl + "/api/session/heartbeat", heartbeatJSON, response);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// EXISTING DATAGEN FUNCTIONS
|
||||
// ============================================================================
|
||||
|
||||
// Signal handling globals
|
||||
static std::atomic<bool> g_shutdownRequested(false);
|
||||
|
||||
@@ -194,10 +225,10 @@ static void SignalHandler(int signal) {
|
||||
static void SetupSignalHandlers() {
|
||||
std::signal(SIGINT, SignalHandler);
|
||||
std::signal(SIGTERM, SignalHandler);
|
||||
|
||||
#ifndef _WIN32
|
||||
|
||||
#ifndef _WIN32
|
||||
std::signal(SIGHUP, SignalHandler);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
static bool IsGameOver(Board &board, SEARCH::SearchContext* ctx) {
|
||||
@@ -206,10 +237,9 @@ static bool IsGameOver(Board &board, SEARCH::SearchContext* ctx) {
|
||||
int moveSeen = 0;
|
||||
|
||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||
if (!board.IsLegal(board.moveList[i])) continue;
|
||||
Board copy = board;
|
||||
bool isLegal = copy.MakeMove(board.moveList[i]);
|
||||
|
||||
if (!isLegal) continue;
|
||||
copy.MakeMove(board.moveList[i]);
|
||||
moveSeen++;
|
||||
}
|
||||
|
||||
@@ -226,14 +256,22 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext* ctx) {
|
||||
for (int count = 0; count < RAND_MOVES + plusOne; count++) {
|
||||
MOVEGEN::GenerateMoves<All>(board, true);
|
||||
|
||||
std::uniform_int_distribution<int> dist(0, board.currentMoveIndex - 1);
|
||||
bool isLegal = board.MakeMove(board.moveList[dist(rng)]);
|
||||
if (board.currentMoveIndex <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!isLegal) {
|
||||
std::uniform_int_distribution<int> dist(0, board.currentMoveIndex - 1);
|
||||
|
||||
Move currMove = board.moveList[dist(rng)];
|
||||
if (!board.IsLegal(currMove)) {
|
||||
count--;
|
||||
continue;
|
||||
}
|
||||
|
||||
board.MakeMove(currMove);
|
||||
|
||||
ctx->positionHistory[board.positionIndex] = board.hashKey;
|
||||
|
||||
MOVEGEN::GenerateMoves<All>(board, true);
|
||||
if (IsGameOver(board, ctx)) break;
|
||||
}
|
||||
@@ -241,7 +279,7 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext* ctx) {
|
||||
|
||||
static void WriteToFile(std::vector<Game> &gamesBuffer, const std::string& basePath, int& fileCounter) {
|
||||
if (gamesBuffer.empty()) return;
|
||||
|
||||
|
||||
int32_t zeroes = 0;
|
||||
std::vector<char> buffer;
|
||||
|
||||
@@ -266,32 +304,32 @@ static void WriteToFile(std::vector<Game> &gamesBuffer, const std::string& baseP
|
||||
|
||||
std::string tmpFileName = basePath + std::to_string(fileCounter) + ".tmp";
|
||||
std::string finalFileName = basePath + std::to_string(fileCounter) + ".binpack";
|
||||
|
||||
|
||||
std::ofstream tmpFile(tmpFileName, std::ios::binary);
|
||||
if (!tmpFile.is_open()) {
|
||||
std::cerr << "Failed to create temporary file: " << tmpFileName << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
tmpFile.write(buffer.data(), buffer.size());
|
||||
tmpFile.flush();
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
#ifdef _WIN32
|
||||
tmpFile.close();
|
||||
#else
|
||||
#else
|
||||
tmpFile.close();
|
||||
sync();
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
std::error_code ec;
|
||||
std::filesystem::rename(tmpFileName, finalFileName, ec);
|
||||
if (ec) {
|
||||
std::cerr << "Failed to rename " << tmpFileName << " to " << finalFileName
|
||||
std::cerr << "Failed to rename " << tmpFileName << " to " << finalFileName
|
||||
<< ": " << ec.message() << std::endl;
|
||||
std::filesystem::remove(tmpFileName, ec);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
fileCounter++;
|
||||
}
|
||||
|
||||
@@ -305,8 +343,8 @@ static std::string MergeThreadFiles() {
|
||||
std::tm tm = *std::localtime(&now_time);
|
||||
|
||||
std::ostringstream oss;
|
||||
oss << "data/datagen_"
|
||||
<< std::put_time(&tm, "%Y-%m-%d_%H-%M")
|
||||
oss << "data/datagen_"
|
||||
<< std::put_time(&tm, "%Y-%m-%d_%H-%M")
|
||||
<< ".binpack";
|
||||
std::string finalFileName = oss.str();
|
||||
|
||||
@@ -342,7 +380,7 @@ static std::string MergeThreadFiles() {
|
||||
for (const auto& entry : fs::directory_iterator(dataDir)) {
|
||||
if (!entry.is_regular_file()) continue;
|
||||
std::string filename = entry.path().filename().string();
|
||||
|
||||
|
||||
if (filename.find("thread") == 0 && filename.find(".tmp") != std::string::npos) {
|
||||
std::error_code ec;
|
||||
fs::remove(entry.path(), ec);
|
||||
@@ -371,7 +409,9 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
|
||||
try {
|
||||
while (!stopFlag && !g_shutdownRequested) {
|
||||
Board board;
|
||||
TTable DatagenTT;
|
||||
auto ctx = std::make_unique<SEARCH::SearchContext>();
|
||||
ctx->TT = &DatagenTT;
|
||||
|
||||
PlayRandMoves(board, ctx.get());
|
||||
if (IsGameOver(board, ctx.get())) continue;
|
||||
@@ -387,12 +427,12 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
|
||||
|
||||
SearchResults safeResults;
|
||||
|
||||
int staticEval = UTILS::ConvertToWhiteRelative(board, NNUE::net.Evaluate(board));
|
||||
int staticEval = UTILS::ConvertToWhiteRelative(board, NNUE::net.Evaluate(board, true));
|
||||
int wdl = 1;
|
||||
|
||||
while (!IsGameOver(board, ctx.get())) {
|
||||
if (g_shutdownRequested) break;
|
||||
|
||||
|
||||
SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx.get());
|
||||
|
||||
if (!results.bestMove) break;
|
||||
@@ -428,48 +468,103 @@ static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& st
|
||||
throw;
|
||||
}
|
||||
|
||||
if (gamesBuffer.size() > 0) {
|
||||
if (!gamesBuffer.empty()) {
|
||||
WriteToFile(gamesBuffer, basePath, fileCounter);
|
||||
gamesBuffer.clear();
|
||||
}
|
||||
|
||||
if (!stopFlag.load() && !g_shutdownRequested.load()) {
|
||||
std::abort();
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
static void CreateWorkerThread(int id, std::atomic<int>& positions, std::atomic<bool>& stopFlag, std::vector<std::thread>& threads) {
|
||||
threads.emplace_back(PlayGames, id, std::ref(positions), std::ref(stopFlag));
|
||||
|
||||
struct WinThreadArgs {
|
||||
int id;
|
||||
std::atomic<int>* positions;
|
||||
std::atomic<bool>* stopFlag;
|
||||
};
|
||||
|
||||
static unsigned __stdcall WinThreadEntry(void* p) {
|
||||
std::unique_ptr<WinThreadArgs> args(static_cast<WinThreadArgs*>(p));
|
||||
PlayGames(args->id, *args->positions, *args->stopFlag);
|
||||
_endthreadex(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool CreateWorkerThread(int id,
|
||||
std::atomic<int>& positions,
|
||||
std::atomic<bool>& stopFlag,
|
||||
std::vector<HANDLE>& threads,
|
||||
size_t stackSizeBytes) {
|
||||
auto* args = new WinThreadArgs{ id, &positions, &stopFlag };
|
||||
|
||||
unsigned tid = 0;
|
||||
HANDLE h = reinterpret_cast<HANDLE>(
|
||||
_beginthreadex(
|
||||
nullptr,
|
||||
static_cast<unsigned>(stackSizeBytes),
|
||||
&WinThreadEntry,
|
||||
args,
|
||||
0,
|
||||
&tid
|
||||
)
|
||||
);
|
||||
|
||||
if (!h) {
|
||||
std::cerr << "Failed to create thread " << id << " (errno=" << errno << ")\n";
|
||||
delete args;
|
||||
return false;
|
||||
}
|
||||
|
||||
threads.push_back(h);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void JoinWorkerThreads(std::vector<HANDLE>& threads) {
|
||||
for (HANDLE h : threads) {
|
||||
if (h) {
|
||||
WaitForSingleObject(h, INFINITE);
|
||||
CloseHandle(h);
|
||||
}
|
||||
}
|
||||
threads.clear();
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static void* ThreadFunc(void* arg) {
|
||||
auto* tup = static_cast<std::tuple<int, std::atomic<int>*, std::atomic<bool>*>*>(arg);
|
||||
PlayGames(std::get<0>(*tup), *std::get<1>(*tup), *std::get<2>(*tup));
|
||||
delete tup;
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
// ============================================================================
|
||||
// MAIN RUN FUNCTIONS
|
||||
// ============================================================================
|
||||
#endif
|
||||
|
||||
void Run(int targetPositions, int threads) {
|
||||
SetupSignalHandlers();
|
||||
|
||||
#ifdef _WIN32
|
||||
system("cls");
|
||||
HideCursor();
|
||||
#else
|
||||
std::cout << "\033[2J\033[H";
|
||||
HideCursor();
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
system("cls");
|
||||
HideCursor();
|
||||
#else
|
||||
std::cout << "\033[2J\033[H";
|
||||
HideCursor();
|
||||
#endif
|
||||
|
||||
std::atomic<int> positions = 0;
|
||||
std::atomic<bool> stopFlag = false;
|
||||
|
||||
#ifdef _WIN32
|
||||
std::vector<std::thread> workerThreads;
|
||||
|
||||
std::vector<HANDLE> workerThreads;
|
||||
workerThreads.reserve((threads > 1) ? (threads - 1) : 0);
|
||||
|
||||
const size_t stackSizeBytes = 8ull * 1024ull * 1024ull;
|
||||
|
||||
for (int i = 0; i < threads - 1; ++i) {
|
||||
CreateWorkerThread(i, positions, stopFlag, workerThreads);
|
||||
CreateWorkerThread(i, positions, stopFlag, workerThreads, stackSizeBytes);
|
||||
}
|
||||
#else
|
||||
std::vector<pthread_t> workerThreads;
|
||||
@@ -506,11 +601,7 @@ void Run(int targetPositions, int threads) {
|
||||
std::cout << "\n\nWaiting for threads to finish writing..." << std::endl;
|
||||
|
||||
#ifdef _WIN32
|
||||
for (std::thread& thread : workerThreads) {
|
||||
if (thread.joinable()) {
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
JoinWorkerThreads(workerThreads);
|
||||
#else
|
||||
for (pthread_t& thread : workerThreads) {
|
||||
pthread_join(thread, nullptr);
|
||||
@@ -520,83 +611,82 @@ void Run(int targetPositions, int threads) {
|
||||
std::cout << "All threads completed. Merging files..." << std::endl;
|
||||
MergeThreadFiles();
|
||||
|
||||
#ifdef _WIN32
|
||||
ShowCursor();
|
||||
#else
|
||||
ShowCursor();
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
ShowCursor();
|
||||
#else
|
||||
ShowCursor();
|
||||
#endif
|
||||
|
||||
std::cout << "\nShutdown complete. All data saved safely." << std::endl;
|
||||
}
|
||||
|
||||
void RunOnline(const std::string& username, int targetPositions, int threads) {
|
||||
const std::string SERVER_URL = "http://13.60.26.163:3000";
|
||||
|
||||
|
||||
SetupSignalHandlers();
|
||||
|
||||
|
||||
std::cout << "=== ONLINE MODE ===" << std::endl;
|
||||
std::cout << "User: " << username << std::endl;
|
||||
std::cout << "Target positions per cycle: " << targetPositions << std::endl;
|
||||
std::cout << "Threads: " << threads << std::endl;
|
||||
std::cout << "===================" << std::endl << std::endl;
|
||||
|
||||
|
||||
while (!g_shutdownRequested) {
|
||||
// Send initial heartbeat
|
||||
std::cout << "Sending heartbeat..." << std::endl;
|
||||
std::string heartbeatResponse;
|
||||
std::string heartbeatJSON = BuildHeartbeatJSON(username, threads);
|
||||
|
||||
|
||||
if (!PostJSON(SERVER_URL + "/api/session/heartbeat", heartbeatJSON, heartbeatResponse)) {
|
||||
std::cerr << "Failed to send heartbeat. Retrying in 10 seconds..." << std::endl;
|
||||
std::this_thread::sleep_for(std::chrono::seconds(10));
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
std::cout << "Heartbeat response: " << heartbeatResponse << std::endl;
|
||||
|
||||
// Start session
|
||||
|
||||
std::string startResponse;
|
||||
std::string startJSON = BuildStartJSON(username, targetPositions);
|
||||
|
||||
|
||||
std::cout << "Starting new session..." << std::endl;
|
||||
if (!PostJSON(SERVER_URL + "/api/session/start", startJSON, startResponse)) {
|
||||
std::cerr << "Failed to start session. Retrying in 10 seconds..." << std::endl;
|
||||
std::this_thread::sleep_for(std::chrono::seconds(10));
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
std::cout << "Server response: " << startResponse << std::endl;
|
||||
|
||||
|
||||
std::string sessionId = ExtractJSONValue(startResponse, "session_id");
|
||||
if (sessionId.empty()) {
|
||||
std::cerr << "Failed to parse session ID. Retrying..." << std::endl;
|
||||
std::this_thread::sleep_for(std::chrono::seconds(10));
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
std::cout << "Session ID: " << sessionId << std::endl;
|
||||
|
||||
// Start heartbeat thread
|
||||
|
||||
std::atomic<bool> heartbeatStop(false);
|
||||
std::thread heartbeat(HeartbeatThread, username, threads, std::ref(heartbeatStop), SERVER_URL);
|
||||
|
||||
// Run datagen
|
||||
#ifdef _WIN32
|
||||
system("cls");
|
||||
HideCursor();
|
||||
#else
|
||||
std::cout << "\033[2J\033[H";
|
||||
HideCursor();
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
system("cls");
|
||||
HideCursor();
|
||||
#else
|
||||
std::cout << "\033[2J\033[H";
|
||||
HideCursor();
|
||||
#endif
|
||||
|
||||
std::atomic<int> positions = 0;
|
||||
std::atomic<bool> stopFlag = false;
|
||||
|
||||
#ifdef _WIN32
|
||||
std::vector<std::thread> workerThreads;
|
||||
|
||||
std::vector<HANDLE> workerThreads;
|
||||
workerThreads.reserve((threads > 1) ? (threads - 1) : 0);
|
||||
|
||||
const size_t stackSizeBytes = 8ull * 1024ull * 1024ull;
|
||||
|
||||
for (int i = 0; i < threads - 1; ++i) {
|
||||
CreateWorkerThread(i, positions, stopFlag, workerThreads);
|
||||
CreateWorkerThread(i, positions, stopFlag, workerThreads, stackSizeBytes);
|
||||
}
|
||||
#else
|
||||
std::vector<pthread_t> workerThreads;
|
||||
@@ -633,18 +723,13 @@ void RunOnline(const std::string& username, int targetPositions, int threads) {
|
||||
std::cout << "\n\nWaiting for threads to finish writing..." << std::endl;
|
||||
|
||||
#ifdef _WIN32
|
||||
for (std::thread& thread : workerThreads) {
|
||||
if (thread.joinable()) {
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
JoinWorkerThreads(workerThreads);
|
||||
#else
|
||||
for (pthread_t& thread : workerThreads) {
|
||||
pthread_join(thread, nullptr);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Stop heartbeat
|
||||
heartbeatStop = true;
|
||||
if (heartbeat.joinable()) {
|
||||
heartbeat.join();
|
||||
@@ -655,20 +740,19 @@ void RunOnline(const std::string& username, int targetPositions, int threads) {
|
||||
std::cout << "All threads completed. Merging files..." << std::endl;
|
||||
std::string mergedFile = MergeThreadFiles();
|
||||
|
||||
#ifdef _WIN32
|
||||
ShowCursor();
|
||||
#else
|
||||
ShowCursor();
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
ShowCursor();
|
||||
#else
|
||||
ShowCursor();
|
||||
#endif
|
||||
|
||||
if (!mergedFile.empty()) {
|
||||
std::cout << "Uploading file to server..." << std::endl;
|
||||
std::string uploadUrl = SERVER_URL + "/api/upload/" + sessionId;
|
||||
|
||||
|
||||
if (UploadFile(uploadUrl, mergedFile, username, positions.load())) {
|
||||
std::cout << "Upload successful!" << std::endl;
|
||||
|
||||
// Delete the file after successful upload
|
||||
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(mergedFile, ec);
|
||||
if (!ec) {
|
||||
@@ -678,13 +762,13 @@ void RunOnline(const std::string& username, int targetPositions, int threads) {
|
||||
std::cerr << "Upload failed. File kept locally: " << mergedFile << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (g_shutdownRequested) break;
|
||||
|
||||
|
||||
std::cout << "\n=== Starting next cycle ===" << std::endl << std::endl;
|
||||
std::this_thread::sleep_for(std::chrono::seconds(2));
|
||||
}
|
||||
|
||||
|
||||
std::cout << "\nOnline mode shutdown complete." << std::endl;
|
||||
}
|
||||
|
||||
@@ -713,44 +797,43 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int
|
||||
int width = 60;
|
||||
|
||||
std::string title = "Eleanor - Datagen";
|
||||
int titlePadding = (width - title.length()) / 2;
|
||||
std::cout << COLOR_TITLE << std::setw(titlePadding + title.length()) << title << COLOR_RESET << std::endl;
|
||||
int titlePadding = (width - static_cast<int>(title.length())) / 2;
|
||||
std::cout << COLOR_TITLE << std::setw(titlePadding + static_cast<int>(title.length())) << title << COLOR_RESET << std::endl;
|
||||
|
||||
std::cout << std::string(width, '=') << std::endl;
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << std::fixed << std::setprecision(0);
|
||||
|
||||
std::ostringstream datagenTextStream;
|
||||
datagenTextStream << std::fixed << std::setprecision(2)
|
||||
<< "Positions processed: " << positions / 1000.0 << "K | Target: " << targetPositions / 1000.0 << "K";
|
||||
<< "Positions processed: " << positions / 1000.0 << "K | Target: " << targetPositions / 1000.0 << "K";
|
||||
std::string datagenText = datagenTextStream.str();
|
||||
int datagenPadding = (width - datagenText.length()) / 2;
|
||||
std::cout << COLOR_SECTION << std::setw(datagenPadding + datagenText.length()) << datagenText << COLOR_RESET << std::endl;
|
||||
int datagenPadding = (width - static_cast<int>(datagenText.length())) / 2;
|
||||
std::cout << COLOR_SECTION << std::setw(datagenPadding + static_cast<int>(datagenText.length())) << datagenText << COLOR_RESET << std::endl;
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
std::string elapsedText = "Elapsed Time: " + std::to_string(static_cast<int>(round(elapsed))) + " sec";
|
||||
int elapsedPadding = (width - elapsedText.length()) / 2;
|
||||
std::cout << COLOR_SECTION << std::setw(elapsedPadding + elapsedText.length()) << elapsedText << COLOR_RESET << std::endl;
|
||||
int elapsedPadding = (width - static_cast<int>(elapsedText.length())) / 2;
|
||||
std::cout << COLOR_SECTION << std::setw(elapsedPadding + static_cast<int>(elapsedText.length())) << elapsedText << COLOR_RESET << std::endl;
|
||||
|
||||
std::string positionsPerSecText = "Positions/sec: " + std::to_string(static_cast<int>(round(positionsPerSec)));
|
||||
int positionsPerSecPadding = (width - positionsPerSecText.length()) / 2;
|
||||
std::cout << COLOR_VALUE << std::setw(positionsPerSecPadding + positionsPerSecText.length()) << positionsPerSecText << COLOR_RESET << std::endl;
|
||||
int positionsPerSecPadding = (width - static_cast<int>(positionsPerSecText.length())) / 2;
|
||||
std::cout << COLOR_VALUE << std::setw(positionsPerSecPadding + static_cast<int>(positionsPerSecText.length())) << positionsPerSecText << COLOR_RESET << std::endl;
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
std::string threadsText = "Threads: " + std::to_string(static_cast<int>(threads));
|
||||
int threadsTextPadding = (width - threadsText.length()) / 2;
|
||||
std::cout << COLOR_SECTION << std::setw(threadsTextPadding + threadsText.length()) << threadsText << COLOR_RESET << std::endl;
|
||||
int threadsTextPadding = (width - static_cast<int>(threadsText.length())) / 2;
|
||||
std::cout << COLOR_SECTION << std::setw(threadsTextPadding + static_cast<int>(threadsText.length())) << threadsText << COLOR_RESET << std::endl;
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
double remainingTime = 0.0;
|
||||
if (positions > 0 && elapsed > 0.0) {
|
||||
double positionsPerSec = positions / elapsed;
|
||||
remainingTime = (targetPositions - positions) / positionsPerSec;
|
||||
double pps = positions / elapsed;
|
||||
remainingTime = (targetPositions - positions) / pps;
|
||||
}
|
||||
|
||||
remainingTime = (std::max)(0.0, remainingTime);
|
||||
@@ -761,14 +844,15 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int
|
||||
|
||||
std::ostringstream remainingTimeStream;
|
||||
remainingTimeStream << "Estimated Time Left: "
|
||||
<< std::setfill('0') << std::setw(2) << remainingHours << "h "
|
||||
<< std::setw(2) << remainingMinutes << "m "
|
||||
<< std::setw(2) << remainingSeconds << "s";
|
||||
<< std::setfill('0') << std::setw(2) << remainingHours << "h "
|
||||
<< std::setw(2) << remainingMinutes << "m "
|
||||
<< std::setw(2) << remainingSeconds << "s";
|
||||
|
||||
std::string remainingTimeText = remainingTimeStream.str();
|
||||
int remainingPadding = (width - remainingTimeText.length()) / 2;
|
||||
std::cout << COLOR_SECTION << std::setw(remainingPadding + remainingTimeText.length())
|
||||
<< remainingTimeText << COLOR_RESET << std::endl;
|
||||
int remainingPadding = (width - static_cast<int>(remainingTimeText.length())) / 2;
|
||||
std::cout << COLOR_SECTION
|
||||
<< std::setw(remainingPadding + static_cast<int>(remainingTimeText.length()))
|
||||
<< remainingTimeText << COLOR_RESET << std::endl;
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
@@ -779,21 +863,18 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int
|
||||
std::string progressBar = "[";
|
||||
|
||||
for (int i = 0; i < barWidth; ++i) {
|
||||
if (i < pos) {
|
||||
progressBar += "=";
|
||||
} else if (i == pos) {
|
||||
progressBar += ">";
|
||||
} else {
|
||||
progressBar += " ";
|
||||
}
|
||||
if (i < pos) progressBar += "=";
|
||||
else if (i == pos) progressBar += ">";
|
||||
else progressBar += " ";
|
||||
}
|
||||
|
||||
progressBar += "] " + std::to_string(static_cast<int>(round(progress * 100.0))) + " %";
|
||||
|
||||
int progressPadding = (width - progressBar.length()) / 2;
|
||||
std::cout << COLOR_BAR << std::setw(progressPadding + progressBar.length()) << progressBar << COLOR_RESET << std::endl;
|
||||
int progressPadding = (width - static_cast<int>(progressBar.length())) / 2;
|
||||
std::cout << COLOR_BAR << std::setw(progressPadding + static_cast<int>(progressBar.length()))
|
||||
<< progressBar << COLOR_RESET << std::endl;
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace DATAGEN
|
||||
+10
-6
@@ -44,12 +44,16 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
|
||||
|
||||
MOVEGEN::initLeaperAttacks();
|
||||
MOVEGEN::initSliderAttacks();
|
||||
UTILS::InitZobrist();
|
||||
SEARCH::InitLMRTable();
|
||||
|
||||
Board board;
|
||||
MOVEGEN::initLeaperAttacks();
|
||||
MOVEGEN::initSliderAttacks();
|
||||
UTILS::InitZobrist();
|
||||
#ifdef TUNING
|
||||
SEARCH::RefreshTunableCaches();
|
||||
#else
|
||||
SEARCH::InitLMRTable();
|
||||
#endif
|
||||
|
||||
Board board;
|
||||
|
||||
if (argc > 1) {
|
||||
if (std::string(argv[1]) == "bench") {
|
||||
|
||||
+14
-2
@@ -64,6 +64,10 @@ public:
|
||||
return (GetFlags() >= knightPromotion);
|
||||
}
|
||||
|
||||
bool IsCastle() {
|
||||
return (GetFlags() == kingCastle || GetFlags() == queenCastle);
|
||||
}
|
||||
|
||||
void PrintMove() {
|
||||
std::cout << squareCoords[MoveFrom()] << squareCoords[MoveTo()];
|
||||
|
||||
@@ -80,7 +84,7 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
std::string GetMoveString() {
|
||||
std::string GetMoveString() {
|
||||
std::string result = squareCoords[MoveFrom()];
|
||||
result += squareCoords[MoveTo()];
|
||||
|
||||
@@ -102,7 +106,7 @@ public:
|
||||
uint16_t ConvertToViriMoveFormat() {
|
||||
uint16_t viriMove = 0;
|
||||
int flags = GetFlags();
|
||||
|
||||
|
||||
viriMove = MoveFrom();
|
||||
|
||||
if (flags == kingCastle || flags == queenCastle) {
|
||||
@@ -146,6 +150,14 @@ public:
|
||||
return viriMove;
|
||||
}
|
||||
|
||||
int GetPromoPiece() {
|
||||
if (IsCapture()) {
|
||||
return GetFlags() - 9;
|
||||
} else {
|
||||
return GetFlags() - 5;
|
||||
}
|
||||
}
|
||||
|
||||
operator uint16_t() {
|
||||
return m_move;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
#pragma once
|
||||
|
||||
#include "search.h"
|
||||
#include "movegen.h"
|
||||
|
||||
#include <limits>
|
||||
|
||||
using namespace SEARCH;
|
||||
|
||||
template <MovegenMode mode>
|
||||
class MovePicker {
|
||||
private:
|
||||
enum Stage {
|
||||
TT,
|
||||
Rest,
|
||||
End
|
||||
};
|
||||
|
||||
Board& board;
|
||||
SearchContext* ctx;
|
||||
Move ttMove;
|
||||
int ply;
|
||||
Stage stage;
|
||||
|
||||
int index;
|
||||
bool generated;
|
||||
|
||||
int scores[MAX_MOVES];
|
||||
|
||||
public:
|
||||
MovePicker(Board& b, SearchContext* c, int p, Move tt)
|
||||
: board(b), ctx(c), ttMove(tt), ply(p), stage(TT), index(0), generated(false)
|
||||
{
|
||||
}
|
||||
|
||||
Move Next() {
|
||||
while (true) {
|
||||
switch (stage) {
|
||||
|
||||
case TT: {
|
||||
stage = Rest;
|
||||
|
||||
if (ttMove && board.IsPseudoLegal(ttMove))
|
||||
return ttMove;
|
||||
break;
|
||||
}
|
||||
|
||||
case Rest: {
|
||||
|
||||
if (!generated) {
|
||||
generated = true;
|
||||
|
||||
MOVEGEN::GenerateMoves<mode>(board, true);
|
||||
|
||||
ScoreAllMoves();
|
||||
index = 0;
|
||||
}
|
||||
|
||||
while (index < board.currentMoveIndex) {
|
||||
int idx = FindNext();
|
||||
Move m = board.moveList[idx];
|
||||
|
||||
if (m == ttMove)
|
||||
continue;
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
stage = End;
|
||||
break;
|
||||
}
|
||||
|
||||
case End:
|
||||
return Move();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
int ScoreMove(Move& move) {
|
||||
|
||||
if (move.IsCapture()) {
|
||||
|
||||
int attackerType = board.GetPieceType(move.MoveFrom());
|
||||
int targetType = board.GetPieceType(move.MoveTo());
|
||||
|
||||
if (move.GetFlags() == epCapture)
|
||||
targetType = Pawn;
|
||||
|
||||
int capthistScore =
|
||||
ctx->capthist[board.sideToMove][attackerType][targetType][move.MoveTo()];
|
||||
|
||||
return 50000 * SEE(board, move, seeOrderingThreshold)
|
||||
+ (100 * targetType - attackerType + 105)
|
||||
+ capthistScore;
|
||||
}
|
||||
|
||||
if (ctx->killerMoves[ply] == move)
|
||||
return 41000;
|
||||
|
||||
bool sourceThreatened = board.IsSquareThreatened(board.sideToMove, move.MoveFrom());
|
||||
bool targetThreatened = board.IsSquareThreatened(board.sideToMove, move.MoveTo());
|
||||
|
||||
int historyScore =
|
||||
ctx->history[board.sideToMove]
|
||||
[move.MoveFrom()]
|
||||
[move.MoveTo()]
|
||||
[sourceThreatened]
|
||||
[targetThreatened];
|
||||
|
||||
int conthistScore = 0;
|
||||
|
||||
if (ply > 0) {
|
||||
conthistScore = ctx->conthist.GetNPly(board, move, ctx, ply, 1);
|
||||
|
||||
if (ply > 1)
|
||||
conthistScore += ctx->conthist.GetNPly(board, move, ctx, ply, 2);
|
||||
}
|
||||
|
||||
return 20000 + historyScore + conthistScore;
|
||||
}
|
||||
|
||||
void ScoreAllMoves() {
|
||||
int moveCount = board.currentMoveIndex;
|
||||
for (int i = 0; i < moveCount; i++)
|
||||
scores[i] = ScoreMove(board.moveList[i]);
|
||||
}
|
||||
|
||||
int FindNext() {
|
||||
const auto toU64 = [](int s) {
|
||||
int64_t widened = s;
|
||||
widened -= std::numeric_limits<int32_t>::min();
|
||||
return static_cast<uint64_t>(widened) << 32;
|
||||
};
|
||||
|
||||
int moveCount = board.currentMoveIndex;
|
||||
|
||||
uint64_t best = toU64(scores[index]) | static_cast<uint64_t>(256 - index);
|
||||
for (int i = index + 1; i < moveCount; i++) {
|
||||
uint64_t curr = toU64(scores[i]) | static_cast<uint64_t>(256 - i);
|
||||
if (curr > best)
|
||||
best = curr;
|
||||
}
|
||||
|
||||
int bestIdx = 256 - static_cast<int>(best & 0xFFFFFFFF);
|
||||
|
||||
if (bestIdx != index) {
|
||||
std::swap(board.moveList[index], board.moveList[bestIdx]);
|
||||
std::swap(scores[index], scores[bestIdx]);
|
||||
}
|
||||
|
||||
return index++;
|
||||
}
|
||||
};
|
||||
+12
-3
@@ -17,7 +17,9 @@ void Network::Load(const std::string& path) {
|
||||
|
||||
// HL weights
|
||||
for (size_t i = 0; i < accumulator_weights.size(); i++) {
|
||||
file.read(reinterpret_cast<char*>(&accumulator_weights[i]), sizeof(int16_t));
|
||||
for (size_t j = 0; j < accumulator_weights[i].size(); j++) {
|
||||
file.read(reinterpret_cast<char*>(&accumulator_weights[i][j]), sizeof(int16_t));
|
||||
}
|
||||
}
|
||||
|
||||
// HL biases
|
||||
@@ -185,8 +187,15 @@ int Forward(const Board& board, const Network& net) {
|
||||
return (eval * SCALE) / (QA * QB);
|
||||
}
|
||||
|
||||
int16_t Network::Evaluate(const Board& board) {
|
||||
return std::clamp(Forward(board, *this), (-SEARCH::MATE_SCORE + SEARCH::MAX_DEPTH),
|
||||
int16_t Network::Evaluate(const Board& board, bool datagen) {
|
||||
// Disabled in datagen
|
||||
const int materialScale = datagen ? 4096 : 2048
|
||||
+ 90 * board.pieces[Knight].PopCount()
|
||||
+ 90 * board.pieces[Bishop].PopCount()
|
||||
+ 180 * board.pieces[Rook].PopCount()
|
||||
+ 360 * board.pieces[Queen].PopCount();
|
||||
|
||||
return std::clamp(Forward(board, *this) * materialScale / 4096, (-SEARCH::MATE_SCORE + SEARCH::MAX_DEPTH),
|
||||
(SEARCH::MATE_SCORE - SEARCH::MAX_DEPTH));
|
||||
}
|
||||
|
||||
|
||||
+28
-5
@@ -3,8 +3,6 @@
|
||||
#include <string>
|
||||
#include <array>
|
||||
|
||||
#include "../external/fmt/fmt/format.h"
|
||||
|
||||
class Board;
|
||||
|
||||
#ifdef __AVX512F__
|
||||
@@ -17,21 +15,46 @@ namespace NNUE {
|
||||
|
||||
constexpr size_t INPUT_SIZE = 768;
|
||||
constexpr size_t HL_SIZE = 1536;
|
||||
constexpr size_t INPUT_BUCKETS = 8;
|
||||
constexpr size_t OUTPUT_BUCKETS = 8;
|
||||
|
||||
constexpr int16_t SCALE = 400;
|
||||
constexpr int16_t QA = 255;
|
||||
constexpr int16_t QB = 64;
|
||||
|
||||
|
||||
constexpr int kingBuckets[2][64] = {
|
||||
{
|
||||
0, 1, 2, 3, 3, 2, 1, 0,
|
||||
4, 4, 5, 5, 5, 5, 4, 4,
|
||||
6, 6, 6, 6, 6, 6, 6, 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6,
|
||||
7, 7, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7
|
||||
},
|
||||
|
||||
{
|
||||
7, 7, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7,
|
||||
6, 6, 6, 6, 6, 6, 6, 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6,
|
||||
4, 4, 5, 5, 5, 5, 4, 4,
|
||||
0, 1, 2, 3, 3, 2, 1, 0,
|
||||
}
|
||||
};
|
||||
|
||||
struct Network {
|
||||
alignas(ALIGNMENT) std::array<int16_t, HL_SIZE * INPUT_SIZE> accumulator_weights;
|
||||
alignas(ALIGNMENT) std::array<std::array<int16_t, HL_SIZE * INPUT_SIZE>, INPUT_BUCKETS> accumulator_weights;
|
||||
alignas(ALIGNMENT) std::array<int16_t, HL_SIZE> accumulator_biases;
|
||||
alignas(ALIGNMENT) std::array<std::array<int16_t, 2 * HL_SIZE>, OUTPUT_BUCKETS> output_weights;
|
||||
std::array<int16_t, OUTPUT_BUCKETS> output_bias;
|
||||
|
||||
void Load(const std::string& path);
|
||||
|
||||
int16_t Evaluate(const Board& board);
|
||||
int16_t Evaluate(const Board& board, bool datagen = false);
|
||||
};
|
||||
|
||||
inline Network net;
|
||||
|
||||
+9
-5
@@ -1,8 +1,9 @@
|
||||
#include "perft.h"
|
||||
#include "stopwatch.h"
|
||||
#include "movegen.h"
|
||||
#include <cassert>
|
||||
|
||||
static U64 HelperPerft(Board board, int depth) {
|
||||
static U64 HelperPerft(Board &board, int depth) {
|
||||
MOVEGEN::GenerateMoves<All>(board, true);
|
||||
|
||||
if (depth == 0) return 1ULL;
|
||||
@@ -10,24 +11,27 @@ static U64 HelperPerft(Board board, int depth) {
|
||||
U64 nodes = 0;
|
||||
|
||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||
if (!board.IsLegal(board.moveList[i])) continue;
|
||||
|
||||
Board copy = board;
|
||||
bool isLegal = copy.MakeMove(board.moveList[i]);
|
||||
if (!isLegal) continue;
|
||||
copy.MakeMove(board.moveList[i]);
|
||||
|
||||
nodes += HelperPerft(copy, depth - 1);
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
void Perft(Board &board, int depth) {
|
||||
//board.SetByFen(StartingFen);
|
||||
MOVEGEN::GenerateMoves<All>(board, true);
|
||||
|
||||
U64 totalNodes = 0;
|
||||
|
||||
Stopwatch sw;
|
||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||
if (!board.IsLegal(board.moveList[i])) continue;
|
||||
Board copy = board;
|
||||
bool isLegal = copy.MakeMove(board.moveList[i]);
|
||||
if (!isLegal) continue;
|
||||
copy.MakeMove(board.moveList[i]);
|
||||
U64 nodeCount = HelperPerft(copy, depth - 1);
|
||||
totalNodes += nodeCount;
|
||||
board.moveList[i].PrintMove();
|
||||
|
||||
+394
-368
File diff suppressed because it is too large.
Load diff
+42
-10
@@ -23,6 +23,7 @@ enum searchMode {
|
||||
constexpr int MATE_SCORE = 32767;
|
||||
constexpr int MAX_DEPTH = 256;
|
||||
constexpr int MAX_HISTORY = 16384;
|
||||
constexpr int WIN_SCORE = 30000;
|
||||
|
||||
constexpr int CORRHIST_WEIGHT_SCALE = 256;
|
||||
constexpr int CORRHIST_GRAIN = 256;
|
||||
@@ -47,13 +48,40 @@ constexpr std::array<int, 6> SEEPieceValues = {
|
||||
#endif
|
||||
|
||||
void InitLMRTable();
|
||||
|
||||
#ifdef TUNING
|
||||
void InitSEEPieceValues();
|
||||
void RefreshTunableCaches();
|
||||
#endif
|
||||
|
||||
class SearchContext;
|
||||
|
||||
class CaptHistory {
|
||||
private:
|
||||
// indexed by [stm][moving pt][capture pt][to]
|
||||
MultiArray<int, 2, 6, 6, 64> historyMoves;
|
||||
public:
|
||||
void Update(bool stm, int moving, int capture, int to, int bonus) {
|
||||
int clampedBonus = std::clamp(bonus, -MAX_HISTORY, MAX_HISTORY);
|
||||
historyMoves[stm][moving][capture][to] +=
|
||||
clampedBonus - historyMoves[stm][moving][capture][to] * std::abs(clampedBonus) / MAX_HISTORY;
|
||||
}
|
||||
|
||||
void Clear() {
|
||||
std::fill(&historyMoves[0][0][0][0], &historyMoves[0][0][0][0] + sizeof(historyMoves) / sizeof(int), 0);
|
||||
}
|
||||
|
||||
auto& operator[](int index) {
|
||||
return historyMoves[index];
|
||||
}
|
||||
};
|
||||
|
||||
class CorrHist {
|
||||
private:
|
||||
// indexed by [stm][key]
|
||||
MultiArray<int, 2, CORRHIST_SIZE> pawnHist;
|
||||
MultiArray<int, 2, CORRHIST_SIZE> nonPawnHist;
|
||||
MultiArray<int, 2, CORRHIST_SIZE> majorHist;
|
||||
public:
|
||||
void Update(Board& board, int depth, int diff, int* entry) {
|
||||
const int scaledDiff = diff * CORRHIST_GRAIN;
|
||||
@@ -66,16 +94,19 @@ public:
|
||||
void UpdateAll(Board& board, int depth, int diff) {
|
||||
Update(board, depth, diff, &pawnHist[board.sideToMove][board.pawnKey % CORRHIST_SIZE]);
|
||||
Update(board, depth, diff, &nonPawnHist[board.sideToMove][board.nonPawnKey % CORRHIST_SIZE]);
|
||||
Update(board, depth, diff, &majorHist[board.sideToMove][board.majorKey % CORRHIST_SIZE]);
|
||||
}
|
||||
|
||||
int GetAllHist(Board& board) {
|
||||
return pawnHist[board.sideToMove][board.pawnKey % CORRHIST_SIZE]
|
||||
+ nonPawnHist[board.sideToMove][board.nonPawnKey % CORRHIST_SIZE];
|
||||
+ nonPawnHist[board.sideToMove][board.nonPawnKey % CORRHIST_SIZE]
|
||||
+ majorHist[board.sideToMove][board.majorKey % CORRHIST_SIZE];
|
||||
}
|
||||
|
||||
void Clear() {
|
||||
std::fill(&pawnHist[0][0], &pawnHist[0][0] + sizeof(pawnHist) / sizeof(int), 0);
|
||||
std::fill(&nonPawnHist[0][0], &nonPawnHist[0][0] + sizeof(nonPawnHist) / sizeof(int), 0);
|
||||
std::fill(&majorHist[0][0], &majorHist[0][0] + sizeof(majorHist) / sizeof(int), 0);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -147,7 +178,7 @@ public:
|
||||
std::cout << termcolor::bright_cyan;
|
||||
table[n][i].PrintMove();
|
||||
std::cout << termcolor::reset;
|
||||
|
||||
|
||||
if (isEven) {
|
||||
std::cout << termcolor::color<247>;
|
||||
} else {
|
||||
@@ -187,6 +218,8 @@ public:
|
||||
|
||||
class SearchContext {
|
||||
public:
|
||||
bool doPrint = false;
|
||||
|
||||
U64 nodes = 0;
|
||||
U64 nodesToGo = 0;
|
||||
int timeToSearch = 0;
|
||||
@@ -194,22 +227,23 @@ public:
|
||||
std::array<U64, 4096> nodesTable{};
|
||||
|
||||
int seldepth = 0;
|
||||
int rootDepth = 0;
|
||||
|
||||
Move excluded = Move();
|
||||
|
||||
bool doingNullMove = false;
|
||||
bool searchStopped = false;
|
||||
int minNmpPly = 0;
|
||||
|
||||
PVLine pvLine;
|
||||
History history;
|
||||
ContHistory conthist;
|
||||
CorrHist corrhist;
|
||||
CaptHistory capthist;
|
||||
|
||||
std::array<std::array<int, MAX_DEPTH>, 2> killerMoves{};
|
||||
std::array<Move, MAX_DEPTH> killerMoves{};
|
||||
|
||||
std::vector<U64> positionHistory;
|
||||
|
||||
TTable TT;
|
||||
TTable* TT = &SharedTT;
|
||||
|
||||
std::array<Stack, MAX_DEPTH> ss{};
|
||||
|
||||
@@ -220,7 +254,7 @@ public:
|
||||
history.Clear();
|
||||
conthist.Clear();
|
||||
corrhist.Clear();
|
||||
TT.Clear();
|
||||
capthist.Clear();
|
||||
sw.Restart();
|
||||
killerMoves = {};
|
||||
ss = {};
|
||||
@@ -238,9 +272,7 @@ SearchResults SearchPosition(Board &board, SearchParams params, SearchContext* c
|
||||
|
||||
bool IsDraw(Board &board, SearchContext* ctx);
|
||||
|
||||
void ListScores(Board &board, SearchContext* ctx);
|
||||
|
||||
void PrintSearchInfo(SearchContext* ctx, SearchResults& results, int depth, int elapsed);
|
||||
void PrintSearchInfo(Board& board, SearchContext* ctx, SearchResults& results, int depth, int elapsed);
|
||||
|
||||
int MoveEstimatedValue(Board& board, Move& move);
|
||||
}
|
||||
+43
-4
@@ -1,11 +1,50 @@
|
||||
#include "tt.h"
|
||||
|
||||
void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove) {
|
||||
TTEntry *current = &table[hashKey % table.size()];
|
||||
TTable SharedTT;
|
||||
|
||||
void TTable::WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove, bool ttpv) {
|
||||
TTBucket *bucket = &table[hashKey % table.size()];
|
||||
TTEntry *current = nullptr;
|
||||
|
||||
if (bucket->depthPreferred && bucket->depthPreferred.hashKey == hashKey) {
|
||||
current = &bucket->depthPreferred;
|
||||
} else if (bucket->alwaysReplace && bucket->alwaysReplace.hashKey == hashKey) {
|
||||
current = &bucket->alwaysReplace;
|
||||
} else if (!bucket->alwaysReplace) {
|
||||
current = &bucket->alwaysReplace;
|
||||
} else if (!bucket->depthPreferred) {
|
||||
current = &bucket->depthPreferred;
|
||||
} else if (ttpv) {
|
||||
current = &bucket->depthPreferred;
|
||||
} else {
|
||||
const int depthPreferredValue = GetReplacementValue(bucket->depthPreferred);
|
||||
const int alwaysReplaceValue = GetReplacementValue(bucket->alwaysReplace);
|
||||
|
||||
if (depthPreferredValue <= alwaysReplaceValue) {
|
||||
current = &bucket->depthPreferred;
|
||||
} else {
|
||||
current = &bucket->alwaysReplace;
|
||||
}
|
||||
}
|
||||
|
||||
const bool samePosition = current->hashKey == hashKey;
|
||||
const bool exactBound = nodeType == PV;
|
||||
const bool entryFromCurrentAge = current->age == age;
|
||||
const int writeDepth = depth + 4 + ttpv * 2;
|
||||
|
||||
if (samePosition && !exactBound && entryFromCurrentAge && writeDepth <= current->depth) {
|
||||
return;
|
||||
}
|
||||
|
||||
current->hashKey = hashKey;
|
||||
current->nodeType = nodeType;
|
||||
current->score = score;
|
||||
current->depth = depth;
|
||||
current->bestMove = bestMove;
|
||||
}
|
||||
|
||||
if (bestMove || !samePosition) {
|
||||
current->bestMove = bestMove;
|
||||
}
|
||||
|
||||
current->age = age;
|
||||
current->ttpv = ttpv;
|
||||
}
|
||||
+35
-10
@@ -26,6 +26,8 @@ public:
|
||||
Move bestMove = Move();
|
||||
uint8_t depth = 0;
|
||||
uint8_t nodeType = 0;
|
||||
uint8_t age = 0;
|
||||
bool ttpv = false;
|
||||
|
||||
TTEntry() {}
|
||||
|
||||
@@ -38,16 +40,29 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class TTBucket {
|
||||
public:
|
||||
TTEntry depthPreferred;
|
||||
TTEntry alwaysReplace;
|
||||
};
|
||||
|
||||
// 1024 MB
|
||||
constexpr U64 maxHash = (1024 * 1000000) / sizeof(TTEntry);
|
||||
constexpr U64 maxHash = (1024 * 1000000) / sizeof(TTBucket);
|
||||
// 8 MB
|
||||
constexpr U64 defaultHash = (8 * 1000000) / sizeof(TTEntry);
|
||||
constexpr U64 defaultHash = (8 * 1000000) / sizeof(TTBucket);
|
||||
|
||||
constexpr int16_t invalidEntry = 11111;
|
||||
|
||||
class TTable {
|
||||
private:
|
||||
std::vector<TTEntry> table;
|
||||
std::vector<TTBucket> table;
|
||||
|
||||
uint8_t age = 0;
|
||||
|
||||
int GetReplacementValue(const TTEntry& entry) const {
|
||||
const int ageDelta = uint8_t(age - entry.age);
|
||||
return entry.depth - ageDelta * 4 + entry.ttpv * 2;
|
||||
}
|
||||
public:
|
||||
TTable() : table(defaultHash) {};
|
||||
|
||||
@@ -65,9 +80,19 @@ public:
|
||||
__builtin_prefetch(&table[hashKey % table.size()]);
|
||||
}
|
||||
|
||||
TTEntry GetRawEntry(U64 &hashKey) {
|
||||
TTEntry *current = &table[hashKey % table.size()];
|
||||
return *current;
|
||||
void IncreaseAge() {
|
||||
age++;
|
||||
}
|
||||
|
||||
TTEntry GetEntry(U64 &hashKey) {
|
||||
TTBucket *current = &table[hashKey % table.size()];
|
||||
if (current->depthPreferred.hashKey == hashKey) {
|
||||
return current->depthPreferred;
|
||||
} else if (current->alwaysReplace.hashKey == hashKey) {
|
||||
return current->alwaysReplace;
|
||||
}
|
||||
|
||||
return TTEntry();
|
||||
}
|
||||
|
||||
// returns used space per mill
|
||||
@@ -75,13 +100,13 @@ public:
|
||||
int count = 0;
|
||||
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
if (table[i]) count++;
|
||||
if (table[i].depthPreferred || table[i].alwaysReplace) count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
TTEntry ReadEntry(U64 &hashKey, int depth, int alpha, int beta);
|
||||
|
||||
void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move besteMove);
|
||||
void WriteEntry(U64 &hashKey, int depth, int score, int nodeType, Move bestMove, bool ttpv);
|
||||
};
|
||||
|
||||
extern TTable SharedTT;
|
||||
+49
-29
@@ -1,38 +1,58 @@
|
||||
#pragma once
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
//#define TUNING
|
||||
|
||||
#define AUTO_STEP(min_val, max_val) ((max_val - min_val) / 20.0)
|
||||
#define AUTO_R_END_INT(min_val, max_val) \
|
||||
(0.002 / (std::min(0.5, static_cast<double>(AUTO_STEP(min_val, max_val))) / 0.5))
|
||||
#define AUTO_R_END_FLOAT 0.002
|
||||
|
||||
#define TUNABLE_LIST \
|
||||
X_DOUBLE(lmrBaseQuiet, 1.1, 0.5, 1.5) \
|
||||
X_DOUBLE(lmrDivisorQuiet, 2.33, 1.5, 3.5) \
|
||||
X_DOUBLE(lmrBaseNoisy, 0.41, 0.5, 1.5) \
|
||||
X_DOUBLE(lmrDivisorNoisy, 3.20, 1.5, 3.5) \
|
||||
X_INT(lmrCutnode, 2048, 1024, 3072) \
|
||||
X_INT(lmrIsPV, 1024, 512, 2048) \
|
||||
X_INT(lmrHistoryDivisor, 8192, 6000, 10000) \
|
||||
X_INT(lmrImproving, 1024, 6000, 10000) \
|
||||
X_INT(lmrCorrplexity, 1024, 6000, 10000) \
|
||||
X_INT(rfpBase, 25, 50, 200) \
|
||||
X_INT(rfpMargin, 100, 50, 200) \
|
||||
X_INT(fpMargin, 100, 50, 200) \
|
||||
X_INT(razoringScalar, 200, 50, 200) \
|
||||
X_INT(doubleExtMargin, 30, 10, 60) \
|
||||
X_INT(seeQuietThreshold, -80, -150, -20) \
|
||||
X_INT(seeNoisyThreshold, -30, -80, 20) \
|
||||
X_INT(aspInitialDelta, 30, 20, 80) \
|
||||
X_INT(nmpBetaMargin, 30, 20, 80) \
|
||||
X_INT(seePawnValue, 100, 80, 120) \
|
||||
X_INT(seeKnightValue, 300, 250, 350) \
|
||||
X_INT(seeBishopValue, 350, 300, 400) \
|
||||
X_INT(seeRookValue, 500, 400, 600) \
|
||||
X_INT(seeQueenValue, 900, 750, 1100) \
|
||||
X_INT(seeQsThreshold, 0, -50, 50) \
|
||||
X_INT(seeOrderingThreshold, -100, -120, -80) \
|
||||
X_INT(historyBonusMultiplier, 300, 270, 330) \
|
||||
X_INT(historyBonusSub, 250, 230, 280)
|
||||
X_DOUBLE(lmrBaseQuiet, 1.1029723689888005, 0.6, 1.6) \
|
||||
X_DOUBLE(lmrDivisorQuiet, 2.3063513397139945, 1.6, 3.6) \
|
||||
X_DOUBLE(lmrBaseNoisy, 0.451805556082857, 0.1, 1.1) \
|
||||
X_DOUBLE(lmrDivisorNoisy, 3.1355330830404338, 2.0, 4.5) \
|
||||
X_INT(lmrCutnode, 2005, 1024, 3072) \
|
||||
X_INT(lmrIsPV, 982, 256, 2048) \
|
||||
X_INT(lmrTTPV, 936, 256, 2048) \
|
||||
X_INT(lmrHistoryDivisor, 8377, 4096, 16384) \
|
||||
X_INT(lmrImproving, 1131, 256, 2048) \
|
||||
X_INT(lmrCorrplexity, 830, 256, 2048) \
|
||||
X_INT(lmrTTPVFailLow, 913, 256, 2048) \
|
||||
X_INT(lmrDeeperBase, 46, 0, 96) \
|
||||
X_INT(rfpBase, 21, 0, 100) \
|
||||
X_INT(rfpMargin, 97, 50, 200) \
|
||||
X_INT(fpMargin, 86, 50, 200) \
|
||||
X_INT(qsFPMargin, 100, 50, 200) \
|
||||
X_INT(probcutBetaMargin, 190, 100, 300) \
|
||||
X_INT(razoringScalar, 195, 80, 320) \
|
||||
X_INT(doubleExtMargin, 32, 8, 64) \
|
||||
X_INT(seeQuietThreshold, -81, -160, 0) \
|
||||
X_INT(seeNoisyThreshold, -32, -120, 40) \
|
||||
X_INT(aspInitialDelta, 26, 8, 96) \
|
||||
X_INT(nmpBetaMargin, 25, 8, 96) \
|
||||
X_INT(seePawnValue, 98, 80, 140) \
|
||||
X_INT(seeKnightValue, 301, 240, 420) \
|
||||
X_INT(seeBishopValue, 346, 250, 450) \
|
||||
X_INT(seeRookValue, 507, 400, 700) \
|
||||
X_INT(seeQueenValue, 924, 700, 1300) \
|
||||
X_INT(seeQsThreshold, -13, -120, 120) \
|
||||
X_INT(seeOrderingThreshold, -102, -200, 0) \
|
||||
X_INT(ldseMargin, 26, 0, 96) \
|
||||
X_INT(historyBonusMultiplier, 306, 280, 320) \
|
||||
X_INT(historyBonusSub, 252, 235, 265) \
|
||||
X_INT(historyMalusMultiplier, 301, 280, 320) \
|
||||
X_INT(historyMalusSub, 250, 235, 265) \
|
||||
X_INT(contHistoryBonusMultiplier, 300, 280, 320) \
|
||||
X_INT(contHistoryBonusSub, 252, 235, 265) \
|
||||
X_INT(contHistoryMalusMultiplier, 300, 280, 320) \
|
||||
X_INT(contHistoryMalusSub, 249, 235, 265) \
|
||||
X_INT(captHistoryBonusMultiplier, 302, 280, 320) \
|
||||
X_INT(captHistoryBonusSub, 249, 235, 265) \
|
||||
X_INT(captHistoryMalusMultiplier, 300, 280, 320) \
|
||||
X_INT(captHistoryMalusSub, 249, 235, 265)
|
||||
|
||||
#ifdef TUNING
|
||||
#define X_DOUBLE(name, default_val, min_val, max_val) \
|
||||
@@ -61,14 +81,14 @@ inline void PrintTunables() {
|
||||
<< ", " << min_val \
|
||||
<< ", " << max_val \
|
||||
<< ", " << name##_step \
|
||||
<< ", 0.002" << std::endl;
|
||||
<< ", " << AUTO_R_END_FLOAT << std::endl;
|
||||
|
||||
#define X_INT(name, default_val, min_val, max_val) \
|
||||
std::cout << #name << ", int, " << name \
|
||||
<< ", " << min_val \
|
||||
<< ", " << max_val \
|
||||
<< ", " << name##_step \
|
||||
<< ", 0.002" << std::endl;
|
||||
<< ", " << AUTO_R_END_INT(min_val, max_val) << std::endl;
|
||||
|
||||
TUNABLE_LIST
|
||||
|
||||
|
||||
+39
-16
@@ -91,17 +91,19 @@ static double ReadParam(const std::string& param, const std::string &command) {
|
||||
template <SEARCH::searchMode mode>
|
||||
static void ThreadFunc(Board* board, SearchParams params, SEARCH::SearchContext* ctx) {
|
||||
SEARCH::SearchPosition<mode>(*board, params, ctx);
|
||||
delete ctx;
|
||||
}
|
||||
|
||||
static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx) {
|
||||
static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx, int id) {
|
||||
std::thread searchThread;
|
||||
|
||||
SEARCH::SearchContext* ctxCopy = new SEARCH::SearchContext(*ctx);
|
||||
if (id == 0)
|
||||
ctxCopy->doPrint = true;
|
||||
if (params.nodes) {
|
||||
searchThread = std::thread(ThreadFunc<SEARCH::nodesMode>, &board, params, ctx);
|
||||
searchThread = std::thread(ThreadFunc<SEARCH::nodesMode>, &board, params, ctxCopy);
|
||||
} else {
|
||||
searchThread = std::thread(ThreadFunc<SEARCH::normal>, &board, params, ctx);
|
||||
searchThread = std::thread(ThreadFunc<SEARCH::normal>, &board, params, ctxCopy);
|
||||
}
|
||||
|
||||
searchThread.detach();
|
||||
}
|
||||
|
||||
@@ -109,24 +111,29 @@ static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchC
|
||||
// Unix/Linux implementation using pthread
|
||||
template <SEARCH::searchMode mode>
|
||||
static void* ThreadFunc(void* arg) {
|
||||
auto* tup = static_cast<std::tuple<Board*, SearchParams, SEARCH::SearchContext*>*>(arg);
|
||||
SEARCH::SearchPosition<mode>(*std::get<0>(*tup), std::get<1>(*tup), std::get<2>(*tup));
|
||||
auto* tup = static_cast<std::tuple<Board, SearchParams, SEARCH::SearchContext*>*>(arg);
|
||||
SEARCH::SearchContext* ctx = std::get<2>(*tup);
|
||||
SEARCH::SearchPosition<mode>(std::get<0>(*tup), std::get<1>(*tup), ctx);
|
||||
delete ctx;
|
||||
delete tup;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx) {
|
||||
static void StartSearchThread(Board& board, SearchParams params, SEARCH::SearchContext* ctx, int id) {
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setstacksize(&attr, 8 * 1024 * 1024); // 8 MB stack
|
||||
|
||||
pthread_t thread;
|
||||
|
||||
SEARCH::SearchContext* ctxCopy = new SEARCH::SearchContext(*ctx);
|
||||
if (id == 0)
|
||||
ctxCopy->doPrint = true;
|
||||
if (params.nodes) {
|
||||
auto* arg = new std::tuple<Board*, SearchParams, SEARCH::SearchContext*>(&board, params, ctx);
|
||||
auto* arg = new std::tuple<Board, SearchParams, SEARCH::SearchContext*>(board, params, ctxCopy);
|
||||
pthread_create(&thread, &attr, ThreadFunc<SEARCH::nodesMode>, arg);
|
||||
} else {
|
||||
auto* arg = new std::tuple<Board*, SearchParams, SEARCH::SearchContext*>(&board, params, ctx);
|
||||
auto* arg = new std::tuple<Board, SearchParams, SEARCH::SearchContext*>(board, params, ctxCopy);
|
||||
pthread_create(&thread, &attr, ThreadFunc<SEARCH::normal>, arg);
|
||||
}
|
||||
|
||||
@@ -153,13 +160,26 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* c
|
||||
params.btime = 99999999;
|
||||
}
|
||||
|
||||
StartSearchThread(board, params, ctx);
|
||||
for (int i = 0; i < threads; i++) {
|
||||
StartSearchThread(board, params, ctx, i);
|
||||
}
|
||||
}
|
||||
|
||||
static void SetOption(std::string& command, SEARCH::SearchContext* ctx) {
|
||||
if (command.find("Hash") != std::string::npos) {
|
||||
U64 hashSize = (ReadParam("value", command) * 1000000ULL) / sizeof(TTEntry);
|
||||
ctx->TT.Resize(hashSize);
|
||||
ctx->TT->Resize(hashSize);
|
||||
return;
|
||||
}
|
||||
|
||||
if (command.find("Threads") != std::string::npos) {
|
||||
threads = ReadParam("value", command);
|
||||
return;
|
||||
}
|
||||
|
||||
if (command.find("UCI_ShowWDL") != std::string::npos) {
|
||||
UCIShowWDL = command.find("value true") != std::string::npos
|
||||
|| command.find("value 1") != std::string::npos;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -186,6 +206,7 @@ static void SetOption(std::string& command, SEARCH::SearchContext* ctx) {
|
||||
if (v < min_val) v = min_val; \
|
||||
if (v > max_val) v = max_val; \
|
||||
paramName = v; \
|
||||
SEARCH::RefreshTunableCaches(); \
|
||||
return; \
|
||||
}
|
||||
|
||||
@@ -195,6 +216,7 @@ static void SetOption(std::string& command, SEARCH::SearchContext* ctx) {
|
||||
if (v < min_val) v = min_val; \
|
||||
if (v > max_val) v = max_val; \
|
||||
paramName = v; \
|
||||
SEARCH::RefreshTunableCaches(); \
|
||||
return; \
|
||||
}
|
||||
|
||||
@@ -206,10 +228,11 @@ static void SetOption(std::string& command, SEARCH::SearchContext* ctx) {
|
||||
}
|
||||
|
||||
static void PrintEngineInfo() {
|
||||
std::cout << "id name Eleanor v3.0" << std::endl;
|
||||
std::cout << "id name Eleanor v4.0" << std::endl;
|
||||
std::cout << "id author rektdie" << std::endl;
|
||||
std::cout << "option name Hash type spin default 8 min 1 max 1024" << std::endl;
|
||||
std::cout << "option name Threads type spin default 1 min 1 max 1" << std::endl;
|
||||
std::cout << "option name Threads type spin default 1 min 1 max 512" << std::endl;
|
||||
std::cout << "option name UCI_ShowWDL type check default false" << std::endl;
|
||||
|
||||
#ifdef TUNING
|
||||
PrintTunablesUCI();
|
||||
@@ -245,7 +268,7 @@ void UCILoop(Board &board) {
|
||||
board.SetByFen(StartingFen);
|
||||
|
||||
// Clearing
|
||||
ctx->TT.Clear();
|
||||
ctx->TT->Clear();
|
||||
|
||||
ctx->killerMoves = {};
|
||||
ctx->history.Clear();
|
||||
@@ -272,7 +295,7 @@ void UCILoop(Board &board) {
|
||||
// parse UCI "stop" command
|
||||
if (input.find("stop") != std::string::npos) {
|
||||
// stop the loop
|
||||
ctx->searchStopped = true;
|
||||
searchStopped = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
#include "board.h"
|
||||
|
||||
inline bool UCIEnabled = false;
|
||||
inline bool UCIShowWDL = false;
|
||||
inline int threads = 1;
|
||||
inline bool searchStopped = false;
|
||||
|
||||
class SearchParams {
|
||||
public:
|
||||
@@ -13,6 +16,8 @@ public:
|
||||
int movesToGo = 0;
|
||||
int nodes = 0;
|
||||
|
||||
int threads = 1;
|
||||
|
||||
SearchParams(){}
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
#include "board.h"
|
||||
|
||||
struct WinRateParams {
|
||||
double a;
|
||||
double b;
|
||||
};
|
||||
|
||||
// From SF
|
||||
inline WinRateParams winRateParams(const Board& board) {
|
||||
int material = board.pieces[Pawn].PopCount() +
|
||||
3 * board.pieces[Knight].PopCount() +
|
||||
3 * board.pieces[Bishop].PopCount() +
|
||||
5 * board.pieces[Rook].PopCount() +
|
||||
9 * board.pieces[Queen].PopCount();
|
||||
|
||||
// The fitted model only uses data for material counts in [17, 78], and is anchored at count 58.
|
||||
double m = std::clamp(material, 17, 78) / 58.0;
|
||||
|
||||
// Return a = p_a(material) and b = p_b(material), see github.com/official-stockfish/WDL_model
|
||||
constexpr double as[] = {-977.81543545, 2519.99289616, -2279.71042020, 1197.60065700};
|
||||
constexpr double bs[] = {-224.61509659, 557.03359920, -440.33347428, 226.68436468};
|
||||
|
||||
double a = (((as[0] * m + as[1]) * m + as[2]) * m) + as[3];
|
||||
double b = (((bs[0] * m + bs[1]) * m + bs[2]) * m) + bs[3];
|
||||
|
||||
return {a, b};
|
||||
}
|
||||
|
||||
// The win rate model is 1 / (1 + exp((a - eval) / b)), where a = p_a(material) and b = p_b(material).
|
||||
// It fits the LTC fishtest statistics rather accurately.
|
||||
inline int winRateModel(int v, const Board& board) {
|
||||
auto [a, b] = winRateParams(board);
|
||||
|
||||
// Return the win rate in per mille units, rounded to the nearest integer.
|
||||
return int(0.5 + 1000 / (1 + std::exp((a - static_cast<double>(v)) / b)));
|
||||
}
|
||||
|
||||
struct WDLTriplet {
|
||||
int wins;
|
||||
int draws;
|
||||
int losses;
|
||||
};
|
||||
|
||||
inline WDLTriplet getWDL(int v, const Board& board) {
|
||||
int wins = std::clamp(winRateModel(v, board), 0, 1000);
|
||||
int losses = std::clamp(winRateModel(-v, board), 0, 1000);
|
||||
int draws = 1000 - wins - losses;
|
||||
|
||||
if (draws < 0) {
|
||||
if (wins >= losses) {
|
||||
wins += draws;
|
||||
} else {
|
||||
losses += draws;
|
||||
}
|
||||
|
||||
draws = 0;
|
||||
}
|
||||
|
||||
return {wins, draws, losses};
|
||||
}
|
||||
|
||||
inline int scaleEval(int eval, const Board& board) {
|
||||
auto [a, b] = winRateParams(board);
|
||||
|
||||
return std::round(100 * eval / a);
|
||||
}
|
||||
Reference in new issue
Block a user