Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
370dc43660 | ||
|
|
f1b99803a1 | ||
|
|
7595b0a3fd | ||
|
|
cdedcbb5f5 | ||
|
|
49994dc480 | ||
|
|
6c8869040f | ||
|
|
6288bdc3f8 | ||
|
|
9e068e3f7d | ||
|
|
7ac8efa027 | ||
|
|
5754772a70 | ||
|
|
90bdd3cc96 | ||
|
|
d35eb66dbc | ||
|
|
2e46f0d1cd | ||
|
|
9ce97946d5 | ||
|
|
90cfd93135 | ||
|
|
a56e983bf7 | ||
|
|
66865fa84e | ||
|
|
acee84c134 | ||
|
|
2474c13fe0 | ||
|
|
d37ba0cdaa | ||
|
|
925289015f | ||
|
|
318d52b2dd | ||
|
|
6ed9e6f0c8 | ||
|
|
c6f2c3cacb | ||
|
|
b3472092e2 | ||
|
|
d76e7796bc | ||
|
|
0d3c23ed73 | ||
|
|
222a079c1f | ||
|
|
d5d51c9888 | ||
|
|
3a02122fd4 | ||
|
|
e8aec47b71 | ||
|
|
17a0a97896 | ||
|
|
be6585268b | ||
|
|
916da35e4f | ||
|
|
5c247f5e1d | ||
|
|
a8ac4c7feb | ||
|
|
f71e6c20e1 | ||
|
|
1e62f6adef | ||
|
|
0ff5a226ee | ||
|
|
670907dd8c | ||
|
|
5c6d75e9ef | ||
|
|
6b5c1bbaa7 | ||
|
|
480216e1c8 | ||
|
|
5358bd599e | ||
|
|
435805a207 | ||
|
|
2ef378d4d2 | ||
|
|
0dfbb1c27b | ||
|
|
b152fd40eb | ||
|
|
93339c7728 | ||
|
|
4a050aeddb | ||
|
|
136af4adef | ||
|
|
307bbfede2 | ||
|
|
e7d8044745 | ||
|
|
10a0f502ae | ||
|
|
1dd0e33d2b | ||
|
|
729d24c7c8 | ||
|
|
e2eb763012 | ||
|
|
b4b804027c | ||
|
|
9d26dc028d | ||
|
|
ca51e59dd9 | ||
|
|
0cb4ba1a29 | ||
|
|
83e36b0883 | ||
|
|
bf9b97aaba | ||
|
|
861a354215 | ||
|
|
c108bfa786 | ||
|
|
271ec4dc2a | ||
|
|
78aa6e2338 | ||
|
|
5bf770c1a9 | ||
|
|
32b2b9c92e | ||
|
|
4c04bf2830 | ||
|
|
5519f300b5 | ||
|
|
1c4762e872 | ||
|
|
32691d2d65 | ||
|
|
3e652df8e8 | ||
|
|
f32251ac98 | ||
|
|
cabebb502e | ||
|
|
6d2500428e | ||
|
|
de2d441b0f | ||
|
|
4123ab731f | ||
|
|
014d5e05b1 | ||
|
|
f81a730994 | ||
|
|
07abe2529e | ||
|
|
d2586bec7d | ||
|
|
f5f51496b3 | ||
|
|
4579172fab | ||
|
|
a3d505aec4 | ||
|
|
9b86aaa0a1 | ||
|
|
1ca2ba0352 | ||
|
|
95945633f1 | ||
|
|
cc64cbd6a9 | ||
|
|
875363376b | ||
|
|
21b64a271a | ||
|
|
cba880886b | ||
|
|
768b8ed178 | ||
|
|
4b2f342d85 | ||
|
|
cb9051c477 | ||
|
|
9f28552643 | ||
|
|
070528286c | ||
|
|
b6c9bbbb8d | ||
|
|
c365bd58d8 | ||
|
|
40339b2d45 | ||
|
|
6ccd416840 | ||
|
|
2e1f84bda5 | ||
|
|
bf7adcb06b | ||
|
|
1cf5a2971d | ||
|
|
8d552cefd6 | ||
|
|
9bb603200c | ||
|
|
ee404a9992 | ||
|
|
cc8a9a625b | ||
|
|
f788005e40 | ||
|
|
1e073bb30d | ||
|
|
e46076116f | ||
|
|
ab68f5a6b6 | ||
|
|
769979cafe | ||
|
|
06b24ef1f2 | ||
|
|
3451f825bb | ||
|
|
a52bb1f967 | ||
|
|
86590918e1 | ||
|
|
5f0ce8c1cf | ||
|
|
e282098265 |
No files matched your search
@@ -33,13 +33,19 @@ LDFLAGS := -O3 -flto
|
|||||||
|
|
||||||
ifeq ($(PLATFORM),mac)
|
ifeq ($(PLATFORM),mac)
|
||||||
ifeq ($(ARCH),arm64)
|
ifeq ($(ARCH),arm64)
|
||||||
ARCH_FLAGS :=
|
# Apple Silicon
|
||||||
|
ARCH_FLAGS := -mcpu=apple-m1
|
||||||
else
|
else
|
||||||
ARCH_FLAGS := -march=native
|
ARCH_FLAGS := -march=native
|
||||||
endif
|
endif
|
||||||
else
|
else
|
||||||
ARCH_FLAGS := -march=native
|
ifeq ($(ARCH),aarch64)
|
||||||
LDFLAGS += -fuse-ld=lld -static
|
# Generic Linux ARM64
|
||||||
|
ARCH_FLAGS := -mcpu=native
|
||||||
|
else
|
||||||
|
ARCH_FLAGS := -march=native
|
||||||
|
LDFLAGS += -fuse-ld=lld -static
|
||||||
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
CXXFLAGS += $(ARCH_FLAGS)
|
CXXFLAGS += $(ARCH_FLAGS)
|
||||||
|
|||||||
@@ -1,37 +1,32 @@
|
|||||||
|
<div align="center">
|
||||||
<p align="center">
|
<p>
|
||||||
<img src="https://i.postimg.cc/NjR2CdBB/Eleanor.png" alt="Eleanor Chess Engine">
|
<img src="https://i.postimg.cc/bvDr095w/image.png" alt="Eleanor Chess Engine" width="300">
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<h1 align="center">Eleanor Chess Engine</h1>
|
<h1 align="center">Eleanor Chess Engine</h1>
|
||||||
|
|
||||||
## Search Features
|

|
||||||
|
<br>
|
||||||
|
[](https://rektdie.pythonanywhere.com/)
|
||||||
|
[](https://rektgen.online/)<br>
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
| 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 | ? | ?
|
||||||
|
| 2.0 | 2025-08-23 | 3312 (#115) | 3392 (#105)
|
||||||
|
| 1.0 | 2025-06-02 | 2856 (#244) | 2759 (#256)
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
- Negamax
|
|
||||||
- Quiescent Search
|
|
||||||
- Transposition Table
|
|
||||||
- Move Ordering (TT, SEE + MVV-LVA, Killers, History)
|
|
||||||
- Iterative Deepening
|
|
||||||
- Aspiration Windows
|
|
||||||
- Principal Variation Search
|
|
||||||
- Reverse Futility Pruning
|
|
||||||
- Improving
|
|
||||||
- Null Move Pruning
|
|
||||||
- Late Move Reductions
|
|
||||||
- Log formula
|
|
||||||
- Cutnode
|
|
||||||
- History
|
|
||||||
- Late Move Pruning
|
|
||||||
- Futility Pruning
|
|
||||||
- QS SEE Pruning
|
|
||||||
- PVS SEE Pruning
|
|
||||||
|
|
||||||
## Evaluation
|
## Evaluation
|
||||||
|
|
||||||
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.
|
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:** `(768 -> 256)x2 -> 1x8`
|
**Architecture:** `(768hm -> 1536)x2 -> 1x8`
|
||||||
A basic 256-hidden-layer *perspective net* with 8 output buckets.
|
A 1536-hidden-layer *perspective net* with 8 output buckets and horizontal mirroring.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -63,6 +58,8 @@ You can interact with the engine via these commands:
|
|||||||
|
|
||||||
The engine can be used in any GUI with **UCI support**.
|
The engine can be used in any GUI with **UCI support**.
|
||||||
|
|
||||||
|
[Testing site](https://rektdie.pythonanywhere.com/)
|
||||||
|
|
||||||
## Sources I Used
|
## Sources I Used
|
||||||
|
|
||||||
- [Stockfish Discord Community](https://discord.com/invite/GWDRS3kU6R) — The best possible source for an engine dev.
|
- [Stockfish Discord Community](https://discord.com/invite/GWDRS3kU6R) — The best possible source for an engine dev.
|
||||||
@@ -73,4 +70,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.**
|
**The project is a work in progress and actively developed. Contributions are highly appreciated.**
|
||||||
|
|
||||||
###### Last updated: 2025-06-02
|
###### Last updated: 2025-12-05
|
||||||
+50
-36
@@ -1,45 +1,59 @@
|
|||||||
[Event "?"]
|
[Event "?"]
|
||||||
[Site "?"]
|
[Site "?"]
|
||||||
[Date "2025.04.01"]
|
[Date "2025.09.04"]
|
||||||
[Round "1"]
|
[Round "1"]
|
||||||
[White "Eleanor-base"]
|
[White "Eleanor-base"]
|
||||||
[Black "Eleanor-dev"]
|
[Black "Eleanor-dev"]
|
||||||
[Result "1-0"]
|
[Result "1-0"]
|
||||||
[FEN "2r5/1k6/2p4p/4p3/3b1p1B/5B1P/2R5/7K b - - 0 1"]
|
[FEN "rn1qk1nr/ppp2pRp/3bp3/3p4/3P4/2P2P1P/PP2PP2/RNBQKB2 b Qkq - 0 1"]
|
||||||
[GameDuration "00:00:05"]
|
[GameDuration "00:01:31"]
|
||||||
[GameEndTime "2025-04-01T18:10:20.331 Central Europe Daylight Time"]
|
[GameEndTime "2025-09-04T22:39:24.808 CEST"]
|
||||||
[GameStartTime "2025-04-01T18:10:14.365 Central Europe Daylight Time"]
|
[GameStartTime "2025-09-04T22:37:53.089 CEST"]
|
||||||
[PlyCount "58"]
|
[PlyCount "84"]
|
||||||
[SetUp "1"]
|
[SetUp "1"]
|
||||||
[Termination "abandoned"]
|
[Termination "abandoned"]
|
||||||
[TimeControl "2.53+0.03"]
|
[TimeControl "37.56+0.38"]
|
||||||
|
|
||||||
1... Kb6 {-0.43 11/0 127 176492} 2. Kg2 {+0.45 10/0 117 182773}
|
1... Ne7 {-1.65 14/26 1861 1544983} 2. Rg1 {+1.64 12/23 2087 934687}
|
||||||
Kb5 {-0.43 10/0 139 43190} 3. Bf2 {+0.48 10/0 94 154681}
|
c5 {-1.74 15/32 1565 1167061} 3. f4 {+1.61 13/27 2002 772197}
|
||||||
Rg8+ {-0.41 10/0 104 181006} 4. Kf1 {+0.43 11/0 137 107880}
|
Nd7 {-1.59 15/31 1455 1090717} 4. e3 {+1.84 14/31 1753 1387350}
|
||||||
c5 {-0.42 11/0 114 186919} 5. Ke2 {+0.42 10/0 131 26558}
|
Qb6 {-1.65 14/31 1385 1273463} 5. a4 {+1.98 14/28 1505 1375155}
|
||||||
Rd8 {-0.31 11/0 126 178073} 6. Rd2 {+0.36 8/0 129 64726}
|
Qc7 {-2.11 14/29 1849 830961} 6. dxc5 {+1.98 15/30 1571 1101192}
|
||||||
c4 {+0.11 11/0 124 101858} 7. Bxd4 {-0.11 11/0 86 135900}
|
Nxc5 {-2.16 15/34 1778 545375} 7. a5 {+1.90 14/29 1555 1021903}
|
||||||
Rxd4 {+0.10 11/0 124 121262} 8. Rxd4 {-0.01 13/0 117 207646}
|
a6 {-2.22 15/33 1706 667700} 8. Nd2 {+2.38 14/29 1267 922229}
|
||||||
exd4 {0.00 14/0 95 197856} 9. h4 {0.00 13/0 105 197264}
|
O-O-O {-2.34 14/31 1641 580655} 9. b4 {+2.62 14/31 1463 975771}
|
||||||
Kb4 {0.00 14/0 91 195163} 10. Kd2 {0.00 13/0 111 105382}
|
Ne4 {-2.37 15/39 1336 932258} 10. Nxe4 {+2.55 13/31 1579 228776}
|
||||||
Kc5 {0.00 13/0 109 62420} 11. h5 {0.00 14/0 78 150612} Kb4 {0.00 16/0 79 181784}
|
dxe4 {-2.85 15/31 1004 694847} 11. Bd2 {+3.02 13/30 1247 753167}
|
||||||
12. Bd1 {0.00 15/0 86 193192} Kc5 {0.00 16/0 97 111839}
|
Ng6 {-2.40 14/35 1118 862794} 12. Qb1 {+2.40 12/26 1475 339681}
|
||||||
13. Kc2 {0.00 17/0 90 192373} Kb4 {0.00 17/0 95 120438}
|
f5 {-1.97 13/31 1465 304169} 13. Qb3 {+2.25 12/34 957 597544}
|
||||||
14. Bf3 {0.00 17/0 76 176571} Kc5 {0.00 17/0 101 66844}
|
Nh4 {-2.21 14/38 1409 432849} 14. Be2 {+2.30 13/35 1343 843064}
|
||||||
15. Ba8 {0.00 18/0 75 165249} Kb4 {0.00 18/0 86 198233}
|
Nf3+ {-2.18 14/40 1355 318067} 15. Bxf3 {+2.49 14/39 1346 645681}
|
||||||
16. Bf3 {0.00 18/0 77 183908} d3+ {+0.01 16/0 69 164278}
|
exf3 {-2.59 15/30 1115 864898} 16. O-O-O {+2.85 14/35 1297 425564}
|
||||||
17. Kd2 {-0.03 16/0 81 145118} Kb3 {0.00 17/0 84 107987}
|
Rhe8 {-2.11 13/34 1269 275858} 17. c4 {+2.35 13/30 1249 86929}
|
||||||
18. Be4 {0.00 16/0 61 125373} f3 {+0.09 17/0 75 163654}
|
e5 {-1.53 14/40 1188 1007892} 18. Bc3 {+2.71 12/37 1207 154226}
|
||||||
19. Bxd3 {-0.01 16/0 86 109653} cxd3 {+0.57 18/0 55 140500}
|
exf4 {-0.71 13/45 1186 280646} 19. c5 {+0.71 13/34 883 618110}
|
||||||
20. Kxd3 {-0.97 22/0 73 227257} f2 {+0.98 22/0 71 204108}
|
Be5 {-0.52 13/65 1145 440206} 20. Rxd8+ {+0.56 13/36 861 757974}
|
||||||
21. Ke2 {-0.99 20/0 81 29772} Kc4 {+1.48 22/0 62 180691}
|
Qxd8 {-0.55 15/56 1106 373775} 21. Bxe5 {+0.98 14/49 845 689690}
|
||||||
22. Kxf2 {-2.11 23/0 59 175088} Kd4 {+2.51 27/0 52 189697}
|
Rxe5 {-0.87 14/83 799 511973} 22. exf4 {+0.98 13/36 1092 370730}
|
||||||
23. Kf3 {-3.34 26/0 62 187791} Ke5 {+2.50 32/0 48 167062}
|
Re2 {-0.87 13/89 751 367978} 23. Rg8 {+1.05 13/43 1056 334030}
|
||||||
24. Ke3 {-2.24 28/0 54 178563} Kf5 {+2.15 31/0 72 166014}
|
Re8 {-0.87 13/79 1030 242199} 24. Rxe8 {+1.13 12/34 1023 141620}
|
||||||
25. Kf3 {-2.22 32/0 52 188757} Kg5 {0.00 35/0 49 143292}
|
Qxe8 {-0.87 13/41 998 22345} 25. Qd1 {+1.46 11/36 991 227470}
|
||||||
26. Ke4 {-2.09 32/0 62 209505} Kxh5 {0.00 39/0 48 159744}
|
Qe4 {-0.89 12/59 968 192246} 26. Qd2 {+1.17 13/42 856 592181}
|
||||||
27. Kf5 {-2.09 33/0 70 226933} Kh4 {0.00 52/0 46 161548}
|
Qc4+ {-0.90 11/95 891 510827} 27. Kb2 {+1.46 12/38 937 154015}
|
||||||
28. Kf4 {0.00 37/0 51 178144} h5 {0.00 64/0 29 95868}
|
h5 {-1.01 11/66 913 404234} 28. Qc3 {+1.38 13/37 909 437481}
|
||||||
29. Kf3 {0.00 51/0 46 165319} Kh3 {0.00 64/0 15 14259}
|
Qe2+ {-0.73 10/76 886 55851} 29. Qc2 {+1.42 14/34 882 272787}
|
||||||
30. Kf4 {0.00 52/0 64 186059, Black disconnects} 1-0
|
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
|
||||||
+41
-41
@@ -4,82 +4,82 @@
|
|||||||
|
|
||||||
namespace ACC {
|
namespace ACC {
|
||||||
|
|
||||||
int CalculateIndex(bool perspective, bool side, int pieceType, int square) {
|
int CalculateIndex(bool perspective, bool side, int pieceType, int square, bool mirrored) {
|
||||||
if (perspective == Black)
|
if (perspective == Black)
|
||||||
{
|
{
|
||||||
side = 1 - side; // Flip side
|
side = 1 - side; // Flip side
|
||||||
square = square ^ 0b111000; // Mirror square
|
square = square ^ 0b111000; // Mirror square
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mirrored)
|
||||||
|
square ^= 7;
|
||||||
|
|
||||||
return side * 64 * 6 + pieceType * 64 + square;
|
return side * 64 * 6 + pieceType * 64 + square;
|
||||||
}
|
}
|
||||||
|
|
||||||
// All friendly, for quiets
|
// 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) {
|
||||||
int addW = CalculateIndex(White, stm, addPT, add);
|
int addW = CalculateIndex(White, stm, addPT, add, mirroredWhite);
|
||||||
int addB = CalculateIndex(Black, stm, addPT, add);
|
int addB = CalculateIndex(Black, stm, addPT, add, mirroredBlack);
|
||||||
|
|
||||||
int subW = CalculateIndex(White, stm, subPT, sub);
|
int subW = CalculateIndex(White, stm, subPT, sub, mirroredWhite);
|
||||||
int subB = CalculateIndex(Black, stm, subPT, sub);
|
int subB = CalculateIndex(Black, stm, subPT, sub, mirroredBlack);
|
||||||
|
|
||||||
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
||||||
white[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i];
|
white[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i]
|
||||||
black[i] += NNUE::net.accumulator_weights[addB * NNUE::HL_SIZE + i];
|
- NNUE::net.accumulator_weights[subW * NNUE::HL_SIZE + i];
|
||||||
|
|
||||||
white[i] -= NNUE::net.accumulator_weights[subW * NNUE::HL_SIZE + i];
|
black[i] += NNUE::net.accumulator_weights[addB * NNUE::HL_SIZE + i]
|
||||||
black[i] -= NNUE::net.accumulator_weights[subB * NNUE::HL_SIZE + i];
|
- NNUE::net.accumulator_weights[subB * NNUE::HL_SIZE + i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Captures
|
// 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) {
|
||||||
int addW = CalculateIndex(White, stm, addPT, add);
|
int addW = CalculateIndex(White, stm, addPT, add, mirroredWhite);
|
||||||
int addB = CalculateIndex(Black, stm, addPT, add);
|
int addB = CalculateIndex(Black, stm, addPT, add, mirroredBlack);
|
||||||
|
|
||||||
int subW1 = CalculateIndex(White, stm, subPT1, sub1);
|
int subW1 = CalculateIndex(White, stm, subPT1, sub1, mirroredWhite);
|
||||||
int subB1 = CalculateIndex(Black, stm, subPT1, sub1);
|
int subB1 = CalculateIndex(Black, stm, subPT1, sub1, mirroredBlack);
|
||||||
|
|
||||||
int subW2 = CalculateIndex(White, !stm, subPT2, sub2);
|
int subW2 = CalculateIndex(White, !stm, subPT2, sub2, mirroredWhite);
|
||||||
int subB2 = CalculateIndex(Black, !stm, subPT2, sub2);
|
int subB2 = CalculateIndex(Black, !stm, subPT2, sub2, mirroredBlack);
|
||||||
|
|
||||||
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
||||||
white[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i];
|
white[i] += NNUE::net.accumulator_weights[addW * NNUE::HL_SIZE + i]
|
||||||
black[i] += NNUE::net.accumulator_weights[addB * 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[subW1 * NNUE::HL_SIZE + i];
|
black[i] += NNUE::net.accumulator_weights[addB * NNUE::HL_SIZE + i]
|
||||||
black[i] -= NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i];
|
- NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i]
|
||||||
|
- NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i];
|
||||||
white[i] -= NNUE::net.accumulator_weights[subW2 * NNUE::HL_SIZE + i];
|
|
||||||
black[i] -= NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Castling
|
// 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) {
|
||||||
int addW1 = CalculateIndex(White, stm, addPT1, add1);
|
int addW1 = CalculateIndex(White, stm, addPT1, add1, mirroredWhite);
|
||||||
int addB1 = CalculateIndex(Black, stm, addPT1, add1);
|
int addB1 = CalculateIndex(Black, stm, addPT1, add1, mirroredBlack);
|
||||||
|
|
||||||
int addW2 = CalculateIndex(White, stm, addPT2, add2);
|
int addW2 = CalculateIndex(White, stm, addPT2, add2, mirroredWhite);
|
||||||
int addB2 = CalculateIndex(Black, stm, addPT2, add2);
|
int addB2 = CalculateIndex(Black, stm, addPT2, add2, mirroredBlack);
|
||||||
|
|
||||||
int subW1 = CalculateIndex(White, stm, subPT1, sub1);
|
int subW1 = CalculateIndex(White, stm, subPT1, sub1, mirroredWhite);
|
||||||
int subB1 = CalculateIndex(Black, stm, subPT1, sub1);
|
int subB1 = CalculateIndex(Black, stm, subPT1, sub1, mirroredBlack);
|
||||||
|
|
||||||
int subW2 = CalculateIndex(White, stm, subPT2, sub2);
|
int subW2 = CalculateIndex(White, stm, subPT2, sub2, mirroredWhite);
|
||||||
int subB2 = CalculateIndex(Black, stm, subPT2, sub2);
|
int subB2 = CalculateIndex(Black, stm, subPT2, sub2, mirroredBlack);
|
||||||
|
|
||||||
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
||||||
white[i] += NNUE::net.accumulator_weights[addW1 * NNUE::HL_SIZE + i];
|
white[i] += NNUE::net.accumulator_weights[addW1 * NNUE::HL_SIZE + i]
|
||||||
black[i] += NNUE::net.accumulator_weights[addB1 * 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[addW2 * NNUE::HL_SIZE + i];
|
black[i] += NNUE::net.accumulator_weights[addB1 * NNUE::HL_SIZE + i]
|
||||||
black[i] += NNUE::net.accumulator_weights[addB2 * NNUE::HL_SIZE + i];
|
+ NNUE::net.accumulator_weights[addB2 * NNUE::HL_SIZE + i]
|
||||||
|
- NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i]
|
||||||
white[i] -= NNUE::net.accumulator_weights[subW1 * NNUE::HL_SIZE + i];
|
- NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i];
|
||||||
black[i] -= NNUE::net.accumulator_weights[subB1 * NNUE::HL_SIZE + i];
|
|
||||||
|
|
||||||
white[i] -= NNUE::net.accumulator_weights[subW2 * NNUE::HL_SIZE + i];
|
|
||||||
black[i] -= NNUE::net.accumulator_weights[subB2 * NNUE::HL_SIZE + i];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,9 @@ using Accumulator = std::array<int16_t, NNUE::HL_SIZE>;
|
|||||||
struct AccumulatorPair {
|
struct AccumulatorPair {
|
||||||
alignas(32) Accumulator white;
|
alignas(32) Accumulator white;
|
||||||
alignas(32) Accumulator black;
|
alignas(32) Accumulator black;
|
||||||
|
|
||||||
|
bool mirroredWhite = false;
|
||||||
|
bool mirroredBlack = false;
|
||||||
|
|
||||||
// yoinked from Quinniboi - Prelude
|
// yoinked from Quinniboi - Prelude
|
||||||
void addSub(bool stm, int add, int addPT, int sub, int subPT);
|
void addSub(bool stm, int add, int addPT, int sub, int subPT);
|
||||||
@@ -15,6 +18,6 @@ struct AccumulatorPair {
|
|||||||
void addAddSubSub(bool stm, int add1, int addPT1, int add2, int addPT2, 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);
|
||||||
};
|
};
|
||||||
|
|
||||||
int CalculateIndex(bool perspective, bool side, int pieceType, int square);
|
int CalculateIndex(bool perspective, bool side, int pieceType, int square, bool mirrored);
|
||||||
|
|
||||||
}
|
}
|
||||||
+82
-41
@@ -9,7 +9,7 @@
|
|||||||
void Board::Reset() {
|
void Board::Reset() {
|
||||||
castlingRights = 0;
|
castlingRights = 0;
|
||||||
enPassantTarget = noEPTarget;
|
enPassantTarget = noEPTarget;
|
||||||
|
|
||||||
halfMoves = 0;
|
halfMoves = 0;
|
||||||
fullMoves = 1;
|
fullMoves = 1;
|
||||||
|
|
||||||
@@ -31,6 +31,8 @@ void Board::Reset() {
|
|||||||
currentMoveIndex = 0;
|
currentMoveIndex = 0;
|
||||||
|
|
||||||
hashKey = 0ULL;
|
hashKey = 0ULL;
|
||||||
|
pawnKey = 0ULL;
|
||||||
|
nonPawnKey = 0ULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Board::SetByFen(std::string_view fen) {
|
void Board::SetByFen(std::string_view fen) {
|
||||||
@@ -82,72 +84,83 @@ void Board::SetByFen(std::string_view fen) {
|
|||||||
occupied = colors[White] | colors[Black];
|
occupied = colors[White] | colors[Black];
|
||||||
hashKey = UTILS::GetHashKey(*this);
|
hashKey = UTILS::GetHashKey(*this);
|
||||||
MOVEGEN::GenThreatMaps(*this);
|
MOVEGEN::GenThreatMaps(*this);
|
||||||
MOVEGEN::GenerateMoves<All>(*this);
|
MOVEGEN::GenerateMoves<All>(*this, true);
|
||||||
ResetAccPair();
|
|
||||||
|
int whiteKingFile = (pieces[King] & colors[White]).getLS1BIndex() % 8;
|
||||||
|
int blackKingFile = (pieces[King] & colors[Black]).getLS1BIndex() % 8;
|
||||||
|
|
||||||
|
if (whiteKingFile > 3) {
|
||||||
|
accPair.mirroredWhite = true;
|
||||||
|
}
|
||||||
|
if (blackKingFile > 3) {
|
||||||
|
accPair.mirroredBlack = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ResetAccPair();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Board::GetFen() {
|
std::string Board::GetFen() {
|
||||||
std::ostringstream fen;
|
std::ostringstream fen;
|
||||||
|
|
||||||
for (int rank = 7; rank >= 0; --rank) {
|
for (int rank = 7; rank >= 0; --rank) {
|
||||||
int emptyCount = 0;
|
int emptyCount = 0;
|
||||||
|
|
||||||
for (int file = 0; file < 8; ++file) {
|
for (int file = 0; file < 8; ++file) {
|
||||||
int square = rank * 8 + file;
|
int square = rank * 8 + file;
|
||||||
const uint64_t squareBit = 1ULL << square;
|
const uint64_t squareBit = 1ULL << square;
|
||||||
|
|
||||||
if (!(occupied & squareBit)) {
|
if (!(occupied & squareBit)) {
|
||||||
++emptyCount;
|
++emptyCount;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (emptyCount > 0) {
|
if (emptyCount > 0) {
|
||||||
fen << emptyCount;
|
fen << emptyCount;
|
||||||
emptyCount = 0;
|
emptyCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static constexpr std::array<char, 6> pieceChars = {'p', 'n', 'b', 'r', 'q', 'k'};
|
static constexpr std::array<char, 6> pieceChars = {'p', 'n', 'b', 'r', 'q', 'k'};
|
||||||
char pieceChar = '?';
|
char pieceChar = '?';
|
||||||
|
|
||||||
for (size_t pieceType = 0; pieceType < pieceChars.size(); ++pieceType) {
|
for (size_t pieceType = 0; pieceType < pieceChars.size(); ++pieceType) {
|
||||||
if (pieces[pieceType] & squareBit) {
|
if (pieces[pieceType] & squareBit) {
|
||||||
pieceChar = pieceChars[pieceType];
|
pieceChar = pieceChars[pieceType];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fen << (colors[White] & squareBit ? static_cast<char>(std::toupper(pieceChar)) : pieceChar);
|
fen << (colors[White] & squareBit ? static_cast<char>(std::toupper(pieceChar)) : pieceChar);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (emptyCount > 0) {
|
if (emptyCount > 0) {
|
||||||
fen << emptyCount;
|
fen << emptyCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rank > 0) {
|
if (rank > 0) {
|
||||||
fen << '/';
|
fen << '/';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fen << ' ' << (sideToMove ? 'b' : 'w');
|
fen << ' ' << (sideToMove ? 'b' : 'w');
|
||||||
|
|
||||||
fen << ' ';
|
fen << ' ';
|
||||||
bool hasCastlingRights = false;
|
bool hasCastlingRights = false;
|
||||||
|
|
||||||
const std::array<std::pair<uint8_t, char>, 4> castlingOptions = {
|
const std::array<std::pair<uint8_t, char>, 4> castlingOptions = {
|
||||||
{{whiteKingRight, 'K'}, {whiteQueenRight, 'Q'}, {blackKingRight, 'k'}, {blackQueenRight, 'q'}}
|
{{whiteKingRight, 'K'}, {whiteQueenRight, 'Q'}, {blackKingRight, 'k'}, {blackQueenRight, 'q'}}
|
||||||
};
|
};
|
||||||
|
|
||||||
for (const auto& [right, symbol] : castlingOptions) {
|
for (const auto& [right, symbol] : castlingOptions) {
|
||||||
if (castlingRights & right) {
|
if (castlingRights & right) {
|
||||||
fen << symbol;
|
fen << symbol;
|
||||||
hasCastlingRights = true;
|
hasCastlingRights = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!hasCastlingRights) {
|
if (!hasCastlingRights) {
|
||||||
fen << '-';
|
fen << '-';
|
||||||
}
|
}
|
||||||
|
|
||||||
fen << ' ';
|
fen << ' ';
|
||||||
if (enPassantTarget == noEPTarget) {
|
if (enPassantTarget == noEPTarget) {
|
||||||
fen << '-';
|
fen << '-';
|
||||||
@@ -156,11 +169,11 @@ std::string Board::GetFen() {
|
|||||||
const int rank = enPassantTarget / 8;
|
const int rank = enPassantTarget / 8;
|
||||||
fen << static_cast<char>('a' + file) << (rank + 1);
|
fen << static_cast<char>('a' + file) << (rank + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
fen << ' ' << halfMoves;
|
fen << ' ' << halfMoves;
|
||||||
|
|
||||||
fen << ' ' << fullMoves;
|
fen << ' ' << fullMoves;
|
||||||
|
|
||||||
return fen.str();
|
return fen.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -271,6 +284,12 @@ void Board::SetPiece(int piece, int square, bool color) {
|
|||||||
occupied.SetBit(square);
|
occupied.SetBit(square);
|
||||||
|
|
||||||
hashKey ^= UTILS::zKeys[color][piece][square];
|
hashKey ^= UTILS::zKeys[color][piece][square];
|
||||||
|
|
||||||
|
if (piece == Pawn) {
|
||||||
|
pawnKey ^= UTILS::zKeys[color][Pawn][square];
|
||||||
|
} else {
|
||||||
|
nonPawnKey ^= UTILS::zKeys[color][piece][square];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Board::RemovePiece(int piece, int square, bool color) {
|
void Board::RemovePiece(int piece, int square, bool color) {
|
||||||
@@ -279,6 +298,12 @@ void Board::RemovePiece(int piece, int square, bool color) {
|
|||||||
occupied.PopBit(square);
|
occupied.PopBit(square);
|
||||||
|
|
||||||
hashKey ^= UTILS::zKeys[color][piece][square];
|
hashKey ^= UTILS::zKeys[color][piece][square];
|
||||||
|
|
||||||
|
if (piece == Pawn) {
|
||||||
|
pawnKey ^= UTILS::zKeys[color][Pawn][square];
|
||||||
|
} else {
|
||||||
|
nonPawnKey ^= UTILS::zKeys[color][piece][square];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Updates castling rights
|
// Updates castling rights
|
||||||
@@ -322,23 +347,19 @@ bool Board::MakeMove(Move move) {
|
|||||||
// Null Move
|
// Null Move
|
||||||
if (!move) {
|
if (!move) {
|
||||||
int newEpTarget = noEPTarget;
|
int newEpTarget = noEPTarget;
|
||||||
|
|
||||||
sideToMove = !sideToMove;
|
sideToMove = !sideToMove;
|
||||||
hashKey ^= UTILS::zSide;
|
hashKey ^= UTILS::zSide;
|
||||||
|
|
||||||
if (enPassantTarget != noEPTarget) {
|
if (enPassantTarget != noEPTarget) {
|
||||||
hashKey ^= UTILS::zEnPassant[enPassantTarget % 8];
|
hashKey ^= UTILS::zEnPassant[enPassantTarget % 8];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newEpTarget != noEPTarget) {
|
|
||||||
hashKey ^= UTILS::zEnPassant[newEpTarget % 8];
|
|
||||||
}
|
|
||||||
|
|
||||||
enPassantTarget = newEpTarget;
|
enPassantTarget = newEpTarget;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int newEpTarget = noEPTarget;
|
int newEpTarget = noEPTarget;
|
||||||
|
|
||||||
int attackerPiece = GetPieceType(move.MoveFrom());
|
int attackerPiece = GetPieceType(move.MoveFrom());
|
||||||
@@ -397,7 +418,7 @@ bool Board::MakeMove(Move move) {
|
|||||||
case kingCastle:
|
case kingCastle:
|
||||||
{
|
{
|
||||||
int rookSquare = attackerColor ? h8 : h1;
|
int rookSquare = attackerColor ? h8 : h1;
|
||||||
|
|
||||||
// Removing rook from old position
|
// Removing rook from old position
|
||||||
RemovePiece(Rook, rookSquare, attackerColor);
|
RemovePiece(Rook, rookSquare, attackerColor);
|
||||||
|
|
||||||
@@ -427,6 +448,22 @@ bool Board::MakeMove(Move move) {
|
|||||||
break;
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Removing the right to castle on king and rook movement
|
// Removing the right to castle on king and rook movement
|
||||||
UpdateCastlingRights(*this, move.MoveFrom(), attackerPiece, attackerColor);
|
UpdateCastlingRights(*this, move.MoveFrom(), attackerPiece, attackerColor);
|
||||||
|
|
||||||
@@ -448,7 +485,7 @@ bool Board::MakeMove(Move move) {
|
|||||||
sideToMove = !sideToMove;
|
sideToMove = !sideToMove;
|
||||||
if (InCheck()) {
|
if (InCheck()) {
|
||||||
*this = save;
|
*this = save;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
sideToMove = !sideToMove;
|
sideToMove = !sideToMove;
|
||||||
|
|
||||||
@@ -458,7 +495,7 @@ bool Board::MakeMove(Move move) {
|
|||||||
} else {
|
} else {
|
||||||
halfMoves++;
|
halfMoves++;
|
||||||
}
|
}
|
||||||
|
|
||||||
positionIndex++;
|
positionIndex++;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -485,8 +522,8 @@ void Board::ResetAccPair() {
|
|||||||
while (whitePieces) {
|
while (whitePieces) {
|
||||||
int square = whitePieces.getLS1BIndex();
|
int square = whitePieces.getLS1BIndex();
|
||||||
|
|
||||||
int wInput = ACC::CalculateIndex(White, White, GetPieceType(square), square);
|
int wInput = ACC::CalculateIndex(White, White, GetPieceType(square), square, accPair.mirroredWhite);
|
||||||
int bInput = ACC::CalculateIndex(Black, White, GetPieceType(square), square);
|
int bInput = ACC::CalculateIndex(Black, White, GetPieceType(square), square, accPair.mirroredBlack);
|
||||||
|
|
||||||
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
||||||
accPair.white[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i];
|
accPair.white[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i];
|
||||||
@@ -499,8 +536,8 @@ void Board::ResetAccPair() {
|
|||||||
while (blackPieces) {
|
while (blackPieces) {
|
||||||
int square = blackPieces.getLS1BIndex();
|
int square = blackPieces.getLS1BIndex();
|
||||||
|
|
||||||
int wInput = ACC::CalculateIndex(White, Black, GetPieceType(square), square);
|
int wInput = ACC::CalculateIndex(White, Black, GetPieceType(square), square, accPair.mirroredWhite);
|
||||||
int bInput = ACC::CalculateIndex(Black, Black, GetPieceType(square), square);
|
int bInput = ACC::CalculateIndex(Black, Black, GetPieceType(square), square, accPair.mirroredBlack);
|
||||||
|
|
||||||
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
for (int i = 0; i < NNUE::HL_SIZE; i++) {
|
||||||
accPair.white[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i];
|
accPair.white[i] += NNUE::net.accumulator_weights[wInput * NNUE::HL_SIZE + i];
|
||||||
@@ -513,16 +550,16 @@ void Board::ResetAccPair() {
|
|||||||
|
|
||||||
Bitboard Board::AttacksTo(int square, Bitboard occupancy) {
|
Bitboard Board::AttacksTo(int square, Bitboard occupancy) {
|
||||||
Bitboard attacks;
|
Bitboard attacks;
|
||||||
|
|
||||||
// Pawn attacks
|
// Pawn attacks
|
||||||
|
|
||||||
// Opponent attacks
|
// Opponent attacks
|
||||||
attacks |= (MOVEGEN::pawnAttacks[sideToMove][square] & colors[!sideToMove] & pieces[Pawn]);
|
attacks |= (MOVEGEN::pawnAttacks[sideToMove][square] & colors[!sideToMove] & pieces[Pawn]);
|
||||||
|
|
||||||
// Own piece attacks
|
// Own piece attacks
|
||||||
attacks |= (MOVEGEN::pawnAttacks[!sideToMove][square] & colors[sideToMove] & pieces[Pawn]);
|
attacks |= (MOVEGEN::pawnAttacks[!sideToMove][square] & colors[sideToMove] & pieces[Pawn]);
|
||||||
|
|
||||||
// Knight
|
// Knight
|
||||||
attacks |= (MOVEGEN::knightAttacks[square] & pieces[Knight]);
|
attacks |= (MOVEGEN::knightAttacks[square] & pieces[Knight]);
|
||||||
|
|
||||||
// King
|
// King
|
||||||
@@ -539,3 +576,7 @@ Bitboard Board::AttacksTo(int square, Bitboard occupancy) {
|
|||||||
|
|
||||||
return attacks;
|
return attacks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Board::IsSquareThreatened(bool side, int square) {
|
||||||
|
return colorThreats[!side].IsSet(square);
|
||||||
|
}
|
||||||
+5
-1
@@ -40,6 +40,8 @@ public:
|
|||||||
int fullMoves = 1;
|
int fullMoves = 1;
|
||||||
|
|
||||||
U64 hashKey = 0ULL;
|
U64 hashKey = 0ULL;
|
||||||
|
U64 pawnKey = 0ULL;
|
||||||
|
U64 nonPawnKey = 0ULL;
|
||||||
|
|
||||||
void Reset();
|
void Reset();
|
||||||
void SetByFen(std::string_view fen);
|
void SetByFen(std::string_view fen);
|
||||||
@@ -56,7 +58,7 @@ public:
|
|||||||
|
|
||||||
int GetPieceType(int square);
|
int GetPieceType(int square);
|
||||||
int GetPieceColor(int square);
|
int GetPieceColor(int square);
|
||||||
|
|
||||||
bool InCheck();
|
bool InCheck();
|
||||||
|
|
||||||
void SetPiece(int piece, int square, bool color);
|
void SetPiece(int piece, int square, bool color);
|
||||||
@@ -68,4 +70,6 @@ public:
|
|||||||
bool InPossibleZug();
|
bool InPossibleZug();
|
||||||
|
|
||||||
Bitboard AttacksTo(int square, Bitboard occupancy);
|
Bitboard AttacksTo(int square, Bitboard occupancy);
|
||||||
|
|
||||||
|
bool IsSquareThreatened(bool side, int square);
|
||||||
};
|
};
|
||||||
+472
-75
@@ -5,6 +5,15 @@
|
|||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <chrono>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <sstream>
|
||||||
|
#include <csignal>
|
||||||
|
#include <cstdlib>
|
||||||
#include "datagen.h"
|
#include "datagen.h"
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
@@ -14,8 +23,10 @@
|
|||||||
// OS-dependent threading includes
|
// OS-dependent threading includes
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
#include <io.h> // for _commit
|
||||||
#else
|
#else
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
#include <unistd.h> // for sync
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@@ -46,7 +57,148 @@ static void ShowCursor() {
|
|||||||
|
|
||||||
namespace DATAGEN {
|
namespace DATAGEN {
|
||||||
|
|
||||||
static std::mutex fileOpenMutex;
|
// ============================================================================
|
||||||
|
// 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";
|
||||||
|
std::ofstream out(tempFile);
|
||||||
|
if (!out) {
|
||||||
|
std::cerr << "Failed to create temp file for request" << std::endl;
|
||||||
|
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";
|
||||||
|
|
||||||
|
int result = system(cmd.str().c_str());
|
||||||
|
|
||||||
|
std::ifstream responseIn(responseFile);
|
||||||
|
if (responseIn) {
|
||||||
|
std::stringstream buffer;
|
||||||
|
buffer << responseIn.rdbuf();
|
||||||
|
response = buffer.str();
|
||||||
|
responseIn.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::remove(tempFile.c_str());
|
||||||
|
std::remove(responseFile.c_str());
|
||||||
|
|
||||||
|
return (result == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
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";
|
||||||
|
|
||||||
|
int result = system(cmd.str().c_str());
|
||||||
|
return (result == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string BuildStartJSON(const std::string& username, int targetPositions) {
|
||||||
|
std::ostringstream json;
|
||||||
|
json << "{"
|
||||||
|
<< "\"user\":\"" << username << "\","
|
||||||
|
<< "\"target\":" << targetPositions
|
||||||
|
<< "}";
|
||||||
|
return json.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string BuildHeartbeatJSON(const std::string& username, int threads) {
|
||||||
|
std::ostringstream json;
|
||||||
|
json << "{"
|
||||||
|
<< "\"user\":\"" << username << "\","
|
||||||
|
<< "\"threads\":" << 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
|
||||||
|
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++;
|
||||||
|
}
|
||||||
|
return json.substr(pos, endPos - pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// HEARTBEAT THREAD
|
||||||
|
// ============================================================================
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
static void SignalHandler(int signal) {
|
||||||
|
if (signal == SIGINT || signal == SIGTERM) {
|
||||||
|
std::cout << "\n\nShutdown signal received. Saving data safely...\n" << std::flush;
|
||||||
|
g_shutdownRequested.store(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void SetupSignalHandlers() {
|
||||||
|
std::signal(SIGINT, SignalHandler);
|
||||||
|
std::signal(SIGTERM, SignalHandler);
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
std::signal(SIGHUP, SignalHandler);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
static bool IsGameOver(Board &board, SEARCH::SearchContext* ctx) {
|
static bool IsGameOver(Board &board, SEARCH::SearchContext* ctx) {
|
||||||
if (SEARCH::IsDraw(board, ctx)) return true;
|
if (SEARCH::IsDraw(board, ctx)) return true;
|
||||||
@@ -72,7 +224,7 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext* ctx) {
|
|||||||
bool plusOne = moveDist(rng);
|
bool plusOne = moveDist(rng);
|
||||||
|
|
||||||
for (int count = 0; count < RAND_MOVES + plusOne; count++) {
|
for (int count = 0; count < RAND_MOVES + plusOne; count++) {
|
||||||
MOVEGEN::GenerateMoves<All>(board);
|
MOVEGEN::GenerateMoves<All>(board, true);
|
||||||
|
|
||||||
std::uniform_int_distribution<int> dist(0, board.currentMoveIndex - 1);
|
std::uniform_int_distribution<int> dist(0, board.currentMoveIndex - 1);
|
||||||
bool isLegal = board.MakeMove(board.moveList[dist(rng)]);
|
bool isLegal = board.MakeMove(board.moveList[dist(rng)]);
|
||||||
@@ -82,12 +234,14 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext* ctx) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
ctx->positionHistory[board.positionIndex] = board.hashKey;
|
ctx->positionHistory[board.positionIndex] = board.hashKey;
|
||||||
MOVEGEN::GenerateMoves<All>(board);
|
MOVEGEN::GenerateMoves<All>(board, true);
|
||||||
if (IsGameOver(board, ctx)) break;
|
if (IsGameOver(board, ctx)) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteToFile(std::vector<Game> &gamesBuffer, std::ofstream &file) {
|
static void WriteToFile(std::vector<Game> &gamesBuffer, const std::string& basePath, int& fileCounter) {
|
||||||
|
if (gamesBuffer.empty()) return;
|
||||||
|
|
||||||
int32_t zeroes = 0;
|
int32_t zeroes = 0;
|
||||||
std::vector<char> buffer;
|
std::vector<char> buffer;
|
||||||
|
|
||||||
@@ -110,105 +264,181 @@ static void WriteToFile(std::vector<Game> &gamesBuffer, std::ofstream &file) {
|
|||||||
buffer.insert(buffer.end(), zeroesPtr, zeroesPtr + sizeof(zeroes));
|
buffer.insert(buffer.end(), zeroesPtr, zeroesPtr + sizeof(zeroes));
|
||||||
}
|
}
|
||||||
|
|
||||||
file.write(buffer.data(), buffer.size());
|
std::string tmpFileName = basePath + std::to_string(fileCounter) + ".tmp";
|
||||||
file.flush();
|
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
|
||||||
|
tmpFile.close();
|
||||||
|
#else
|
||||||
|
tmpFile.close();
|
||||||
|
sync();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
std::error_code ec;
|
||||||
|
std::filesystem::rename(tmpFileName, finalFileName, ec);
|
||||||
|
if (ec) {
|
||||||
|
std::cerr << "Failed to rename " << tmpFileName << " to " << finalFileName
|
||||||
|
<< ": " << ec.message() << std::endl;
|
||||||
|
std::filesystem::remove(tmpFileName, ec);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fileCounter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string MergeThreadFiles() {
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
fs::create_directories("data");
|
||||||
|
|
||||||
|
auto now = std::chrono::system_clock::now();
|
||||||
|
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
|
||||||
|
std::tm tm = *std::localtime(&now_time);
|
||||||
|
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << "data/datagen_"
|
||||||
|
<< std::put_time(&tm, "%Y-%m-%d_%H-%M")
|
||||||
|
<< ".binpack";
|
||||||
|
std::string finalFileName = oss.str();
|
||||||
|
|
||||||
|
std::ofstream finalFile(finalFileName, std::ios::binary);
|
||||||
|
if (!finalFile.is_open()) {
|
||||||
|
std::cerr << "Failed to create final merged file: " << finalFileName << std::endl;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
fs::path dataDir("data");
|
||||||
|
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(".binpack") != std::string::npos) {
|
||||||
|
std::ifstream threadFile(entry.path(), std::ios::binary);
|
||||||
|
if (!threadFile.is_open()) {
|
||||||
|
std::cerr << "Failed to open " << filename << std::endl;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
finalFile << threadFile.rdbuf();
|
||||||
|
threadFile.close();
|
||||||
|
|
||||||
|
std::error_code ec;
|
||||||
|
fs::remove(entry.path(), ec);
|
||||||
|
if (ec) {
|
||||||
|
std::cerr << "Failed to delete " << filename << ": " << ec.message() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
if (ec) {
|
||||||
|
std::cerr << "Failed to delete incomplete temp file " << filename << ": " << ec.message() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
finalFile.close();
|
||||||
|
std::cout << "Merged all thread files into: " << finalFileName << std::endl;
|
||||||
|
return finalFileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& stopFlag) {
|
static void PlayGames(int id, std::atomic<int>& positions, std::atomic<bool>& stopFlag) {
|
||||||
std::vector<Game> gamesBuffer;
|
std::vector<Game> gamesBuffer;
|
||||||
gamesBuffer.reserve(GAME_BUFFER);
|
gamesBuffer.reserve(GAME_BUFFER);
|
||||||
|
|
||||||
std::string fileName = "thread" + std::to_string(id) + ".binpack";
|
std::filesystem::create_directories("data");
|
||||||
|
|
||||||
std::ofstream file;
|
std::string basePath = "data/thread" + std::to_string(id) + "_";
|
||||||
{
|
int fileCounter = 0;
|
||||||
std::lock_guard<std::mutex> lock(fileOpenMutex);
|
|
||||||
file.open(fileName, std::ios::app | std::ios::binary);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!file.is_open()) {
|
|
||||||
std::cerr << "Failed to open file: " << fileName << std::endl;
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
const int evenityMargin = 200;
|
const int evenityMargin = 200;
|
||||||
|
|
||||||
/*
|
try {
|
||||||
const int winAdjMoveCount = 3;
|
while (!stopFlag && !g_shutdownRequested) {
|
||||||
const int winAdjScore = 400;
|
Board board;
|
||||||
|
auto ctx = std::make_unique<SEARCH::SearchContext>();
|
||||||
|
|
||||||
const int drawAdjMoveNumber = 40;
|
PlayRandMoves(board, ctx.get());
|
||||||
const int drawAdjMoveCount = 8;
|
if (IsGameOver(board, ctx.get())) continue;
|
||||||
const int drawAdjScore = 10;
|
|
||||||
*/
|
|
||||||
|
|
||||||
while (!stopFlag) {
|
const int startingScore = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx.get()).score;
|
||||||
Board board;
|
|
||||||
auto ctx = std::make_unique<SEARCH::SearchContext>();
|
|
||||||
|
|
||||||
PlayRandMoves(board, ctx.get());
|
if (std::abs(startingScore) >= evenityMargin) continue;
|
||||||
if (IsGameOver(board, ctx.get())) continue;
|
|
||||||
|
|
||||||
// Filter out games with a highly uneven starting position
|
ctx->nodes = 0;
|
||||||
const int startingScore = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx.get()).score;
|
|
||||||
|
|
||||||
if (std::abs(startingScore) >= evenityMargin) continue;
|
Game game;
|
||||||
|
Board startpos = board;
|
||||||
|
|
||||||
ctx->nodes = 0;
|
SearchResults safeResults;
|
||||||
|
|
||||||
Game game;
|
int staticEval = UTILS::ConvertToWhiteRelative(board, NNUE::net.Evaluate(board));
|
||||||
Board startpos = board;
|
int wdl = 1;
|
||||||
|
|
||||||
SearchResults safeResults;
|
while (!IsGameOver(board, ctx.get())) {
|
||||||
|
if (g_shutdownRequested) break;
|
||||||
|
|
||||||
|
SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx.get());
|
||||||
|
|
||||||
int staticEval = UTILS::ConvertToWhiteRelative(board, NNUE::net.Evaluate(board));
|
if (!results.bestMove) break;
|
||||||
int wdl = 1;
|
safeResults = results;
|
||||||
|
|
||||||
while (!IsGameOver(board, ctx.get())) {
|
game.moves.emplace_back(ScoredMove(results.bestMove.ConvertToViriMoveFormat(),
|
||||||
SearchResults results = SEARCH::SearchPosition<SEARCH::datagen>(board, SearchParams(), ctx.get());
|
UTILS::ConvertToWhiteRelative(board, results.score)));
|
||||||
|
|
||||||
if (!results.bestMove) break;
|
board.MakeMove(results.bestMove);
|
||||||
safeResults = results;
|
ctx->positionHistory[board.positionIndex] = board.hashKey;
|
||||||
|
|
||||||
game.moves.emplace_back(ScoredMove(results.bestMove.ConvertToViriMoveFormat(),
|
positions++;
|
||||||
UTILS::ConvertToWhiteRelative(board, results.score)));
|
|
||||||
|
|
||||||
board.MakeMove(results.bestMove);
|
MOVEGEN::GenerateMoves<All>(board, true);
|
||||||
ctx->positionHistory[board.positionIndex] = board.hashKey;
|
}
|
||||||
|
|
||||||
positions++;
|
if (!SEARCH::IsDraw(board, ctx.get())) {
|
||||||
|
wdl = board.sideToMove ? 2 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
game.format.packFrom(startpos, staticEval, wdl);
|
||||||
|
gamesBuffer.emplace_back(game);
|
||||||
|
|
||||||
MOVEGEN::GenerateMoves<All>(board);
|
if (gamesBuffer.size() >= GAME_BUFFER) {
|
||||||
|
WriteToFile(gamesBuffer, basePath, fileCounter);
|
||||||
|
gamesBuffer.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (...) {
|
||||||
if (!SEARCH::IsDraw(board, ctx.get())) {
|
if (!gamesBuffer.empty()) {
|
||||||
wdl = board.sideToMove ? 2 : 0;
|
WriteToFile(gamesBuffer, basePath, fileCounter);
|
||||||
}
|
|
||||||
|
|
||||||
game.format.packFrom(startpos, staticEval, wdl);
|
|
||||||
gamesBuffer.emplace_back(game);
|
|
||||||
|
|
||||||
if (gamesBuffer.size() >= GAME_BUFFER) {
|
|
||||||
WriteToFile(gamesBuffer, file);
|
|
||||||
gamesBuffer.clear();
|
|
||||||
}
|
}
|
||||||
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write remaining games to file
|
|
||||||
if (gamesBuffer.size() > 0) {
|
if (gamesBuffer.size() > 0) {
|
||||||
WriteToFile(gamesBuffer, file);
|
WriteToFile(gamesBuffer, basePath, fileCounter);
|
||||||
gamesBuffer.clear();
|
gamesBuffer.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
// Windows implementation using std::thread
|
|
||||||
static void CreateWorkerThread(int id, std::atomic<int>& positions, std::atomic<bool>& stopFlag, std::vector<std::thread>& threads) {
|
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));
|
threads.emplace_back(PlayGames, id, std::ref(positions), std::ref(stopFlag));
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
// Unix/Linux implementation using pthread
|
|
||||||
static void* ThreadFunc(void* arg) {
|
static void* ThreadFunc(void* arg) {
|
||||||
auto* tup = static_cast<std::tuple<int, std::atomic<int>*, std::atomic<bool>*>*>(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));
|
PlayGames(std::get<0>(*tup), *std::get<1>(*tup), *std::get<2>(*tup));
|
||||||
@@ -217,7 +447,13 @@ static void* ThreadFunc(void* arg) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// MAIN RUN FUNCTIONS
|
||||||
|
// ============================================================================
|
||||||
|
|
||||||
void Run(int targetPositions, int threads) {
|
void Run(int targetPositions, int threads) {
|
||||||
|
SetupSignalHandlers();
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
system("cls");
|
system("cls");
|
||||||
HideCursor();
|
HideCursor();
|
||||||
@@ -230,20 +466,18 @@ void Run(int targetPositions, int threads) {
|
|||||||
std::atomic<bool> stopFlag = false;
|
std::atomic<bool> stopFlag = false;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
// Windows implementation using std::thread
|
|
||||||
std::vector<std::thread> workerThreads;
|
std::vector<std::thread> workerThreads;
|
||||||
|
|
||||||
for (int i = 0; i < threads - 1; ++i) {
|
for (int i = 0; i < threads - 1; ++i) {
|
||||||
CreateWorkerThread(i, positions, stopFlag, workerThreads);
|
CreateWorkerThread(i, positions, stopFlag, workerThreads);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
// Unix/Linux implementation using pthread
|
|
||||||
std::vector<pthread_t> workerThreads;
|
std::vector<pthread_t> workerThreads;
|
||||||
|
|
||||||
for (int i = 0; i < threads - 1; ++i) {
|
for (int i = 0; i < threads - 1; ++i) {
|
||||||
pthread_attr_t attr;
|
pthread_attr_t attr;
|
||||||
pthread_attr_init(&attr);
|
pthread_attr_init(&attr);
|
||||||
pthread_attr_setstacksize(&attr, 8 * 1024 * 1024); // 8 MB stack
|
pthread_attr_setstacksize(&attr, 8 * 1024 * 1024);
|
||||||
|
|
||||||
auto* args = new std::tuple<int, std::atomic<int>*, std::atomic<bool>*>(i, &positions, &stopFlag);
|
auto* args = new std::tuple<int, std::atomic<int>*, std::atomic<bool>*>(i, &positions, &stopFlag);
|
||||||
|
|
||||||
@@ -260,7 +494,7 @@ void Run(int targetPositions, int threads) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
Stopwatch sw;
|
Stopwatch sw;
|
||||||
while (positions < targetPositions) {
|
while (positions < targetPositions && !g_shutdownRequested) {
|
||||||
PrintProgress(positions, targetPositions, sw, threads);
|
PrintProgress(positions, targetPositions, sw, threads);
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
}
|
}
|
||||||
@@ -269,34 +503,197 @@ void Run(int targetPositions, int threads) {
|
|||||||
|
|
||||||
stopFlag = true;
|
stopFlag = true;
|
||||||
|
|
||||||
|
std::cout << "\n\nWaiting for threads to finish writing..." << std::endl;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
// Join Windows threads
|
|
||||||
for (std::thread& thread : workerThreads) {
|
for (std::thread& thread : workerThreads) {
|
||||||
if (thread.joinable()) {
|
if (thread.joinable()) {
|
||||||
thread.join();
|
thread.join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
// Join pthread threads
|
|
||||||
for (pthread_t& thread : workerThreads) {
|
for (pthread_t& thread : workerThreads) {
|
||||||
pthread_join(thread, nullptr);
|
pthread_join(thread, nullptr);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
std::cout << "All threads completed. Merging files..." << std::endl;
|
||||||
|
MergeThreadFiles();
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
ShowCursor();
|
ShowCursor();
|
||||||
#else
|
#else
|
||||||
ShowCursor();
|
ShowCursor();
|
||||||
#endif
|
#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
|
||||||
|
|
||||||
|
std::atomic<int> positions = 0;
|
||||||
|
std::atomic<bool> stopFlag = false;
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
std::vector<std::thread> workerThreads;
|
||||||
|
|
||||||
|
for (int i = 0; i < threads - 1; ++i) {
|
||||||
|
CreateWorkerThread(i, positions, stopFlag, workerThreads);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
std::vector<pthread_t> workerThreads;
|
||||||
|
|
||||||
|
for (int i = 0; i < threads - 1; ++i) {
|
||||||
|
pthread_attr_t attr;
|
||||||
|
pthread_attr_init(&attr);
|
||||||
|
pthread_attr_setstacksize(&attr, 8 * 1024 * 1024);
|
||||||
|
|
||||||
|
auto* args = new std::tuple<int, std::atomic<int>*, std::atomic<bool>*>(i, &positions, &stopFlag);
|
||||||
|
|
||||||
|
pthread_t thread;
|
||||||
|
if (pthread_create(&thread, &attr, ThreadFunc, args) == 0) {
|
||||||
|
workerThreads.push_back(thread);
|
||||||
|
} else {
|
||||||
|
std::cerr << "Failed to create thread " << i << "\n";
|
||||||
|
delete args;
|
||||||
|
}
|
||||||
|
|
||||||
|
pthread_attr_destroy(&attr);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
Stopwatch sw;
|
||||||
|
while (positions < targetPositions && !g_shutdownRequested) {
|
||||||
|
PrintProgress(positions, targetPositions, sw, threads);
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
PrintProgress(positions, targetPositions, sw, threads);
|
||||||
|
|
||||||
|
stopFlag = true;
|
||||||
|
|
||||||
|
std::cout << "\n\nWaiting for threads to finish writing..." << std::endl;
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
for (std::thread& thread : workerThreads) {
|
||||||
|
if (thread.joinable()) {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
for (pthread_t& thread : workerThreads) {
|
||||||
|
pthread_join(thread, nullptr);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Stop heartbeat
|
||||||
|
heartbeatStop = true;
|
||||||
|
if (heartbeat.joinable()) {
|
||||||
|
heartbeat.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (g_shutdownRequested) break;
|
||||||
|
|
||||||
|
std::cout << "All threads completed. Merging files..." << std::endl;
|
||||||
|
std::string mergedFile = MergeThreadFiles();
|
||||||
|
|
||||||
|
#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) {
|
||||||
|
std::cout << "Local file cleaned up." << std::endl;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int threads) {
|
void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int threads) {
|
||||||
// Define ANSI escape codes for colors as std::string
|
|
||||||
const std::string COLOR_RESET = "\033[0m";
|
const std::string COLOR_RESET = "\033[0m";
|
||||||
const std::string COLOR_TITLE = "\033[1;36m"; // Bright cyan
|
const std::string COLOR_TITLE = "\033[1;36m";
|
||||||
const std::string COLOR_SECTION = "\033[1;33m"; // Bright yellow
|
const std::string COLOR_SECTION = "\033[1;33m";
|
||||||
const std::string COLOR_VALUE = "\033[1;32m"; // Bright green
|
const std::string COLOR_VALUE = "\033[1;32m";
|
||||||
const std::string COLOR_BAR = "\033[1;34m"; // Bright blue
|
const std::string COLOR_BAR = "\033[1;34m";
|
||||||
|
|
||||||
static double lastPositions = 0;
|
static double lastPositions = 0;
|
||||||
static double lastElapsed = 0;
|
static double lastElapsed = 0;
|
||||||
@@ -399,4 +796,4 @@ void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int
|
|||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+2
-20
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
namespace DATAGEN {
|
namespace DATAGEN {
|
||||||
|
|
||||||
constexpr int GAME_BUFFER = 1000;
|
constexpr int GAME_BUFFER = 500;
|
||||||
constexpr int SOFT_NODES = 5000;
|
constexpr int SOFT_NODES = 5000;
|
||||||
constexpr int HARD_NODES = 100000;
|
constexpr int HARD_NODES = 100000;
|
||||||
constexpr int RAND_MOVES = 8;
|
constexpr int RAND_MOVES = 8;
|
||||||
@@ -34,25 +34,6 @@ struct MarlinFormat {
|
|||||||
this->wdl = wdl;
|
this->wdl = wdl;
|
||||||
extra = 0;
|
extra = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeToFile(const std::string& filename) const {
|
|
||||||
std::ofstream outFile(filename, std::ios::binary);
|
|
||||||
if (!outFile) {
|
|
||||||
std::cerr << "Error opening file for writing!" << std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
outFile.write(reinterpret_cast<const char*>(&occupancy), sizeof(occupancy));
|
|
||||||
outFile.write(reinterpret_cast<const char*>(&pieces), sizeof(pieces));
|
|
||||||
outFile.write(reinterpret_cast<const char*>(&stmEPSquare), sizeof(stmEPSquare));
|
|
||||||
outFile.write(reinterpret_cast<const char*>(&halfmoveClock), sizeof(halfmoveClock));
|
|
||||||
outFile.write(reinterpret_cast<const char*>(&fullmoveNumber), sizeof(fullmoveNumber));
|
|
||||||
outFile.write(reinterpret_cast<const char*>(&eval), sizeof(eval));
|
|
||||||
outFile.write(reinterpret_cast<const char*>(&wdl), sizeof(wdl));
|
|
||||||
outFile.write(reinterpret_cast<const char*>(&extra), sizeof(extra));
|
|
||||||
|
|
||||||
outFile.close();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ScoredMove {
|
struct ScoredMove {
|
||||||
@@ -80,6 +61,7 @@ struct Game {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void Run(int targetPositions, int threads);
|
void Run(int targetPositions, int threads);
|
||||||
|
void RunOnline(const std::string& username, int targetPositions, int threads);
|
||||||
|
|
||||||
void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int threads);
|
void PrintProgress(int positions, int targetPositions, Stopwatch &stopwatch, int threads);
|
||||||
}
|
}
|
||||||
+12
-2
@@ -57,14 +57,24 @@ int main(int argc, char* argv[]) {
|
|||||||
} else if (std::string(argv[1]) == "datagen") {
|
} else if (std::string(argv[1]) == "datagen") {
|
||||||
int positions = 1;
|
int positions = 1;
|
||||||
int threads = 1;
|
int threads = 1;
|
||||||
|
std::string username = "";
|
||||||
|
|
||||||
if (argc > 2) {
|
if (argc > 2) {
|
||||||
positions = std::stoi(argv[2]) * 1000;
|
positions = std::stoi(argv[2]) * 1000;
|
||||||
|
|
||||||
if (argc > 3) {
|
if (argc > 3) {
|
||||||
threads = std::stoi(argv[3]);
|
threads = std::stoi(argv[3]);
|
||||||
|
if (argc > 4) {
|
||||||
|
|
||||||
|
username = argv[4];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DATAGEN::Run(positions, threads);
|
|
||||||
|
if (!username.empty()) {
|
||||||
|
DATAGEN::RunOnline(username, positions, threads);
|
||||||
|
} else {
|
||||||
|
DATAGEN::Run(positions, threads);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
UCILoop(board);
|
UCILoop(board);
|
||||||
|
|||||||
@@ -80,6 +80,25 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string GetMoveString() {
|
||||||
|
std::string result = squareCoords[MoveFrom()];
|
||||||
|
result += squareCoords[MoveTo()];
|
||||||
|
|
||||||
|
int flags = GetFlags();
|
||||||
|
|
||||||
|
if (flags == knightPromotion || flags == knightPromoCapture) {
|
||||||
|
result += 'n';
|
||||||
|
} else if (flags == bishopPromotion || flags == bishopPromoCapture) {
|
||||||
|
result += 'b';
|
||||||
|
} else if (flags == rookPromotion || flags == rookPromoCapture) {
|
||||||
|
result += 'r';
|
||||||
|
} else if (flags == queenPromotion || flags == queenPromoCapture) {
|
||||||
|
result += 'q';
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
uint16_t ConvertToViriMoveFormat() {
|
uint16_t ConvertToViriMoveFormat() {
|
||||||
uint16_t viriMove = 0;
|
uint16_t viriMove = 0;
|
||||||
int flags = GetFlags();
|
int flags = GetFlags();
|
||||||
|
|||||||
+4
-3
@@ -4,8 +4,9 @@
|
|||||||
#include "board.h"
|
#include "board.h"
|
||||||
|
|
||||||
enum MovegenMode {
|
enum MovegenMode {
|
||||||
All,
|
Quiet,
|
||||||
Noisy
|
Noisy,
|
||||||
|
All
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace MOVEGEN {
|
namespace MOVEGEN {
|
||||||
@@ -37,7 +38,7 @@ template <MovegenMode mode>
|
|||||||
void GenKingMoves(Board &board);
|
void GenKingMoves(Board &board);
|
||||||
|
|
||||||
template <MovegenMode mode>
|
template <MovegenMode mode>
|
||||||
void GenerateMoves(Board &board);
|
void GenerateMoves(Board &board, bool clear);
|
||||||
|
|
||||||
void GenThreatMaps(Board &board);
|
void GenThreatMaps(Board &board);
|
||||||
|
|
||||||
|
|||||||
+81
-141
@@ -11,8 +11,8 @@ void GenPawnMoves(Board &board) {
|
|||||||
|
|
||||||
Bitboard lastRank = board.sideToMove ? ranks[r_1] : ranks[r_8];
|
Bitboard lastRank = board.sideToMove ? ranks[r_1] : ranks[r_8];
|
||||||
|
|
||||||
if constexpr (mode == Noisy) {
|
if constexpr (mode == Noisy || mode == All) {
|
||||||
pushes &= lastRank;
|
Bitboard noisyPushes = pushes & lastRank;
|
||||||
|
|
||||||
if (board.enPassantTarget != noEPTarget) {
|
if (board.enPassantTarget != noEPTarget) {
|
||||||
if (attacks.IsSet(board.enPassantTarget)) {
|
if (attacks.IsSet(board.enPassantTarget)) {
|
||||||
@@ -35,8 +35,8 @@ void GenPawnMoves(Board &board) {
|
|||||||
captures.PopBit(targetSquare);
|
captures.PopBit(targetSquare);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (pushes) {
|
while (noisyPushes) {
|
||||||
int pushSquare = pushes.getLS1BIndex();
|
int pushSquare = noisyPushes.getLS1BIndex();
|
||||||
|
|
||||||
// Only promotions because NOISY
|
// Only promotions because NOISY
|
||||||
board.AddMove(Move(square, pushSquare, knightPromotion));
|
board.AddMove(Move(square, pushSquare, knightPromotion));
|
||||||
@@ -44,29 +44,17 @@ void GenPawnMoves(Board &board) {
|
|||||||
board.AddMove(Move(square, pushSquare, rookPromotion));
|
board.AddMove(Move(square, pushSquare, rookPromotion));
|
||||||
board.AddMove(Move(square, pushSquare, queenPromotion));
|
board.AddMove(Move(square, pushSquare, queenPromotion));
|
||||||
|
|
||||||
pushes.PopBit(pushSquare);
|
noisyPushes.PopBit(pushSquare);
|
||||||
}
|
}
|
||||||
|
|
||||||
pawns.PopBit(square);
|
pawns.PopBit(square);
|
||||||
continue;
|
}
|
||||||
} else {
|
|
||||||
// Checking for en passant
|
|
||||||
if (board.enPassantTarget != noEPTarget) {
|
|
||||||
if (attacks.IsSet(board.enPassantTarget)) {
|
|
||||||
board.AddMove(Move(square, board.enPassantTarget, epCapture));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if constexpr (mode == Quiet || mode == All) {
|
||||||
while (pushes) {
|
while (pushes) {
|
||||||
int pushSquare = pushes.getLS1BIndex();
|
int pushSquare = pushes.getLS1BIndex();
|
||||||
|
|
||||||
// Checking for promotion
|
if (!lastRank.IsSet(pushSquare)) {
|
||||||
if (lastRank.IsSet(pushSquare)) {
|
|
||||||
board.AddMove(Move(square, pushSquare, knightPromotion));
|
|
||||||
board.AddMove(Move(square, pushSquare, bishopPromotion));
|
|
||||||
board.AddMove(Move(square, pushSquare, rookPromotion));
|
|
||||||
board.AddMove(Move(square, pushSquare, queenPromotion));
|
|
||||||
} else {
|
|
||||||
if (std::abs(square - pushSquare) == 16) {
|
if (std::abs(square - pushSquare) == 16) {
|
||||||
board.AddMove(Move(square, pushSquare, doublePawnPush));
|
board.AddMove(Move(square, pushSquare, doublePawnPush));
|
||||||
} else {
|
} else {
|
||||||
@@ -76,22 +64,6 @@ void GenPawnMoves(Board &board) {
|
|||||||
|
|
||||||
pushes.PopBit(pushSquare);
|
pushes.PopBit(pushSquare);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (captures) {
|
|
||||||
int targetSquare = captures.getLS1BIndex();
|
|
||||||
|
|
||||||
// Checking for promotion
|
|
||||||
if (lastRank.IsSet(targetSquare)) {
|
|
||||||
board.AddMove(Move(square, targetSquare, knightPromoCapture));
|
|
||||||
board.AddMove(Move(square, targetSquare, bishopPromoCapture));
|
|
||||||
board.AddMove(Move(square, targetSquare, rookPromoCapture));
|
|
||||||
board.AddMove(Move(square, targetSquare, queenPromoCapture));
|
|
||||||
} else {
|
|
||||||
board.AddMove(Move(square, targetSquare, capture));
|
|
||||||
}
|
|
||||||
|
|
||||||
captures.PopBit(targetSquare);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pawns.PopBit(square);
|
pawns.PopBit(square);
|
||||||
@@ -108,7 +80,7 @@ void GenKnightMoves(Board &board) {
|
|||||||
Bitboard captures = moves & board.colors[!board.sideToMove];
|
Bitboard captures = moves & board.colors[!board.sideToMove];
|
||||||
moves &= ~captures;
|
moves &= ~captures;
|
||||||
|
|
||||||
if constexpr (mode == Noisy) {
|
if constexpr (mode == Noisy || mode == All) {
|
||||||
while (captures) {
|
while (captures) {
|
||||||
int targetSquare = captures.getLS1BIndex();
|
int targetSquare = captures.getLS1BIndex();
|
||||||
|
|
||||||
@@ -118,25 +90,18 @@ void GenKnightMoves(Board &board) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
knights.PopBit(square);
|
knights.PopBit(square);
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
while (moves) {
|
|
||||||
int targetSquare = moves.getLS1BIndex();
|
|
||||||
|
|
||||||
board.AddMove(Move(square, targetSquare, quiet));
|
|
||||||
|
|
||||||
moves.PopBit(targetSquare);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (captures) {
|
|
||||||
int targetSquare = captures.getLS1BIndex();
|
|
||||||
|
|
||||||
board.AddMove(Move(square, targetSquare, capture));
|
|
||||||
|
|
||||||
captures.PopBit(targetSquare);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if constexpr (mode == Quiet || mode == All) {
|
||||||
|
while (moves) {
|
||||||
|
int targetSquare = moves.getLS1BIndex();
|
||||||
|
|
||||||
|
board.AddMove(Move(square, targetSquare, quiet));
|
||||||
|
|
||||||
|
moves.PopBit(targetSquare);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
knights.PopBit(square);
|
knights.PopBit(square);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -151,7 +116,7 @@ void GenRookMoves(Board &board) {
|
|||||||
Bitboard captures = moves & board.colors[!board.sideToMove];
|
Bitboard captures = moves & board.colors[!board.sideToMove];
|
||||||
moves &= ~captures;
|
moves &= ~captures;
|
||||||
|
|
||||||
if constexpr (mode == Noisy) {
|
if constexpr (mode == Noisy || mode == All) {
|
||||||
while (captures) {
|
while (captures) {
|
||||||
int targetSquare = captures.getLS1BIndex();
|
int targetSquare = captures.getLS1BIndex();
|
||||||
|
|
||||||
@@ -161,24 +126,17 @@ void GenRookMoves(Board &board) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rooks.PopBit(square);
|
rooks.PopBit(square);
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (moves) {
|
if constexpr (mode == Quiet || mode == All) {
|
||||||
int targetSquare = moves.getLS1BIndex();
|
while (moves) {
|
||||||
|
int targetSquare = moves.getLS1BIndex();
|
||||||
|
|
||||||
board.AddMove(Move(square, targetSquare, quiet));
|
board.AddMove(Move(square, targetSquare, quiet));
|
||||||
|
|
||||||
moves.PopBit(targetSquare);
|
moves.PopBit(targetSquare);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
while (captures) {
|
|
||||||
int targetSquare = captures.getLS1BIndex();
|
|
||||||
|
|
||||||
board.AddMove(Move(square, targetSquare, capture));
|
|
||||||
|
|
||||||
captures.PopBit(targetSquare);
|
|
||||||
}
|
|
||||||
|
|
||||||
rooks.PopBit(square);
|
rooks.PopBit(square);
|
||||||
}
|
}
|
||||||
@@ -194,7 +152,7 @@ void GenBishopMoves(Board &board) {
|
|||||||
Bitboard captures = moves & board.colors[!board.sideToMove];
|
Bitboard captures = moves & board.colors[!board.sideToMove];
|
||||||
moves &= ~captures;
|
moves &= ~captures;
|
||||||
|
|
||||||
if constexpr (mode == Noisy) {
|
if constexpr (mode == Noisy || mode == All) {
|
||||||
while (captures) {
|
while (captures) {
|
||||||
int targetSquare = captures.getLS1BIndex();
|
int targetSquare = captures.getLS1BIndex();
|
||||||
|
|
||||||
@@ -204,24 +162,17 @@ void GenBishopMoves(Board &board) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bishops.PopBit(square);
|
bishops.PopBit(square);
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (moves) {
|
if constexpr (mode == Quiet || mode == All) {
|
||||||
int targetSquare = moves.getLS1BIndex();
|
while (moves) {
|
||||||
|
int targetSquare = moves.getLS1BIndex();
|
||||||
|
|
||||||
board.AddMove(Move(square, targetSquare, quiet));
|
board.AddMove(Move(square, targetSquare, quiet));
|
||||||
|
|
||||||
moves.PopBit(targetSquare);
|
moves.PopBit(targetSquare);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
while (captures) {
|
|
||||||
int targetSquare = captures.getLS1BIndex();
|
|
||||||
|
|
||||||
board.AddMove(Move(square, targetSquare, capture));
|
|
||||||
|
|
||||||
captures.PopBit(targetSquare);
|
|
||||||
}
|
|
||||||
|
|
||||||
bishops.PopBit(square);
|
bishops.PopBit(square);
|
||||||
}
|
}
|
||||||
@@ -237,7 +188,7 @@ void GenQueenMoves(Board &board) {
|
|||||||
Bitboard captures = moves & board.colors[!board.sideToMove];
|
Bitboard captures = moves & board.colors[!board.sideToMove];
|
||||||
moves &= ~captures;
|
moves &= ~captures;
|
||||||
|
|
||||||
if constexpr (mode == Noisy) {
|
if constexpr (mode == Noisy || mode == All) {
|
||||||
while (captures) {
|
while (captures) {
|
||||||
int targetSquare = captures.getLS1BIndex();
|
int targetSquare = captures.getLS1BIndex();
|
||||||
|
|
||||||
@@ -247,24 +198,17 @@ void GenQueenMoves(Board &board) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
queens.PopBit(square);
|
queens.PopBit(square);
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (moves) {
|
if constexpr (mode == Quiet || mode == All) {
|
||||||
int targetSquare = moves.getLS1BIndex();
|
while (moves) {
|
||||||
|
int targetSquare = moves.getLS1BIndex();
|
||||||
|
|
||||||
board.AddMove(Move(square, targetSquare, quiet));
|
board.AddMove(Move(square, targetSquare, quiet));
|
||||||
|
|
||||||
moves.PopBit(targetSquare);
|
moves.PopBit(targetSquare);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
while (captures) {
|
|
||||||
int targetSquare = captures.getLS1BIndex();
|
|
||||||
|
|
||||||
board.AddMove(Move(square, targetSquare, capture));
|
|
||||||
|
|
||||||
captures.PopBit(targetSquare);
|
|
||||||
}
|
|
||||||
|
|
||||||
queens.PopBit(square);
|
queens.PopBit(square);
|
||||||
}
|
}
|
||||||
@@ -277,7 +221,7 @@ void GenKingMoves(Board &board) {
|
|||||||
Bitboard captures = moves & board.colors[!board.sideToMove];
|
Bitboard captures = moves & board.colors[!board.sideToMove];
|
||||||
moves &= ~board.colors[!board.sideToMove];
|
moves &= ~board.colors[!board.sideToMove];
|
||||||
|
|
||||||
if constexpr (mode == Noisy) {
|
if constexpr (mode == Noisy || mode == All) {
|
||||||
while (captures) {
|
while (captures) {
|
||||||
int square = captures.getLS1BIndex();
|
int square = captures.getLS1BIndex();
|
||||||
|
|
||||||
@@ -286,57 +230,53 @@ void GenKingMoves(Board &board) {
|
|||||||
|
|
||||||
captures.PopBit(square);
|
captures.PopBit(square);
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (moves) {
|
if constexpr (mode == Quiet || mode == All) {
|
||||||
int square = moves.getLS1BIndex();
|
while (moves) {
|
||||||
|
int square = moves.getLS1BIndex();
|
||||||
|
|
||||||
board.AddMove(Move(kingSquare, square, quiet));
|
board.AddMove(Move(kingSquare, square, quiet));
|
||||||
|
|
||||||
moves.PopBit(square);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (captures) {
|
|
||||||
int square = captures.getLS1BIndex();
|
|
||||||
|
|
||||||
Move move(kingSquare, square, capture);
|
|
||||||
board.AddMove(move);
|
|
||||||
|
|
||||||
captures.PopBit(square);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generating castles
|
|
||||||
int kingRight = board.sideToMove ? blackKingRight : whiteKingRight;
|
|
||||||
int queenRight = board.sideToMove ? blackQueenRight : whiteQueenRight;
|
|
||||||
|
|
||||||
if (board.castlingRights & queenRight) {
|
|
||||||
U64 QueenSide = board.sideToMove ? 0x1f00000000000000 : 0x1f;
|
|
||||||
U64 mask = board.sideToMove ? 0x1c00000000000000 : 0x1c;
|
|
||||||
int targetSquare = board.sideToMove ? c8 : c1;
|
|
||||||
if ((Bitboard(QueenSide) & board.occupied).PopCount() == 2
|
|
||||||
&& !(mask & board.colorThreats[!board.sideToMove])) {
|
|
||||||
board.AddMove(Move(kingSquare, targetSquare, queenCastle));
|
|
||||||
|
|
||||||
|
moves.PopBit(square);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (board.castlingRights & kingRight) {
|
// Generating castles
|
||||||
U64 KingSide = board.sideToMove ? 0xf000000000000000 : 0xf0;
|
int kingRight = board.sideToMove ? blackKingRight : whiteKingRight;
|
||||||
U64 mask = board.sideToMove ? 0x7000000000000000 : 0x70;
|
int queenRight = board.sideToMove ? blackQueenRight : whiteQueenRight;
|
||||||
int targetSquare = board.sideToMove ? g8 : g1;
|
|
||||||
if ((Bitboard(KingSide) & board.occupied).PopCount() == 2
|
if (board.castlingRights & queenRight) {
|
||||||
&& !(mask & board.colorThreats[!board.sideToMove])) {
|
U64 QueenSide = board.sideToMove ? 0x1f00000000000000 : 0x1f;
|
||||||
board.AddMove(Move(kingSquare, targetSquare, kingCastle));
|
U64 mask = board.sideToMove ? 0x1c00000000000000 : 0x1c;
|
||||||
|
int targetSquare = board.sideToMove ? c8 : c1;
|
||||||
|
if ((Bitboard(QueenSide) & board.occupied).PopCount() == 2
|
||||||
|
&& !(mask & board.colorThreats[!board.sideToMove])) {
|
||||||
|
board.AddMove(Move(kingSquare, targetSquare, queenCastle));
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (board.castlingRights & kingRight) {
|
||||||
|
U64 KingSide = board.sideToMove ? 0xf000000000000000 : 0xf0;
|
||||||
|
U64 mask = board.sideToMove ? 0x7000000000000000 : 0x70;
|
||||||
|
int targetSquare = board.sideToMove ? g8 : g1;
|
||||||
|
if ((Bitboard(KingSide) & board.occupied).PopCount() == 2
|
||||||
|
&& !(mask & board.colorThreats[!board.sideToMove])) {
|
||||||
|
board.AddMove(Move(kingSquare, targetSquare, kingCastle));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <MovegenMode mode>
|
template <MovegenMode mode>
|
||||||
void GenerateMoves(Board &board) {
|
void GenerateMoves(Board &board, bool clear) {
|
||||||
board.ResetMoves();
|
if (clear) {
|
||||||
board.pieceThreats = std::array<Bitboard, 6>();
|
board.ResetMoves();
|
||||||
board.colorThreats = std::array<Bitboard, 2>();
|
board.pieceThreats = {};
|
||||||
|
board.colorThreats = {};
|
||||||
|
}
|
||||||
|
|
||||||
GenThreatMaps(board);
|
GenThreatMaps(board);
|
||||||
|
|
||||||
GenPawnMoves<mode>(board);
|
GenPawnMoves<mode>(board);
|
||||||
|
|||||||
+2
-2
@@ -186,8 +186,8 @@ int Forward(const Board& board, const Network& net) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int16_t Network::Evaluate(const Board& board) {
|
int16_t Network::Evaluate(const Board& board) {
|
||||||
return std::clamp(Forward(board, *this), (-SEARCH::MATE_SCORE + SEARCH::MAX_PLY),
|
return std::clamp(Forward(board, *this), (-SEARCH::MATE_SCORE + SEARCH::MAX_DEPTH),
|
||||||
(SEARCH::MATE_SCORE - SEARCH::MAX_PLY));
|
(SEARCH::MATE_SCORE - SEARCH::MAX_DEPTH));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+1
-1
@@ -16,7 +16,7 @@ constexpr size_t ALIGNMENT = 32;
|
|||||||
namespace NNUE {
|
namespace NNUE {
|
||||||
|
|
||||||
constexpr size_t INPUT_SIZE = 768;
|
constexpr size_t INPUT_SIZE = 768;
|
||||||
constexpr size_t HL_SIZE = 1024;
|
constexpr size_t HL_SIZE = 1536;
|
||||||
constexpr size_t OUTPUT_BUCKETS = 8;
|
constexpr size_t OUTPUT_BUCKETS = 8;
|
||||||
|
|
||||||
constexpr int16_t SCALE = 400;
|
constexpr int16_t SCALE = 400;
|
||||||
|
|||||||
+2
-2
@@ -3,7 +3,7 @@
|
|||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
|
|
||||||
static U64 HelperPerft(Board board, int depth) {
|
static U64 HelperPerft(Board board, int depth) {
|
||||||
MOVEGEN::GenerateMoves<All>(board);
|
MOVEGEN::GenerateMoves<All>(board, true);
|
||||||
|
|
||||||
if (depth == 0) return 1ULL;
|
if (depth == 0) return 1ULL;
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@ static U64 HelperPerft(Board board, int depth) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Perft(Board &board, int depth) {
|
void Perft(Board &board, int depth) {
|
||||||
MOVEGEN::GenerateMoves<All>(board);
|
MOVEGEN::GenerateMoves<All>(board, true);
|
||||||
|
|
||||||
U64 totalNodes = 0;
|
U64 totalNodes = 0;
|
||||||
|
|
||||||
|
|||||||
+387
-101
@@ -4,42 +4,46 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include "datagen.h"
|
#include "datagen.h"
|
||||||
#include "benchmark.h"
|
#include "benchmark.h"
|
||||||
|
#include "types.h"
|
||||||
|
#include "tunables.h"
|
||||||
|
#include "termcolor.hpp"
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
namespace SEARCH {
|
namespace SEARCH {
|
||||||
|
|
||||||
void InitLMRTable() {
|
void InitLMRTable() {
|
||||||
for (int depth = 0; depth <= MAX_DEPTH; depth++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
for (int move = 0; move < MAX_MOVES; move++) {
|
const double base = i ? lmrBaseQuiet : lmrBaseNoisy;
|
||||||
if (depth == 0 || move == 0) {
|
const double divisor = i ? lmrDivisorQuiet : lmrDivisorNoisy;
|
||||||
lmrTable[depth][move] = 0;
|
|
||||||
} else {
|
|
||||||
double base = 0.77;
|
|
||||||
double divisor = 2.36;
|
|
||||||
|
|
||||||
lmrTable[depth][move] = std::floor(base + std::log(depth) * std::log(move) / divisor);
|
for (int depth = 0; depth <= MAX_DEPTH; depth++) {
|
||||||
|
for (int move = 0; move < MAX_MOVES; move++) {
|
||||||
|
if (depth == 0 || move == 0) {
|
||||||
|
lmrTable[i][depth][move] = 0;
|
||||||
|
} else {
|
||||||
|
lmrTable[i][depth][move] = std::floor(base + std::log(depth) * std::log(move) / divisor);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t ContHistory::GetOnePly(Board& board, Move& move, SearchContext* ctx, int ply) {
|
int16_t ContHistory::GetNPly(Board& board, Move& move, SearchContext* ctx, int ply, int n) {
|
||||||
int prevType = ctx->ss[ply-1].pieceType;
|
int prevType = ctx->ss[ply-n].pieceType;
|
||||||
int prevTo = ctx->ss[ply-1].moveTo;
|
int prevTo = ctx->ss[ply-n].moveTo;
|
||||||
int pieceType = board.GetPieceType(move.MoveFrom());
|
int pieceType = board.GetPieceType(move.MoveFrom());
|
||||||
int to = move.MoveTo();
|
int to = move.MoveTo();
|
||||||
bool otherColor = ctx->ss[ply-1].side;
|
bool otherColor = ctx->ss[ply-n].side;
|
||||||
|
|
||||||
return ctx->conthist[otherColor][prevType][prevTo][board.sideToMove][pieceType][to];
|
return ctx->conthist[otherColor][prevType][prevTo][board.sideToMove][pieceType][to];
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t ContHistory::GetTwoPly(Board& board, Move& move, SearchContext* ctx, int ply) {
|
static int AdjustEval(Board &board, SearchContext* ctx, int eval) {
|
||||||
int prevType = ctx->ss[ply-2].pieceType;
|
int corrhist = ctx->corrhist.GetAllHist(board);
|
||||||
int prevTo = ctx->ss[ply-2].moveTo;
|
|
||||||
int pieceType = board.GetPieceType(move.MoveFrom());
|
|
||||||
int to = move.MoveTo();
|
|
||||||
bool otherColor = ctx->ss[ply-2].side;
|
|
||||||
|
|
||||||
return ctx->conthist[otherColor][prevType][prevTo][board.sideToMove][pieceType][to];
|
int mateFound = MATE_SCORE - MAX_DEPTH;
|
||||||
|
|
||||||
|
return std::clamp(eval + corrhist / CORRHIST_GRAIN, -mateFound + 1, mateFound - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) {
|
static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) {
|
||||||
@@ -56,18 +60,21 @@ static int ScoreMove(Board &board, Move &move, int ply, SearchContext* ctx) {
|
|||||||
targetType = Pawn;
|
targetType = Pawn;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 50000 * ((SEE(board, move, -100))) + (100 * targetType - attackerType + 105);
|
return 50000 * ((SEE(board, move, seeOrderingThreshold))) + (100 * targetType - attackerType + 105);
|
||||||
} else {
|
} else {
|
||||||
if (ctx->killerMoves[0][ply] == move) {
|
if (ctx->killerMoves[0][ply] == move) {
|
||||||
return 41000;
|
return 41000;
|
||||||
} else if (ctx->killerMoves[1][ply] == move) {
|
} else if (ctx->killerMoves[1][ply] == move) {
|
||||||
return 40000;
|
return 40000;
|
||||||
} else {
|
} else {
|
||||||
int historyScore = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()];
|
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;
|
int conthistScore = 0;
|
||||||
|
|
||||||
if (ply > 0) {
|
if (ply > 0) {
|
||||||
conthistScore = ctx->conthist.GetOnePly(board, move, ctx, ply);
|
conthistScore = ctx->conthist.GetNPly(board, move, ctx, ply, 1);
|
||||||
/*
|
/*
|
||||||
if (ply > 1) {
|
if (ply > 1) {
|
||||||
conthistScore += ctx->conthist.GetTwoPly(board, move, ctx, ply);
|
conthistScore += ctx->conthist.GetTwoPly(board, move, ctx, ply);
|
||||||
@@ -146,29 +153,51 @@ bool IsDraw(Board &board, SearchContext* ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <bool isPV>
|
template <bool isPV>
|
||||||
static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply, bool cutnode, SearchContext* ctx) {
|
static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply, bool cutnode, bool improving, bool corrplexity, SearchContext* ctx) {
|
||||||
int reduction = 0;
|
int reduction = 0;
|
||||||
|
|
||||||
// Late Move Reduction
|
// Late Move Reduction
|
||||||
if (depth >= 3 && moveSeen >= 2 + (2 * isPV) && !move.IsCapture()) {
|
if (depth >= 2 && moveSeen >= 2 + (2 * isPV)) {
|
||||||
reduction = lmrTable[depth][moveSeen];
|
reduction = lmrTable[move.IsQuiet()][depth][moveSeen] * 1024;
|
||||||
|
|
||||||
if (cutnode)
|
if (cutnode)
|
||||||
reduction += 2;
|
reduction += lmrCutnode;
|
||||||
|
|
||||||
// History LMR
|
if constexpr (isPV)
|
||||||
int historyReduction = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()];
|
reduction -= lmrIsPV;
|
||||||
|
|
||||||
|
if (!improving)
|
||||||
|
reduction += lmrImproving;
|
||||||
|
|
||||||
if (ply > 0)
|
if (corrplexity)
|
||||||
historyReduction += ctx->conthist.GetOnePly(board, move, ctx, ply);
|
reduction -= lmrCorrplexity;
|
||||||
|
|
||||||
historyReduction /= 8192;
|
if (move.IsQuiet()) {
|
||||||
reduction -= historyReduction;
|
// History LMR
|
||||||
|
|
||||||
|
bool sourceThreatened = board.IsSquareThreatened(board.sideToMove, move.MoveFrom());
|
||||||
|
bool targetThreatened = board.IsSquareThreatened(board.sideToMove, move.MoveTo());
|
||||||
|
|
||||||
|
int historyReduction = ctx->history[board.sideToMove][move.MoveFrom()][move.MoveTo()][sourceThreatened][targetThreatened];
|
||||||
|
if (ply > 0) {
|
||||||
|
historyReduction += ctx->conthist.GetNPly(board, move, ctx, ply, 1);
|
||||||
|
|
||||||
|
if (ply > 1)
|
||||||
|
historyReduction += ctx->conthist.GetNPly(board, move, ctx, ply, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
historyReduction = historyReduction / lmrHistoryDivisor;
|
||||||
|
|
||||||
|
reduction -= historyReduction * 1024;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reduction /= 1024;
|
||||||
|
|
||||||
return std::clamp(reduction, -1, depth - 1);
|
return std::clamp(reduction, -1, depth - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool SEE(Board& board, Move& move, int threshold) {
|
bool SEE(Board& board, Move& move, int threshold) {
|
||||||
int from = move.MoveFrom();
|
int from = move.MoveFrom();
|
||||||
int to = move.MoveTo();
|
int to = move.MoveTo();
|
||||||
@@ -242,32 +271,40 @@ bool SEE(Board& board, Move& move, int threshold) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <searchMode mode>
|
template <searchMode mode>
|
||||||
static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, SearchContext* ctx) {
|
static bool ShouldStop(SearchContext* ctx) {
|
||||||
if constexpr (mode != nodesMode) {
|
if constexpr (mode == normal) {
|
||||||
if constexpr (mode != bench) {
|
if (ctx->nodes % 1024 == 0) {
|
||||||
if (ctx->nodes % 1024 == 0) {
|
if (ctx->sw.GetElapsedMS() >= ctx->timeToSearch) {
|
||||||
if (ctx->sw.GetElapsedMS() >= ctx->timeToSearch) {
|
ctx->searchStopped = true;
|
||||||
ctx->searchStopped = true;
|
return true;
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == nodesMode) {
|
} else if constexpr (mode == nodesMode) {
|
||||||
if (ctx->nodes > ctx->nodesToGo) {
|
if (ctx->nodes > ctx->nodesToGo) {
|
||||||
ctx->searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
return 0;
|
return true;
|
||||||
}
|
}
|
||||||
} else if constexpr (mode == datagen) {
|
} else if constexpr (mode == datagen) {
|
||||||
if (ctx->nodes > DATAGEN::HARD_NODES) {
|
if (ctx->nodes > DATAGEN::HARD_NODES) {
|
||||||
ctx->searchStopped = true;
|
ctx->searchStopped = true;
|
||||||
return 0;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <searchMode mode>
|
||||||
|
static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, SearchContext* ctx) {
|
||||||
|
if (ShouldStop<mode>(ctx)) return 0;
|
||||||
|
|
||||||
|
if (ply >= MAX_DEPTH)
|
||||||
|
return NNUE::net.Evaluate(board);
|
||||||
|
|
||||||
if (ply > ctx->seldepth)
|
if (ply > ctx->seldepth)
|
||||||
ctx->seldepth = ply;
|
ctx->seldepth = ply;
|
||||||
|
|
||||||
int bestScore = NNUE::net.Evaluate(board);
|
int bestScore = AdjustEval(board, ctx, NNUE::net.Evaluate(board));
|
||||||
ctx->ss[ply].eval = bestScore;
|
ctx->ss[ply].eval = bestScore;
|
||||||
|
|
||||||
if (!ctx->excluded) {
|
if (!ctx->excluded) {
|
||||||
@@ -286,13 +323,25 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
|
|||||||
alpha = bestScore;
|
alpha = bestScore;
|
||||||
}
|
}
|
||||||
|
|
||||||
MOVEGEN::GenerateMoves<Noisy>(board);
|
const int fpScore = bestScore + 100;
|
||||||
|
|
||||||
|
MOVEGEN::GenerateMoves<Noisy>(board, true);
|
||||||
|
|
||||||
SortMoves(board, ply, ctx);
|
SortMoves(board, ply, ctx);
|
||||||
|
|
||||||
SearchResults results;
|
SearchResults results;
|
||||||
|
|
||||||
|
int nodeType = AllNode;
|
||||||
|
|
||||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
|
// QS FP
|
||||||
|
if (!board.InCheck() && board.moveList[i].IsCapture() &&
|
||||||
|
fpScore <= alpha && !SEE(board, board.moveList[i], 1)) {
|
||||||
|
|
||||||
|
bestScore = std::max(bestScore, fpScore);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
int isLegal = copy.MakeMove(board.moveList[i]);
|
int isLegal = copy.MakeMove(board.moveList[i]);
|
||||||
if (!isLegal) continue;
|
if (!isLegal) continue;
|
||||||
@@ -301,7 +350,7 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
|
|||||||
ctx->ss[ply].moveTo = board.moveList[i].MoveTo();
|
ctx->ss[ply].moveTo = board.moveList[i].MoveTo();
|
||||||
ctx->ss[ply].side = board.sideToMove;
|
ctx->ss[ply].side = board.sideToMove;
|
||||||
|
|
||||||
if (!SEE(board, board.moveList[i], 0))
|
if (!SEE(board, board.moveList[i], seeQsThreshold))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (copy.positionIndex >= ctx->positionHistory.size()) {
|
if (copy.positionIndex >= ctx->positionHistory.size()) {
|
||||||
@@ -309,15 +358,22 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
|
|||||||
}
|
}
|
||||||
ctx->positionHistory[copy.positionIndex] = copy.hashKey;
|
ctx->positionHistory[copy.positionIndex] = copy.hashKey;
|
||||||
ctx->nodes++;
|
ctx->nodes++;
|
||||||
|
|
||||||
|
ctx->TT.PrefetchEntry(copy.hashKey);
|
||||||
|
|
||||||
int score = -Quiescence<mode>(copy, -beta, -alpha, ply + 1, ctx).score;
|
int score = -Quiescence<mode>(copy, -beta, -alpha, ply + 1, ctx).score;
|
||||||
|
|
||||||
if (score >= beta) {
|
if (score >= beta) {
|
||||||
|
if (!ctx->excluded) {
|
||||||
|
ctx->TT.WriteEntry(board.hashKey, 0, score, CutNode, board.moveList[i]);
|
||||||
|
}
|
||||||
return score;
|
return score;
|
||||||
}
|
}
|
||||||
|
|
||||||
bestScore = std::max(score, bestScore);
|
bestScore = std::max(score, bestScore);
|
||||||
|
|
||||||
if (score > alpha) {
|
if (score > alpha) {
|
||||||
|
nodeType = PV;
|
||||||
alpha = score;
|
alpha = score;
|
||||||
results.bestMove = board.moveList[i];
|
results.bestMove = board.moveList[i];
|
||||||
}
|
}
|
||||||
@@ -325,31 +381,18 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
|
|||||||
|
|
||||||
results.score = bestScore;
|
results.score = bestScore;
|
||||||
if (ctx->searchStopped) return 0;
|
if (ctx->searchStopped) return 0;
|
||||||
|
if (!ctx->excluded) {
|
||||||
|
ctx->TT.WriteEntry(board.hashKey, 0, results.score, nodeType, results.bestMove);
|
||||||
|
}
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <bool isPV, searchMode mode>
|
template <bool isPV, searchMode mode>
|
||||||
SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext* ctx, bool cutnode) {
|
SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchContext* ctx, bool cutnode) {
|
||||||
if constexpr (mode != bench && mode != nodesMode) {
|
if (ShouldStop<mode>(ctx)) return 0;
|
||||||
if (ctx->nodes % 1024 == 0) {
|
|
||||||
if constexpr (mode == normal) {
|
if (ply >= MAX_DEPTH)
|
||||||
if (ctx->sw.GetElapsedMS() >= ctx->timeToSearch) {
|
return NNUE::net.Evaluate(board);
|
||||||
ctx->searchStopped = true;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if constexpr (mode == nodesMode) {
|
|
||||||
if (ctx->nodes > ctx->nodesToGo) {
|
|
||||||
ctx->searchStopped = true;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
} else if constexpr (mode == datagen) {
|
|
||||||
if (ctx->nodes > DATAGEN::HARD_NODES) {
|
|
||||||
ctx->searchStopped = true;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ply > ctx->seldepth)
|
if (ply > ctx->seldepth)
|
||||||
ctx->seldepth = ply;
|
ctx->seldepth = ply;
|
||||||
@@ -379,9 +422,12 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
|
|
||||||
if (depth <= 0) return Quiescence<mode>(board, alpha, beta, ply, ctx);
|
if (depth <= 0) return Quiescence<mode>(board, alpha, beta, ply, ctx);
|
||||||
|
|
||||||
const int staticEval = NNUE::net.Evaluate(board);
|
int rawEval = NNUE::net.Evaluate(board);
|
||||||
|
const int staticEval = AdjustEval(board, ctx, rawEval);
|
||||||
ctx->ss[ply].eval = staticEval;
|
ctx->ss[ply].eval = staticEval;
|
||||||
|
|
||||||
|
bool corrplexity = std::abs(rawEval - staticEval) > 70;
|
||||||
|
|
||||||
const bool improving = [&]
|
const bool improving = [&]
|
||||||
{
|
{
|
||||||
if (board.InCheck())
|
if (board.InCheck())
|
||||||
@@ -397,18 +443,25 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
if (!board.InCheck() && !ctx->excluded) {
|
if (!board.InCheck() && !ctx->excluded) {
|
||||||
if (ply) {
|
if (ply) {
|
||||||
// Reverse Futility Pruning
|
// Reverse Futility Pruning
|
||||||
int margin = 100 * (depth - improving);
|
int margin = rfpBase + rfpMargin * (depth - improving);
|
||||||
if (!ttHit && staticEval - margin >= beta && depth < 7) {
|
if (!ttHit && staticEval - margin >= beta && depth < 7) {
|
||||||
return (beta + (staticEval - beta) / 3);
|
return (beta + (staticEval - beta) / 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Razoring
|
||||||
|
if (!isPV && depth <= 3 && staticEval + razoringScalar * depth < alpha) {
|
||||||
|
return Quiescence<mode>(board, alpha, beta, ply, ctx).score;
|
||||||
|
}
|
||||||
|
|
||||||
// Null Move Pruning
|
// Null Move Pruning
|
||||||
if (!ctx->doingNullMove && staticEval >= beta) {
|
if (!ctx->doingNullMove && staticEval >= beta + nmpBetaMargin) {
|
||||||
if (depth > 1 && !board.InPossibleZug()) {
|
if (depth > 1 && !board.InPossibleZug()) {
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
copy.MakeMove(Move());
|
copy.MakeMove(Move());
|
||||||
|
|
||||||
const int reduction = 3 + improving;
|
const int reduction = 4 + improving + depth / 3;
|
||||||
|
|
||||||
|
ctx->TT.PrefetchEntry(copy.hashKey);
|
||||||
|
|
||||||
ctx->doingNullMove = true;
|
ctx->doingNullMove = true;
|
||||||
int score = -PVS<false, mode>(copy, depth - reduction, -beta, -beta + 1, ply + 1, ctx, !cutnode).score;
|
int score = -PVS<false, mode>(copy, depth - reduction, -beta, -beta + 1, ply + 1, ctx, !cutnode).score;
|
||||||
@@ -421,7 +474,58 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MOVEGEN::GenerateMoves<All>(board);
|
// Probcut
|
||||||
|
const int probcutBeta = beta + 200;
|
||||||
|
const int probcutDepth = std::max(depth - 3, 1);
|
||||||
|
|
||||||
|
if (depth >= 7 && std::abs(beta) < MATE_SCORE - MAX_DEPTH
|
||||||
|
&& (!entry.bestMove || !entry.bestMove.IsQuiet())
|
||||||
|
&& !(ttHit && entry.depth >= probcutDepth && entry.score < probcutBeta)) {
|
||||||
|
|
||||||
|
MOVEGEN::GenerateMoves<Noisy>(board, true);
|
||||||
|
SortMoves(board, ply, ctx);
|
||||||
|
|
||||||
|
const int seeThreshold = (probcutBeta - staticEval) * 15 / 16;
|
||||||
|
|
||||||
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
|
Move currMove = board.moveList[i];
|
||||||
|
|
||||||
|
if (ctx->excluded == currMove)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!SEE(board, currMove, seeThreshold))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Board copy = board;
|
||||||
|
bool isLegal = copy.MakeMove(currMove);
|
||||||
|
|
||||||
|
if (!isLegal) continue;
|
||||||
|
|
||||||
|
if (copy.positionIndex >= ctx->positionHistory.size()) {
|
||||||
|
ctx->positionHistory.resize(copy.positionIndex + 100);
|
||||||
|
}
|
||||||
|
ctx->positionHistory[copy.positionIndex] = copy.hashKey;
|
||||||
|
ctx->nodes++;
|
||||||
|
|
||||||
|
int score = -Quiescence<mode>(copy, -probcutBeta, -probcutBeta + 1, ply + 1, ctx).score;
|
||||||
|
|
||||||
|
if (score >= probcutBeta) {
|
||||||
|
score = -PVS<isPV, mode>(copy, probcutDepth - 1, -probcutBeta, -probcutBeta + 1,
|
||||||
|
ply + 1, ctx, !cutnode).score;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ctx->searchStopped) return 0;
|
||||||
|
|
||||||
|
if (score >= probcutBeta) {
|
||||||
|
ctx->TT.WriteEntry(board.hashKey, probcutDepth, score, CutNode, currMove);
|
||||||
|
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MOVEGEN::GenerateMoves<Quiet>(board, false);
|
||||||
|
} else {
|
||||||
|
MOVEGEN::GenerateMoves<All>(board, true);
|
||||||
|
}
|
||||||
|
|
||||||
SortMoves(board, ply, ctx);
|
SortMoves(board, ply, ctx);
|
||||||
|
|
||||||
@@ -440,30 +544,40 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
if (ctx->excluded == currMove)
|
if (ctx->excluded == currMove)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
bool notMated = results.score > (-MATE_SCORE + MAX_PLY);
|
bool notMated = results.score > (-MATE_SCORE + MAX_DEPTH);
|
||||||
|
int lmrDepth = depth - lmrTable[currMove.IsQuiet()][depth][moveSeen];
|
||||||
|
|
||||||
// Late move pruning
|
// Late move pruning
|
||||||
// If we are near a leaf node we prune moves
|
// If we are near a leaf node we prune moves
|
||||||
// that are late in the list
|
// that are late in the list
|
||||||
if (!isPV && !board.InCheck() && currMove.IsQuiet() && notMated) {
|
if (currMove.IsQuiet() && notMated) {
|
||||||
int lmpBase = 7;
|
|
||||||
|
|
||||||
|
int lmpThreshold = 7 + depth * depth * (1 + improving);
|
||||||
int lmpThreshold = lmpBase + 4 * depth;
|
|
||||||
|
|
||||||
if (moveSeen >= lmpThreshold) {
|
if (moveSeen >= lmpThreshold) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool sourceThreatened = board.IsSquareThreatened(board.sideToMove, currMove.MoveFrom());
|
||||||
|
bool targetThreatened = board.IsSquareThreatened(board.sideToMove, currMove.MoveTo());
|
||||||
|
|
||||||
// Futility pruning
|
// Futility pruning
|
||||||
// If our static eval is far below alpha, there is only a small chance
|
// If our static eval is far below alpha, there is only a small chance
|
||||||
// that a quiet move will help us so we skip them
|
// that a quiet move will help us so we skip them
|
||||||
int historyScore = ctx->history[board.sideToMove][currMove.MoveFrom()][currMove.MoveTo()];
|
int historyScore = ctx->history[board.sideToMove][currMove.MoveFrom()][currMove.MoveTo()][sourceThreatened][targetThreatened];
|
||||||
int fpMargin = 100 * depth + historyScore / 32;
|
|
||||||
|
if (ply > 0) {
|
||||||
|
historyScore += ctx->conthist.GetNPly(board, currMove, ctx, ply, 1);
|
||||||
|
|
||||||
|
if (ply > 1)
|
||||||
|
historyScore += ctx->conthist.GetNPly(board, currMove, ctx, ply, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
int margin = fpMargin * (lmrDepth + improving) + historyScore / 32;
|
||||||
|
|
||||||
if (!isPV && ply && currMove.IsQuiet()
|
if (!isPV && ply && currMove.IsQuiet()
|
||||||
&& depth <= 5 && staticEval + fpMargin < alpha && notMated) {
|
&& lmrDepth <= 5 && staticEval + margin < alpha && notMated) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -482,6 +596,8 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
ctx->positionHistory[copy.positionIndex] = copy.hashKey;
|
ctx->positionHistory[copy.positionIndex] = copy.hashKey;
|
||||||
ctx->nodes++;
|
ctx->nodes++;
|
||||||
|
|
||||||
|
ctx->TT.PrefetchEntry(copy.hashKey);
|
||||||
|
|
||||||
int extension = 0;
|
int extension = 0;
|
||||||
|
|
||||||
if (ply
|
if (ply
|
||||||
@@ -498,20 +614,43 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
ctx->excluded = currMove;
|
ctx->excluded = currMove;
|
||||||
const int singularScore = PVS<false, mode>(board, sDepth, sBeta-1, sBeta, ply, ctx, cutnode).score;
|
const int singularScore = PVS<false, mode>(board, sDepth, sBeta-1, sBeta, ply, ctx, cutnode).score;
|
||||||
ctx->excluded = Move();
|
ctx->excluded = Move();
|
||||||
|
|
||||||
|
// Singular extension
|
||||||
|
if (singularScore < sBeta) {
|
||||||
|
extension++;
|
||||||
|
|
||||||
if (singularScore < sBeta)
|
if constexpr (!isPV) {
|
||||||
extension = 1;
|
// Double extension
|
||||||
|
if (singularScore <= sBeta - 1 - doubleExtMargin) {
|
||||||
|
extension++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Multicut
|
||||||
|
else if (sBeta >= beta) {
|
||||||
|
return sBeta;
|
||||||
|
}
|
||||||
|
// Negative extension
|
||||||
|
else if (entry.score >= beta) {
|
||||||
|
extension--;
|
||||||
|
} else if (cutnode) {
|
||||||
|
extension--;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cutnode |= extension < 0;
|
||||||
|
|
||||||
// PVS SEE
|
// PVS SEE
|
||||||
int SEEThreshold = currMove.IsQuiet() ? -80 * depth : -30 * depth * depth;
|
int SEEThreshold = currMove.IsQuiet() ? seeQuietThreshold * depth : seeNoisyThreshold * depth * depth;
|
||||||
|
|
||||||
if (ply && depth <= 10 && !SEE(board, currMove, SEEThreshold))
|
if (ply && depth <= 10 && !SEE(board, currMove, SEEThreshold))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
int reductions = GetReductions<isPV>(board, currMove, depth, moveSeen, ply, cutnode, ctx);
|
int reductions = GetReductions<isPV>(board, currMove, depth, moveSeen, ply, cutnode, improving, corrplexity, ctx);
|
||||||
|
|
||||||
int newDepth = depth + copy.InCheck() - 1 + extension;
|
int newDepth = depth + (copy.InCheck() && !ctx->excluded) - 1 + extension;
|
||||||
|
|
||||||
|
U64 nodesBeforeSearch = ctx->nodes;
|
||||||
|
|
||||||
// First move (suspected PV node)
|
// First move (suspected PV node)
|
||||||
if (!moveSeen) {
|
if (!moveSeen) {
|
||||||
@@ -541,6 +680,11 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
|
|
||||||
moveSeen++;
|
moveSeen++;
|
||||||
|
|
||||||
|
// Root
|
||||||
|
if (!ply) {
|
||||||
|
ctx->nodesTable[currMove % 4096] += ctx->nodes - nodesBeforeSearch;
|
||||||
|
}
|
||||||
|
|
||||||
if (ctx->searchStopped) return 0;
|
if (ctx->searchStopped) return 0;
|
||||||
|
|
||||||
if (currMove != 0 && currMove.IsQuiet()) {
|
if (currMove != 0 && currMove.IsQuiet()) {
|
||||||
@@ -554,9 +698,9 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
ctx->killerMoves[1][ply] = ctx->killerMoves[0][ply];
|
ctx->killerMoves[1][ply] = ctx->killerMoves[0][ply];
|
||||||
ctx->killerMoves[0][ply] = currMove;
|
ctx->killerMoves[0][ply] = currMove;
|
||||||
|
|
||||||
int bonus = 300 * depth - 250;
|
int bonus = historyBonusMultiplier * depth - historyBonusSub;
|
||||||
|
|
||||||
ctx->history.Update(board.sideToMove, currMove, bonus);
|
ctx->history.Update(board.sideToMove, currMove, sourceThreatened, targetThreatened, bonus);
|
||||||
|
|
||||||
if (ply > 0) {
|
if (ply > 0) {
|
||||||
int prevType = ctx->ss[ply-1].pieceType;
|
int prevType = ctx->ss[ply-1].pieceType;
|
||||||
@@ -596,7 +740,10 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
|
|
||||||
// Malus
|
// Malus
|
||||||
for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) {
|
for (int moveIndex = 0; moveIndex < seenQuietsCount - 1; moveIndex++) {
|
||||||
ctx->history.Update(board.sideToMove, seenQuiets[moveIndex], -bonus);
|
sourceThreatened = board.IsSquareThreatened(board.sideToMove, seenQuiets[moveIndex].MoveFrom());
|
||||||
|
targetThreatened = board.IsSquareThreatened(board.sideToMove, seenQuiets[moveIndex].MoveTo());
|
||||||
|
|
||||||
|
ctx->history.Update(board.sideToMove, seenQuiets[moveIndex], sourceThreatened, targetThreatened, -bonus);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -624,14 +771,24 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (ctx->searchStopped) return 0;
|
if (ctx->searchStopped) return 0;
|
||||||
if (!ctx->excluded)
|
if (!ctx->excluded) {
|
||||||
|
|
||||||
|
if (!board.InCheck() && ((results.bestMove.IsQuiet() || !results.bestMove))
|
||||||
|
&& !(nodeType == AllNode && results.score >= staticEval)) {
|
||||||
|
|
||||||
|
int corrHistBonus = std::clamp(results.score - staticEval, -CORRHIST_LIMIT, CORRHIST_LIMIT);
|
||||||
|
|
||||||
|
ctx->corrhist.UpdateAll(board, depth, corrHistBonus);
|
||||||
|
}
|
||||||
|
|
||||||
ctx->TT.WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove);
|
ctx->TT.WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove);
|
||||||
|
}
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
class AspirationWindow {
|
class AspirationWindow {
|
||||||
public:
|
public:
|
||||||
int delta = 50;
|
int delta = aspInitialDelta;
|
||||||
|
|
||||||
int alpha = -inf;
|
int alpha = -inf;
|
||||||
int beta = inf;
|
int beta = inf;
|
||||||
@@ -647,12 +804,20 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Set(int score) {
|
void Set(int score) {
|
||||||
delta = 50;
|
delta = aspInitialDelta;
|
||||||
alpha = score - delta;
|
alpha = score - delta;
|
||||||
beta = score + delta;
|
beta = score + delta;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static double ScaleTime(SearchContext *ctx, Move &move) {
|
||||||
|
double notBmNodesFraction =
|
||||||
|
ctx->nodesTable[move % 4096] / double(ctx->nodes);
|
||||||
|
double nodeScalingFactor = (1.5f - notBmNodesFraction) * 1.35f;
|
||||||
|
|
||||||
|
return nodeScalingFactor;
|
||||||
|
}
|
||||||
|
|
||||||
// Iterative deepening
|
// Iterative deepening
|
||||||
template <searchMode mode>
|
template <searchMode mode>
|
||||||
static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) {
|
static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) {
|
||||||
@@ -673,15 +838,35 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) {
|
|||||||
|
|
||||||
int elapsed = 0;
|
int elapsed = 0;
|
||||||
|
|
||||||
|
double nodeScaling = 1;
|
||||||
|
|
||||||
|
if (!UCIEnabled && (mode == normal || mode == nodesMode)) {
|
||||||
|
std::cout << termcolor::bold;
|
||||||
|
std::cout << std::setw(6) << std::left << "Depth"
|
||||||
|
<< std::setw(10) << std::right << "Time"
|
||||||
|
<< std::setw(9) << std::right << "Score"
|
||||||
|
<< std::setw(11) << std::right << "Nodes"
|
||||||
|
<< std::setw(12) << std::right << "NPS"
|
||||||
|
<< " PV" << std::endl;
|
||||||
|
std::cout << termcolor::reset;
|
||||||
|
std::cout << std::string(70, '-') << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
ctx->sw.Restart();
|
ctx->sw.Restart();
|
||||||
|
|
||||||
for (int depth = 1; depth <= toDepth; depth++) {
|
for (int depth = 1; depth <= toDepth; depth++) {
|
||||||
ctx->timeToSearch = std::max((fullTime / movesToGo) + (inc / 2), 4);
|
ctx->timeToSearch = std::max((fullTime / movesToGo) + (inc / 2), 4);
|
||||||
int softTime = ctx->timeToSearch * 0.65;
|
int softTime = ctx->timeToSearch * 0.65 * nodeScaling;
|
||||||
|
ctx->seldepth = 0;
|
||||||
|
|
||||||
SearchResults currentResults = PVS<true, mode>(board, depth, aw.alpha, aw.beta, 0, ctx, false);
|
SearchResults currentResults = PVS<true, mode>(board, depth, aw.alpha, aw.beta, 0, ctx, false);
|
||||||
|
|
||||||
elapsed = ctx->sw.GetElapsedMS();
|
elapsed = ctx->sw.GetElapsedMS();
|
||||||
|
|
||||||
|
if (depth > 6) {
|
||||||
|
nodeScaling = ScaleTime(ctx, currentResults.bestMove);
|
||||||
|
}
|
||||||
|
|
||||||
if (aw.alpha != -inf && currentResults.score <= aw.alpha) {
|
if (aw.alpha != -inf && currentResults.score <= aw.alpha) {
|
||||||
aw.WidenDown();
|
aw.WidenDown();
|
||||||
depth--;
|
depth--;
|
||||||
@@ -703,16 +888,7 @@ static SearchResults ID(Board &board, SearchParams params, SearchContext* ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if constexpr (mode == normal || mode == nodesMode) {
|
if constexpr (mode == normal || mode == nodesMode) {
|
||||||
std::cout << "info ";
|
PrintSearchInfo(ctx, safeResults, depth, elapsed);
|
||||||
std::cout << "depth " << depth;
|
|
||||||
std::cout << " seldepth " << ctx->seldepth;
|
|
||||||
std::cout << " time " << elapsed;
|
|
||||||
std::cout << " score cp " << safeResults.score;
|
|
||||||
std::cout << " nodes " << ctx->nodes << " nps " << int(ctx->nodes/ctx->sw.GetElapsedSec());
|
|
||||||
std::cout << " hashfull " << ctx->TT.GetUsedPercentage();
|
|
||||||
std::cout << " pv ";
|
|
||||||
ctx->pvLine.Print(0);
|
|
||||||
std::cout << std::endl;
|
|
||||||
|
|
||||||
if constexpr (mode == normal) {
|
if constexpr (mode == normal) {
|
||||||
if (ctx->sw.GetElapsedMS() >= softTime) {
|
if (ctx->sw.GetElapsedMS() >= softTime) {
|
||||||
@@ -736,6 +912,7 @@ template <searchMode mode>
|
|||||||
SearchResults SearchPosition(Board &board, SearchParams params, SearchContext* ctx) {
|
SearchResults SearchPosition(Board &board, SearchParams params, SearchContext* ctx) {
|
||||||
ctx->searchStopped = false;
|
ctx->searchStopped = false;
|
||||||
ctx->seldepth = 0;
|
ctx->seldepth = 0;
|
||||||
|
ctx->nodesTable = {};
|
||||||
if constexpr (mode != bench) {
|
if constexpr (mode != bench) {
|
||||||
ctx->nodes = 0;
|
ctx->nodes = 0;
|
||||||
|
|
||||||
@@ -747,7 +924,7 @@ SearchResults SearchPosition(Board &board, SearchParams params, SearchContext* c
|
|||||||
|
|
||||||
SearchResults results = ID<mode>(board, params, ctx);
|
SearchResults results = ID<mode>(board, params, ctx);
|
||||||
|
|
||||||
if constexpr (mode != normal) return results;
|
if constexpr (mode != normal && mode != nodesMode) return results;
|
||||||
|
|
||||||
std::cout << "bestmove ";
|
std::cout << "bestmove ";
|
||||||
results.bestMove.PrintMove();
|
results.bestMove.PrintMove();
|
||||||
@@ -778,6 +955,115 @@ int MoveEstimatedValue(Board& board, Move& move) {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PrintSearchInfo(SearchContext* ctx, SearchResults& results, int depth, int elapsed) {
|
||||||
|
if (UCIEnabled) {
|
||||||
|
std::cout << "info ";
|
||||||
|
std::cout << "depth " << depth;
|
||||||
|
std::cout << " seldepth " << ctx->seldepth;
|
||||||
|
std::cout << " time " << elapsed;
|
||||||
|
std::cout << " score ";
|
||||||
|
|
||||||
|
if (std::abs(results.score) + MAX_DEPTH >= MATE_SCORE) {
|
||||||
|
int mateIn = (MATE_SCORE - (std::abs(results.score) - 1)) / 2;
|
||||||
|
mateIn = results.score < 0 ? mateIn * -1 : mateIn;
|
||||||
|
|
||||||
|
std::cout << "mate " << mateIn;
|
||||||
|
} else {
|
||||||
|
std::cout << "cp " << results.score;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << " nodes " << ctx->nodes << " nps " << int(ctx->nodes/ctx->sw.GetElapsedSec());
|
||||||
|
std::cout << " hashfull " << ctx->TT.GetUsedPercentage();
|
||||||
|
std::cout << " pv ";
|
||||||
|
ctx->pvLine.Print(0, depth % 2 == 0);
|
||||||
|
std::cout << std::endl;
|
||||||
|
} else {
|
||||||
|
if (depth % 2 == 0) {
|
||||||
|
std::cout << termcolor::color<247>;
|
||||||
|
} else {
|
||||||
|
std::cout << termcolor::color<251>;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::stringstream depthStr;
|
||||||
|
depthStr << depth << '/' << ctx->seldepth;
|
||||||
|
std::cout << std::setw(6) << std::left << depthStr.str();
|
||||||
|
|
||||||
|
std::stringstream timeStr;
|
||||||
|
if (elapsed >= 1000) {
|
||||||
|
timeStr << std::fixed << std::setprecision(1) << (elapsed / 1000.0) << "s";
|
||||||
|
} else {
|
||||||
|
timeStr << elapsed << "ms";
|
||||||
|
}
|
||||||
|
std::cout << std::setw(10) << std::right << timeStr.str();
|
||||||
|
|
||||||
|
std::stringstream scoreStr;
|
||||||
|
bool isMate = false;
|
||||||
|
int mateIn = 0;
|
||||||
|
|
||||||
|
if (std::abs(results.score) + MAX_DEPTH >= MATE_SCORE) {
|
||||||
|
isMate = true;
|
||||||
|
mateIn = (MATE_SCORE - (std::abs(results.score) - 1)) / 2;
|
||||||
|
mateIn = results.score < 0 ? mateIn * -1 : mateIn;
|
||||||
|
if (mateIn < 0) {
|
||||||
|
scoreStr << "-M" << std::abs(mateIn);
|
||||||
|
} else {
|
||||||
|
scoreStr << "+M" << mateIn;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
scoreStr << std::showpos << std::fixed << std::setprecision(2)
|
||||||
|
<< (results.score / 100.0) << std::noshowpos;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isMate) {
|
||||||
|
if (mateIn > 0) {
|
||||||
|
std::cout << termcolor::bright_green;
|
||||||
|
} else {
|
||||||
|
std::cout << termcolor::bright_red;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (results.score > 0) {
|
||||||
|
std::cout << termcolor::green;
|
||||||
|
} else if (results.score < 0) {
|
||||||
|
std::cout << termcolor::red;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << std::setw(9) << std::right << scoreStr.str();
|
||||||
|
std::cout << termcolor::reset;
|
||||||
|
if (depth % 2 == 0) {
|
||||||
|
std::cout << termcolor::color<247>;
|
||||||
|
} else {
|
||||||
|
std::cout << termcolor::color<251>;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::stringstream nodesStr;
|
||||||
|
if (ctx->nodes >= 1000000) {
|
||||||
|
nodesStr << std::fixed << std::setprecision(1) << (ctx->nodes / 1000000.0) << "M";
|
||||||
|
} else if (ctx->nodes >= 1000) {
|
||||||
|
nodesStr << std::fixed << std::setprecision(1) << (ctx->nodes / 1000.0) << "k";
|
||||||
|
} else {
|
||||||
|
nodesStr << ctx->nodes;
|
||||||
|
}
|
||||||
|
std::cout << std::setw(11) << std::right << nodesStr.str();
|
||||||
|
|
||||||
|
int nps = int(ctx->nodes/ctx->sw.GetElapsedSec());
|
||||||
|
std::stringstream npsStr;
|
||||||
|
if (nps >= 1000000) {
|
||||||
|
npsStr << std::fixed << std::setprecision(1) << (nps / 1000000.0) << "M/s";
|
||||||
|
} else if (nps >= 1000) {
|
||||||
|
npsStr << std::fixed << std::setprecision(1) << (nps / 1000.0) << "k/s";
|
||||||
|
} else {
|
||||||
|
npsStr << nps << "/s";
|
||||||
|
}
|
||||||
|
std::cout << std::setw(12) << std::right << npsStr.str();
|
||||||
|
|
||||||
|
std::cout << " ";
|
||||||
|
ctx->pvLine.Print(0, depth % 2 == 0);
|
||||||
|
|
||||||
|
std::cout << termcolor::reset << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template SearchResults PVS<true, searchMode::bench>(Board&, int, int, int, int, SearchContext* ctx, bool cutnode);
|
template SearchResults PVS<true, searchMode::bench>(Board&, int, int, int, int, SearchContext* ctx, bool cutnode);
|
||||||
template SearchResults PVS<false, searchMode::bench>(Board&, int, int, int, int, SearchContext* ctx, bool cutnode);
|
template SearchResults PVS<false, searchMode::bench>(Board&, int, int, int, int, SearchContext* ctx, bool cutnode);
|
||||||
template SearchResults PVS<true, searchMode::normal>(Board&, int, int, int, int, SearchContext* ctx, bool cutnode);
|
template SearchResults PVS<true, searchMode::normal>(Board&, int, int, int, int, SearchContext* ctx, bool cutnode);
|
||||||
|
|||||||
+94
-22
@@ -5,6 +5,8 @@
|
|||||||
#include "move.h"
|
#include "move.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include "stopwatch.h"
|
#include "stopwatch.h"
|
||||||
|
#include "tunables.h"
|
||||||
|
#include "termcolor.hpp"
|
||||||
#include "tt.h"
|
#include "tt.h"
|
||||||
|
|
||||||
#include "../external/multi_array.h"
|
#include "../external/multi_array.h"
|
||||||
@@ -19,22 +21,64 @@ enum searchMode {
|
|||||||
};
|
};
|
||||||
|
|
||||||
constexpr int MATE_SCORE = 32767;
|
constexpr int MATE_SCORE = 32767;
|
||||||
constexpr int MAX_DEPTH = 128;
|
constexpr int MAX_DEPTH = 256;
|
||||||
constexpr int MAX_PLY = 512;
|
|
||||||
constexpr int MAX_HISTORY = 16384;
|
constexpr int MAX_HISTORY = 16384;
|
||||||
|
|
||||||
|
constexpr int CORRHIST_WEIGHT_SCALE = 256;
|
||||||
|
constexpr int CORRHIST_GRAIN = 256;
|
||||||
|
constexpr int CORRHIST_LIMIT = 1024;
|
||||||
|
constexpr int CORRHIST_SIZE = 16384;
|
||||||
|
constexpr int CORRHIST_MAX = 16384;
|
||||||
|
|
||||||
constexpr int32_t ScoreNone = -255000;
|
constexpr int32_t ScoreNone = -255000;
|
||||||
constexpr int inf = 100000;
|
constexpr int inf = 100000;
|
||||||
|
|
||||||
inline int lmrTable[MAX_DEPTH+1][MAX_MOVES];
|
// first index: [0] noisy, [1] quiet
|
||||||
|
inline int lmrTable[2][MAX_DEPTH+1][MAX_MOVES];
|
||||||
|
|
||||||
constexpr std::array<int, 6> SEEPieceValues = {
|
#ifdef TUNING
|
||||||
100, 300, 350, 500, 900, 0
|
inline std::array<int, 6> SEEPieceValues = {
|
||||||
|
seePawnValue, seeKnightValue, seeBishopValue, seeRookValue, seeQueenValue, 0
|
||||||
};
|
};
|
||||||
|
#else
|
||||||
|
constexpr std::array<int, 6> SEEPieceValues = {
|
||||||
|
seePawnValue, seeKnightValue, seeBishopValue, seeRookValue, seeQueenValue, 0
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
void InitLMRTable();
|
void InitLMRTable();
|
||||||
class SearchContext;
|
class SearchContext;
|
||||||
|
|
||||||
|
class CorrHist {
|
||||||
|
private:
|
||||||
|
// indexed by [stm][key]
|
||||||
|
MultiArray<int, 2, CORRHIST_SIZE> pawnHist;
|
||||||
|
MultiArray<int, 2, CORRHIST_SIZE> nonPawnHist;
|
||||||
|
public:
|
||||||
|
void Update(Board& board, int depth, int diff, int* entry) {
|
||||||
|
const int scaledDiff = diff * CORRHIST_GRAIN;
|
||||||
|
const int newWeight = std::min(depth * depth + 2 * depth + 1, 128);
|
||||||
|
|
||||||
|
*entry = (*entry * (CORRHIST_WEIGHT_SCALE - newWeight) + scaledDiff * newWeight) / CORRHIST_WEIGHT_SCALE;
|
||||||
|
*entry = std::clamp(*entry, -CORRHIST_MAX, CORRHIST_MAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int GetAllHist(Board& board) {
|
||||||
|
return pawnHist[board.sideToMove][board.pawnKey % CORRHIST_SIZE]
|
||||||
|
+ nonPawnHist[board.sideToMove][board.nonPawnKey % 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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class ContHistory {
|
class ContHistory {
|
||||||
private:
|
private:
|
||||||
// indexed by [other color][prev move piece][stm][prev move to][piece][to]
|
// indexed by [other color][prev move piece][stm][prev move to][piece][to]
|
||||||
@@ -50,9 +94,8 @@ public:
|
|||||||
void Clear() {
|
void Clear() {
|
||||||
std::fill(&contHistMoves[0][0][0][0][0][0], &contHistMoves[0][0][0][0][0][0] + sizeof(contHistMoves) / sizeof(int16_t), 0);
|
std::fill(&contHistMoves[0][0][0][0][0][0], &contHistMoves[0][0][0][0][0][0] + sizeof(contHistMoves) / sizeof(int16_t), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t GetOnePly(Board& board, Move& move, SearchContext* ctx, int ply);
|
int16_t GetNPly(Board& board, Move& move, SearchContext* ctx, int ply, int n);
|
||||||
int16_t GetTwoPly(Board& board, Move& move, SearchContext* ctx, int ply);
|
|
||||||
|
|
||||||
auto& operator[](int index) {
|
auto& operator[](int index) {
|
||||||
return contHistMoves[index];
|
return contHistMoves[index];
|
||||||
@@ -61,16 +104,17 @@ public:
|
|||||||
|
|
||||||
class History {
|
class History {
|
||||||
private:
|
private:
|
||||||
MultiArray<int, 2, 64, 64> historyMoves;
|
// indexed by [stm][from][to][threatenedSource][threatenedTarget]
|
||||||
|
MultiArray<int, 2, 64, 64, 2, 2> historyMoves;
|
||||||
public:
|
public:
|
||||||
void Update(bool stm, Move move, int bonus) {
|
void Update(bool stm, Move move, bool source, bool target, int bonus) {
|
||||||
int clampedBonus = std::clamp(bonus, -MAX_HISTORY, MAX_HISTORY);
|
int clampedBonus = std::clamp(bonus, -MAX_HISTORY, MAX_HISTORY);
|
||||||
historyMoves[stm][move.MoveFrom()][move.MoveTo()] +=
|
historyMoves[stm][move.MoveFrom()][move.MoveTo()][source][target] +=
|
||||||
clampedBonus - historyMoves[stm][move.MoveFrom()][move.MoveTo()] * std::abs(clampedBonus) / MAX_HISTORY;
|
clampedBonus - historyMoves[stm][move.MoveFrom()][move.MoveTo()][source][target] * std::abs(clampedBonus) / MAX_HISTORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Clear() {
|
void Clear() {
|
||||||
std::fill(&historyMoves[0][0][0], &historyMoves[0][0][0] + sizeof(historyMoves) / sizeof(int), 0);
|
std::fill(&historyMoves[0][0][0][0][0], &historyMoves[0][0][0][0][0] + sizeof(historyMoves) / sizeof(int), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto& operator[](int index) {
|
auto& operator[](int index) {
|
||||||
@@ -80,8 +124,8 @@ public:
|
|||||||
|
|
||||||
class PVLine {
|
class PVLine {
|
||||||
private:
|
private:
|
||||||
int length[MAX_PLY] = {};
|
int length[MAX_DEPTH] = {};
|
||||||
Move table[MAX_PLY][MAX_PLY] = {};
|
Move table[MAX_DEPTH][MAX_DEPTH] = {};
|
||||||
public:
|
public:
|
||||||
void SetLength(int ply) {
|
void SetLength(int ply) {
|
||||||
length[ply] = ply;
|
length[ply] = ply;
|
||||||
@@ -96,12 +140,34 @@ public:
|
|||||||
|
|
||||||
length[ply] = length[ply + 1];
|
length[ply] = length[ply + 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
void Print(int n) {
|
void Print(int n, bool isEven = false) {
|
||||||
for (int i = 0; i < length[n]; i++) {
|
for (int i = 0; i < length[n]; i++) {
|
||||||
table[n][i].PrintMove();
|
if (i == 0 && !UCIEnabled) {
|
||||||
|
std::cout << termcolor::bright_cyan;
|
||||||
|
table[n][i].PrintMove();
|
||||||
|
std::cout << termcolor::reset;
|
||||||
|
|
||||||
|
if (isEven) {
|
||||||
|
std::cout << termcolor::color<247>;
|
||||||
|
} else {
|
||||||
|
std::cout << termcolor::color<251>;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
table[n][i].PrintMove();
|
||||||
|
}
|
||||||
std::cout << ' ';
|
std::cout << ' ';
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string GetLine(int n) {
|
||||||
|
std::string result = "";
|
||||||
|
for (int i = 0; i < length[n]; i++) {
|
||||||
|
result += table[n][i].GetMoveString();
|
||||||
|
result += ' ';
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Clear() {
|
void Clear() {
|
||||||
@@ -125,24 +191,27 @@ public:
|
|||||||
U64 nodesToGo = 0;
|
U64 nodesToGo = 0;
|
||||||
int timeToSearch = 0;
|
int timeToSearch = 0;
|
||||||
|
|
||||||
|
std::array<U64, 4096> nodesTable{};
|
||||||
|
|
||||||
int seldepth = 0;
|
int seldepth = 0;
|
||||||
|
|
||||||
Move excluded = Move();
|
Move excluded = Move();
|
||||||
|
|
||||||
bool doingNullMove = false;
|
bool doingNullMove = false;
|
||||||
bool searchStopped = false;
|
bool searchStopped = false;
|
||||||
|
|
||||||
PVLine pvLine;
|
PVLine pvLine;
|
||||||
History history;
|
History history;
|
||||||
ContHistory conthist;
|
ContHistory conthist;
|
||||||
|
CorrHist corrhist;
|
||||||
|
|
||||||
std::array<std::array<int, MAX_PLY>, 2> killerMoves{};
|
std::array<std::array<int, MAX_DEPTH>, 2> killerMoves{};
|
||||||
|
|
||||||
std::vector<U64> positionHistory;
|
std::vector<U64> positionHistory;
|
||||||
|
|
||||||
TTable TT;
|
TTable TT;
|
||||||
|
|
||||||
std::array<Stack, MAX_PLY> ss{};
|
std::array<Stack, MAX_DEPTH> ss{};
|
||||||
|
|
||||||
Stopwatch sw;
|
Stopwatch sw;
|
||||||
|
|
||||||
@@ -150,6 +219,7 @@ public:
|
|||||||
pvLine.Clear();
|
pvLine.Clear();
|
||||||
history.Clear();
|
history.Clear();
|
||||||
conthist.Clear();
|
conthist.Clear();
|
||||||
|
corrhist.Clear();
|
||||||
TT.Clear();
|
TT.Clear();
|
||||||
sw.Restart();
|
sw.Restart();
|
||||||
killerMoves = {};
|
killerMoves = {};
|
||||||
@@ -170,5 +240,7 @@ bool IsDraw(Board &board, SearchContext* ctx);
|
|||||||
|
|
||||||
void ListScores(Board &board, SearchContext* ctx);
|
void ListScores(Board &board, SearchContext* ctx);
|
||||||
|
|
||||||
|
void PrintSearchInfo(SearchContext* ctx, SearchResults& results, int depth, int elapsed);
|
||||||
|
|
||||||
int MoveEstimatedValue(Board& board, Move& move);
|
int MoveEstimatedValue(Board& board, Move& move);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,940 @@
|
|||||||
|
//!
|
||||||
|
//! termcolor
|
||||||
|
//! ~~~~~~~~~
|
||||||
|
//!
|
||||||
|
//! termcolor is a header-only c++ library for printing colored messages
|
||||||
|
//! to the terminal. Written just for fun with a help of the Force.
|
||||||
|
//!
|
||||||
|
//! :copyright: (c) 2013 by Ihor Kalnytskyi
|
||||||
|
//! :license: BSD, see LICENSE for details
|
||||||
|
//!
|
||||||
|
|
||||||
|
#ifndef TERMCOLOR_HPP_
|
||||||
|
#define TERMCOLOR_HPP_
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// Detect target's platform and set some macros in order to wrap platform
|
||||||
|
// specific code this library depends on.
|
||||||
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
|
# define TERMCOLOR_TARGET_WINDOWS
|
||||||
|
#elif defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
|
||||||
|
# define TERMCOLOR_TARGET_POSIX
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// If implementation has not been explicitly set, try to choose one based on
|
||||||
|
// target platform.
|
||||||
|
#if !defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) && !defined(TERMCOLOR_USE_WINDOWS_API) && !defined(TERMCOLOR_USE_NOOP)
|
||||||
|
# if defined(TERMCOLOR_TARGET_POSIX)
|
||||||
|
# define TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES
|
||||||
|
# define TERMCOLOR_AUTODETECTED_IMPLEMENTATION
|
||||||
|
# elif defined(TERMCOLOR_TARGET_WINDOWS)
|
||||||
|
# define TERMCOLOR_USE_WINDOWS_API
|
||||||
|
# define TERMCOLOR_AUTODETECTED_IMPLEMENTATION
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// These headers provide isatty()/fileno() functions, which are used for
|
||||||
|
// testing whether a standard stream refers to the terminal.
|
||||||
|
#if defined(TERMCOLOR_TARGET_POSIX)
|
||||||
|
# include <unistd.h>
|
||||||
|
#elif defined(TERMCOLOR_TARGET_WINDOWS)
|
||||||
|
# include <io.h>
|
||||||
|
# include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
namespace termcolor
|
||||||
|
{
|
||||||
|
// Forward declaration of the `_internal` namespace.
|
||||||
|
// All comments are below.
|
||||||
|
namespace _internal
|
||||||
|
{
|
||||||
|
inline int colorize_index();
|
||||||
|
inline FILE* get_standard_stream(const std::ostream& stream);
|
||||||
|
inline FILE* get_standard_stream(const std::wostream& stream);
|
||||||
|
template <typename CharT>
|
||||||
|
bool is_colorized(std::basic_ostream<CharT>& stream);
|
||||||
|
template <typename CharT>
|
||||||
|
bool is_atty(const std::basic_ostream<CharT>& stream);
|
||||||
|
|
||||||
|
#if defined(TERMCOLOR_TARGET_WINDOWS)
|
||||||
|
template <typename CharT>
|
||||||
|
void win_change_attributes(std::basic_ostream<CharT>& stream, int foreground, int background = -1);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& colorize(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
stream.iword(_internal::colorize_index()) = 1L;
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& nocolorize(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
stream.iword(_internal::colorize_index()) = 0L;
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& reset(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[00m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream, -1, -1);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& bold(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[1m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& dark(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[2m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& italic(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[3m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& underline(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[4m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream, -1, COMMON_LVB_UNDERSCORE);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& blink(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[5m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& reverse(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[7m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& concealed(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[8m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& crossed(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[9m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <uint8_t code, typename CharT>
|
||||||
|
std::basic_ostream<CharT>& color(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[38;5;" << +code << "m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <uint8_t code, typename CharT>
|
||||||
|
std::basic_ostream<CharT>& on_color(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[48;5;" << +code << "m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <uint8_t r, uint8_t g, uint8_t b, typename CharT>
|
||||||
|
std::basic_ostream<CharT>& color(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[38;2;" << +r << ";" << +g << ";" << +b << "m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <uint8_t r, uint8_t g, uint8_t b, typename CharT>
|
||||||
|
std::basic_ostream<CharT>& on_color(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[48;2;" << +r << ";" << +g << ";" << +b << "m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& grey(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[30m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream,
|
||||||
|
0 // grey (black)
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& red(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[31m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream,
|
||||||
|
FOREGROUND_RED
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& green(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[32m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream,
|
||||||
|
FOREGROUND_GREEN
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& yellow(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[33m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream,
|
||||||
|
FOREGROUND_GREEN | FOREGROUND_RED
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& blue(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[34m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream,
|
||||||
|
FOREGROUND_BLUE
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& magenta(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[35m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream,
|
||||||
|
FOREGROUND_BLUE | FOREGROUND_RED
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& cyan(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[36m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream,
|
||||||
|
FOREGROUND_BLUE | FOREGROUND_GREEN
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& white(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[37m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream,
|
||||||
|
FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& bright_grey(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[90m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream,
|
||||||
|
0 | FOREGROUND_INTENSITY // grey (black)
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& bright_red(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[91m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream,
|
||||||
|
FOREGROUND_RED | FOREGROUND_INTENSITY
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& bright_green(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[92m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream,
|
||||||
|
FOREGROUND_GREEN | FOREGROUND_INTENSITY
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& bright_yellow(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[93m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream,
|
||||||
|
FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& bright_blue(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[94m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream,
|
||||||
|
FOREGROUND_BLUE | FOREGROUND_INTENSITY
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& bright_magenta(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[95m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream,
|
||||||
|
FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& bright_cyan(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[96m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream,
|
||||||
|
FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& bright_white(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[97m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream,
|
||||||
|
FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& on_grey(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[40m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream, -1,
|
||||||
|
0 // grey (black)
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& on_red(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[41m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream, -1,
|
||||||
|
BACKGROUND_RED
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& on_green(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[42m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream, -1,
|
||||||
|
BACKGROUND_GREEN
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& on_yellow(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[43m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream, -1,
|
||||||
|
BACKGROUND_GREEN | BACKGROUND_RED
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& on_blue(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[44m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream, -1,
|
||||||
|
BACKGROUND_BLUE
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& on_magenta(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[45m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream, -1,
|
||||||
|
BACKGROUND_BLUE | BACKGROUND_RED
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& on_cyan(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[46m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream, -1,
|
||||||
|
BACKGROUND_GREEN | BACKGROUND_BLUE
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& on_white(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[47m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream, -1,
|
||||||
|
BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_RED
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& on_bright_grey(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[100m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream, -1,
|
||||||
|
0 | BACKGROUND_INTENSITY // grey (black)
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& on_bright_red(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[101m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream, -1,
|
||||||
|
BACKGROUND_RED | BACKGROUND_INTENSITY
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& on_bright_green(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[102m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream, -1,
|
||||||
|
BACKGROUND_GREEN | BACKGROUND_INTENSITY
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& on_bright_yellow(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[103m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream, -1,
|
||||||
|
BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& on_bright_blue(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[104m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream, -1,
|
||||||
|
BACKGROUND_BLUE | BACKGROUND_INTENSITY
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& on_bright_magenta(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[105m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream, -1,
|
||||||
|
BACKGROUND_BLUE | BACKGROUND_RED | BACKGROUND_INTENSITY
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& on_bright_cyan(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[106m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream, -1,
|
||||||
|
BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename CharT>
|
||||||
|
std::basic_ostream<CharT>& on_bright_white(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
if (_internal::is_colorized(stream))
|
||||||
|
{
|
||||||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||||||
|
stream << "\033[107m";
|
||||||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||||||
|
_internal::win_change_attributes(stream, -1,
|
||||||
|
BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_RED | BACKGROUND_INTENSITY
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//! Since C++ hasn't a way to hide something in the header from
|
||||||
|
//! the outer access, I have to introduce this namespace which
|
||||||
|
//! is used for internal purpose and should't be access from
|
||||||
|
//! the user code.
|
||||||
|
namespace _internal
|
||||||
|
{
|
||||||
|
// An index to be used to access a private storage of I/O streams. See
|
||||||
|
// colorize / nocolorize I/O manipulators for details. Due to the fact
|
||||||
|
// that static variables ain't shared between translation units, inline
|
||||||
|
// function with local static variable is used to do the trick and share
|
||||||
|
// the variable value between translation units.
|
||||||
|
inline int colorize_index()
|
||||||
|
{
|
||||||
|
static int colorize_index = std::ios_base::xalloc();
|
||||||
|
return colorize_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Since C++ hasn't a true way to extract stream handler
|
||||||
|
//! from the a given `std::ostream` object, I have to write
|
||||||
|
//! this kind of hack.
|
||||||
|
inline
|
||||||
|
FILE* get_standard_stream(const std::ostream& stream)
|
||||||
|
{
|
||||||
|
if (&stream == &std::cout)
|
||||||
|
return stdout;
|
||||||
|
else if (&stream == &std::cerr || &stream == &std::clog)
|
||||||
|
return stderr;
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Since C++ hasn't a true way to extract stream handler
|
||||||
|
//! from the a given `std::wostream` object, I have to write
|
||||||
|
//! this kind of hack.
|
||||||
|
inline
|
||||||
|
FILE* get_standard_stream(const std::wostream& stream)
|
||||||
|
{
|
||||||
|
if (&stream == &std::wcout)
|
||||||
|
return stdout;
|
||||||
|
else if (&stream == &std::wcerr || &stream == &std::wclog)
|
||||||
|
return stderr;
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Say whether a given stream should be colorized or not. It's always
|
||||||
|
// true for ATTY streams and may be true for streams marked with
|
||||||
|
// colorize flag.
|
||||||
|
template <typename CharT>
|
||||||
|
bool is_colorized(std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
return is_atty(stream) || static_cast<bool>(stream.iword(colorize_index()));
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Test whether a given `std::ostream` object refers to
|
||||||
|
//! a terminal.
|
||||||
|
template <typename CharT>
|
||||||
|
bool is_atty(const std::basic_ostream<CharT>& stream)
|
||||||
|
{
|
||||||
|
FILE* std_stream = get_standard_stream(stream);
|
||||||
|
|
||||||
|
// Unfortunately, fileno() ends with segmentation fault
|
||||||
|
// if invalid file descriptor is passed. So we need to
|
||||||
|
// handle this case gracefully and assume it's not a tty
|
||||||
|
// if standard stream is not detected, and 0 is returned.
|
||||||
|
if (!std_stream)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
#if defined(TERMCOLOR_TARGET_POSIX)
|
||||||
|
return ::isatty(fileno(std_stream));
|
||||||
|
#elif defined(TERMCOLOR_TARGET_WINDOWS)
|
||||||
|
return ::_isatty(_fileno(std_stream));
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(TERMCOLOR_TARGET_WINDOWS)
|
||||||
|
|
||||||
|
//! same hack as used in get_standard_stream function, but for Windows with `std::ostream`
|
||||||
|
inline HANDLE get_terminal_handle(std::ostream& stream)
|
||||||
|
{
|
||||||
|
if (&stream == &std::cout)
|
||||||
|
return GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
|
else if (&stream == &std::cerr || &stream == &std::clog)
|
||||||
|
return GetStdHandle(STD_ERROR_HANDLE);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! same hack as used in get_standard_stream function, but for Windows with `std::wostream`
|
||||||
|
inline HANDLE get_terminal_handle(std::wostream& stream)
|
||||||
|
{
|
||||||
|
if (&stream == &std::wcout)
|
||||||
|
return GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
|
else if (&stream == &std::wcerr || &stream == &std::wclog)
|
||||||
|
return GetStdHandle(STD_ERROR_HANDLE);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Change Windows Terminal colors attribute. If some
|
||||||
|
//! parameter is `-1` then attribute won't changed.
|
||||||
|
template <typename CharT>
|
||||||
|
void win_change_attributes(std::basic_ostream<CharT>& stream, int foreground, int background)
|
||||||
|
{
|
||||||
|
// yeah, i know.. it's ugly, it's windows.
|
||||||
|
static WORD defaultAttributes = 0;
|
||||||
|
|
||||||
|
// Windows doesn't have ANSI escape sequences and so we use special
|
||||||
|
// API to change Terminal output color. That means we can't
|
||||||
|
// manipulate colors by means of "std::stringstream" and hence
|
||||||
|
// should do nothing in this case.
|
||||||
|
if (!_internal::is_atty(stream))
|
||||||
|
return;
|
||||||
|
|
||||||
|
// get terminal handle
|
||||||
|
HANDLE hTerminal = INVALID_HANDLE_VALUE;
|
||||||
|
hTerminal = get_terminal_handle(stream);
|
||||||
|
|
||||||
|
// save default terminal attributes if it unsaved
|
||||||
|
if (!defaultAttributes)
|
||||||
|
{
|
||||||
|
CONSOLE_SCREEN_BUFFER_INFO info;
|
||||||
|
if (!GetConsoleScreenBufferInfo(hTerminal, &info))
|
||||||
|
return;
|
||||||
|
defaultAttributes = info.wAttributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
// restore all default settings
|
||||||
|
if (foreground == -1 && background == -1)
|
||||||
|
{
|
||||||
|
SetConsoleTextAttribute(hTerminal, defaultAttributes);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get current settings
|
||||||
|
CONSOLE_SCREEN_BUFFER_INFO info;
|
||||||
|
if (!GetConsoleScreenBufferInfo(hTerminal, &info))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (foreground != -1)
|
||||||
|
{
|
||||||
|
info.wAttributes &= ~(info.wAttributes & 0x0F);
|
||||||
|
info.wAttributes |= static_cast<WORD>(foreground);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (background != -1)
|
||||||
|
{
|
||||||
|
info.wAttributes &= ~(info.wAttributes & 0xF0);
|
||||||
|
info.wAttributes |= static_cast<WORD>(background);
|
||||||
|
}
|
||||||
|
|
||||||
|
SetConsoleTextAttribute(hTerminal, info.wAttributes);
|
||||||
|
}
|
||||||
|
#endif // TERMCOLOR_TARGET_WINDOWS
|
||||||
|
|
||||||
|
} // namespace _internal
|
||||||
|
|
||||||
|
} // namespace termcolor
|
||||||
|
|
||||||
|
|
||||||
|
#undef TERMCOLOR_TARGET_POSIX
|
||||||
|
#undef TERMCOLOR_TARGET_WINDOWS
|
||||||
|
|
||||||
|
#if defined(TERMCOLOR_AUTODETECTED_IMPLEMENTATION)
|
||||||
|
# undef TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES
|
||||||
|
# undef TERMCOLOR_USE_WINDOWS_API
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // TERMCOLOR_HPP_
|
||||||
@@ -61,6 +61,10 @@ public:
|
|||||||
table.resize(size);
|
table.resize(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PrefetchEntry(U64 &hashKey) {
|
||||||
|
__builtin_prefetch(&table[hashKey % table.size()]);
|
||||||
|
}
|
||||||
|
|
||||||
TTEntry GetRawEntry(U64 &hashKey) {
|
TTEntry GetRawEntry(U64 &hashKey) {
|
||||||
TTEntry *current = &table[hashKey % table.size()];
|
TTEntry *current = &table[hashKey % table.size()];
|
||||||
return *current;
|
return *current;
|
||||||
|
|||||||
@@ -0,0 +1,98 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
//#define TUNING
|
||||||
|
|
||||||
|
#define AUTO_STEP(min_val, max_val) ((max_val - min_val) / 20.0)
|
||||||
|
|
||||||
|
#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)
|
||||||
|
|
||||||
|
#ifdef TUNING
|
||||||
|
#define X_DOUBLE(name, default_val, min_val, max_val) \
|
||||||
|
inline double name = default_val; \
|
||||||
|
constexpr double name##_step = AUTO_STEP(min_val, max_val);
|
||||||
|
#define X_INT(name, default_val, min_val, max_val) \
|
||||||
|
inline int name = default_val; \
|
||||||
|
constexpr double name##_step = AUTO_STEP(min_val, max_val);
|
||||||
|
#else
|
||||||
|
#define X_DOUBLE(name, default_val, min_val, max_val) \
|
||||||
|
constexpr double name = default_val; \
|
||||||
|
constexpr double name##_step = AUTO_STEP(min_val, max_val);
|
||||||
|
#define X_INT(name, default_val, min_val, max_val) \
|
||||||
|
constexpr int name = default_val; \
|
||||||
|
constexpr double name##_step = AUTO_STEP(min_val, max_val);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
TUNABLE_LIST
|
||||||
|
|
||||||
|
#undef X_DOUBLE
|
||||||
|
#undef X_INT
|
||||||
|
|
||||||
|
inline void PrintTunables() {
|
||||||
|
#define X_DOUBLE(name, default_val, min_val, max_val) \
|
||||||
|
std::cout << #name << ", float, " << name \
|
||||||
|
<< ", " << min_val \
|
||||||
|
<< ", " << max_val \
|
||||||
|
<< ", " << name##_step \
|
||||||
|
<< ", 0.002" << 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;
|
||||||
|
|
||||||
|
TUNABLE_LIST
|
||||||
|
|
||||||
|
#undef X_DOUBLE
|
||||||
|
#undef X_INT
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void PrintTunablesUCI() {
|
||||||
|
#define X_DOUBLE(name, default_val, min_val, max_val) \
|
||||||
|
std::cout << "option name " #name \
|
||||||
|
<< " type string default " << name \
|
||||||
|
<< " min " << min_val \
|
||||||
|
<< " max " << max_val \
|
||||||
|
<< std::endl;
|
||||||
|
|
||||||
|
#define X_INT(name, default_val, min_val, max_val) \
|
||||||
|
std::cout << "option name " #name \
|
||||||
|
<< " type spin default " << name \
|
||||||
|
<< " min " << min_val \
|
||||||
|
<< " max " << max_val \
|
||||||
|
<< std::endl;
|
||||||
|
|
||||||
|
TUNABLE_LIST
|
||||||
|
|
||||||
|
#undef X_DOUBLE
|
||||||
|
#undef X_INT
|
||||||
|
}
|
||||||
+89
-25
@@ -11,6 +11,7 @@
|
|||||||
#include "perft.h"
|
#include "perft.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "datagen.h"
|
#include "datagen.h"
|
||||||
|
#include "tunables.h"
|
||||||
|
|
||||||
// OS-dependent threading includes
|
// OS-dependent threading includes
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@@ -44,38 +45,47 @@ static void ParsePosition(Board &board, std::string_view command, SEARCH::Search
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MOVEGEN::GenerateMoves<All>(board);
|
MOVEGEN::GenerateMoves<All>(board, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ReadParam(std::string param, std::string &command) {
|
static double ReadParam(const std::string& param, const std::string &command) {
|
||||||
size_t pos = command.find(param);
|
size_t pos = command.find(param);
|
||||||
if (pos != std::string::npos) {
|
if (pos == std::string::npos)
|
||||||
pos += param.length() + 1;
|
return 0.0;
|
||||||
bool isNegative = false;
|
|
||||||
std::string result = "";
|
|
||||||
|
|
||||||
// Check for negative sign
|
pos += param.length() + 1; // skip "param "
|
||||||
if (pos < command.length() && command[pos] == '-') {
|
std::string result;
|
||||||
isNegative = true;
|
|
||||||
pos++;
|
// optional sign
|
||||||
|
if (pos < command.length() && (command[pos] == '-' || command[pos] == '+')) {
|
||||||
|
result += command[pos];
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// digits and optional decimal point
|
||||||
|
bool decimalSeen = false;
|
||||||
|
while (pos < command.length() && (std::isdigit(command[pos]) || command[pos] == '.')) {
|
||||||
|
if (command[pos] == '.') {
|
||||||
|
if (decimalSeen) break; // only one decimal point
|
||||||
|
decimalSeen = true;
|
||||||
}
|
}
|
||||||
|
result += command[pos++];
|
||||||
|
}
|
||||||
|
|
||||||
// Extract digits
|
if (!result.empty()) {
|
||||||
while (pos < command.length() && std::isdigit(command[pos])) {
|
try {
|
||||||
result += command[pos];
|
return std::stod(result);
|
||||||
pos++;
|
} catch (const std::out_of_range&) {
|
||||||
}
|
return (result[0] == '-') ? -1e308 : 1e308; // clamp to double range
|
||||||
|
} catch (const std::invalid_argument&) {
|
||||||
// Ensure result is not empty before calling stoi
|
return 0.0;
|
||||||
if (!result.empty()) {
|
|
||||||
int value = std::stoi(result);
|
|
||||||
return isNegative ? -value : value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
// Windows implementation using std::thread
|
// Windows implementation using std::thread
|
||||||
template <SEARCH::searchMode mode>
|
template <SEARCH::searchMode mode>
|
||||||
@@ -146,18 +156,65 @@ static void ParseGo(Board &board, std::string &command, SEARCH::SearchContext* c
|
|||||||
StartSearchThread(board, params, ctx);
|
StartSearchThread(board, params, ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SetOption(std::string &command, SEARCH::SearchContext* ctx) {
|
static void SetOption(std::string& command, SEARCH::SearchContext* ctx) {
|
||||||
if (command.find("Hash") != std::string::npos) {
|
if (command.find("Hash") != std::string::npos) {
|
||||||
U64 hashSize = (ReadParam("value", command) * 1000000) / sizeof(TTEntry);
|
U64 hashSize = (ReadParam("value", command) * 1000000ULL) / sizeof(TTEntry);
|
||||||
ctx->TT.Resize(hashSize);
|
ctx->TT.Resize(hashSize);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef TUNING
|
||||||
|
|
||||||
|
size_t namePos = command.find("name");
|
||||||
|
if (namePos == std::string::npos)
|
||||||
|
return;
|
||||||
|
|
||||||
|
namePos += 5; // skip "name "
|
||||||
|
size_t valuePos = command.find(" value");
|
||||||
|
if (valuePos == std::string::npos)
|
||||||
|
return;
|
||||||
|
|
||||||
|
std::string name = command.substr(namePos, valuePos - namePos);
|
||||||
|
name.erase(0, name.find_first_not_of(' '));
|
||||||
|
name.erase(name.find_last_not_of(' ') + 1);
|
||||||
|
|
||||||
|
double rawValue = ReadParam("value", command);
|
||||||
|
|
||||||
|
#define X_DOUBLE(paramName, default_val, min_val, max_val) \
|
||||||
|
if (name == #paramName) { \
|
||||||
|
double v = rawValue; \
|
||||||
|
if (v < min_val) v = min_val; \
|
||||||
|
if (v > max_val) v = max_val; \
|
||||||
|
paramName = v; \
|
||||||
|
return; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define X_INT(paramName, default_val, min_val, max_val) \
|
||||||
|
if (name == #paramName) { \
|
||||||
|
int v = static_cast<int>(rawValue); \
|
||||||
|
if (v < min_val) v = min_val; \
|
||||||
|
if (v > max_val) v = max_val; \
|
||||||
|
paramName = v; \
|
||||||
|
return; \
|
||||||
|
}
|
||||||
|
|
||||||
|
TUNABLE_LIST
|
||||||
|
|
||||||
|
#undef X_DOUBLE
|
||||||
|
#undef X_INT
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static void PrintEngineInfo() {
|
static void PrintEngineInfo() {
|
||||||
std::cout << "id name Eleanor v2.0" << std::endl;
|
std::cout << "id name Eleanor v3.0" << std::endl;
|
||||||
std::cout << "id author rektdie" << 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 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 1" << std::endl;
|
||||||
|
|
||||||
|
#ifdef TUNING
|
||||||
|
PrintTunablesUCI();
|
||||||
|
#endif
|
||||||
|
|
||||||
std::cout << "uciok" << std::endl;
|
std::cout << "uciok" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,6 +250,7 @@ void UCILoop(Board &board) {
|
|||||||
ctx->killerMoves = {};
|
ctx->killerMoves = {};
|
||||||
ctx->history.Clear();
|
ctx->history.Clear();
|
||||||
ctx->conthist.Clear();
|
ctx->conthist.Clear();
|
||||||
|
ctx->corrhist.Clear();
|
||||||
ctx->positionHistory = {};
|
ctx->positionHistory = {};
|
||||||
ctx->ss = {};
|
ctx->ss = {};
|
||||||
|
|
||||||
@@ -220,6 +278,7 @@ void UCILoop(Board &board) {
|
|||||||
|
|
||||||
// parse UCI "uci" command
|
// parse UCI "uci" command
|
||||||
if (input.find("uci") != std::string::npos) {
|
if (input.find("uci") != std::string::npos) {
|
||||||
|
UCIEnabled = true;
|
||||||
// print engine info
|
// print engine info
|
||||||
PrintEngineInfo();
|
PrintEngineInfo();
|
||||||
continue;
|
continue;
|
||||||
@@ -256,9 +315,14 @@ void UCILoop(Board &board) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (input.find("tunables") != std::string::npos) {
|
||||||
|
PrintTunables();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (input.find("datagen") != std::string::npos) {
|
if (input.find("datagen") != std::string::npos) {
|
||||||
DATAGEN::Run(500000*2, 22);
|
DATAGEN::Run(500000*2, 22);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,8 @@
|
|||||||
#include "move.h"
|
#include "move.h"
|
||||||
#include "board.h"
|
#include "board.h"
|
||||||
|
|
||||||
|
inline bool UCIEnabled = false;
|
||||||
|
|
||||||
class SearchParams {
|
class SearchParams {
|
||||||
public:
|
public:
|
||||||
int wtime = 0;
|
int wtime = 0;
|
||||||
|
|||||||
+2
-2
@@ -111,7 +111,7 @@ int parseSquare(std::string_view str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Move parseMove(Board &board, std::string_view str) {
|
Move parseMove(Board &board, std::string_view str) {
|
||||||
MOVEGEN::GenerateMoves<All>(board);
|
MOVEGEN::GenerateMoves<All>(board, true);
|
||||||
|
|
||||||
int from = parseSquare(str.substr(0, 2));
|
int from = parseSquare(str.substr(0, 2));
|
||||||
int to = parseSquare(str.substr(2, 2));
|
int to = parseSquare(str.substr(2, 2));
|
||||||
@@ -202,4 +202,4 @@ int ConvertToWhiteRelative(Board &board, int score) {
|
|||||||
return board.sideToMove == White ? score : -score;
|
return board.sideToMove == White ? score : -score;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user